Exemplo n.º 1
0
        private void BuildMeshesWireframes(OptFile opt)
        {
            this.meshesWireframes = null;

            if (opt == null)
            {
                return;
            }

            this.meshesWireframes = opt.Meshes
                .AsParallel()
                .AsOrdered()
                .Select(mesh =>
                {
                    var positions = new Point3DCollection(
                        mesh.Vertices
                        .Select(t => new Point3D(-t.Y, -t.X, t.Z)));

                    var wireframes = new IList<Point3D>[mesh.Lods.Count];

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

                        var lines = new List<Tuple<int, int>>(lod.TrianglesCount * 3);

                        var addLine = new Action<int, int>((a, b) =>
                        {
                            if (a < b)
                            {
                                lines.Add(new Tuple<int, int>(a, b));
                            }
                            else
                            {
                                lines.Add(new Tuple<int, int>(b, a));
                            }
                        });

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

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

                                Index positionsIndex = face.VerticesIndex;

                                addLine(positionsIndex.A, positionsIndex.B);
                                addLine(positionsIndex.B, positionsIndex.C);

                                if (positionsIndex.D < 0)
                                {
                                    addLine(positionsIndex.C, positionsIndex.A);
                                }
                                else
                                {
                                    addLine(positionsIndex.C, positionsIndex.D);
                                    addLine(positionsIndex.D, positionsIndex.A);
                                }
                            }
                        }

                        var points = lines
                            .Distinct()
                            .SelectMany(t => new List<Point3D>
                        {
                            positions.ElementAtOrDefault(t.Item1),
                            positions.ElementAtOrDefault(t.Item2)
                        })
                            .ToList();

                        wireframes[lodIndex] = points;
                    }

                    return wireframes;
                })
                .ToArray();
        }
Exemplo n.º 2
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;

                            Index positionsIndex = face.VerticesIndex;
                            Index normalsIndex = face.VertexNormalsIndex;
                            Index 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;
                    }
                }
            }
        }