Ejemplo n.º 1
0
            /// <summary>
            /// Unconditionally export this mesh object to the file.
            /// We have decided; this mesh is definitely getting exported.
            /// </summary>
            public FbxMesh ExportMesh(GameObject unityGo, FbxScene fbxScene)
            {
                var meshInfo = GetMeshInfo(unityGo);

                if (!meshInfo.IsValid)
                {
                    return(null);
                }

                // create the mesh structure.
                FbxMesh fbxMesh = FbxMesh.Create(fbxScene, "Scene");

                // Create control points.
                int NumControlPoints = meshInfo.VertexCount;

                fbxMesh.InitControlPoints(NumControlPoints);

                // copy control point data from Unity to FBX
                for (int v = 0; v < NumControlPoints; v++)
                {
                    fbxMesh.SetControlPointAt(new FbxVector4(meshInfo.Vertices [v].x, meshInfo.Vertices [v].y, meshInfo.Vertices [v].z), v);
                }

                for (int f = 0; f < meshInfo.Triangles.Length / 3; f++)
                {
                    fbxMesh.BeginPolygon();
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 1]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 2]);
                    fbxMesh.EndPolygon();
                }

                return(fbxMesh);
            }
Ejemplo n.º 2
0
            /// <summary>
            /// Convert scene's system units but leave scaling unchanged
            /// </summary>
            public void ConvertScene(FbxScene fbxScene, FbxSystemUnit toUnits)
            {
                // Get scale factor.
                float scaleFactor = 1.0f;

                scaleFactor = (float)fbxScene.GetGlobalSettings().GetSystemUnit().GetConversionFactorTo(toUnits);

                if (scaleFactor.Equals(1.0f))
                {
                    return;
                }

                // Get root node.
                FbxNode fbxRootNode = fbxScene.GetRootNode();

                // For all the nodes to convert the translations
                Queue <FbxNode> fbxNodes = new Queue <FbxNode> ();

                fbxNodes.Enqueue(fbxRootNode);

                while (fbxNodes.Count > 0)
                {
                    FbxNode fbxNode = fbxNodes.Dequeue();

                    // Convert node's translation.
                    FbxDouble3 lclTrs = fbxNode.LclTranslation.Get();

#if UNI_18844
                    lclTrs *= scaleFactor;
                    lclTrs *= scaleFactor;
                    lclTrs *= scaleFactor;
#endif
                    fbxNode.LclTranslation.Set(lclTrs);

                    FbxMesh fbxMesh = fbxNode.GetMesh();

                    if (fbxMesh != null)
                    {
                        for (int i = 0; i < fbxMesh.GetControlPointsCount(); ++i)
                        {
                            FbxVector4 fbxVector4 = fbxMesh.GetControlPointAt(i);
#if UNI_18844
                            fbxVector4 *= scaleFactor;
#endif
                            fbxMesh.SetControlPointAt(fbxVector4, i);
                        }
                    }

                    for (int i = 0; i < fbxNode.GetChildCount(); ++i)
                    {
                        fbxNodes.Enqueue(fbxNode.GetChild(i));
                    }
                }
            }
Ejemplo n.º 3
0
            /// <summary>
            /// Unconditionally export this mesh object to the file.
            /// We have decided; this mesh is definitely getting exported.
            /// </summary>
            public void ExportMesh(MeshInfo mesh, FbxNode fbxNode, FbxScene fbxScene)
            {
                if (!mesh.IsValid)
                {
                    return;
                }

                NumMeshes++;
                NumTriangles += mesh.Triangles.Length / 3;
                NumVertices  += mesh.VertexCount;

                // create the mesh structure.
                FbxMesh fbxMesh = FbxMesh.Create(fbxScene, "Mesh");

                // Create control points.
                int NumControlPoints = mesh.VertexCount;

                fbxMesh.InitControlPoints(NumControlPoints);

                // copy control point data from Unity to FBX
                for (int v = 0; v < NumControlPoints; v++)
                {
                    // convert from left to right-handed by negating x (Unity negates x again on import)
                    fbxMesh.SetControlPointAt(new FbxVector4(-mesh.Vertices [v].x, mesh.Vertices [v].y, mesh.Vertices [v].z), v);
                }

                ExportNormalsEtc(mesh, fbxMesh);
                ExportVertexColors(mesh, fbxMesh);
                ExportUVs(mesh, fbxMesh);

                /*
                 * Create polygons
                 * Triangles have to be added in reverse order,
                 * or else they will be inverted on import
                 * (due to the conversion from left to right handed coords)
                 */
                for (int f = 0; f < mesh.Triangles.Length / 3; f++)
                {
                    fbxMesh.BeginPolygon();
                    fbxMesh.AddPolygon(mesh.Triangles [3 * f + 2]);
                    fbxMesh.AddPolygon(mesh.Triangles [3 * f + 1]);
                    fbxMesh.AddPolygon(mesh.Triangles [3 * f]);
                    fbxMesh.EndPolygon();
                }

                // set the fbxNode containing the mesh
                fbxNode.SetNodeAttribute(fbxMesh);
                fbxNode.SetShadingMode(FbxNode.EShadingMode.eWireFrame);
            }
Ejemplo n.º 4
0
            /// <summary>
            /// Unconditionally export this mesh object to the file.
            /// We have decided; this mesh is definitely getting exported.
            /// </summary>
            public FbxNode ExportMesh(MeshInfo meshInfo, FbxScene fbxScene, FbxNode fbxNode)
            {
                if (!meshInfo.IsValid)
                {
                    Debug.LogError("Invalid mesh info");
                    return(null);
                }

                // create a node for the mesh
                FbxNode meshNode = FbxNode.Create(fbxScene, "geo");

                // create the mesh structure.
                FbxMesh fbxMesh = FbxMesh.Create(fbxScene, "Mesh");

                // Create control points.
                int NumControlPoints = meshInfo.VertexCount;

                fbxMesh.InitControlPoints(NumControlPoints);

                // copy control point data from Unity to FBX
                for (int v = 0; v < NumControlPoints; v++)
                {
                    // convert from left to right-handed by negating x (Unity negates x again on import)
                    fbxMesh.SetControlPointAt(new FbxVector4(-meshInfo.Vertices [v].x, meshInfo.Vertices [v].y, meshInfo.Vertices [v].z), v);
                }

                /*
                 * Create polygons
                 * Triangles have to be added in reverse order,
                 * or else they will be inverted on import
                 * (due to the conversion from left to right handed coords)
                 */
                for (int f = 0; f < meshInfo.Triangles.Length / 3; f++)
                {
                    fbxMesh.BeginPolygon();
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 2]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 1]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f]);
                    fbxMesh.EndPolygon();
                }

                // set the fbxNode containing the mesh
                meshNode.SetNodeAttribute(fbxMesh);
                meshNode.SetShadingMode(FbxNode.EShadingMode.eWireFrame);

                fbxNode.AddChild(meshNode);

                return(meshNode);
            }
Ejemplo n.º 5
0
            /// <summary>
            /// Unconditionally export this mesh object to the file.
            /// We have decided; this mesh is definitely getting exported.
            /// </summary>
            public FbxMesh ExportMesh(MeshInfo meshInfo, FbxNode fbxNode, FbxScene fbxScene)
            {
                if (!meshInfo.IsValid)
                {
                    return(null);
                }

                // create the mesh structure.
                FbxMesh fbxMesh = FbxMesh.Create(fbxScene, "Mesh");

                // Create control points.
                int NumControlPoints = meshInfo.VertexCount;

                fbxMesh.InitControlPoints(NumControlPoints);

                // copy control point data from Unity to FBX
                for (int v = 0; v < NumControlPoints; v++)
                {
                    // convert from left to right-handed by negating x (Unity negates x again on import)
                    fbxMesh.SetControlPointAt(new FbxVector4(-meshInfo.Vertices [v].x, meshInfo.Vertices [v].y, meshInfo.Vertices [v].z), v);
                }

                ExportNormalsEtc(meshInfo, fbxMesh);
                ExportUVs(meshInfo, fbxMesh);

                var fbxMaterial = ExportMaterial(meshInfo.Material, fbxScene);

                fbxNode.AddMaterial(fbxMaterial);

                /*
                 * Create polygons after FbxLayerElementMaterial have been created.
                 * Triangles have to be added in reverse order,
                 * or else they will be inverted on import
                 * (due to the conversion from left to right handed coords)
                 */
                for (int f = 0; f < meshInfo.Triangles.Length / 3; f++)
                {
                    fbxMesh.BeginPolygon();
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 2]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f + 1]);
                    fbxMesh.AddPolygon(meshInfo.Triangles [3 * f]);
                    fbxMesh.EndPolygon();
                }

                return(fbxMesh);
            }
        protected override FbxScene CreateScene(FbxManager manager)
        {
            // Create a cube as a static mesh

            FbxScene scene = FbxScene.Create(manager, "myScene");

            FbxNode meshNode = FbxNode.Create(scene, "MeshNode");
            FbxMesh cubeMesh = FbxMesh.Create(scene, "cube");

            meshNode.SetNodeAttribute(cubeMesh);

            scene.GetRootNode().AddChild(meshNode);

            cubeMesh.InitControlPoints(24);

            for (int i = 0; i < cubeMesh.GetControlPointsCount(); i++)
            {
                cubeMesh.SetControlPointAt(m_controlPoints [i], i);
            }

            // A cube: 6 polygons with 4 vertices each
            int[] vertices =
            {
                0,   1,  2,  3,
                4,   5,  6,  7,
                8,   9, 10, 11,
                12, 13, 14, 15,
                16, 17, 18, 19,
                20, 21, 22, 23
            };

            for (int i = 0; i < 6; i++)
            {
                cubeMesh.BeginPolygon(pGroup: -1);
                for (int j = 0; j < 4; j++)
                {
                    cubeMesh.AddPolygon(vertices[i * 4 + j]);
                }

                cubeMesh.EndPolygon();
            }

            return(scene);
        }
Ejemplo n.º 7
0
            /// <summary>
            /// Unconditionally export this mesh object to the file.
            /// We have decided; this mesh is definitely getting exported.
            /// </summary>
            public void ExportMesh(MeshInfo mesh, FbxNode fbxNode, FbxScene fbxScene)
            {
                if (!mesh.IsValid)
                {
                    return;
                }

                NumMeshes++;
                NumTriangles += mesh.Triangles.Length / 3;
                NumVertices  += mesh.VertexCount;

                // create the mesh structure.
                FbxMesh fbxMesh = FbxMesh.Create(fbxScene, "Mesh");

                // Create control points.
                int NumControlPoints = mesh.VertexCount;

                fbxMesh.InitControlPoints(NumControlPoints);

                // copy control point data from Unity to FBX
                for (int v = 0; v < NumControlPoints; v++)
                {
                    fbxMesh.SetControlPointAt(new FbxVector4(mesh.Vertices[v].x, mesh.Vertices[v].y, mesh.Vertices[v].z), v);
                }

                /*
                 * Create polygons after FbxGeometryElementMaterial are created.
                 * TODO: Assign material indices.
                 */
                int vId = 0;

                for (int f = 0; f < mesh.Triangles.Length / 3; f++)
                {
                    fbxMesh.BeginPolygon();
                    fbxMesh.AddPolygon(mesh.Triangles [vId++]);
                    fbxMesh.AddPolygon(mesh.Triangles [vId++]);
                    fbxMesh.AddPolygon(mesh.Triangles [vId++]);
                    fbxMesh.EndPolygon();
                }

                // set the fbxNode containing the mesh
                fbxNode.SetNodeAttribute(fbxMesh);
                fbxNode.SetShadingMode(FbxNode.EShadingMode.eWireFrame);
            }
Ejemplo n.º 8
0
        protected override FbxScene CreateScene(FbxManager manager)
        {
            // Create a cube as a static mesh

            FbxScene scene = FbxScene.Create(manager, "myScene");

            FbxNode meshNode = FbxNode.Create(scene, "MeshNode");
            FbxMesh cubeMesh = FbxMesh.Create(scene, "cube");

            meshNode.SetNodeAttribute(cubeMesh);

            scene.GetRootNode().AddChild(meshNode);

            cubeMesh.InitControlPoints(24);

            for (int i = 0; i < cubeMesh.GetControlPointsCount(); i++)
            {
                cubeMesh.SetControlPointAt(m_controlPoints [i], i);
            }

            return(scene);
        }
Ejemplo n.º 9
0
        public void GeometryBase_SetControlPointAt_SetsControlPoint()
        {
            // given:
            var gb = new FbxMesh("");

            gb.InitControlPoints(4);

            // require:
            Assert.AreEqual(4, gb.GetControlPointsCount());
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(0));
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(1));
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(2));
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(3));

            // when:
            gb.SetControlPointAt(new FbxVector4(1.2, 3.4, 5.6, 7.8), 2);

            // then:
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(0));
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(1));
            Assert.AreEqual(new FbxVector4(1.2, 3.4, 5.6, 7.8), gb.GetControlPointAt(2));
            Assert.AreEqual(new FbxVector4(0, 0, 0, 0), gb.GetControlPointAt(3));
        }