Esempio n. 1
0
        internal MeshTemplate(IGeometryModel model, IGeometryMesh mesh)
        {
            submeshCount = mesh.Submeshes.Count;
            matIndex     = new int[submeshCount];
            submeshes    = new Helix.MeshGeometry3D[submeshCount];

            var texMatrix    = SharpDX.Matrix.Identity;
            var boundsMatrix = SharpDX.Matrix.Identity;

            if (mesh.BoundsIndex >= 0)
            {
                var bounds = model.Bounds[mesh.BoundsIndex.Value];
                boundsMatrix = bounds.ToMatrix3();
                texMatrix    = bounds.ToMatrix2();
            }

            for (int i = 0; i < submeshCount; i++)
            {
                var sub = mesh.Submeshes[i];
                matIndex[i] = sub.MaterialIndex;

                var subIndices = mesh.Indicies.Skip(sub.IndexStart).Take(sub.IndexLength).ToList();
                if (mesh.IndexFormat == IndexFormat.TriangleStrip)
                {
                    subIndices = subIndices.Unstrip().ToList();
                }

                var vertStart  = subIndices.Min();
                var vertLength = subIndices.Max() - vertStart + 1;
                var subVerts   = mesh.Vertices.Skip(vertStart).Take(vertLength);

                IEnumerable <SharpDX.Vector3> subPositions;
                if (boundsMatrix.IsIdentity)
                {
                    subPositions = subVerts.Select(v => v.Position[0].ToVector3());
                }
                else
                {
                    subPositions = subVerts.Select(v => SharpDX.Vector3.TransformCoordinate(v.Position[0].ToVector3(), boundsMatrix));
                }

                IEnumerable <SharpDX.Vector2> subTexcoords;
                if (texMatrix.IsIdentity)
                {
                    subTexcoords = subVerts.Select(v => v.TexCoords[0].ToVector2());
                }
                else
                {
                    subTexcoords = subVerts.Select(v => SharpDX.Vector2.TransformCoordinate(v.TexCoords[0].ToVector2(), texMatrix));
                }

                submeshes[i] = new Helix.MeshGeometry3D
                {
                    Indices            = new Helix.IntCollection(subIndices.Select(j => j - vertStart)),
                    Positions          = new Helix.Vector3Collection(subPositions),
                    TextureCoordinates = new Helix.Vector2Collection(subTexcoords)
                };

                if (mesh.Vertices[0].Normal.Count > 0)
                {
                    var subNormals = subVerts.Select(v => new SharpDX.Vector3(v.Normal[0].X, v.Normal[0].Y, v.Normal[0].Z));
                    submeshes[i].Normals = new Helix.Vector3Collection(subNormals);
                }

                submeshes[i].UpdateOctree();
            }
        }
Esempio n. 2
0
 internal InstancedMeshTemplate(IGeometryModel model, IGeometryMesh mesh)
     : base(model, mesh)
 {
     rootMeshes = new Helix.InstancingMeshGeometryModel3D[submeshCount];
 }