public static GlassModelCombined FromModel(GraphicsDevice graphicsDevice, Model model)
        {
            // Combine Meshes
            var meshCount   = model.Meshes.Count;
            var vertexCount = model.Meshes[01].MeshParts[0].VertexBuffer.VertexCount;

            // Declare new arrays
            var vertexData = new VertexPositionNormalTextureColor[vertexCount];

            var transforms = new Matrix[model.Bones.Count];

            model.CopyAbsoluteBoneTransformsTo(transforms);

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                var mesh      = model.Meshes[i];
                var part      = mesh.MeshParts[0];
                var vertCount = part.NumVertices;

                var vertexElements = new VertexPositionNormalTextureColor[vertCount];
                var stride         = VertexPositionNormalTextureColor.VertexDeclaration.VertexStride;
                model.Meshes[0].MeshParts[0].VertexBuffer.GetData(part.VertexOffset * stride, vertexElements, 0, vertCount, stride);


                var transform = transforms[mesh.ParentBone.Index];

                for (int j = 0; j < vertCount; j++)
                {
                    vertexData[part.VertexOffset + j] = vertexElements[j];
                }
            }

            var primitiveCount = 0;
            var indicies       = new short[model.Meshes[0].MeshParts[0].IndexBuffer.IndexCount];

            model.Meshes[0].MeshParts[0].IndexBuffer.GetData(indicies);

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                var part = model.Meshes[i].MeshParts[0];

                var startIndex = part.StartIndex;
                var indexCount = part.PrimitiveCount * 3;
                var numVerts   = part.NumVertices;

                primitiveCount += part.PrimitiveCount;
                var indexElements = new short[indexCount];
                part.IndexBuffer.GetData(startIndex * 2, indexElements, 0, indexCount);


                for (int j = 0; j < indexCount; j++)
                {
                    indicies[j + startIndex] = (short)(indexElements[j] + startIndex);
                }
            }


            var r             = new Random();
            var glassVertices = new GlassVertexDeclaration[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                var vert = vertexData[i];
                glassVertices[i].Position          = vert.Position;
                glassVertices[i].Position.Z        = 0;
                glassVertices[i].TextureCoordinate = vert.TextureCoordinate;
                glassVertices[i].Normal            = vert.Normal;
                glassVertices[i].Color             = vert.Color;
            }



            return(new GlassModelCombined(graphicsDevice, glassVertices, indicies, primitiveCount));
        }
示例#2
0
        public static GlassModel FromModel(Model model)
        {
            var random = new Random();

            var meshLst = new List <GlassModelShard>();
            var vb      = model.Meshes[0].MeshParts[0].VertexBuffer;
            var ib      = model.Meshes[0].MeshParts[0].IndexBuffer;

            foreach (var mesh in model.Meshes)
            {
                var partLst = new List <GlassPart>();
                foreach (var part in mesh.MeshParts)
                {
                    var glassPart = new GlassPart
                    {
                        StartIndex     = part.StartIndex,
                        NumVerticies   = part.NumVertices,
                        PrimitiveCount = part.PrimitiveCount,
                        VertexOffset   = part.VertexOffset
                    };

                    partLst.Add(glassPart);
                }

                var randZ = random.Next(1, 2);
                var randX = random.Next(1, 2);
                var randY = random.Next(1, 2);

                var flipX = random.Next(0, 2);
                if (flipX == 0)
                {
                    randX *= -1;
                }

                var flipY = random.Next(0, 2);
                if (flipY == 0)
                {
                    randY *= -1;
                }

                var flipZ = random.Next(0, 2);
                if (flipZ == 0)
                {
                    randZ *= -1;
                }


                // Get the centre of the shard
                var stride       = vb.VertexDeclaration.VertexStride;
                var numVerticies = mesh.MeshParts[0].NumVertices;
                var vertexOffset = mesh.MeshParts[0].VertexOffset;

                var vertexData = new VertexPositionNormalTextureColor[numVerticies];
                mesh.MeshParts[0].VertexBuffer.GetData(vertexOffset * stride, vertexData, 0, numVerticies, stride);

                // Distance from impact pt centre

                var roation = new Vector3(randX, randY, randZ);

                meshLst.Add(new GlassModelShard(partLst, roation, vertexData));
            }

            return(new GlassModel(vb, ib, meshLst));
        }