Esempio n. 1
0
        private void BuildMeshes(OptFile opt)
        {
            this.meshes = null;

            if (opt == null)
            {
                return;
            }

            this.meshes = new MeshGeometry3D[opt.Meshes.Count][][][];

            for (int meshIndex = 0; meshIndex < opt.Meshes.Count; meshIndex++)
            {
                var mesh = opt.Meshes[meshIndex];

                var positions = new Point3DCollection(
                    mesh.Vertices
                    .Select(t => new Point3D(-t.Y, -t.X, t.Z)));

                var normals = new Vector3DCollection(
                    mesh.VertexNormals
                    .Select(t => new Vector3D(-t.Y, -t.X, t.Z)));

                var textureCoordinates = new PointCollection(
                    mesh.TextureCoordinates
                    .Select(t => new Point(t.U, -t.V)));

                this.meshes[meshIndex] = new MeshGeometry3D[mesh.Lods.Count][][];

                for (int lodIndex = 0; lodIndex < mesh.Lods.Count; lodIndex++)
                {
                    var lod = mesh.Lods[lodIndex];

                    this.meshes[meshIndex][lodIndex] = new MeshGeometry3D[lod.FaceGroups.Count][];

                    for (int faceGroupIndex = 0; faceGroupIndex < lod.FaceGroups.Count; faceGroupIndex++)
                    {
                        var faceGroup = lod.FaceGroups[faceGroupIndex];

                        MeshGeometry3D[] geometries = new MeshGeometry3D[faceGroup.Faces.Count + 1];

                        for (int faceIndex = 0; faceIndex < faceGroup.Faces.Count; faceIndex++)
                        {
                            var face = faceGroup.Faces[faceIndex];

                            MeshGeometry3D geometry = new MeshGeometry3D();
                            int            index    = 0;

                            Indices positionsIndex          = face.VerticesIndex;
                            Indices normalsIndex            = face.VertexNormalsIndex;
                            Indices textureCoordinatesIndex = face.TextureCoordinatesIndex;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.A));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.A));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.A));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.B));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.B));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.B));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.C));
                            geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.C));
                            geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.C));
                            geometry.TriangleIndices.Add(index);
                            index++;

                            if (positionsIndex.D >= 0)
                            {
                                geometry.TriangleIndices.Add(index - 3);
                                geometry.TriangleIndices.Add(index - 1);

                                geometry.Positions.Add(positions.ElementAtOrDefault(positionsIndex.D));
                                geometry.Normals.Add(normals.ElementAtOrDefault(normalsIndex.D));
                                geometry.TextureCoordinates.Add(textureCoordinates.ElementAtOrDefault(textureCoordinatesIndex.D));
                                geometry.TriangleIndices.Add(index);
                                index++;
                            }

                            geometry.Freeze();
                            geometries[1 + faceIndex] = geometry;
                        }

                        MeshGeometry3D geometryGroup = new MeshGeometry3D();

                        for (int i = 1; i < geometries.Length; i++)
                        {
                            MergeGeometry(geometryGroup, geometries[i]);
                        }

                        geometryGroup.Freeze();
                        geometries[0] = geometryGroup;

                        this.meshes[meshIndex][lodIndex][faceGroupIndex] = geometries;
                    }
                }
            }
        }