Example #1
0
        public void TestSetTangents()
        {
            // make sure nothing crashes

            m_fbxLayer.SetTangents(FbxLayerElementTangent.Create(m_fbxMesh, ""));
            Assert.IsNotNull(m_fbxLayer.GetTangents());

            // test null
            m_fbxLayer.SetTangents(null);
            Assert.IsNull(m_fbxLayer.GetTangents());

            // test destroyed
            FbxLayerElementTangent tangents = FbxLayerElementTangent.Create(m_fbxMesh, "");

            tangents.Dispose();
            m_fbxLayer.SetTangents(tangents);
        }
        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);
        }
            /// <summary>
            /// Export the mesh's normals, binormals and tangents using
            /// layer 0.
            /// </summary>
            ///
            public void ExportNormalsEtc(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 = FbxLayerElementNormal.Create(fbxMesh, "Normals"))
                {
                    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.Normals.Length; n++)
                    {
                        fbxElementArray.Add(new FbxVector4(mesh.Normals [n] [0],
                                                           mesh.Normals [n] [1],
                                                           mesh.Normals [n] [2]));
                    }
                    fbxLayer.SetNormals(fbxLayerElement);
                }

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

                    // TODO: normals for each triangle vertex instead of averaged per control point
                    //fbxBinormalLayer.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.Binormals.Length; n++)
                    {
                        fbxElementArray.Add(new FbxVector4(mesh.Binormals [n] [0],
                                                           mesh.Binormals [n] [1],
                                                           mesh.Binormals [n] [2]));
                    }
                    fbxLayer.SetBinormals(fbxLayerElement);
                }

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

                    // TODO: normals for each triangle vertex instead of averaged per control point
                    //fbxBinormalLayer.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.Normals.Length; n++)
                    {
                        fbxElementArray.Add(new FbxVector4(mesh.Tangents [n] [0],
                                                           mesh.Tangents [n] [1],
                                                           mesh.Tangents [n] [2]));
                    }
                    fbxLayer.SetTangents(fbxLayerElement);
                }
            }
Example #4
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);
        }
Example #5
0
        protected override FbxMesh GenerateFbx()
        {
            string meshName = (Souls.meshRoot != null ? Souls.meshRoot.Name : "") + "_Mesh";

            FbxMesh mesh = FbxMesh.Create(Scene, meshName);

            mesh.InitControlPoints(Souls.mesh.Vertices.Count);

            int layerIndex = mesh.CreateLayer();

            FbxLayer layer = mesh.GetLayer(layerIndex);

            FbxLayerContainer layerContainer = FbxLayerContainer.Create(Scene, meshName + "_LayerContainer");

            FbxLayerElementUV uv = FbxLayerElementUV.Create(layerContainer, "Diffuse");

            layer.SetUVs(uv);

            FbxLayerElementNormal normal = FbxLayerElementNormal.Create(layerContainer, "Normal");

            layer.SetNormals(normal);
            normal.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            normal.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);

            FbxLayerElementBinormal binormal = FbxLayerElementBinormal.Create(layerContainer, "Binormal");

            layer.SetBinormals(binormal);

            FbxLayerElementTangent tangent = FbxLayerElementTangent.Create(layerContainer, "Tangent");

            layer.SetTangents(tangent);
            tangent.SetReferenceMode(FbxLayerElement.EReferenceMode.eDirect);
            tangent.SetMappingMode(FbxLayerElement.EMappingMode.eByControlPoint);

            for (int vertexIndex = 0; vertexIndex < Souls.mesh.Vertices.Count; ++vertexIndex)
            {
                FLVER.Vertex vertex = Souls.mesh.Vertices[vertexIndex];

                Vector3 position = vertex.Position;

                // this fixes vertex positions since otherwise the model is turned inside out
                // and it appears like it holds weapons in the left hand
                position.Z = -position.Z;

                normal.GetDirectArray().Add(vertex.Normal.ToFbxVector4());

                tangent.GetDirectArray().Add(new FbxVector4(vertex.Tangents[0].X, vertex.Tangents[0].Y, vertex.Tangents[0].Z));

                Vector2 uvValue = new Vector2(0);

                if (vertex.UVs.Count > 0)
                {
                    uvValue.X = vertex.UVs[0].X;
                    uvValue.Y = vertex.UVs[0].Y;
                }

                uv.GetDirectArray().Add(uvValue.ToFbxVector2());

                mesh.SetControlPointAt(position.ToFbxVector4(), vertexIndex);
            }

            for (int faceSetIndex = 0; faceSetIndex < Souls.mesh.FaceSets.Count; ++faceSetIndex)
            {
                FLVER2.FaceSet faceSet = Souls.mesh.FaceSets[faceSetIndex];

                if (faceSet.Flags != FLVER2.FaceSet.FSFlags.None)
                {
                    continue;
                }

                for (int faceStartIndex = 0; faceStartIndex < faceSet.Indices.Count; faceStartIndex += 3)
                {
                    mesh.AddCompletePolygon(
                        faceSet.Indices[faceStartIndex],
                        faceSet.Indices[faceStartIndex + 1],
                        faceSet.Indices[faceStartIndex + 2]
                        );
                    //mesh.AddCompletePolygon(
                    //    faceSet.Indices[faceStartIndex + 2],
                    //    faceSet.Indices[faceStartIndex + 1],
                    //    faceSet.Indices[faceStartIndex]
                    //);
                }
            }

            mesh.BuildMeshEdgeArray();

            FbxGeometryConverter converter = new FbxGeometryConverter(Scene.GetFbxManager());

            converter.ComputeEdgeSmoothingFromNormals(mesh);
            converter.ComputePolygonSmoothingFromEdgeSmoothing(mesh);

            return(mesh);
        }