Jump to content

BabylonExporter Right->Left Coordinate System


ProfessorF
 Share

Recommended Posts

I noticed that my Maya model:

post-5799-0-32432700-1387150550.png

 

Was coming out backwards in Babylon:

post-5799-0-94842500-1387150598.png

 

I eventually figured out that it was because Maya is a right-handed coordinate system & Babylon.js is left-handed.  I think it's most efficient to code the transformation in the BabylonExporter, so I did the following:

 

1. Change the winding of the triangles (otherwise faces become invisible)

2. Invert Z position

3. Invert Z normal

 

The relevant code in BabylonExport.Core > Exporters > ThroughXNA > XNAExporter.cs

 

        void ParseMesh(ModelMesh modelMesh, BabylonScene scene, BabylonSkeleton skeleton)
        {
            var proxyID = ProxyMesh.CreateBabylonMesh(modelMesh.Name, scene);
            int indexName = 0;
 
            foreach (var part in modelMesh.MeshParts)
            {
                var material = exportedMaterials.First(m => m.Name == part.Effect.GetHashCode().ToString());
 
                var indices = new ushort[part.PrimitiveCount * 3];
                                
                part.IndexBuffer.GetData(part.StartIndex * 2, indices, 0, indices.Length);
                for (int ib = 0; ib < indices.Length; ib += 3) // reverse winding of triangles
                {
                    ushort ti;
                    ti = indices[ib];
                    indices[ib] = indices[ib + 2];
                    indices[ib + 2] = ti;
                }
                
                
                if (part.VertexBuffer.VertexDeclaration.VertexStride >= PositionNormalTexturedWeights.Stride)
                {
                    var mesh = new Mesh<PositionNormalTexturedWeights>(material);
                    var vertices = new PositionNormalTexturedWeights[part.NumVertices];
 
                    part.VertexBuffer.GetData(part.VertexOffset * part.VertexBuffer.VertexDeclaration.VertexStride, vertices, 0, vertices.Length, part.VertexBuffer.VertexDeclaration.VertexStride);
 
                    for (int index = 0; index < vertices.Length; index++)
                    {
                        vertices[index].TextureCoordinates.Y = 1.0f - vertices[index].TextureCoordinates.Y;
                        vertices[index].Position.Z = -vertices[index].Position.Z;
                        vertices[index].Normal.Z = -vertices[index].Normal.Z;
                    }
 
                    mesh.AddPart(indexName.ToString(), vertices.ToList(), indices.Select(i => (int)i).ToList());
                    mesh.CreateBabylonMesh(scene, proxyID, skeleton);
                }
                else
                {
                    if (part.VertexBuffer.VertexDeclaration.VertexStride < 32) return;
                    var mesh = new Mesh<PositionNormalTextured>(material);
                    var vertices = new PositionNormalTextured[part.NumVertices];
                    part.VertexBuffer.GetData(part.VertexOffset * part.VertexBuffer.VertexDeclaration.VertexStride, vertices, 0, vertices.Length, part.VertexBuffer.VertexDeclaration.VertexStride);
 
                    for (int index = 0; index < vertices.Length; index++)
                    {
                        vertices[index].TextureCoordinates.Y = 1.0f - vertices[index].TextureCoordinates.Y;
                        vertices[index].Position.Z = -vertices[index].Position.Z;
                        vertices[index].Normal.Z = -vertices[index].Normal.Z;
                    }
 
                    mesh.AddPart(indexName.ToString(), vertices.ToList(), indices.Select(i => (int)i).ToList());
                    mesh.CreateBabylonMesh(scene, proxyID, skeleton);
                }
 
                indexName++;
            }
        }
 
 

This fixed the problem:

 

post-5799-0-76719900-1387151016.png

 

DeltaKosh, should this be a permanent fix, or maybe can you add a flag to the command-line (/rightleft ?) , to invoke the green code? 

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...