Direct3D | Load Mesh From “.x” files (C#)Direct3D | .x ファイルから Meshを読み込む(C#)


This is exmaple how to load Mesh from .x file. (c#)

Use Microsoft.DirectX.Direct3D.Mesh.FromFile(String,MeshFlags,Device,ExtendedMaterial) API

Definition:

public static Mesh FromFile(
    string filename,
    MeshFlags options,
    Device device,
    out ExtendedMaterial materials
);

ExtendedMaterial will be used for getting about material info.

Load Mesh From .X Example:

ExtendedMaterial[] materials = null;
// Load Mesh From .x file
Mesh mc = Mesh.FromFile(
	inXFilename,	// .x filename path
	MeshFlags.SystemMemory,	//  RAM
	myDev,		// DeviceInfo
	out materials	//  material info
	);

Then, add vector if mesh has not yet.

Compute Vector Mesh Example:

// Remake mesh with vector
if ((mc.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal)
{
    Mesh tempMesh = mc.Clone(
        mc.Options.Value,
        mc.VertexFormat | VertexFormats.Normal,
        myDev
        );

    // compute
    tempMesh.ComputeNormals();

    // refresh
    mc.Dispose();
    mc = tempMesh;
}

done !!Microsoft.DirectX.Direct3DでMeshファイルを .x のファイルから読み込む方法

情報があるようでない、DirectX関連。
皆さん独自にローダー作ったりしているので載せるメリットがない、というのもありますが。
さて、どうやるか。

Microsoft.DirectX.Direct3D.Mesh.FromFile(String,MeshFlags,Device,ExtendedMaterial) を使います。

Definition:

public static Mesh FromFile(
    string filename,
    MeshFlags options,
    Device device,
    out ExtendedMaterial materials
);

ExtendedMaterialはマテリアル情報を取得するために使います。
使い方はこんな感じ。

Load Mesh From .X Example:

ExtendedMaterial[] materials = null;
// Load Mesh From .x file
Mesh mc = Mesh.FromFile(
	inXFilename,	// .x filename path
	MeshFlags.SystemMemory,	//  RAM
	myDev,		// DeviceInfo
	out materials	//  material info
	);

この時、Meshに法線情報が含まれていない場合は、
自前で計算して置き換えます。

Compute Vector Mesh Example:

// Remake mesh with vector
if ((mc.VertexFormat & VertexFormats.Normal) != VertexFormats.Normal)
{
    Mesh tempMesh = mc.Clone(
        mc.Options.Value,
        mc.VertexFormat | VertexFormats.Normal,
        myDev
        );

    // compute
    tempMesh.ComputeNormals();

    // refresh
    mc.Dispose();
    mc = tempMesh;
}

以上!

次回はXファイルから読み込んだMeshの、Vertex情報の取得について。

しかし、生ファイル見てて思うのは3Dクリエイターの人たちはすごいってこと。
ほんと、世界は広いですね。


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