public static Model FromScene(Scene scene, Device device) { VertexDeclaration vertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements); Model result = new Model(scene, device, vertexDeclaration); foreach (Mesh mesh in scene.Meshes) { VertexBuffer vertexBuffer = new VertexBuffer(device, mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Default); DataStream vertexDataStream = vertexBuffer.Lock(0, mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes, LockFlags.None); VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.Positions.Count]; for (int i = 0; i < vertices.Length; ++i) vertices[i] = new VertexPositionNormalTexture(mesh.Positions[i], (mesh.Normals.Count > i) ? mesh.Normals[i] : Vector3D.Zero, Point2D.Zero); vertexDataStream.WriteRange(vertices); vertexBuffer.Unlock(); IndexBuffer indexBuffer = new IndexBuffer(device, mesh.Indices.Count * sizeof(int), Usage.WriteOnly, Pool.Default, false); DataStream indexDataStream = indexBuffer.Lock(0, mesh.Indices.Count * sizeof(int), LockFlags.None); indexDataStream.WriteRange(mesh.Indices.ToArray()); indexBuffer.Unlock(); ModelMesh modelMesh = new ModelMesh(mesh, device, vertexBuffer, mesh.Positions.Count, indexBuffer, mesh.PrimitiveCount, Matrix3D.Identity, mesh.Material); result.Meshes.Add(modelMesh); } return result; }
public static Model FromScene(Scene scene, Device device) { VertexDeclaration vertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements); Model result = new Model(scene, device, vertexDeclaration); foreach (Mesh mesh in scene.Meshes.Where(x => x.Positions.Any())) { VertexBuffer vertexBuffer = new VertexBuffer(device, mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes, Usage.WriteOnly, VertexFormat.None, Pool.Default); DataStream vertexDataStream = vertexBuffer.Lock(0, mesh.Positions.Count * VertexPositionNormalTexture.SizeInBytes, LockFlags.None); VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[mesh.Positions.Count]; for (int i = 0; i < vertices.Length; ++i) { vertices[i] = new VertexPositionNormalTexture(mesh.Positions[i], (mesh.Normals.Count > i) ? mesh.Normals[i] : Vector3D.Zero, (mesh.TextureCoordinates.Count > i) ? mesh.TextureCoordinates[i].Xy : Point2D.Zero); } vertexDataStream.WriteRange(vertices); vertexBuffer.Unlock(); IndexBuffer indexBuffer = new IndexBuffer(device, mesh.Indices.Count * sizeof(int), Usage.WriteOnly, Pool.Default, false); DataStream indexDataStream = indexBuffer.Lock(0, mesh.Indices.Count * sizeof(int), LockFlags.None); indexDataStream.WriteRange(mesh.Indices.ToArray()); indexBuffer.Unlock(); ModelMesh modelMesh = new ModelMesh(mesh, device, vertexBuffer, mesh.Positions.Count, indexBuffer, mesh.PrimitiveCount, Matrix3D.Identity, mesh.Material); result.Meshes.Add(modelMesh); } return(result); }