コード例 #1
0
 public VertexShapeNodeElement(GeometricSet geometricSet, int objectId)
     : base(geometricSet, objectId)
 {
     NormalBinding          = geometricSet.Normals == null ? 1 : 0;
     TextureBinding         = 0;
     ColourBinding          = 0;
     QuantizationParameters = new QuantizationParameters(0, 0, 0, 0);
 }
コード例 #2
0
 public BaseShapeNodeElement(GeometricSet geometricSet, int objectId)
     : base(objectId)
 {
     TransformedBBox   = new BBoxF32();
     UntransformedBBox = geometricSet.UntransformedBoundingBox;
     Area              = geometricSet.Area;
     VertexCountRange  = new CountRange(geometricSet.Positions.Length);
     NodeCountRange    = new CountRange(1);
     PolygonCountRange = new CountRange(geometricSet.TriangleCount);
     Size              = geometricSet.Size;
     CompressionLevel  = 0;
 }
コード例 #3
0
ファイル: ThreeDXMLReader.cs プロジェクト: MazvydasT/JTfy8.1
        public static GeometricSet[] GetGeometricSets(Stream stream)
        {
            var xmlDocument = new XmlDocument();

            try
            {
                xmlDocument.Load(stream);
            }

            catch (Exception) { return(null); }

            var xmlNamespaceManager = new XmlNamespaceManager(xmlDocument.NameTable);

            xmlNamespaceManager.AddNamespace("ns", "http://www.3ds.com/xsd/3DXML");

            var geometricSets = new List <GeometricSet>();

            var repElements = xmlDocument.SelectNodes("//ns:Rep[ns:Faces/ns:Face[@fans or @strips or @triangles]][ns:VertexBuffer/ns:Positions]", xmlNamespaceManager);

            for (int repElementIndex = 0, repElementCount = repElements.Count; repElementIndex < repElementCount; ++repElementIndex)
            {
                var repElement = repElements[repElementIndex];

                var positionStrings = repElement.SelectSingleNode("./ns:VertexBuffer/ns:Positions/text()", xmlNamespaceManager).Value.Trim().Split(new char[] { ',' });

                var normalsElement = repElement.SelectSingleNode("./ns:VertexBuffer/ns:Normals/text()", xmlNamespaceManager);
                var normalStrings  = normalsElement?.Value.Trim().Split(new char[] { ',' });

                var positionsCount = positionStrings.Length;

                var positions = new float[positionsCount][];
                var normals   = normalStrings == null ? null : new float[positionsCount][];

                for (int positionIndex = 0; positionIndex < positionsCount; ++positionIndex)
                {
                    var positionComponents = positionStrings[positionIndex].Trim().Split(new char[] { ' ' });

                    positions[positionIndex] = new float[]
                    {
                        float.Parse(positionComponents[0]),
                        float.Parse(positionComponents[1]),
                        float.Parse(positionComponents[2])
                    };

                    if (normals != null)
                    {
                        var normalComponents = normalStrings[positionIndex].Trim().Split(new char[] { ' ' });

                        normals[positionIndex] = new float[]
                        {
                            float.Parse(normalComponents[0]),
                            float.Parse(normalComponents[1]),
                            float.Parse(normalComponents[2])
                        };
                    }
                }

                var faceElements = repElement.SelectNodes("./ns:Faces/ns:Face", xmlNamespaceManager);

                for (int faceIndex = 0, faceCount = faceElements.Count; faceIndex < faceCount; ++faceIndex)
                {
                    var faceElement           = faceElements[faceIndex];
                    var faceElementAttributes = faceElement.Attributes;

                    var triStrips = new List <int[]>();

                    for (int attributeIndex = 0, attributeCount = faceElementAttributes.Count; attributeIndex < attributeCount; ++attributeIndex)
                    {
                        var attribute = faceElementAttributes[attributeIndex];

                        switch (attribute.Name)
                        {
                        case "strips":
                        {
                            var stripsAttribute = attribute;
                            var stripStrings    = stripsAttribute != null?stripsAttribute.Value.Trim().Split(new char[] { ',' }) : new string[0];

                            for (int stripIndex = 0, stripCount = stripStrings.Length; stripIndex < stripCount; ++stripIndex)
                            {
                                var stripIndexStrings = stripStrings[stripIndex].Trim().Split(new char[] { ' ' });

                                var stripIndexCount = stripIndexStrings.Length;

                                if (stripIndexCount < 3)
                                {
                                    continue;
                                }

                                var stripIndices = new int[stripIndexCount];

                                for (int stripIndexIndex = 0; stripIndexIndex < stripIndexCount; ++stripIndexIndex)
                                {
                                    stripIndices[stripIndexIndex] = int.Parse(stripIndexStrings[stripIndexIndex]);
                                }

                                triStrips.Add(stripIndices);
                            }

                            break;
                        }

                        case "fans":
                        {
                            var fansAttribute = attribute;
                            var fanStrings    = fansAttribute != null?fansAttribute.Value.Trim().Split(new char[] { ',' }) : new string[0];

                            for (int fanIndex = 0, fanCount = fanStrings.Length; fanIndex < fanCount; ++fanIndex)
                            {
                                var fanIndexStrings = new List <string>(fanStrings[fanIndex].Trim().Split(new char[] { ' ' }));

                                var fanIndexCount = fanIndexStrings.Count;

                                if (fanIndexCount < 3)
                                {
                                    continue;
                                }

                                var firstFanIndex = int.Parse(fanIndexStrings[0]);
                                fanIndexStrings.RemoveAt(0);
                                --fanIndexCount;

                                var triStripIndices = new List <int>(5);

                                for (int fanIndexIndex = 0; fanIndexIndex < fanIndexCount; ++fanIndexIndex)
                                {
                                    if (fanIndexIndex == 0 || fanIndexIndex % 3 == 0)
                                    {
                                        if (triStripIndices.Count > 0)
                                        {
                                            triStripIndices.Add(int.Parse(fanIndexStrings[fanIndexIndex]));
                                            triStrips.Add(triStripIndices.ToArray());
                                        }

                                        triStripIndices.Clear();

                                        if (fanIndexCount - fanIndexIndex < 2)
                                        {
                                            break;
                                        }

                                        triStripIndices.Add(int.Parse(fanIndexStrings[fanIndexIndex]));
                                        triStripIndices.Add(int.Parse(fanIndexStrings[fanIndexIndex + 1]));
                                        triStripIndices.Add(firstFanIndex);

                                        ++fanIndexIndex;
                                    }

                                    else
                                    {
                                        triStripIndices.Add(int.Parse(fanIndexStrings[fanIndexIndex]));
                                    }
                                }

                                if (triStripIndices.Count > 0)
                                {
                                    triStrips.Add(triStripIndices.ToArray());
                                }
                            }

                            break;
                        }


                        case "triangles":
                        {
                            var trianglesAttribute    = attribute;
                            var trianglesIndexStrings = trianglesAttribute != null?trianglesAttribute.Value.Trim().Split(new char[] { ' ' }) : new string[0];

                            for (int triangleIndexIndex = 2, triangleIndexCount = trianglesIndexStrings.Length; triangleIndexIndex < triangleIndexCount; triangleIndexIndex += 3)
                            {
                                triStrips.Add(new int[]
                                    {
                                        int.Parse(trianglesIndexStrings[triangleIndexIndex - 2]),
                                        int.Parse(trianglesIndexStrings[triangleIndexIndex - 1]),
                                        int.Parse(trianglesIndexStrings[triangleIndexIndex])
                                    });
                            }

                            break;
                        }
                        }
                    }

                    if (triStrips.Count == 0 || positions.Length == 0)
                    {
                        continue;
                    }

                    var geometricSet = new GeometricSet(triStrips.ToArray(), positions)
                    {
                        Normals = normals
                    };

                    var colorElement = faceElement.SelectSingleNode("./ns:SurfaceAttributes/ns:Color[@alpha and @red and @green and @blue]", xmlNamespaceManager);
                    if (colorElement != null)
                    {
                        var attributes = colorElement.Attributes;
                        var a          = (int)(float.Parse(attributes["alpha"].Value) * 255);
                        var r          = (int)(float.Parse(attributes["red"].Value) * 255);
                        var g          = (int)(float.Parse(attributes["green"].Value) * 255);
                        var b          = (int)(float.Parse(attributes["blue"].Value) * 255);

                        geometricSet.Colour = Color.FromArgb(a, r, g, b);
                    }

                    geometricSets.Add(geometricSet);
                }
            }

            /*geometricSets.Sort((a, b) =>
             * {
             *  var alphaA = a.Colour.A;
             *  var alphaB = b.Colour.A;
             *
             *  return alphaA > alphaB ? -1 : (alphaA == alphaB ? 0 : 1);
             * });*/

            return(geometricSets.Count == 0 ? null : geometricSets.ToArray());
        }
コード例 #4
0
 public TriStripSetShapeNodeElement(GeometricSet geometrySet, int objectId) : base(geometrySet, objectId)
 {
 }