「MonoGame/クラスライブラリ/VertexBufferクラス」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator |
imported>Administrator |
||
145行目: | 145行目: | ||
| (GameResourceクラスから継承) | | (GameResourceクラスから継承) | ||
|} | |} | ||
+ | |||
+ | == 実装例 == | ||
+ | まずVertexBufferを作ってSetDataする。 | ||
+ | <source lang="csharp"> | ||
+ | // 頂点データを作成する | ||
+ | var vertices = new VertexPositionColor[6]; | ||
+ | vertices[0] = new VertexPositionColor(new Vector3(-10, -10, 0), Color.Red); | ||
+ | vertices[1] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green); | ||
+ | vertices[2] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue); | ||
+ | vertices[3] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green); | ||
+ | vertices[4] = new VertexPositionColor(new Vector3(+10, +10, 0), Color.White); | ||
+ | vertices[5] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue); | ||
+ | |||
+ | // 頂点データをGPUに送り込む | ||
+ | _vertexBuffer = new VertexBuffer(_graphics.GraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.None); | ||
+ | _vertexBuffer.SetData(vertices); | ||
+ | </source> | ||
+ | |||
+ | 描画するときは、GraphicsDeviceクラスのSetVertexBufferメソッドで読み出して、DrawPrimitivesメソッドで描画する。 | ||
+ | 別途[[インデックスバッファ]]を使用しているときはDrawPrimitivesメソッドではなくDrawIndexedPrimitivesメソッドを使うこと。 | ||
+ | <source lang="csharp"> | ||
+ | _graphics.GraphicsDevice.SetVertexBuffer(_vertexBuffer); | ||
+ | |||
+ | foreach (var pass in _effect.CurrentTechnique.Passes) | ||
+ | { | ||
+ | pass.Apply(); | ||
+ | _graphics.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3); | ||
+ | } | ||
+ | </source> | ||
== 関連項目 == | == 関連項目 == |
2017年11月7日 (火) 02:49時点における最新版
VertexBufferクラスは、ポリゴンを表示するのに使う頂点のリストである。
MonoGameでは頂点構造体を単純な配列で保持しておいてもDrawUserPrimitevesメソッドで描画できるが、VertexBufferクラスを使っておくと頂点バッファをメインメモリからVRAMへ事前に転送しておけるので高速化が見込める。
C#でゴリゴリと頂点データを加工したい場合はメインメモリとVRAMを行ったり来たりが相当な無駄になるのであまり向いていない。 逆にほとんどのケースが該当する「頂点データを決めたら破棄するまで変形させないもの」には使ったほうがよいとされている。 また、C#ではなくHLSLの頂点シェーダーで頂点バッファの加工を行う場合は必須となる。
ちなみに一部のOpenGL系プラットフォームでは一度格納すると二度と取り出せない(GetDataできない)ものも存在している。 3Dモデルの頂点データを入れて頂点シェーダーで当たり判定の範囲を動的に生成しGetDataで取得していたものが動かなくて悲しくなった。
継承[編集 | ソースを編集]
- System.Object
- Microsoft.Xna.Framework.Graphics.GraphicsResource
- Microsoft.Xna.Framework.Graphics.VertexBuffer
- Microsoft.Xna.Framework.Graphics.DynamicVertexBuffer
- Microsoft.Xna.Framework.Graphics.VertexBuffer
- Microsoft.Xna.Framework.Graphics.GraphicsResource
名前空間[編集 | ソースを編集]
構文[編集 | ソースを編集]
public class VertexBuffer : GraphicsResource
コンストラクタ[編集 | ソースを編集]
名称 | 概要 | |
---|---|---|
protected | VertexBuffer(GraphicsDevice, VertexDeclaration, int, BufferUsage, bool) | |
public | VertexBuffer(GraphicsDevice, VertexDeclaration, int, BufferUsage) | |
public | VertexBuffer(GraphicsDevice, Type, int, BufferUsage) |
プロパティ[編集 | ソースを編集]
名称 | 概要 | |
---|---|---|
public | BufferUsage | |
public | GraphicsDevice | (GameResourceクラスから継承) |
public | IsDisposed | (GameResourceクラスから継承) |
public | Name | (GameResourceクラスから継承) |
public | Tag | (GameResourceクラスから継承) |
public | VertexCount | |
public | VertexDeclaration |
メソッド[編集 | ソースを編集]
名称 | 概要 | |
---|---|---|
public | Dispose(bool) | 派生クラスを作る場合は自前管理のリソースの破棄を実装する必要がある。 |
protected | Dispose() | (GameResourceクラスから継承) |
protected | Finalize | (GameResourceクラスから継承) |
public | GetData<T>(int, T[], int, int, int) | この頂点バッファから頂点データを取得する。 |
public | GetData<T>(T[], int, int) | |
public | GetData<T>(T[]) | |
protected internal | GraphicsDeviceResetting | GraphicsDeviceがリセットされGPU上のリソースを再作成する必要があるときに呼ばれる。 |
public | SetData<T>(int, T[], int, int, int) | この頂点バッファに頂点データを設定する。大雑把に言えば頂点データの配列をメインメモリからVRAMにコピーする。 |
public | SetData<T>(T[]) | |
public | SetData<T>(T[], int, int) | |
protected | SetDataInternal<T> | |
public | ToString | (GameResourceクラスから継承) |
イベント[編集 | ソースを編集]
名称 | 概要 | |
---|---|---|
public | Disposing | (GameResourceクラスから継承) |
実装例[編集 | ソースを編集]
まずVertexBufferを作ってSetDataする。
// 頂点データを作成する
var vertices = new VertexPositionColor[6];
vertices[0] = new VertexPositionColor(new Vector3(-10, -10, 0), Color.Red);
vertices[1] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green);
vertices[2] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue);
vertices[3] = new VertexPositionColor(new Vector3(-10, +10, 0), Color.Green);
vertices[4] = new VertexPositionColor(new Vector3(+10, +10, 0), Color.White);
vertices[5] = new VertexPositionColor(new Vector3(+10, -10, 0), Color.Blue);
// 頂点データをGPUに送り込む
_vertexBuffer = new VertexBuffer(_graphics.GraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.None);
_vertexBuffer.SetData(vertices);
描画するときは、GraphicsDeviceクラスのSetVertexBufferメソッドで読み出して、DrawPrimitivesメソッドで描画する。 別途インデックスバッファを使用しているときはDrawPrimitivesメソッドではなくDrawIndexedPrimitivesメソッドを使うこと。
_graphics.GraphicsDevice.SetVertexBuffer(_vertexBuffer);
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
_graphics.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, _vertexBuffer.VertexCount / 3);
}