DAE importing seems to have incorrect transformations/matrices?

Issue #20 resolved
nathan miller created an issue

I'm not %100 sure if it's the cause of the library but most DAE imports often gives off improper matrices. The build I am using is 4.1.0.0 from the latest nuget package. I have some example code for how i transformed them. A DAE file is also attached with the issue.

        //Load the dae file
        public void LoadFile(string FileName)
        {
            AssimpContext Importer = new AssimpContext();

            scene = Importer.ImportFile(FileName, PostProcessSteps.Triangulate | PostProcessSteps.JoinIdenticalVertices
                 | PostProcessSteps.FlipUVs | PostProcessSteps.ValidateDataStructure);
            processNode();
        }
        //Process the nodes to get transform data
        public void processNode()
        {
            Matrix4x4 identity = Matrix4x4.Identity;
            if (scene.RootNode != null)
            {
                BuildNode(scene.RootNode, ref identity);
            }
        }
        private void BuildNode(Node node, ref Matrix4x4 rootTransform)
        {
            Matrix4x4 trafo = node.Transform;
            Matrix4x4 world = rootTransform * trafo; //Multiply here to get output transform
            Matrix4 worldTK = TKMatrix(world); //Convert our matrices

            if (node.HasMeshes)
            {
                foreach (int index in node.MeshIndices)
                {
                    objects.Add(CreateGenericObject(scene.Meshes[index], index, worldTK));
                }
            }
            for (int i = 0; i < node.ChildCount; i++)
            {
                BuildNode(node.Children[i], ref world);
            }
        }
        //This code just makes a new object from a custom class
        public STGenericObject CreateGenericObject(Mesh msh, int Index, Matrix4 transform)
        {
            STGenericObject obj = new STGenericObject();
            obj.vertices = GetVertices(msh, transform);
            obj.faces = GetFaces(msh);
            return obj;
        }
        public List<Vertex> GetVertices(Mesh msh, Matrix4 transform)
        {
            List<Vertex> vertices = new List<Vertex>();
            for (int v = 0; v < msh.VertexCount; v++)
            {
                Vertex vert = new Vertex();

                if (msh.HasVertices)
                    vert.pos = Vector3.TransformPosition(FromVector(msh.Vertices[v]), transform);
                if (msh.HasNormals)
                    vert.nrm = Vector3.TransformNormal(FromVector(msh.Normals[v]), transform);

                vertices.Add(vert);
            }

            return vertices;
        }

        public static OpenTK.Matrix4 TKMatrix(Assimp.Matrix4x4 input)
        {
            return new OpenTK.Matrix4(input.A1, input.B1, input.C1, input.D1,
                                       input.A2, input.B2, input.C2, input.D2,
                                       input.A3, input.B3, input.C3, input.D3,
                                       input.A4, input.B4, input.C4, input.D4);
        }

Also here are some screenshots

How it turns out WaYqu5t.png

How it should look. NY3qKCD.png

Comments (7)

  1. Nicholas Woodfield repo owner

    Thanks for the report.

    Have you tried loading this model in the Sample project? Have you used previous AssimpNet versions?

    It might just be a case of not using the 4x4 matrices correctly (they are row-major/Column-vector)

  2. nathan miller reporter

    I have tried loading in in AssimpNet.Sample however only one model seemed to appear so it's hard to tell if the matrices are correct. Tried loading older versions but it gave the same results.

  3. Nicholas Woodfield repo owner

    It loads fine in Open 3D Model Viewer (https://github.com/acgessler/open3mod) which was created by the Assimp devs and uses the previous AssimpNet version. The matrix multiplication was the wrong order in the AssimpNet sample, I just pushed a change to fix it and your model loads fine in the sample now too.

    Probably the same issue for you, in your BuildNode function.

  4. Nicholas Woodfield repo owner

    No, thank you :)

    Many users mistakenly file issues to an old github repository that I never setup/administer, so it's great to see a user using the bitbucket tracker!

  5. Log in to comment