Direct3D | how to get Vertex from Mesh is loaded from .x file (C#)Direct3D | .x ファイルから読み込んだ Meshから頂点情報を取得する(C#)


How to get Vertex from Mesh that loaded by .x File.

Load Mesh from .x File(how to load mesh from .x file)
Then, get CustomVertex.PositionNormalTextured from Mesh.

How to get CustomVertex.PositionNormalTextured:

// Load Mesh from .x File
Mesh mc = FromFile(
 :

// get Vertex Buffer from Mesh
VertexBuffer vb = mc.VertexBuffer;

// get Stream of VertexBuffer
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);

// create vertex array
CustomVertex.PositionNormalTextured[] vertA = new CustomVertex.PositionNormalTextured[mc.NumberVertices];

// read all vertex from mesh
for (int i = 0; i < mc.NumberVertices; i++)
{
	// Create new PositionNormalTextured
	CustomVertex.PositionNormalTextured pnTex = new CustomVertex.PositionNormalTextured();

	// Read stream with PositionNormalTextured Type
	pnTex = (CustomVertex.PositionNormalTextured)data.Read(pnTex.GetType());
	
	vertA[i] =pnTex;
}

// Unlock Buffer
vb.Unlock();

It is simple.
Don’t forget cast to VertexTypeDirect3D | .x ファイルから Meshを読み込む(C#)でMeshを .x ファイルから取得しました。

ここではMeshからVertex情報の取得の仕方を説明します。
前回の記事でMeshを取得した後、CustomVertex.PositionNormalTextured型データを取得します。

How to get CustomVertex.PositionNormalTextured:

// Load Mesh from .x File
Mesh mc = FromFile(
 :

// get Vertex Buffer from Mesh
VertexBuffer vb = mc.VertexBuffer;

// get Stream of VertexBuffer
GraphicsStream data = vb.Lock(0, 0, LockFlags.None);

// create vertex array
CustomVertex.PositionNormalTextured[] vertA = new CustomVertex.PositionNormalTextured[mc.NumberVertices];

// read all vertex from mesh
for (int i = 0; i < mc.NumberVertices; i++)
{
	// Create new PositionNormalTextured
	CustomVertex.PositionNormalTextured pnTex = new CustomVertex.PositionNormalTextured();

	// Read stream with PositionNormalTextured Type
	pnTex = (CustomVertex.PositionNormalTextured)data.Read(pnTex.GetType());
	
	vertA[i] =pnTex;
}

// Unlock Buffer
vb.Unlock();

意外とシンプルですね。
Vertex型のキャストを忘れないように注意してください。


タイトルとURLをコピーしました