예제 #1
0
        private void ImportVertices()
        {
            var vertexSemantics = new Dictionary <String, List <Vector3> >();

            foreach (var input in Mesh.vertices.input)
            {
                ColladaSource inputSource = FindSource(input.source);
                var           vertices    = ColladaHelpers.SourceToPositions(inputSource);
                vertexSemantics.Add(input.semantic, vertices);
            }

            List <Vector3> vertexPositions    = null;
            List <Vector3> perVertexNormals   = null;
            List <Vector3> perVertexTangents  = null;
            List <Vector3> perVertexBinormals = null;

            vertexSemantics.TryGetValue("POSITION", out vertexPositions);
            vertexSemantics.TryGetValue("NORMAL", out perVertexNormals);
            vertexSemantics.TryGetValue("TANGENT", out perVertexTangents);
            vertexSemantics.TryGetValue("BINORMAL", out perVertexBinormals);

            foreach (var input in Inputs)
            {
                if (input.semantic == "VERTEX")
                {
                    VertexInputIndex = (int)input.offset;
                }
                else if (input.semantic == "NORMAL")
                {
                    var normalsSource = FindSource(input.source);
                    Normals           = ColladaHelpers.SourceToPositions(normalsSource);
                    NormalsInputIndex = (int)input.offset;
                }
                else if (input.semantic == "TANGENT")
                {
                    var tangentsSource = FindSource(input.source);
                    Tangents           = ColladaHelpers.SourceToPositions(tangentsSource);
                    TangentsInputIndex = (int)input.offset;
                }
                else if (input.semantic == "BINORMAL")
                {
                    var binormalsSource = FindSource(input.source);
                    Binormals           = ColladaHelpers.SourceToPositions(binormalsSource);
                    BinormalsInputIndex = (int)input.offset;
                }
            }

            if (VertexInputIndex == -1)
            {
                throw new ParsingException("Required triangle input semantic missing: VERTEX");
            }

            Vertices = new List <Vertex>(vertexPositions.Count);
            for (var vert = 0; vert < vertexPositions.Count; vert++)
            {
                var vertex = OutputVertexType.CreateInstance();
                vertex.Position = vertexPositions[vert];

                if (perVertexNormals != null)
                {
                    vertex.Normal = perVertexNormals[vert];
                }

                if (perVertexTangents != null)
                {
                    vertex.Tangent = perVertexTangents[vert];
                }

                if (perVertexBinormals != null)
                {
                    vertex.Binormal = perVertexBinormals[vert];
                }

                Vertices.Add(vertex);
            }

            HasNormals  = perVertexNormals != null || NormalsInputIndex != -1;
            HasTangents = (perVertexTangents != null || TangentsInputIndex != -1) &&
                          (perVertexBinormals != null || BinormalsInputIndex != -1);
        }
예제 #2
0
        private void ImportVertices()
        {
            var vertexSemantics = new Dictionary <String, List <Vector3> >();

            foreach (var input in Mesh.vertices.input)
            {
                if (input.source[0] != '#')
                {
                    throw new ParsingException("Only ID references are supported for vertex input sources");
                }

                ColladaSource inputSource = null;
                if (!Sources.TryGetValue(input.source.Substring(1), out inputSource))
                {
                    throw new ParsingException("Vertex input source does not exist: " + input.source);
                }

                List <Single> x = null, y = null, z = null;
                if (!inputSource.FloatParams.TryGetValue("X", out x) ||
                    !inputSource.FloatParams.TryGetValue("Y", out y) ||
                    !inputSource.FloatParams.TryGetValue("Z", out z))
                {
                    throw new ParsingException("Vertex input source " + input.source + " must have X, Y, Z float attributes");
                }

                var vertices = new List <Vector3>(x.Count);
                for (var i = 0; i < x.Count; i++)
                {
                    vertices.Add(new Vector3(x[i], y[i], z[i]));
                }

                vertexSemantics.Add(input.semantic, vertices);
            }

            List <Vector3> positions = null;
            List <Vector3> normals   = null;
            List <Vector3> tangents  = null;
            List <Vector3> binormals = null;

            vertexSemantics.TryGetValue("POSITION", out positions);
            vertexSemantics.TryGetValue("NORMAL", out normals);
            vertexSemantics.TryGetValue("TANGENT", out tangents);
            vertexSemantics.TryGetValue("BINORMAL", out binormals);

            int normalInputIndex = -1;

            foreach (var input in Inputs)
            {
                if (input.semantic == "VERTEX")
                {
                    VertexInputIndex = (int)input.offset;
                }
                else if (input.semantic == "NORMAL")
                {
                    normals          = new List <Vector3>();
                    normalInputIndex = (int)input.offset;

                    if (input.source[0] != '#')
                    {
                        throw new ParsingException("Only ID references are supported for Normal input sources");
                    }

                    ColladaSource inputSource = null;
                    if (!Sources.TryGetValue(input.source.Substring(1), out inputSource))
                    {
                        throw new ParsingException("Normal input source does not exist: " + input.source);
                    }

                    List <Single> x = null, y = null, z = null;
                    if (!inputSource.FloatParams.TryGetValue("X", out x) ||
                        !inputSource.FloatParams.TryGetValue("Y", out y) ||
                        !inputSource.FloatParams.TryGetValue("Z", out z))
                    {
                        throw new ParsingException("Normal input source " + input.source + " must have X, Y, Z float attributes");
                    }

                    for (var i = 0; i < x.Count; i++)
                    {
                        normals.Add(new Vector3(x[i], y[i], z[i]));
                    }
                }
            }

            if (VertexInputIndex == -1)
            {
                throw new ParsingException("Required triangle input semantic missing: VERTEX");
            }

            Vertices = new List <Vertex>(positions.Count);
            for (var vert = 0; vert < positions.Count; vert++)
            {
                var vertex = OutputVertexType.CreateInstance();
                vertex.Position = positions[vert];

                if (tangents != null)
                {
                    vertex.Tangent = tangents[vert];
                }

                if (binormals != null)
                {
                    vertex.Binormal = binormals[vert];
                }

                if (normals != null && normalInputIndex == -1)
                {
                    vertex.Normal = normals[vert];
                }

                Vertices.Add(vertex);
            }

            if (normalInputIndex != -1)
            {
                Normals           = normals;
                NormalsInputIndex = normalInputIndex;
            }

            HasNormals  = normals != null;
            HasTangents = tangents != null && binormals != null;
        }