Пример #1
0
        public static Mesh ImportFromCollada(mesh mesh, string vertexFormat, bool rebuildNormals = false, bool rebuildTangents = false)
        {
            var collada = new ColladaMesh();

            collada.ImportFromCollada(mesh, vertexFormat, rebuildNormals, rebuildTangents);

            var m = new Mesh();

            m.VertexFormat = VertexFormatRegistry.Resolve(vertexFormat);
            m.Name         = "Unnamed";

            m.PrimaryVertexData = new VertexData();
            var components = new List <GrannyString>();

            components.Add(new GrannyString("Position"));

            var vertexDesc = Vertex.Description(m.VertexFormat);

            if (vertexDesc.BoneWeights)
            {
                components.Add(new GrannyString("BoneWeights"));
                components.Add(new GrannyString("BoneIndices"));
            }

            components.Add(new GrannyString("Normal"));
            components.Add(new GrannyString("Tangent"));
            components.Add(new GrannyString("Binormal"));
            components.Add(new GrannyString("MaxChannel_1"));
            m.PrimaryVertexData.VertexComponentNames = components;
            m.PrimaryVertexData.Vertices             = collada.ConsolidatedVertices;

            m.PrimaryTopology         = new TriTopology();
            m.PrimaryTopology.Indices = collada.ConsolidatedIndices;
            m.PrimaryTopology.Groups  = new List <TriTopologyGroup>();
            var triGroup = new TriTopologyGroup();

            triGroup.MaterialIndex = 0;
            triGroup.TriFirst      = 0;
            triGroup.TriCount      = collada.TriangleCount;
            m.PrimaryTopology.Groups.Add(triGroup);

            m.MaterialBindings = new List <MaterialBinding>();
            m.MaterialBindings.Add(new MaterialBinding());

            // m.BoneBindings; - TODO

            m.OriginalToConsolidatedVertexIndexMap = collada.OriginalToConsolidatedVertexIndexMap;
            Utils.Info(String.Format("Imported {0} mesh ({1} tri groups, {2} tris)", (vertexDesc.BoneWeights ? "skinned" : "rigid"), m.PrimaryTopology.Groups.Count, collada.TriangleCount));

            return(m);
        }
Пример #2
0
 public Type SelectType(MemberDefinition member, StructDefinition defn, object parent)
 {
     return(VertexFormatRegistry.FindByStruct(defn));
 }
Пример #3
0
        public void ImportFromCollada(mesh mesh, string vertexFormat, bool rebuildNormals = false, bool rebuildTangents = false)
        {
            Mesh       = mesh;
            VertexType = VertexFormatRegistry.Resolve(vertexFormat);
            ImportSources();
            ImportFaces();
            ImportVertices();

            // TODO: This should be done before deduplication!
            // TODO: Move this to somewhere else ... ?
            if (!HasNormals || rebuildNormals)
            {
                if (!HasNormals)
                {
                    Utils.Info(String.Format("Channel 'NORMAL' not found, will rebuild vertex normals after import."));
                }
                computeNormals();
            }

            ImportUVs();
            if (UVInputIndices.Count() > 0)
            {
                var outVertexIndices = new Dictionary <int[], int>(new VertexIndexComparer());
                ConsolidatedIndices  = new List <int>(TriangleCount * 3);
                ConsolidatedVertices = new List <Vertex>(Vertices.Count);
                OriginalToConsolidatedVertexIndexMap = new Dictionary <int, List <int> >();
                for (var vert = 0; vert < TriangleCount * 3; vert++)
                {
                    var index = new int[Inputs.Length];
                    for (var i = 0; i < Inputs.Length; i++)
                    {
                        index[i] = Indices[vert * Inputs.Length + i];
                    }

                    int consolidatedIndex;
                    if (!outVertexIndices.TryGetValue(index, out consolidatedIndex))
                    {
                        var vertexIndex = index[VertexInputIndex];
                        consolidatedIndex = ConsolidatedVertices.Count;
                        Vertex vertex = Vertices[vertexIndex].Clone();
                        for (int uv = 0; uv < UVInputIndices.Count(); uv++)
                        {
                            vertex.SetTextureCoordinates(uv, UVs[uv][index[UVInputIndices[uv]]]);
                        }
                        outVertexIndices.Add(index, consolidatedIndex);
                        ConsolidatedVertices.Add(vertex);

                        List <int> mappedIndices = null;
                        if (!OriginalToConsolidatedVertexIndexMap.TryGetValue(vertexIndex, out mappedIndices))
                        {
                            mappedIndices = new List <int>();
                            OriginalToConsolidatedVertexIndexMap.Add(vertexIndex, mappedIndices);
                        }

                        mappedIndices.Add(consolidatedIndex);
                    }

                    ConsolidatedIndices.Add(consolidatedIndex);
                }

                Utils.Info(String.Format("Merged {0} vertices into {1} output vertices", Vertices.Count, ConsolidatedVertices.Count));
            }
            else
            {
                Utils.Info(String.Format("Mesh has no UV map, vertex consolidation step skipped."));

                ConsolidatedVertices = Vertices;

                ConsolidatedIndices = new List <int>(TriangleCount * 3);
                for (var vert = 0; vert < TriangleCount * 3; vert++)
                {
                    ConsolidatedIndices.Add(VertexIndex(vert));
                }

                OriginalToConsolidatedVertexIndexMap = new Dictionary <int, List <int> >();
                for (var i = 0; i < Vertices.Count; i++)
                {
                    OriginalToConsolidatedVertexIndexMap.Add(i, new List <int> {
                        i
                    });
                }
            }

            if (!HasTangents || rebuildTangents)
            {
                if (!HasTangents)
                {
                    Utils.Info(String.Format("Channel 'TANGENT'/'BINROMAL' not found, will rebuild vertex tangents after import."));
                }
                computeTangents();
            }
        }
Пример #4
0
        public void ImportFromCollada(mesh mesh, string vertexFormat, bool isSkinned, ExporterOptions options)
        {
            Options = options;
            Mesh    = mesh;
            ImportSources();
            ImportFaces();

            if (vertexFormat == null)
            {
                vertexFormat = FindVertexFormat(isSkinned);
            }

            VertexType = VertexFormatRegistry.Resolve(vertexFormat);
            ImportVertices();

            // TODO: This should be done before deduplication!
            // TODO: Move this to somewhere else ... ?
            if (!HasNormals || Options.RecalculateNormals)
            {
                if (!HasNormals)
                {
                    Utils.Info(String.Format("Channel 'NORMAL' not found, will rebuild vertex normals after import."));
                }
                computeNormals();
            }

            ImportColors();
            ImportUVs();
            if (UVInputIndices.Count() > 0 || ColorInputIndices.Count() > 0)
            {
                var outVertexIndices = new Dictionary <int[], int>(new VertexIndexComparer());
                ConsolidatedIndices  = new List <int>(TriangleCount * 3);
                ConsolidatedVertices = new List <Vertex>(Vertices.Count);
                OriginalToConsolidatedVertexIndexMap = new Dictionary <int, List <int> >();
                for (var vert = 0; vert < TriangleCount * 3; vert++)
                {
                    var index = new int[InputOffsetCount];
                    for (var i = 0; i < InputOffsetCount; i++)
                    {
                        index[i] = Indices[vert * InputOffsetCount + i];
                    }

                    int consolidatedIndex;
                    if (!outVertexIndices.TryGetValue(index, out consolidatedIndex))
                    {
                        var vertexIndex = index[VertexInputIndex];
                        consolidatedIndex = ConsolidatedVertices.Count;
                        Vertex vertex = Vertices[vertexIndex].Clone();
                        for (int uv = 0; uv < UVInputIndices.Count(); uv++)
                        {
                            vertex.SetUV(uv, UVs[uv][index[UVInputIndices[uv]]]);
                        }
                        for (int color = 0; color < ColorInputIndices.Count(); color++)
                        {
                            vertex.SetColor(color, Colors[color][index[ColorInputIndices[color]]]);
                        }
                        outVertexIndices.Add(index, consolidatedIndex);
                        ConsolidatedVertices.Add(vertex);

                        List <int> mappedIndices = null;
                        if (!OriginalToConsolidatedVertexIndexMap.TryGetValue(vertexIndex, out mappedIndices))
                        {
                            mappedIndices = new List <int>();
                            OriginalToConsolidatedVertexIndexMap.Add(vertexIndex, mappedIndices);
                        }

                        mappedIndices.Add(consolidatedIndex);
                    }

                    ConsolidatedIndices.Add(consolidatedIndex);
                }

                Utils.Info(String.Format("Merged {0} vertices into {1} output vertices", Vertices.Count, ConsolidatedVertices.Count));
            }
            else
            {
                Utils.Info(String.Format("Mesh has no UV map, vertex consolidation step skipped."));

                ConsolidatedVertices = Vertices;

                ConsolidatedIndices = new List <int>(TriangleCount * 3);
                for (var vert = 0; vert < TriangleCount * 3; vert++)
                {
                    ConsolidatedIndices.Add(VertexIndex(vert));
                }

                OriginalToConsolidatedVertexIndexMap = new Dictionary <int, List <int> >();
                for (var i = 0; i < Vertices.Count; i++)
                {
                    OriginalToConsolidatedVertexIndexMap.Add(i, new List <int> {
                        i
                    });
                }
            }

            var description = Vertex.Description(VertexType);

            if ((description.Tangent && description.Binormal) && (!HasTangents || Options.RecalculateTangents))
            {
                if (!HasTangents)
                {
                    Utils.Info(String.Format("Channel 'TANGENT'/'BINROMAL' not found, will rebuild vertex tangents after import."));
                }
                computeTangents();
            }
        }