コード例 #1
1
            /// <summary>
            /// Export the mesh's vertex color using layer 0.
            /// </summary>
            ///
            public void ExportVertexColors(MeshInfo mesh, FbxMesh fbxMesh)
            {
                // Set the normals on Layer 0.
                FbxLayer fbxLayer = fbxMesh.GetLayer(0 /* default layer */);

                if (fbxLayer == null)
                {
                    fbxMesh.CreateLayer();
                    fbxLayer = fbxMesh.GetLayer(0 /* default layer */);
                }

                using (var fbxLayerElement = FbxLayerElementVertexColor.Create(fbxMesh, "VertexColors"))
                {
                    fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);

                    // TODO: normals for each triangle vertex instead of averaged per control point
                    //fbxNormalLayer.SetMappingMode (FbxLayerElement.eByPolygonVertex);

                    fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                    // Add one normal per each vertex face index (3 per triangle)
                    FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                    for (int n = 0; n < mesh.VertexColors.Length; n++)
                    {
                        // Converting to Color from Color32, as Color32 stores the colors
                        // as ints between 0-255, while FbxColor and Color
                        // use doubles between 0-1
                        Color color = mesh.VertexColors [n];
                        fbxElementArray.Add(new FbxColor(color.r,
                                                         color.g,
                                                         color.b,
                                                         color.a));
                    }
                    fbxLayer.SetVertexColors(fbxLayerElement);
                }
            }
コード例 #2
0
        public FbxLayerElementVertexColor GetElementVertexColor(int pIndex)
        {
            global::System.IntPtr      cPtr = FbxWrapperNativePINVOKE.FbxGeometryBase_GetElementVertexColor__SWIG_0(swigCPtr, pIndex);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            return(ret);
        }
コード例 #3
0
        public FbxLayerElementVertexColor CreateElementVertexColor()
        {
            global::System.IntPtr      cPtr = FbxWrapperNativePINVOKE.FbxGeometryBase_CreateElementVertexColor(swigCPtr);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            return(ret);
        }
コード例 #4
0
        public static FbxLayerElementVertexColor Create(FbxLayerContainer pOwner, string pName)
        {
            global::System.IntPtr      cPtr = FbxWrapperNativePINVOKE.FbxLayerElementVertexColor_Create(FbxLayerContainer.getCPtr(pOwner), pName);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            return(ret);
        }
コード例 #5
0
        public FbxLayerElementVertexColor GetElementVertexColor()
        {
            global::System.IntPtr      cPtr = fbx_wrapperPINVOKE.FbxGeometryBase_GetElementVertexColor__SWIG_1(swigCPtr);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            return(ret);
        }
コード例 #6
0
        public FbxLayerElementVertexColor GetVertexColors()
        {
            global::System.IntPtr      cPtr = FbxWrapperNativePINVOKE.FbxLayer_GetVertexColors__SWIG_0(swigCPtr);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            return(ret);
        }
コード例 #7
0
 public void SetVertexColors(FbxLayerElementVertexColor pVertexColors)
 {
     NativeMethods.FbxLayer_SetVertexColors(swigCPtr, FbxLayerElementVertexColor.getCPtr(pVertexColors));
     if (NativeMethods.SWIGPendingException.Pending)
     {
         throw NativeMethods.SWIGPendingException.Retrieve();
     }
 }
コード例 #8
0
        public static FbxLayerElementVertexColor Create(FbxLayerContainer pOwner, string pName)
        {
            global::System.IntPtr      cPtr = NativeMethods.FbxLayerElementVertexColor_Create(FbxLayerContainer.getCPtr(pOwner), pName);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            if (NativeMethods.SWIGPendingException.Pending)
            {
                throw NativeMethods.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
コード例 #9
0
        public FbxLayerElementVertexColor GetVertexColors()
        {
            global::System.IntPtr      cPtr = NativeMethods.FbxLayer_GetVertexColors(swigCPtr);
            FbxLayerElementVertexColor ret  = (cPtr == global::System.IntPtr.Zero) ? null : new FbxLayerElementVertexColor(cPtr, false);

            if (NativeMethods.SWIGPendingException.Pending)
            {
                throw NativeMethods.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
コード例 #10
0
        public void TestSetVertexColors()
        {
            // make sure nothing crashes

            m_fbxLayer.SetVertexColors(FbxLayerElementVertexColor.Create(m_fbxMesh, ""));
            Assert.IsNotNull(m_fbxLayer.GetVertexColors());

            // test null
            m_fbxLayer.SetVertexColors(null);
            Assert.IsNull(m_fbxLayer.GetVertexColors());

            // test destroyed
            FbxLayerElementVertexColor vertexColor = FbxLayerElementVertexColor.Create(m_fbxMesh, "");

            vertexColor.Dispose();
            m_fbxLayer.SetVertexColors(vertexColor);
        }
コード例 #11
0
ファイル: ProcessMesh.cs プロジェクト: zwiglm/NeoAxisEngine
        static void ReadColor(MeshData data)
        {
            FbxLayerElementVertexColor pVertexColors = data.Mesh.GetElementVertexColor();

            if (pVertexColors == null)
            {
                for (int i = 0; i < data.Vertices.Length; i++)
                {
                    data.Vertices[i].Vertex.Color = new ColorValue(1, 1, 1);
                }
                return;
            }

            var mappingMode = pVertexColors.GetMappingMode();

            if (!CheckPolygonVertexOrControlPoint(mappingMode))
            {
                FbxImportLog.LogWarning(data.Node, $"has unsupported VertexColors mapping mode: {pVertexColors.GetMappingMode()}");
                return;
            }
            data.VertexComponents |= StandardVertex.Components.Color;
            var indexArray  = pVertexColors.GetReferenceMode() != FbxLayerElement.EReferenceMode.eDirect ? pVertexColors.GetIndexArray() : null;
            var directArray = pVertexColors.GetDirectArray();

            for (int i = 0; i < data.Vertices.Length; i++)
            {
                ref VertexInfo vertex = ref data.Vertices[i];
                FbxColor       color  = null;
                switch (pVertexColors.GetMappingMode())
                {
                case FbxLayerElement.EMappingMode.eByPolygonVertex:
                    color = directArray.GetAt(indexArray?.GetAt(vertex.PolygonVertexIndex) ?? vertex.PolygonVertexIndex);
                    break;

                case FbxLayerElement.EMappingMode.eByControlPoint:
                    color = directArray.GetAt(indexArray?.GetAt(vertex.ControlPointIndex) ?? vertex.ControlPointIndex);
                    break;
                }
                data.Vertices[i].Vertex.Color = color?.ToColorValue() ?? new ColorValue(1, 1, 1);
            }
コード例 #12
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FbxLayerElementVertexColor obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
コード例 #13
0
 public void SetVertexColors(FbxLayerElementVertexColor pVertexColors)
 {
     FbxWrapperNativePINVOKE.FbxLayer_SetVertexColors(swigCPtr, FbxLayerElementVertexColor.getCPtr(pVertexColors));
 }
コード例 #14
0
        protected override FbxScene CreateScene(FbxManager manager)
        {
            FbxScene scene = base.CreateScene(manager);

            // Add normals, binormals, UVs, tangents and vertex colors to the cube
            FbxMesh cubeMesh = scene.GetRootNode().GetChild(0).GetMesh();

            // Add normals
            /// Set the Normals on Layer 0.
            FbxLayer fbxLayer = cubeMesh.GetLayer(0 /* default layer */);

            if (fbxLayer == null)
            {
                cubeMesh.CreateLayer();
                fbxLayer = cubeMesh.GetLayer(0 /* default layer */);
            }

            using (var fbxLayerElement = FbxLayerElementNormal.Create(cubeMesh, "Normals"))
            {
                fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                // Add one normal per each vertex face index (3 per triangle)
                FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                // Assign the normal vectors in the same order the control points were defined
                FbxVector4[] normals = { normalZPos, normalXPos, normalZNeg, normalXNeg, normalYPos, normalYNeg };
                for (int n = 0; n < normals.Length; n++)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        fbxElementArray.Add(normals [n]);
                    }
                }
                fbxLayer.SetNormals(fbxLayerElement);
            }

            /// Set the binormals on Layer 0.
            using (var fbxLayerElement = FbxLayerElementBinormal.Create(cubeMesh, "Binormals"))
            {
                fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                // Add one normal per each vertex face index (3 per triangle)
                FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                for (int n = 0; n < cubeMesh.GetControlPointsCount(); n++)
                {
                    fbxElementArray.Add(new FbxVector4(-1, 0, 1)); // TODO: set to correct values
                }
                fbxLayer.SetBinormals(fbxLayerElement);
            }

            /// Set the tangents on Layer 0.
            using (var fbxLayerElement = FbxLayerElementTangent.Create(cubeMesh, "Tangents"))
            {
                fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                // Add one normal per each vertex face index (3 per triangle)
                FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                for (int n = 0; n < cubeMesh.GetControlPointsCount(); n++)
                {
                    fbxElementArray.Add(new FbxVector4(0, -1, 1)); // TODO: set to correct values
                }
                fbxLayer.SetTangents(fbxLayerElement);
            }

            // set the vertex colors
            using (var fbxLayerElement = FbxLayerElementVertexColor.Create(cubeMesh, "VertexColors"))
            {
                fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                // Add one normal per each vertex face index (3 per triangle)
                FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                // make each vertex either black or white
                for (int n = 0; n < cubeMesh.GetControlPointsCount(); n++)
                {
                    fbxElementArray.Add(new FbxColor(n % 2, n % 2, n % 2));
                }

                fbxLayer.SetVertexColors(fbxLayerElement);
            }

            // set the UVs
            using (var fbxLayerElement = FbxLayerElementUV.Create(cubeMesh, "UVSet"))
            {
                fbxLayerElement.SetMappingMode(FbxLayerElement.EMappingMode.eByPolygonVertex);
                fbxLayerElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eIndexToDirect);

                // set texture coordinates per vertex
                FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray();

                for (int n = 0; n < 8; n++)
                {
                    fbxElementArray.Add(new FbxVector2(n % 2, 1));  // TODO: switch to correct values
                }

                // For each face index, point to a texture uv
                FbxLayerElementArray fbxIndexArray = fbxLayerElement.GetIndexArray();
                fbxIndexArray.SetCount(24);

                for (int vertIndex = 0; vertIndex < 24; vertIndex++)
                {
                    fbxIndexArray.SetAt(vertIndex, vertIndex % 8);  // TODO: switch to correct values
                }
                fbxLayer.SetUVs(fbxLayerElement, FbxLayerElement.EType.eTextureDiffuse);
            }

            return(scene);
        }
コード例 #15
0
ファイル: FbxLayer.cs プロジェクト: yeluo-vinager/FbxWrapper
 public void SetVertexColors(FbxLayerElementVertexColor pVertexColors)
 {
     fbx_wrapperPINVOKE.FbxLayer_SetVertexColors(swigCPtr, FbxLayerElementVertexColor.getCPtr(pVertexColors));
 }
コード例 #16
0
        public bool RemoveElementVertexColor(FbxLayerElementVertexColor pElementVertexColor)
        {
            bool ret = fbx_wrapperPINVOKE.FbxGeometryBase_RemoveElementVertexColor(swigCPtr, FbxLayerElementVertexColor.getCPtr(pElementVertexColor));

            return(ret);
        }
コード例 #17
0
        public static bool ExportToFBX(Component_Mesh sourceMesh, string realFileName, out string error)
        {
            //!!!!как для Vegetation. оверрайдить в Component_Mesh?

            //get mesh data
            var operations = new List <Component_RenderingPipeline.RenderSceneData.MeshDataRenderOperation>();

            foreach (var geometry in sourceMesh.GetComponents <Component_MeshGeometry>())
            {
                if (geometry.Enabled)
                {
                    geometry.CompileDataOfThisObject(out var operation);
                    if (operation != null)
                    {
                        operations.Add(operation);
                    }
                }
            }
            //foreach( var geometry in mesh.Result.MeshData.RenderOperations )
            //{
            //}

            FbxManager    manager  = null;
            FbxIOSettings setting  = null;
            FbxExporter   exporter = null;
            FbxScene      scene    = null;

            try
            {
                //init FBX manager
                manager = FbxManager.Create();
                setting = FbxIOSettings.Create(manager, "IOSRoot");
                manager.SetIOSettings(setting);

                scene = FbxScene.Create(manager, "scene");
                scene.GetGlobalSettings().SetAxisSystem(new FbxAxisSystem(FbxAxisSystem.EPreDefinedAxisSystem.eMax));
                scene.GetGlobalSettings().SetSystemUnit(new FbxSystemUnit(100));

                //init FBX scene
                for (int nOper = 0; nOper < operations.Count; nOper++)
                {
                    var oper = operations[nOper];

                    //get data

                    Vector3F[]   positions = null;
                    Vector3F[]   normals   = null;
                    var          texCoords = new List <Vector2F[]>();
                    ColorValue[] colors    = null;
                    Vector3F[]   tangents  = null;
                    Vector3F[]   binormals = null;

                    //Position
                    {
                        if (oper.VertexStructure.GetElementBySemantic(VertexElementSemantic.Position, out VertexElement element) && element.Type == VertexElementType.Float3)
                        {
                            var buffer = oper.VertexBuffers[element.Source];
                            positions = buffer.ExtractChannel <Vector3F>(element.Offset);
                        }
                    }

                    //Normal
                    {
                        if (oper.VertexStructure.GetElementBySemantic(VertexElementSemantic.Normal, out VertexElement element) && element.Type == VertexElementType.Float3)
                        {
                            var buffer = oper.VertexBuffers[element.Source];
                            normals = buffer.ExtractChannel <Vector3F>(element.Offset);
                        }
                    }

                    //TexCoord
                    for (var channel = VertexElementSemantic.TextureCoordinate0; channel <= VertexElementSemantic.TextureCoordinate3; channel++)
                    {
                        if (oper.VertexStructure.GetElementBySemantic(channel, out VertexElement element) && element.Type == VertexElementType.Float2)
                        {
                            var buffer = oper.VertexBuffers[element.Source];
                            texCoords.Add(buffer.ExtractChannel <Vector2F>(element.Offset));
                        }
                    }

                    //Color
                    {
                        if (oper.VertexStructure.GetElementBySemantic(VertexElementSemantic.Color0, out VertexElement element))
                        {
                            if (element.Type == VertexElementType.Float4)
                            {
                                var buffer = oper.VertexBuffers[element.Source];
                                var values = buffer.ExtractChannel <Vector4F>(element.Offset);
                                colors = new ColorValue[positions.Length];
                                int destIndex = 0;
                                foreach (var p in values)
                                {
                                    colors[destIndex++] = p.ToColorValue();
                                }
                            }
                            else if (element.Type == VertexElementType.ColorABGR)
                            {
                                //!!!!check

                                var buffer = oper.VertexBuffers[element.Source];
                                var values = buffer.ExtractChannel <uint>(element.Offset);
                                colors = new ColorValue[positions.Length];
                                int destIndex = 0;
                                foreach (var p in values)
                                {
                                    colors[destIndex++] = new ColorValue(ColorByte.FromABGR(p));
                                }
                            }
                            else if (element.Type == VertexElementType.ColorARGB)
                            {
                                //!!!!check

                                var buffer = oper.VertexBuffers[element.Source];
                                var values = buffer.ExtractChannel <uint>(element.Offset);
                                colors = new ColorValue[positions.Length];
                                int destIndex = 0;
                                foreach (var p in values)
                                {
                                    colors[destIndex++] = new ColorValue(ColorByte.FromARGB(p));
                                }
                            }
                        }
                    }

                    //Tangent, Binormal
                    if (normals != null)
                    {
                        if (oper.VertexStructure.GetElementBySemantic(VertexElementSemantic.Tangent, out VertexElement element) && element.Type == VertexElementType.Float4)
                        {
                            var buffer    = oper.VertexBuffers[element.Source];
                            var tangents4 = buffer.ExtractChannel <Vector4F>(element.Offset);

                            tangents  = new Vector3F[tangents4.Length];
                            binormals = new Vector3F[tangents4.Length];

                            int destIndex = 0;
                            foreach (var p in tangents4)
                            {
                                tangents[destIndex]  = p.ToVector3F();
                                binormals[destIndex] = Vector3F.Cross(p.ToVector3F(), normals[destIndex]) * p.W;
                                destIndex++;
                            }
                        }
                    }

                    //indices
                    int[] indices = null;
                    if (oper.IndexBuffer != null)
                    {
                        indices = oper.IndexBuffer.Indices;
                    }


                    //create geometry

                    var geometryName = "Geometry " + nOper.ToString();
                    var mesh         = FbxMesh.Create(scene, geometryName);

                    mesh.InitControlPoints(positions.Length);

                    FbxLayerElementNormal elementNormals = null;
                    if (normals != null)
                    {
                        elementNormals = mesh.CreateElementNormal();
                        elementNormals.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                        elementNormals.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
                    }

                    FbxLayerElementVertexColor elementColors = null;
                    if (colors != null)
                    {
                        elementColors = mesh.CreateElementVertexColor();
                        elementColors.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                        elementColors.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
                    }

                    FbxLayerElementTangent elementTangents = null;
                    if (tangents != null)
                    {
                        elementTangents = mesh.CreateElementTangent();
                        elementTangents.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                        elementTangents.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
                    }

                    FbxLayerElementBinormal elementBinormals = null;
                    if (binormals != null)
                    {
                        elementBinormals = mesh.CreateElementBinormal();
                        elementBinormals.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                        elementBinormals.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
                    }

                    var uvElements = new List <FbxLayerElementUV>();
                    for (int uvIndex = 0; uvIndex < texCoords.Count; uvIndex++)
                    {
                        var pUVElement = mesh.CreateElementUV("texcoord" + uvIndex.ToString());
                        pUVElement.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                        pUVElement.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                        uvElements.Add(pUVElement);
                    }

                    for (int n = 0; n < positions.Length; n++)
                    {
                        mesh.SetControlPointAt(ToFbxVector4(positions[n]), n);

                        if (normals != null)
                        {
                            elementNormals.GetDirectArray().Add(ToFbxVector4(normals[n]));
                        }

                        for (int uvIndex = 0; uvIndex < texCoords.Count; uvIndex++)
                        {
                            var texCoord = texCoords[uvIndex][n];
                            texCoord.Y = 1.0f - texCoord.Y;
                            uvElements[uvIndex].GetDirectArray().Add(ToFbxVector2(texCoord));
                        }

                        if (colors != null)
                        {
                            elementColors.GetDirectArray().Add(ToFbxColor(colors[n]));
                        }

                        if (tangents != null)
                        {
                            elementTangents.GetDirectArray().Add(ToFbxVector4(tangents[n]));
                        }

                        if (binormals != null)
                        {
                            elementBinormals.GetDirectArray().Add(ToFbxVector4(binormals[n]));
                        }
                    }

                    if (normals != null)
                    {
                        mesh.GetLayer(0).SetNormals(elementNormals);
                    }
                    if (colors != null)
                    {
                        mesh.GetLayer(0).SetVertexColors(elementColors);
                    }
                    if (tangents != null)
                    {
                        mesh.GetLayer(0).SetTangents(elementTangents);
                    }
                    if (binormals != null)
                    {
                        mesh.GetLayer(0).SetBinormals(elementBinormals);
                    }


                    int polygonCount = indices.Length / 3;
                    for (int i = 0; i < polygonCount; i++)
                    {
                        mesh.BeginPolygon(-1, -1, -1, false);
                        for (int j = 0; j < 3; j++)
                        {
                            int currentIndex = i * 3 + j;
                            int vertexIndex  = indices[currentIndex];
                            mesh.AddPolygon(vertexIndex);
                        }
                        mesh.EndPolygon();
                    }

                    var node = FbxNode.Create(scene, geometryName);
                    node.SetNodeAttribute(mesh);

                    scene.GetRootNode().AddChild(mesh.GetNode());
                }

                //save

                exporter = FbxExporter.Create(manager, "");
                if (!exporter.Initialize(realFileName, -1, manager.GetIOSettings()))
                {
                    error = "Can't initialize FBX exporter.";
                    return(false);
                }

                if (!exporter.Export(scene))
                {
                    error = "Export to FBX failed.";
                    return(false);
                }
            }
            finally
            {
                try { scene?.Destroy(); } catch { }
                try { exporter?.Destroy(); } catch { }
                try { setting?.Destroy(); } catch { }
                try { manager?.Destroy(); } catch { }
            }

            foreach (var op in operations)
            {
                op.DisposeBuffers();
            }

            error = "";
            return(true);
        }
コード例 #18
0
        // For use only by FbxExportGlobals.
        internal static FbxMesh CreateFbxMesh(FbxExportGlobals G, GeometryPool pool, string poolName)
        {
            FbxMesh fbxMesh = FbxMesh.Create(G.m_manager, poolName);

            ExportMesh mesh   = new ExportMesh(pool);
            int        nVerts = mesh.m_pool.m_Vertices.Count;

            fbxMesh.InitControlPoints(nVerts);

            unsafe
            {
                fixed(Vector3 *f = mesh.m_pool.m_Vertices.GetBackingArray())
                {
                    Globals.SetControlPoints(fbxMesh, (IntPtr)f);
                }
            }

            List <int> triangles = mesh.m_pool.m_Tris;

            // Not available in Unity's wrappers
            // fbxMesh.ReservePolygonCount(triangles.Count / 3);
            // fbxMesh.ReservePolygonVertexCount(triangles.Count);
            for (int i = 0; i < triangles.Count; i += 3)
            {
                fbxMesh.BeginPolygon(-1 /* Material */, -1 /* Texture */, -1 /* Group */, false /* Legacy */);
                fbxMesh.AddPolygon(triangles[i]);
                fbxMesh.AddPolygon(triangles[i + 1]);
                fbxMesh.AddPolygon(triangles[i + 2]);
                fbxMesh.EndPolygon();
            }

            FbxLayer layer0 = fbxMesh.GetLayer(0);

            if (layer0 == null)
            {
                fbxMesh.CreateLayer();
                layer0 = fbxMesh.GetLayer(0);
            }

            var layerElementNormal = FbxLayerElementNormal.Create(fbxMesh, "normals");

            layerElementNormal.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
            layerElementNormal.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            CopyToFbx(layerElementNormal.GetDirectArray(), mesh.m_pool.m_Normals);
            layer0.SetNormals(layerElementNormal);

            var layerElementColor = FbxLayerElementVertexColor.Create(fbxMesh, "color");

            layerElementColor.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
            layerElementColor.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            CopyToFbx(layerElementColor.GetDirectArray(), mesh.m_linearColor);
            layer0.SetVertexColors(layerElementColor);

            var layerElementTangent = FbxLayerElementTangent.Create(fbxMesh, "tangents");

            layerElementTangent.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
            layerElementTangent.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            CopyToFbx(layerElementTangent.GetDirectArray(), mesh.m_pool.m_Tangents);
            layer0.SetTangents(layerElementTangent);

            // Compute and export binormals since Unity's FBX importer won't import the tangents without
            // them, even though they're not used.
            var layerElementBinormal = FbxLayerElementBinormal.Create(fbxMesh, "binormals");

            layerElementBinormal.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
            layerElementBinormal.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            var binormals = mesh.m_pool.m_Tangents
                            .Select((tan, idx) => {
                var b3 = Vector3.Cross(tan, mesh.m_pool.m_Normals[idx]) * tan.w;
                return(new Vector4(b3.x, b3.y, b3.z, 1));
            })
                            .ToList();

            CopyToFbx(layerElementBinormal.GetDirectArray(), binormals);
            layer0.SetBinormals(layerElementBinormal);

            var layerElementMaterial = FbxLayerElementMaterial.Create(fbxMesh, "materials");

            layerElementMaterial.SetMappingMode(FbxLayerElement.EMappingMode.eAllSame);
            layer0.SetMaterials(layerElementMaterial);

            // Export everything up to the last uvset containing data
            // even if some intermediate uvsets have no data.
            // Otherwise Unity will get the uvset numbering wrong on import

            List <List <Vector2> > uvSets = DemuxTexcoords(mesh.m_pool);

            for (int i = 0; i < uvSets.Count; i++)
            {
                FbxLayer layerN = fbxMesh.GetLayer(i);
                while (layerN == null)
                {
                    fbxMesh.CreateLayer();
                    layerN = fbxMesh.GetLayer(i);
                }
                var layerElementUV = FbxLayerElementUV.Create(fbxMesh, String.Format("uv{0}", i));
                layerElementUV.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);
                layerElementUV.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);

                List <Vector2> uvSet = uvSets[i];
                if (uvSet == null)
                {
                    // Do nothing
                    // Replicates what the old fbx export code did; seems to work fine
                }
                else
                {
                    Debug.Assert(uvSet.Count == nVerts);
                    CopyToFbx(layerElementUV.GetDirectArray(), uvSet);
                }

                layerN.SetUVs(layerElementUV, FbxLayerElement.EType.eTextureDiffuse);
            }
            return(fbxMesh);
        }