Esempio n. 1
0
        private static void RecursivlyWriteDAEJoints(DAEWriter writer, Node joint, string parentName, Matrix4 parentTransform)
        {
            var transform = ModelImporter.FromMatrix(joint.Transform);

            float[] Transform = new float[] { transform.M11, transform.M21, transform.M31, transform.M41,
                                              transform.M12, transform.M22, transform.M32, transform.M42,
                                              transform.M13, transform.M23, transform.M33, transform.M43,
                                              transform.M14, transform.M24, transform.M34, transform.M44 };

            Matrix4 InvWorldTransform = (transform * parentTransform).Inverted();

            float[] InvTransform = new float[] { InvWorldTransform.M11, InvWorldTransform.M21, InvWorldTransform.M31, InvWorldTransform.M41,
                                                 InvWorldTransform.M12, InvWorldTransform.M22, InvWorldTransform.M32, InvWorldTransform.M42,
                                                 InvWorldTransform.M13, InvWorldTransform.M23, InvWorldTransform.M33, InvWorldTransform.M43,
                                                 InvWorldTransform.M14, InvWorldTransform.M24, InvWorldTransform.M34, InvWorldTransform.M44 };

            writer.AddJoint(joint.Name, parentName, Transform, InvTransform);

            foreach (var child in joint.Children)
            {
                RecursivlyWriteDAEJoints(writer, child, joint.Name, transform * parentTransform);
            }
        }
Esempio n. 2
0
        private static void ExportCustomDAE(string filePath, Scene scene, ModelExportSettings settings)
        {
            using (DAEWriter writer = new DAEWriter(filePath, true))
            {
                var path = System.IO.Path.GetDirectoryName(filePath) + '\\';

                writer.WriteAsset();

                //DialogResult dialogResult = MessageBox.Show("Export Materials and Textures?", "DAE Exporter", MessageBoxButtons.YesNo);
                //if (dialogResult == DialogResult.Yes)
                {
                    List <string> TextureNames = new List <string>();

                    foreach (var tex in scene.Materials)
                    {
                        if (!TextureNames.Contains(tex.TextureDiffuse.FilePath))
                        {
                            TextureNames.Add(tex.TextureDiffuse.FilePath);
                        }
                    }

                    writer.WriteLibraryImages(TextureNames.ToArray(), ".png");

                    writer.StartMaterialSection();
                    foreach (var mat in scene.Materials)
                    {
                        writer.WriteMaterial(mat.Name);
                    }
                    writer.EndMaterialSection();

                    writer.StartEffectSection();
                    foreach (var mat in scene.Materials)
                    {
                        writer.WriteEffect(mat.Name, mat.TextureDiffuse.FilePath);
                    }
                    writer.EndEffectSection();
                }
                //else
                //    writer.WriteLibraryImages();

                var rootSkeleton = scene.RootNode.Children[0];

                RecursivlyWriteDAEJoints(writer, rootSkeleton, "", Matrix4.Identity);

                writer.StartGeometrySection();
                for (int i = 1; i < scene.RootNode.Children.Count; i++)
                {
                    var node = scene.RootNode.Children[i];

                    writer.StartGeometryMesh(node.Name);

                    List <uint>  triangles = new List <uint>();
                    List <float> vertices  = new List <float>();
                    List <float> nrm       = new List <float>();
                    List <float> uv0       = new List <float>();
                    List <float> uv1       = new List <float>();
                    List <float> clr0      = new List <float>();
                    List <float> clr1      = new List <float>();

                    List <List <int> >   BoneIndicies = new List <List <int> >();
                    List <List <float> > BoneWeights  = new List <List <float> >();

                    foreach (var meshIndex in node.MeshIndices)
                    {
                        var vertexOffset = vertices.Count / 3;

                        var mesh = scene.Meshes[meshIndex];

                        if (!mesh.HasFaces || !mesh.HasVertices)
                        {
                            continue;
                        }

                        if (scene.Materials.Count != 0)
                        {
                            writer.CurrentMaterial = scene.Materials[mesh.MaterialIndex].Name;
                        }

                        foreach (var face in mesh.Faces)
                        {
                            for (var k = 0; k < 3; k++)
                            {
                                triangles.Add((uint)(vertexOffset + face.Indices[2 - k]));
                            }
                        }

                        if (mesh.HasBones)
                        {
                            for (int v = 0; v < mesh.VertexCount; v++)
                            {
                                BoneIndicies.Add(new List <int>());
                                BoneWeights.Add(new List <float>());
                            }

                            foreach (var bone in mesh.Bones)
                            {
                                if (bone.HasVertexWeights)
                                {
                                    foreach (var w in bone.VertexWeights)
                                    {
                                        BoneIndicies[w.VertexID + vertexOffset].Add(writer.GetJointIndex(bone.Name));
                                        BoneWeights[w.VertexID + vertexOffset].Add(w.Weight);
                                    }
                                }
                            }
                        }

                        vertices.AddRange(ToFloatArray(mesh.Vertices));

                        if (mesh.HasNormals)
                        {
                            nrm.AddRange(ToFloatArray(mesh.Normals));
                        }

                        if (mesh.HasTextureCoords(0))
                        {
                            uv0.AddRange(ToUVFloatArray(mesh.TextureCoordinateChannels[0], settings.FlipUVs));
                        }

                        if (mesh.HasTextureCoords(1))
                        {
                            uv0.AddRange(ToUVFloatArray(mesh.TextureCoordinateChannels[1], settings.FlipUVs));
                        }

                        if (mesh.HasVertexColors(0))
                        {
                            clr0.AddRange(ToFloatArray(mesh.VertexColorChannels[0]));
                        }

                        if (mesh.HasVertexColors(1))
                        {
                            clr0.AddRange(ToFloatArray(mesh.VertexColorChannels[1]));
                        }
                    }

                    var triArr = triangles.ToArray();

                    writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.POSITION, vertices.ToArray(), triArr);

                    if (nrm.Count > 0)
                    {
                        writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.NORMAL, nrm.ToArray(), triArr);
                    }

                    if (uv0.Count > 0)
                    {
                        writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.TEXCOORD, uv0.ToArray(), triArr, 0);
                    }

                    if (uv1.Count > 0)
                    {
                        writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.TEXCOORD, uv1.ToArray(), triArr, 1);
                    }

                    if (clr0.Count > 0)
                    {
                        writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.COLOR, clr0.ToArray(), triArr, 0);
                    }

                    if (clr1.Count > 0)
                    {
                        writer.WriteGeometrySource(node.Name, DAEWriter.VERTEX_SEMANTIC.COLOR, clr0.ToArray(), triArr, 1);
                    }

                    writer.AttachGeometryController(BoneIndicies, BoneWeights);

                    writer.EndGeometryMesh();
                }
                writer.EndGeometrySection();
            }
        }