コード例 #1
0
        private static STGenericMesh LoadMeshData(ColladaScene scene, Node node,
                                                  geometry geom, library_materials materials, controller controller = null)
        {
            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            return(mesh);
        }
コード例 #2
0
ファイル: FileDaeWrite.cs プロジェクト: katascope/Glyphics2
            public static void WriteMesh(string filename, RectList rects)
            {
                List <ulong> palette = FindRgbaPalette(rects);

                _nodeid = 1;

                var scene = new ColladaScene {
                    Id = _nodeid++
                };

                var collada = new Collada();

                collada.Scenes.Add(scene);

                foreach (ulong rgba in palette)
                {
                    var effect = new ColladaEffect();
                    Converter.Ulong2Rgba(rgba, out effect.R, out effect.G, out effect.B, out effect.A);

                    var material = new ColladaMaterial {
                        Id = _nodeid++
                    };
                    effect.Id    = _nodeid++;
                    material.Url = effect.Id;
                    effect.Rgba  = rgba;

                    collada.Materials.Add(material);
                    collada.Effects.Add(effect);
                }

                foreach (Rect rect in rects)
                {
                    var geom = new ColladaGeometry
                    {
                        Id          = _nodeid++,
                        PositionId  = _nodeid++,
                        NormalId    = _nodeid++,
                        PositionId2 = _nodeid++,
                        NormalId2   = _nodeid++,
                        VertexId    = _nodeid++
                    };

                    int material = collada.FindMaterial(rect.Properties.Rgba);
                    geom.Materialid = material;
                    geom.X1         = rect.Pt1[0];
                    geom.Y1         = rect.Pt1[1];
                    geom.Z1         = rect.Pt1[2];
                    geom.X2         = rect.Pt2[0];
                    geom.Y2         = rect.Pt2[1];
                    geom.Z2         = rect.Pt2[2];

                    collada.Geometries.Add(geom);
                    scene.Geometry.Add(geom.Id);
                }


                collada.WriteMesh(filename);
            }
コード例 #3
0
        public static Node LoadHiearchy(Node parent, node daeNode,
                                        STGenericModel model, ColladaScene colladaScene)
        {
            Node node = new Node(parent);

            node.Name      = daeNode.name;
            node.Type      = daeNode.type;
            node.Transform = DaeUtility.GetMatrix(daeNode.Items) * parent.Transform;

            if (daeNode.instance_geometry != null)
            {
                geometry geom = DaeUtility.FindGeoemertyFromNode(daeNode, colladaScene.geometries);
                model.Meshes.Add(LoadMeshData(colladaScene, node, geom, colladaScene.materials));
            }
            if (daeNode.instance_controller != null)
            {
                controller controller = DaeUtility.FindControllerFromNode(daeNode, colladaScene.controllers);
                geometry   geom       = DaeUtility.FindGeoemertyFromController(controller, colladaScene.geometries);
                model.Meshes.Add(LoadMeshData(colladaScene, node, geom, colladaScene.materials, controller));
            }

            try
            {
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to convert mesh {daeNode.name} \n {ex.ToString()}");
            }

            //Find the root bone
            if (node.Type == NodeType.JOINT)
            {
                //Apply axis rotation
                Matrix4 boneTransform = Matrix4.Identity;
                if (colladaScene.UpAxisType == UpAxisType.Y_UP)
                {
                    //  boneTransform = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(90));
                }
                if (colladaScene.UintSize != null && colladaScene.UintSize.meter != 1)
                {
                    var scale = ApplyUintScaling(colladaScene, new Vector3(1));
                    boneTransform *= Matrix4.CreateScale(scale);
                }

                LoadBoneHiearchy(daeNode, model, null, ref boneTransform);
            }
            else if (daeNode.node1 != null)
            {
                foreach (node child in daeNode.node1)
                {
                    node.Children.Add(LoadHiearchy(node, child, model, colladaScene));
                }
            }

            return(node);
        }
コード例 #4
0
        public static STGenericMaterial LoadMaterial(ColladaScene scene, material daeMat)
        {
            STGenericMaterial mat = new STGenericMaterial();

            mat.Name = daeMat.id;

            if (daeMat.instance_effect != null)
            {
                var effectid = daeMat.instance_effect.url.Remove(0, 1);
                if (scene.effectLookup.ContainsKey(effectid))
                {
                    var effect = scene.effectLookup[effectid];
                    if (effect.Items == null)
                    {
                        return(mat);
                    }

                    foreach (var item in effect.Items)
                    {
                        if (item.Items == null)
                        {
                            continue;
                        }

                        foreach (var param in item.Items)
                        {
                            if (param is common_newparam_type)
                            {
                                var newparam = (common_newparam_type)param;
                                if (newparam.ItemElementName == ItemChoiceType.surface)
                                {
                                    var surface = newparam.Item as fx_surface_common;
                                    var name    = surface.init_from[0].Value;
                                    mat.TextureMaps.Add(new STGenericTextureMap()
                                    {
                                        Name = name,
                                        Type = STTextureType.Diffuse,
                                    });
                                }
                            }
                            if (param is technique)
                            {
                                var technique = (technique)param;
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"cannot find id! {effectid}");
                }
            }
            return(mat);
        }
コード例 #5
0
        public static Node LoadScene(visual_scene visualScene,
                                     STGenericModel model, ColladaScene colladaScene)
        {
            Node node = new Node(null);

            node.Name = visualScene.name;

            foreach (node child in visualScene.node)
            {
                node.Children.Add(LoadHiearchy(node, child, model, colladaScene));
            }
            return(node);
        }
コード例 #6
0
ファイル: Collada.cs プロジェクト: Suceru/SCPAK
 public ColladaRoot(XElement node)
 {
     Asset = new Asset(node.Element(ColladaRoot.Namespace + "asset"));
     foreach (XElement current in node.Elements(ColladaRoot.Namespace + "library_geometries"))
     {
         LibraryGeometries.Add(new ColladaLibraryGeometries(this, current));
     }
     foreach (XElement current2 in node.Elements(ColladaRoot.Namespace + "library_visual_scenes"))
     {
         LibraryVisualScenes.Add(new ColladaLibraryVisualScenes(this, current2));
     }
     Scene = new ColladaScene(this, node.Element(ColladaRoot.Namespace + "scene"));
 }
コード例 #7
0
        private static STGenericMesh LoadMeshData(ColladaScene scene, Node node,
                                                  geometry geom, library_materials materials, controller controller = null)
        {
            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            foreach (var vertex in mesh.Vertices)
            {
                if (scene.Settings.FlipUVsVertical)
                {
                    for (int i = 0; i < vertex.TexCoords.Length; i++)
                    {
                        vertex.TexCoords[i] = new Vector2(vertex.TexCoords[i].X, 1 - vertex.TexCoords[i].Y);
                    }
                }

                vertex.Position = Vector3.TransformPosition(vertex.Position, node.Transform);
                vertex.Position = Vector3.TransformPosition(vertex.Position, Matrix4.CreateScale(0.01f));

                //vertex.Normal = Vector3.TransformNormal(vertex.Normal, node.Transform);
            }

            return(mesh);
        }
コード例 #8
0
ファイル: Collada.cs プロジェクト: Lixue9jiu/SCPAK
 public ColladaRoot(XElement node)
 {
     if (node.GetDefaultNamespace() != Namespace)
     {
         throw new NotSupportedException("given collada viersion is not supported: " + node.GetDefaultNamespace());
     }
     Asset = new Asset(node.Element(ColladaRoot.Namespace + "asset"));
     foreach (XElement current in node.Elements(ColladaRoot.Namespace + "library_geometries"))
     {
         LibraryGeometries.Add(new ColladaLibraryGeometries(this, current));
     }
     foreach (XElement current2 in node.Elements(ColladaRoot.Namespace + "library_visual_scenes"))
     {
         LibraryVisualScenes.Add(new ColladaLibraryVisualScenes(this, current2));
     }
     Scene = new ColladaScene(this, node.Element(ColladaRoot.Namespace + "scene"));
 }
コード例 #9
0
        public static Node LoadScene(visual_scene visualScene,
                                     STGenericModel model, ColladaScene colladaScene)
        {
            Node node = new Node(null);

            node.Name = visualScene.name;

            foreach (node child in visualScene.node)
            {
                node.Children.Add(LoadHiearchy(node, child, model, colladaScene));
            }

            //Transform all meshes by node transform
            //TODO //   foreach (var child in node.Children)
            //  TransformMeshInstanced(model, child);

            NodeInstanceTransform.Clear();
            return(node);
        }
コード例 #10
0
        private static Vector3 ApplyUintScaling(ColladaScene scene, Vector3 value)
        {
            if (scene.UintSize == null)
            {
                return(value);
            }

            Vector3 val   = value;
            float   scale = (float)scene.UintSize.meter;

            //Convert scale to centimeters then multiple the vertex
            switch (scene.UintSize.name)
            {
            case "meter":
                scale *= 100;
                break;
            }

            val *= scale;
            return(val);
        }
コード例 #11
0
        static List <STGenericTexture> LoadTextures(string folder, ColladaScene scene)
        {
            if (scene.images == null || scene.images.image == null)
            {
                return(new List <STGenericTexture>());
            }

            List <STGenericTexture> textures = new List <STGenericTexture>();

            foreach (var image in scene.images.image)
            {
                if (image.Item is string)
                {
                    string path = (string)image.Item;
                    if (path.StartsWith("file://"))
                    {
                        path = path.Substring(7, path.Length - 7);
                    }

                    if (File.Exists($"{folder}/{path}"))
                    {
                        var textureFile = LoadImage($"{folder}/{path}");
                        textureFile.Name = image.id;
                        textures.Add(textureFile);
                    }
                    if (File.Exists(path))
                    {
                        var textureFile = LoadImage(path);
                        textureFile.Name = image.id;
                        textures.Add(textureFile);
                    }
                }
                if (image.Item is byte[]) //Embedded. Todo
                {
                }
            }


            return(textures);
        }
コード例 #12
0
        private void loadDae(Mesh target)
        {
            gameWindow.log("load Collada: " + target.pointer);

            ColladaScene colladaScene = new ColladaScene(target.pointer);

            colladaScene.saveTo(ref target);

            target.loaded = true;

            if (target.type != Mesh.Type.empty)
            {
                parseFaceList(ref target, false);

                generateVBO(ref target);
            }

            if (target.identifier != -1)
            {
                meshes[target.identifier] = target;
            }
        }
コード例 #13
0
        private void loadManagedCollada(Mesh target)
        {
            gameWindow.log("load Managed Collada: " + target.pointer);

            ColladaScene colladaScene = new ColladaScene(gameWindow.modelFolder + target.pointer);

            colladaScene.stepSize = 1.0f / target.animationFps;
            colladaScene.appendAnimations(target.animationData);
            colladaScene.saveTo(ref target);

            target.loaded = true;

            if (target.type != Mesh.Type.empty)
            {
                parseFaceList(ref target, false);

                generateVBO(ref target);
            }

            if (target.identifier != -1)
            {
                meshes[target.identifier] = target;
            }
        }
コード例 #14
0
        private static List <BoneWeight[]> ParseWeightController(controller controller, ColladaScene scene)
        {
            if (controller == null)
            {
                return(new List <BoneWeight[]>());
            }

            List <BoneWeight[]> boneWeights = new List <BoneWeight[]>();
            skin skin = controller.Item as skin;

            string[] skinningCounts = skin.vertex_weights.vcount.Trim(' ').Split(' ');
            string[] indices        = skin.vertex_weights.v.Trim(' ').Split(' ');

            int maxSkinning = (int)scene.Settings.MaxSkinningCount;
            int stride      = skin.vertex_weights.input.Length;
            int indexOffset = 0;

            for (int v = 0; v < skinningCounts.Length; v++)
            {
                int numSkinning = Convert.ToInt32(skinningCounts[v]);

                BoneWeight[] boneWeightsArr = new BoneWeight[Math.Min(maxSkinning, numSkinning)];
                for (int j = 0; j < numSkinning; j++)
                {
                    if (j < scene.Settings.MaxSkinningCount)
                    {
                        boneWeightsArr[j] = new BoneWeight();
                        foreach (var input in skin.vertex_weights.input)
                        {
                            int offset = (int)input.offset;
                            var source = DaeUtility.FindSourceFromInput(input, skin.source);
                            int index  = Convert.ToInt32(indices[indexOffset + offset]);
                            if (input.semantic == "WEIGHT")
                            {
                                var weights = source.Item as float_array;
                                boneWeightsArr[j].Weight = (float)weights.Values[index];
                            }
                            if (input.semantic == "JOINT")
                            {
                                var bones = source.Item as Name_array;
                                boneWeightsArr[j].Bone = bones.Values[index];
                            }
                        }
                    }
                    indexOffset += stride;
                }

                boneWeightsArr = RemoveZeroWeights(boneWeightsArr);
                boneWeights.Add(boneWeightsArr);
            }

            return(boneWeights);
        }
コード例 #15
0
        private static void ParseVertexSource(ref STVertex vertex, ColladaScene scene, source source,
                                              int numTexCoordChannels, int numColorChannels, int stride, int index, int set, string semantic)
        {
            float_array array = source.Item as float_array;

            switch (semantic)
            {
            case "VERTEX":
            case "POSITION":
                vertex.Position = new Vector3(
                    (float)array.Values[index + 0],
                    (float)array.Values[index + 1],
                    (float)array.Values[index + 2]);
                vertex.Position = ApplyUintScaling(scene, vertex.Position);
                if (scene.UpAxisType == UpAxisType.Z_UP)
                {
                    vertex.Position = new Vector3(
                        vertex.Position.X,
                        vertex.Position.Z,
                        -vertex.Position.Y);
                }
                break;

            case "NORMAL":
                vertex.Normal = new Vector3(
                    (float)array.Values[index + 0],
                    (float)array.Values[index + 1],
                    (float)array.Values[index + 2]);
                if (scene.UpAxisType == UpAxisType.Z_UP)
                {
                    vertex.Normal = new Vector3(
                        vertex.Normal.X,
                        vertex.Normal.Z,
                        -vertex.Normal.Y);
                }
                break;

            case "TEXCOORD":
                vertex.TexCoords[set] = new Vector2(
                    (float)array.Values[index + 0],
                    (float)array.Values[index + 1]);
                break;

            case "COLOR":
                float R = 1, G = 1, B = 1, A = 1;
                if (stride >= 1)
                {
                    R = (float)array.Values[index + 0];
                }
                if (stride >= 2)
                {
                    G = (float)array.Values[index + 1];
                }
                if (stride >= 3)
                {
                    B = (float)array.Values[index + 2];
                }
                if (stride >= 4)
                {
                    A = (float)array.Values[index + 3];
                }
                vertex.Colors[set] = new Vector4(R, G, B, A);
                break;
            }

            //We need to make sure the axis is converted to Z up

            /*  switch (scene.UpAxisType)
             * {
             *    case UpAxisType.X_UP:
             *        break;
             *    case UpAxisType.Y_UP:
             *        Vector3 pos = vertex.Position;
             *        Vector3 nrm = vertex.Normal;
             *        //   vertex.Position = new Vector3(pos.X, pos.Z, pos.Y);
             *        //    vertex.Normal = new Vector3(nrm.X, nrm.Z, nrm.Y);
             *        break;
             * }*/
        }
コード例 #16
0
        private static void ConvertPolygon(ColladaScene scene, STGenericMesh mesh, mesh daeMesh,
                                           InputLocalOffset[] inputs, List <BoneWeight[]> boneWeights,
                                           library_materials materials, string material, string polys, int polyCount, string vcount = "")
        {
            List <uint> faces = new List <uint>();

            STPolygonGroup group = new STPolygonGroup();

            mesh.PolygonGroups.Add(group);
            group.MaterialIndex = DaeUtility.FindMaterialIndex(materials, material);

            string[] indices     = polys.Trim(' ').Split(' ');
            string[] vertexCount = new string[0];
            if (vcount != string.Empty)
            {
                vertexCount = vcount.Trim(' ').Split(' ');
            }

            int stride = 0;

            for (int i = 0; i < inputs.Length; i++)
            {
                stride = Math.Max(0, (int)inputs[i].offset + 1);
            }

            //Create a current list of all the vertices
            //Use a list to expand duplicate indices
            List <Vertex> vertices     = new List <Vertex>();
            var           vertexSource = DaeUtility.FindSourceFromInput(daeMesh.vertices.input[0], daeMesh.source);
            var           floatArr     = vertexSource.Item as float_array;

            for (int v = 0; v < (int)floatArr.count / 3; v++)
            {
                vertices.Add(new Vertex(vertices.Count, new List <int>()));
            }

            var indexStride = (indices.Length / 3) / polyCount;

            for (int i = 0; i < polyCount; i++)
            {
                int count = 3;
                if (vertexCount.Length > i)
                {
                    count = Convert.ToInt32(vertexCount[i]);
                }

                for (int v = 0; v < count; v++)
                {
                    List <int> semanticIndices = new List <int>();
                    for (int j = 0; j < inputs.Length; j++)
                    {
                        int faceOffset = (indexStride * 3) * i;
                        int index      = Convert.ToInt32(indices[faceOffset + (v * indexStride) + (int)inputs[j].offset]);
                        semanticIndices.Add(index);
                    }

                    BoneWeight[] boneWeightData = new BoneWeight[0];
                    if (boneWeights?.Count > semanticIndices[0])
                    {
                        boneWeightData = boneWeights[semanticIndices[0]];
                    }

                    VertexLoader.LoadVertex(ref faces, ref vertices, semanticIndices, boneWeightData);
                }
            }

            int numTexCoordChannels = 0;
            int numColorChannels    = 0;

            //Find them in both types of inputs
            for (int i = 0; i < inputs.Length; i++)
            {
                if (inputs[i].semantic == "TEXCOORD")
                {
                    numTexCoordChannels++;
                }
                if (inputs[i].semantic == "COLOR")
                {
                    numColorChannels++;
                }
            }

            for (int i = 0; i < daeMesh.vertices.input.Length; i++)
            {
                if (daeMesh.vertices.input[i].semantic == "TEXCOORD")
                {
                    numTexCoordChannels++;
                }
                if (daeMesh.vertices.input[i].semantic == "COLOR")
                {
                    numColorChannels++;
                }
            }


            for (int i = 0; i < vertices.Count; i++)
            {
                if (!vertices[i].IsSet)
                {
                    vertices.Remove(vertices[i]);
                }
            }

            bool hasNormals = false;

            foreach (var daeVertex in vertices)
            {
                if (daeVertex.semanticIndices.Count == 0)
                {
                    continue;
                }

                STVertex vertex = new STVertex();
                vertex.TexCoords   = new Vector2[numTexCoordChannels];
                vertex.Colors      = new Vector4[numColorChannels];
                vertex.BoneWeights = daeVertex.BoneWeights.ToList();
                mesh.Vertices.Add(vertex);

                //DAE has 2 inputs. Vertex and triangle inputs
                //Triangle inputs use indices over vertex inputs which only use one
                //Triangle inputs allow multiple color/uv sets unlike vertex inputs
                //Certain programs ie Noesis DAEs use vertex inputs. Most programs use triangle inputs
                for (int j = 0; j < daeMesh.vertices.input.Length; j++)
                {
                    //Vertex inputs only use the first index
                    var    vertexInput = daeMesh.vertices.input[j];
                    source source      = DaeUtility.FindSourceFromInput(vertexInput, daeMesh.source);
                    if (source == null)
                    {
                        continue;
                    }
                    if (vertexInput.semantic == "NORMAL")
                    {
                        hasNormals = true;
                    }

                    int dataStride = (int)source.technique_common.accessor.stride;
                    int index      = daeVertex.semanticIndices[0] * dataStride;

                    ParseVertexSource(ref vertex, scene, source, numTexCoordChannels, numColorChannels,
                                      dataStride, index, 0, vertexInput.semantic);
                }

                for (int i = 0; i < inputs.Length; i++)
                {
                    var    input  = inputs[i];
                    source source = DaeUtility.FindSourceFromInput(input, daeMesh.source);
                    if (source == null)
                    {
                        continue;
                    }
                    if (input.semantic == "NORMAL")
                    {
                        hasNormals = true;
                    }

                    int dataStride = (int)source.technique_common.accessor.stride;
                    int index      = daeVertex.semanticIndices[i] * dataStride;

                    ParseVertexSource(ref vertex, scene, source, numTexCoordChannels, numColorChannels,
                                      dataStride, index, (int)input.set, input.semantic);
                }
            }

            group.Faces = faces;

            if (!hasNormals)
            {
                CalculateNormals(mesh.Vertices, faces);
            }
            if (scene.Settings.RemoveDuplicateVerts)
            {
                RemoveDuplicateVerts(mesh.Vertices, faces);
            }
        }
コード例 #17
0
        private static List <STGenericMesh> LoadMeshData(ColladaScene scene, Node node,
                                                         geometry geom, library_materials materials, controller controller = null)
        {
            List <STGenericMesh> meshes = new List <STGenericMesh>();

            mesh daeMesh = geom.Item as mesh;

            STGenericMesh mesh = new STGenericMesh();

            mesh.Vertices = new List <STVertex>();
            mesh.Name     = geom.name;
            meshes.Add(mesh);

            var boneWeights = ParseWeightController(controller, scene);

            foreach (var item in daeMesh.Items)
            {
                //Poly lists can control specific amounts of indices for primitive types like quads
                if (item is polylist)
                {
                    var poly = item as polylist;
                    ConvertPolygon(scene, mesh, daeMesh, poly.input,
                                   boneWeights, materials, poly.material, poly.p, (int)poly.count, poly.vcount);
                }
                else if (item is triangles)
                {
                    var triangle = item as triangles;
                    ConvertPolygon(scene, mesh, daeMesh, triangle.input,
                                   boneWeights, materials, triangle.material, triangle.p, (int)triangle.count);
                }
            }

            for (int v = 0; v < mesh.Vertices.Count; v++)
            {
                if (scene.Settings.FlipUVsVertical)
                {
                    for (int i = 0; i < mesh.Vertices[v].TexCoords.Length; i++)
                    {
                        mesh.Vertices[v].TexCoords[i] = new Vector2(mesh.Vertices[v].TexCoords[i].X, 1 - mesh.Vertices[v].TexCoords[i].Y);
                    }
                }

                mesh.Vertices[v].Position = Vector3.TransformPosition(mesh.Vertices[v].Position, node.Transform);
                mesh.Vertices[v].Normal   = Vector3.TransformNormal(mesh.Vertices[v].Normal, node.Transform);
            }

            if (controller != null)
            {
                skin skin       = controller.Item as skin;
                var  bindMatrix = CreateBindMatrix(skin.bind_shape_matrix);
                Console.WriteLine($"bindMatrix {bindMatrix.ExtractScale()}");
                bindMatrix = bindMatrix.ClearScale();
                for (int v = 0; v < mesh.Vertices.Count; v++)
                {
                    mesh.Vertices[v].Position = Vector3.TransformPosition(mesh.Vertices[v].Position, bindMatrix);
                    mesh.Vertices[v].Normal   = Vector3.TransformNormal(mesh.Vertices[v].Normal, bindMatrix);
                }
            }

            //  meshes = STGenericMesh.SeperatePolygonGroups<STGenericMesh>(mesh);

            if (scene.Settings.RemoveDuplicateVerts)
            {
                // mesh.RemoveDuplicateVertices();
            }

            return(meshes);
        }
コード例 #18
0
        public static STGenericScene Read(string fileName, DAE.ImportSettings settings = null)
        {
            if (settings == null)
            {
                settings = new DAE.ImportSettings();
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            STGenericScene Scene = new STGenericScene();

            COLLADA      collada      = COLLADA.Load(fileName);
            ColladaScene colladaScene = new ColladaScene(collada, settings);

            //Usually there is only one scene, but it can be possible some tools use multiple per model
            //Each one contains node hiearchies for bones and meshes
            foreach (var scene in colladaScene.scenes.visual_scene)
            {
                var  model = new STGenericModel(scene.name);
                Node Root  = LoadScene(scene, model, colladaScene);
                Scene.Models.Add(model);

                if (colladaScene.materials != null)
                {
                    foreach (var mat in colladaScene.materials.material)
                    {
                        model.Materials.Add(LoadMaterial(mat));
                    }
                }
                else
                {
                    model.Materials.Add(new STGenericMaterial()
                    {
                        Name = "Dummy"
                    });
                }

                if (model.Skeleton.Bones.Count == 0)
                {
                    model.Skeleton.Bones.Add(new STBone(model.Skeleton, "root"));
                }

                if (settings.FixDuplicateNames)
                {
                    //Adjust duplicate names

                    /*    foreach (var mesh in model.Meshes)
                     *  {
                     *      var names = model.Meshes.Select(x => x.Name).ToList();
                     *      mesh.Name = Utility.RenameDuplicateString(names, mesh.Name, 0, 2);
                     *  }
                     *
                     *  foreach (var mat in model.Materials)
                     *  {
                     *      var names = model.Materials.Select(x => x.Name).ToList();
                     *      mat.Name = Utility.RenameDuplicateString(names, mat.Name, 0, 2);
                     *  }
                     *
                     *  foreach (var bone in model.Skeleton.Bones)
                     *  {
                     *      var names = model.Skeleton.Bones.Select(x => x.Name).ToList();
                     *      bone.Name = Utility.RenameDuplicateString(names, bone.Name, 0, 2);
                     *  }*/
                }
            }

            sw.Stop();
            Console.WriteLine("Elapsed={0}", sw.Elapsed);

            return(Scene);
        }
コード例 #19
0
        static STGenericScene Read(COLLADA collada, DAE.ImportSettings settings)
        {
            if (settings == null)
            {
                settings = new DAE.ImportSettings();
            }

            string    folder = settings.FolderPath;
            Stopwatch sw     = new Stopwatch();

            sw.Start();

            STGenericScene Scene        = new STGenericScene();
            ColladaScene   colladaScene = new ColladaScene(collada, settings);

            BoneNameIds.Clear();

            //Usually there is only one scene, but it can be possible some tools use multiple per model
            //Each one contains node hiearchies for bones and meshes
            foreach (var scene in colladaScene.scenes.visual_scene)
            {
                var model = new STGenericModel(scene.name);
                model.Textures = LoadTextures(folder, colladaScene).OrderBy(x => x.Name).ToList();
                Node Root = LoadScene(scene, model, colladaScene);
                Scene.Models.Add(model);

                if (colladaScene.materials != null)
                {
                    foreach (var mat in colladaScene.materials.material)
                    {
                        model.Materials.Add(LoadMaterial(colladaScene, mat));
                    }
                }
                else
                {
                    model.Materials.Add(new STGenericMaterial()
                    {
                        Name = "Dummy"
                    });
                }

                if (model.Skeleton.Bones.Count == 0)
                {
                    model.Skeleton.Bones.Add(new STBone(model.Skeleton, "root"));
                }

                //Setup bone indices
                foreach (var mesh in model.Meshes)
                {
                    for (int v = 0; v < mesh.Vertices.Count; v++)
                    {
                        for (int j = 0; j < mesh.Vertices[v].BoneNames.Count; j++)
                        {
                            string sid  = mesh.Vertices[v].BoneNames[j];
                            string name = BoneNameIds.ContainsKey(sid) ? BoneNameIds[sid] : sid;
                            mesh.Vertices[v].BoneNames[j] = name;

                            int index = model.Skeleton.Bones.FindIndex(x => x.Name == name);
                            if (index != -1)
                            {
                                mesh.Vertices[v].BoneIndices.Add(index);
                            }
                        }
                    }

                    foreach (var group in mesh.PolygonGroups)
                    {
                        if (group.MaterialIndex < model.Materials.Count && group.MaterialIndex != -1)
                        {
                            group.Material = model.Materials[group.MaterialIndex];
                        }
                    }

                    if (colladaScene.UpAxisType == UpAxisType.Z_UP)
                    {
                        for (int v = 0; v < mesh.Vertices.Count; v++)
                        {
                            mesh.Vertices[v].Position = new Vector3(
                                mesh.Vertices[v].Position.X,
                                mesh.Vertices[v].Position.Z,
                                -mesh.Vertices[v].Position.Y);
                            mesh.Vertices[v].Normal = new Vector3(
                                mesh.Vertices[v].Normal.X,
                                mesh.Vertices[v].Normal.Z,
                                -mesh.Vertices[v].Normal.Y);
                        }
                    }
                }

                if (settings.FixDuplicateNames)
                {
                    //Adjust duplicate names

                    /*    foreach (var mesh in model.Meshes)
                     *  {
                     *      var names = model.Meshes.Select(x => x.Name).ToList();
                     *      mesh.Name = Utility.RenameDuplicateString(names, mesh.Name, 0, 2);
                     *  }
                     *
                     *  foreach (var mat in model.Materials)
                     *  {
                     *      var names = model.Materials.Select(x => x.Name).ToList();
                     *      mat.Name = Utility.RenameDuplicateString(names, mat.Name, 0, 2);
                     *  }
                     *
                     *  foreach (var bone in model.Skeleton.Bones)
                     *  {
                     *      var names = model.Skeleton.Bones.Select(x => x.Name).ToList();
                     *      bone.Name = Utility.RenameDuplicateString(names, bone.Name, 0, 2);
                     *  }*/
                }
            }

            BoneNameIds.Clear();

            sw.Stop();
            Console.WriteLine("DAE Elapsed={0}", sw.Elapsed);

            return(Scene);
        }