Parser for source elements in a collada file.
Пример #1
0
        public GraphicStreamContainer Parse()
        {
            int triangleCount = Convert.ToInt32(
                mTriangleElement.Attribute("count").Value,
                CultureInfo.InvariantCulture);

            var allIndices = ParseUIntArray(mTriangleElement.Element(
                ColladaImporter.Namespace + "p"));
            var segmentLenght = allIndices.Count() / triangleCount / 3;

            var container = new GraphicStreamContainer();
            var indices = allIndices.EachNthElement(segmentLenght).ToArray();
            container.Create(GraphicStreamUsage.Index, indices);
            var vertexCount = (int)indices.Max() + 1;

            var inputs = ParseInputs();

            foreach (var input in inputs)
            {
                var usage = SemanticHelper.GetUsageForSemantic(input.Semantic);
                var data = new SourceParser(input.SourceElement).Parse();

                if (usage != GraphicStreamUsage.Position)
                {
                    var specificIndices = allIndices.EachNthElement(
                        segmentLenght, input.Offset).ToArray();

                    var newData = new float[vertexCount][];
                    for (int i = 0; i < indices.Length; i++)
                    {
                        var positionIndex = (int)indices[i];
                        newData[positionIndex] = data.ElementAt((int)specificIndices[i]);
                    }

                    data = newData;
                }

                var elementLength = data.First().Length;
                switch (elementLength)
                {
                    case 2:
                        container.Create(usage,
                            data.Select(element => new Vector2(element)).ToArray());
                        break;
                    case 3:
                        container.Create(usage,
                            data.Select(element => new Vector3(element)).ToArray());
                        break;
                    case 4:
                        container.Create(usage,
                            data.Select(element => new Vector4(element)).ToArray());
                        break;
                }
            }

            return container;
        }
Пример #2
0
        public void Parse_for_Normals()
        {
            var sourceElement = mMesh.Elements()
                .Where(s => s.Attribute("id").Value == "Plane-Geometry-Normals")
                .FirstOrDefault();
            Assert.IsNotNull(sourceElement);

            var sourceParser = new SourceParser(sourceElement);

            var normals = sourceParser.Parse();

            Assert.IsNotNull(normals);
            var expected = new[]
            {
                new[] { 0f, 0f, 1f },
                new[] { 0f, 0f, 1f },
            };
            Assert.AreElementsEqual(expected, normals.ToArray());
        }
Пример #3
0
        public void Parse_for_Positions()
        {
            var sourceElement = mMesh.Elements()
                .Where(s => s.Attribute("id").Value == "Plane-Geometry-Position")
                .FirstOrDefault();
            Assert.IsNotNull(sourceElement);

            var sourceParser = new SourceParser(sourceElement);

            var positions = sourceParser.Parse();

            Assert.IsNotNull(positions);
            var expected = new[]
            {
                new[] { 1f, 1f, 0f },
                new[] { 1f, -1f, 0f },
                new[] { -1f, -1f, 0f },
                new[] { -1f, 1f, 0f }
            };
            Assert.AreElementsEqual(expected, positions.ToArray());
        }
Пример #4
0
        public void Parse_for_Colors()
        {
            var sourceElement = mMesh.Elements()
                .Where(s => s.Attribute("id").Value == "Plane-Geometry-color")
                .FirstOrDefault();
            Assert.IsNotNull(sourceElement);

            var sourceParser = new SourceParser(sourceElement);

            var texCoords = sourceParser.Parse();

            Assert.IsNotNull(texCoords);
            var expected = new[]
            {
                new[] { 1f, 1f, 1f, 1f },
                new[] { 1f, 1f, 1f, 1f },
                new[] { 1f, 1f, 1f, 1f },
                new[] { 1f, 1f, 1f, 1f },
                new[] { 1f, 1f, 1f, 1f },
                new[] { 1f, 1f, 1f, 1f },
            };
            Assert.AreElementsEqual(expected, texCoords.ToArray());
        }