/// <summary> /// GameObject をそのまま頂点バッファへ設定する HW インスタンシングで GameObject を描画します。 /// </summary> void DrawGameObjectsWithDirectMapping() { instancingBlockEffect.View = view; instancingBlockEffect.Projection = projection; // インスタンスをそのまま頂点バッファへコピー int offset = directMappingVertexBuffer.SetData(gameObjects.Items, 0, gameObjects.Count); mesh.LevelOfDetail = 0; // ゲームオブジェクトを描画 foreach (var meshPart in mesh.MeshParts) { vertexBufferBindings[0] = new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset); vertexBufferBindings[1] = new VertexBufferBinding(directMappingVertexBuffer.VertexBuffer, offset, 1); GraphicsDevice.SetVertexBuffers(vertexBufferBindings); GraphicsDevice.Indices = meshPart.IndexBuffer; instancingBlockEffect.DiffuseColor = meshPart.MeshMaterial.DiffuseColor; instancingBlockEffect.EmissiveColor = meshPart.MeshMaterial.EmissiveColor; instancingBlockEffect.SpecularColor = meshPart.MeshMaterial.SpecularColor; instancingBlockEffect.SpecularPower = meshPart.MeshMaterial.SpecularPower; instancingBlockEffect.Pass.Apply(); GraphicsDevice.DrawInstancedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, gameObjects.Count); } }
/// <summary> /// HW インスタンシングで GameObject を描画します。 /// </summary> void DrawGameObjectsWithHardwareInstancing() { instancingBlockEffect.View = view; instancingBlockEffect.Projection = projection; // インスタンス情報を一旦コピー for (int i = 0; i < gameObjects.Count; ++i) { objectInstances[i].Position = gameObjects.Items[i].Position; objectInstances[i].Scale = gameObjects.Items[i].Scale; objectInstances[i].RotateAxis = gameObjects.Items[i].RotateAxis; objectInstances[i].Rotation = gameObjects.Items[i].Rotation; } // インスタンス用の頂点バッファへ書き込む int offset = instanceVertexBuffer.SetData(objectInstances, 0, gameObjects.Count); mesh.LevelOfDetail = 0; // ゲームオブジェクトを描画 foreach (var meshPart in mesh.MeshParts) { vertexBufferBindings[0] = new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset); vertexBufferBindings[1] = new VertexBufferBinding(instanceVertexBuffer.VertexBuffer, offset, 1); GraphicsDevice.SetVertexBuffers(vertexBufferBindings); GraphicsDevice.Indices = meshPart.IndexBuffer; instancingBlockEffect.DiffuseColor = meshPart.MeshMaterial.DiffuseColor; instancingBlockEffect.EmissiveColor = meshPart.MeshMaterial.EmissiveColor; instancingBlockEffect.SpecularColor = meshPart.MeshMaterial.SpecularColor; instancingBlockEffect.SpecularPower = meshPart.MeshMaterial.SpecularPower; instancingBlockEffect.Pass.Apply(); GraphicsDevice.DrawInstancedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, gameObjects.Count); } }
/// <summary> /// コンストラクタ /// </summary> /// <param name="triangleCount">三角形の個数</param> /// <param name="vertices">頂点配列</param> /// <param name="vertMap">モデルの頂点とMMDの頂点対応</param> /// <param name="indexBuffer">インデックスバッファ</param> public MMDGPUModelPartPNm(int triangleCount, MMDVertexNm[] vertices, Dictionary<long, int[]> vertMap, IndexBuffer indexBuffer) : base(triangleCount, vertices.Length, vertMap, indexBuffer) { this.vertices = vertices; //GPUリソース作成 gpuVertices = new VertexPositionNormal[vertices.Length]; vertexBuffer = new WritableVertexBuffer(indexBuffer.GraphicsDevice, vertices.Length * 4, typeof(VertexPositionNormal)); //初期値代入 for (int i = 0; i < vertices.Length; i++) { gpuVertices[i].Position = vertices[i].Position; gpuVertices[i].Normal = vertices[i].Normal; } // put the vertices into our vertex buffer vertexOffset = vertexBuffer.SetData(gpuVertices); }