问题导入Unity3D中的IFC

时间:2016-04-01 07:44:34

标签: c# unity3d assimp

我正在给你写这封电子邮件,因为我在Unity 3d(版本5.3.4f1)中创建脚本时遇到了一些麻烦。  首先我尝试使用assimpNet 3.0(因为我已经知道它工作得更好)但是当我尝试获取我的ifc文件的树结构时,它并没有真正起作用...脚本给出另一个树结构(例如,我得到fiew节点然后一个节点有7个网格,但在ifc文件中我有2个节点,第一个有6个网格,第二个有1mesh)然后我的网格ind不好所以它不起作用...我可以得到好处与其他PostProcessSteps网格化,但由于optimizeGraph,我只有一个游戏对象。

所以我尝试使用assimpNet 3.3.1(使用源代码,我做了dll能够使用统一)。但是现在我遇到了另一个问题,当我尝试导入我的文件时:

Assimp.Scene model2 = import.ImportFile("sample02.ifc", PostProcessPreset.TargetRealTimeMaximumQuality);

我收到此错误:

AssimpException: Error importing file: The string "E-006,#14,$)" cannot be converted into a value.

Assimp.AssimpContext.ImportFile(System.String文件,PostProcessSteps postProcessFlags) ImportIFC.Start()(在Assets / Tests / ImportIFC / ImportIFC.cs:51)

因此脚本找到了文件但无法读取它......

我给你我的源代码,如果您有任何疑问,我将非常乐意回答您: 我的班级获取信息:

using UnityEngine;

使用System.Collections; 使用Assimp;

公共类MainNodeIFC {

public string name;

public UnityEngine.Mesh[] meshes;
public MainNodeIFC[] children;

}

我的主要代码:

public class ImportIFC : MonoBehaviour

{

// Use this for initialization
void Start()
{


    //using assimpNet 3.3.1
    AssimpContext import = new AssimpContext();

    //using assimpNet 3.0
    //AssimpImporter import = new AssimpImporter();

    import.SetConfig(new NormalSmoothingAngleConfig(66.0f));

    //using assimpNet 3.3.1
    Assimp.Scene model2 = import.ImportFile("sample02.ifc", PostProcessPreset.TargetRealTimeMaximumQuality);
    print("rootNode name: " + model2.RootNode.Name);


    //using assimpNet 3.0
    //can get good import of meshes but erase the tree structure
    /*Assimp.Scene model1 = import.ImportFile(fileName,
        //PostProcessSteps.FindInstances | // No effect + slow?
            PostProcessSteps.FindInvalidData |
            PostProcessSteps.FlipUVs |
            PostProcessSteps.FlipWindingOrder |
        //PostProcessSteps.MakeLeftHanded | // Appears to just mess things up
            PostProcessSteps.JoinIdenticalVertices |
            PostProcessSteps.ImproveCacheLocality |
            PostProcessSteps.OptimizeMeshes |
        PostProcessSteps.OptimizeGraph | // Will eliminate helper nodes
            PostProcessSteps.RemoveRedundantMaterials |
            PostProcessSteps.Triangulate 
            );*/
    //using assimpNet 3.0
    //can get the good tree structure but not the good meshes
    //Assimp.Scene model2=import.ImportFile(fileName, PostProcessPreset.TargetRealTimeMaximumQuality);
    //Assimp.Scene model2 = import.ImportFile(fileName, PostProcessPreset.TargetRealTimeQuality);



   /* MainNodeIFC MainNode1 = new MainNodeIFC();
    GameObject go1 = new GameObject();
    go1.name = model1.RootNode.Name;
    ImportNode(model1.RootNode, MainNode1, model1, go1);*/

    MainNodeIFC MainNode2 = new MainNodeIFC();
    GameObject go2 = new GameObject();
    go2.name = model2.RootNode.Name;
    ImportNode(model2.RootNode, MainNode2, model2, go2);




}



// Update is called once per frame
void Update()
{


}








void ImportNode(Node refNode, MainNodeIFC newNode, Assimp.Scene model, GameObject go)
{


    if (refNode.HasChildren)
    {


        //create new array of node children
        newNode.children = new MainNodeIFC[refNode.ChildCount];
        print(" node: " + refNode.Name + " nombre d'enfant: " + refNode.ChildCount);
        print("type du node: " + refNode.GetType().ToString());

        for (int i = 0; i < refNode.ChildCount; i++)
        {

            //create the child node
            newNode.children[i] = new MainNodeIFC();
            //create the game object for the node
            GameObject childGo = new GameObject();
            MeshFilter mf = childGo.AddComponent(typeof(MeshFilter)) as MeshFilter;
            MeshRenderer mr = childGo.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
            childGo.transform.parent = go.transform;
            childGo.name = refNode.Children[i].Name;

            //Set transform 
            Assimp.Vector3D position, scaling;
            Assimp.Quaternion rotation;
            refNode.Transform.Decompose(out scaling, out rotation, out position);
            childGo.transform.localPosition = new Vector3(position.X, position.Z, position.Y);
            childGo.transform.localRotation = new UnityEngine.Quaternion(rotation.X, rotation.Z, rotation.Y, rotation.W);
            childGo.transform.localScale = new Vector3( scaling.X, scaling.Z, scaling.Y );


            //recursive methode to get all the children node

            ImportNode(refNode.Children[i], newNode.children[i], model, childGo);

        }

    }

        if (refNode.HasMeshes)
        {
            //create array of meshes of this
            newNode.meshes = new UnityEngine.Mesh[refNode.MeshCount];

            print("final node : "+refNode.Name+"a comme enfant: " + refNode.MeshCount);
            print("type du node: " + refNode.GetType().ToString());

            //create the game object for the mesh
            /* */


            for (int i = 0; i < refNode.MeshCount; i++)
            {
                GameObject childGo = new GameObject();
                MeshFilter mf = childGo.AddComponent(typeof(MeshFilter)) as MeshFilter;
                MeshRenderer mr = childGo.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
                childGo.transform.parent = go.transform;
                childGo.name = i.ToString();
                print("nb faces: " + model.Meshes[refNode.MeshIndices[i]].FaceCount);
                //set tranform
                Assimp.Vector3D position, scaling;
                Assimp.Quaternion rotation;
                refNode.Transform.Decompose(out scaling, out rotation, out position);
                childGo.transform.localPosition = new Vector3(position.X, position.Z,-position.Y);
                childGo.transform.localRotation = new UnityEngine.Quaternion(rotation.X, rotation.Z, rotation.Y, rotation.W);
                childGo.transform.localScale = new Vector3( scaling.X, scaling.Z, scaling.Y );

                List<Vector3> verticeTmp = new List<Vector3>();



                //get vertices from model
                Vector3 VectTmp = new Vector3();
                for (int j = 0; j < model.Meshes[refNode.MeshIndices[i]].VertexCount; j++)
                {

                    VectTmp = new Vector3(model.Meshes[refNode.MeshIndices[i]].Vertices[j].X,
                        model.Meshes[refNode.MeshIndices[i]].Vertices[j].Z,
                        model.Meshes[refNode.MeshIndices[i]].Vertices[j].Y);//warning axe Y et Z inverse

                    verticeTmp.Add(VectTmp);
                }


                //set normal : actually doesn’t work

                /* var norm = model.Meshes[refNode.MeshIndices[i]].HasNormals ? model.Meshes[refNode.MeshIndices[i]].Normals[i] : new Vector3D();
                 var texC = model.Meshes[refNode.MeshIndices[i]].HasTextureCoords(0) ? model.Meshes[refNode.MeshIndices[i]].GetTextureCoords(0)[i] : new Vector3D();
                 var tan = model.Meshes[refNode.MeshIndices[i]].HasTangentBasis ? model.Meshes[refNode.MeshIndices[i]].Tangents[i] : new Vector3D();
                 newNode.meshes[i].normals[0].x = norm.X;
                 newNode.meshes[i].normals[0].y = norm.Y;
                 newNode.meshes[i].normals[0].z = norm.Z;*/



                //set vertices and triangle
                newNode.meshes[i] = new UnityEngine.Mesh();

                newNode.meshes[i].SetVertices(verticeTmp);
                newNode.meshes[i].triangles = model.Meshes[i].GetIndices();

                //draw the mesh
                mf.mesh = newNode.meshes[i];
            }
        }

}

}

对不起我的英语,我也是一名工作的学生。

感谢您的关注!

0 个答案:

没有答案