方法 : メッシュを作成します。
[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]
4 つの基本的な方法でメッシュを作成できます。
ファイルからメッシュ データを読み込む。
複製または、既存のメッシュを最適化します。
図形の作成関数を使用して、サイズと、図形の作成に使用される三角形の数を指定するには。
Mesh コンストラクターを使用します。
注意
マネージの Direct3D モバイル アプリケーションが、Pocket PC やスマートフォン Windows Mobile 5. 0 ソフトウェアを必要です。.NET の外部のリソース フレームワークを最適化します。 Windows Mobile ソフトウェアおよび SDK についてを参照してください。
ファイルからメッシュを作成するには
- ファイルからメッシュ データを読み込むおよび [メッシュ データと入力します。 .NET Compact Framework 直接サポートしません、ファイルからのメッシュの読み込みが の Direct3D モバイル メッシュのサンプルは、メッシュの読み込みにクラスを定義します。
既存メッシュからメッシュを作成するには
最適化されたデータを新しいメッシュを作成するのにには、Optimize メソッドを使用します。
現在、メッシュを最適化するのにには、OptimizeInPlace メソッドを使用します。
複製の主な用途では、メッシュを浮動小数点数から固定小数点形式に変換されます。 主な使用を最適化するため目的は描画は高速メッシュを作成します。 メッシュ最適化は、メッシュの描画呼び出しが高速に実行できるように、三角形メッシュに含まれるを並べ替えます。 メッシュの最適化は、別のテクスチャで描画される、状態、および資材をレンダリングするメッシュの領域を識別するため、属性テーブルも生成します。
形状作成関数を使用してメッシュを作成するには
位置と法線の浮動小数点数値演算で指定されたメッシュを作成するのにいずれか、Mesh クラスの次の静的な方法を使用します。
Mesh コンストラクターを使用してメッシュを作成するには
目的の引数で Mesh コンストラクターを呼び出します。
インデックス バッファー、頂点バッファー、および属性テーブルのデータを設定します。 ここで生成されたデータ多くの場合、実行時にします。 この方法でメッシュを作成する手順を次の例に示します。
使用例
次のコード例では、heightfield メッシュを x と y 平面上で、垂直方向のディメンションを表すの z 座標を作成します。 作成、特定のメッシュから実行 (0, 0) を (1, 1) いて GetHeight メソッドによって、高さが指定します。 このメッシュは、1 つのテクスチャ、全体のメッシュでテクスチャもします。 メッシュの端に沿ってポイント数が使用を制御ための例で定義された、tessellation パラメーターです。
Class Form1
PrivateSubNew()
MyBase.New()
' In this example, initialize the mesh with ' 4 tessellationsMe.InitializeMesh(4)
EndSubPrivateSub InitializeMesh(ByVal tessellation AsInteger)
Dim mesh1 As Mesh = CreateHeightfieldMesh(tessellation)
EndSubPrivateFunction GetHeight(ByVal x AsSingle, ByVal y AsSingle) AsSingleReturn 0
EndFunctionPrivateFunction CreateHeightfieldMesh(ByVal tessellation AsInteger) As Mesh
Dim mesh As Mesh
Dim device As Device = NothingDim arrayIndices((tessellation - 1) * (tessellation - 1) * 6) AsShortDim arrayVertices(tessellation * tessellation) As CustomVertex.PositionTextured
Dim attributeRange AsNew AttributeRange()
' Create mesh with desired vertex format and desired size.
mesh = New Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory, CustomVertex.PositionTextured.Format, device)
' For each point in the height field calculate the x, y, z and ' texture coordinates.Dim y AsIntegerFor y = 0 To tessellation
Dim x AsIntegerFor x = 0 To tessellation
Dim arrayIndex AsInteger = y * tessellation + x
Dim xCoordinate AsSingle = System.Convert.ToSingle(x) / System.Convert.ToSingle(tessellation - 1)
Dim yCoordinate AsSingle = System.Convert.ToSingle(y) / System.Convert.ToSingle(tessellation - 1)
Dim vertex AsNew CustomVertex.PositionTextured(xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate)
arrayVertices(arrayIndex) = vertex
Next x
Next y
' Calculate the index buffer.Dim z AsIntegerFor z = 0 To (tessellation - 1)
Dim x AsIntegerFor x = 0 To (tessellation - 1)
Dim arrayIndex AsInteger = (z * (tessellation - 1) + x) * 6
Dim vertexIndex AsInteger = z * tessellation + x
arrayIndices(arrayIndex) = Fix(vertexIndex)
arrayIndices((arrayIndex + 1)) = Fix(vertexIndex + 1)
arrayIndices((arrayIndex + 2)) = Fix(vertexIndex + tessellation)
arrayIndices((arrayIndex + 3)) = Fix(vertexIndex + tessellation)
arrayIndices((arrayIndex + 4)) = Fix(vertexIndex + 1)
arrayIndices((arrayIndex + 5)) = Fix(vertexIndex + tessellation + 1)
Next x
Next z
' There is only one attribute value for this mesh. ' By specifying an attribute range the DrawSubset function ' does not have to scan the entire mesh for all faces that are ' are marked with a particular attribute ID.
attributeRange.AttributeId = 0
attributeRange.FaceStart = 0
attributeRange.FaceCount = arrayIndices.Length / 3
attributeRange.VertexStart = 0
attributeRange.VertexCount = arrayVertices.Length
mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None)
mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None)
mesh.SetAttributeTable(New AttributeRange() {attributeRange})
Return mesh
EndFunctionPublicSharedSub Main()
TryDim Form1 AsNew Form()
Application.Run(Form1)
Catch e As NotSupportedException
MsgBox("Your device does not have the " + _
"needed 3d support to run this sample")
Catch e As DriverUnsupportedException
MsgBox("Your device does not have the " + _
"needed 3d driver support to run this sample")
Catch e As Exception
MsgBox("The sample has run into an error and " + _
"needs to close: " + e.Message)
EndTryEndSubEndClass
class Form1
{
Form1()
{
// In this example, initialize the Mesh object// with 4 tessellationsthis.InitializeMesh(4);
}
privatevoid InitializeMesh(int tessellation)
{
Mesh mesh1 = CreateHeightfieldMesh(tessellation);
}
privatefloat GetHeight(float x, float y)
{
return 0;
//TODO: fill in this function
}
private Mesh CreateHeightfieldMesh(int tessellation)
{
Mesh mesh;
Device device = null; // TODO: initialize thisshort[] arrayIndices = newshort[(tessellation - 1) * (tessellation - 1) * 6];
CustomVertex.PositionTextured[] arrayVertices =
new CustomVertex.PositionTextured[tessellation * tessellation];
AttributeRange attributeRange = new AttributeRange();
// Create mesh with desired vertex format and desired size
mesh = new Mesh(arrayIndices.Length / 3, arrayVertices.Length, MeshFlags.SystemMemory,
CustomVertex.PositionTextured.Format, device);
// For each point in the height field calculate the x, y, z and// texture coordinates.for (int y = 0; y < tessellation; y++)
{
for (int x = 0; x < tessellation; x++)
{
int arrayIndex = y * tessellation + x;
float xCoordinate = (float)x / (float)(tessellation - 1);
float yCoordinate = (float)y / (float)(tessellation - 1);
CustomVertex.PositionTextured vertex = new CustomVertex.PositionTextured
(xCoordinate, yCoordinate, GetHeight(xCoordinate, yCoordinate), xCoordinate, yCoordinate);
arrayVertices[arrayIndex] = vertex;
}
}
// Calculate the index buffer.for (int y = 0; y < (tessellation - 1); y++)
{
for (int x = 0; x < (tessellation - 1); x++)
{
int arrayIndex = (y * (tessellation - 1) + x) * 6;
int vertexIndex = y * tessellation + x;
arrayIndices[arrayIndex] = (short)vertexIndex;
arrayIndices[arrayIndex + 1] = (short)(vertexIndex + 1);
arrayIndices[arrayIndex + 2] = (short)(vertexIndex + tessellation);
arrayIndices[arrayIndex + 3] = (short)(vertexIndex + tessellation);
arrayIndices[arrayIndex + 4] = (short)(vertexIndex + 1);
arrayIndices[arrayIndex + 5] = (short)(vertexIndex + tessellation + 1);
}
}
// There is only one attribute value for this mesh.// By specifying an attribute range the DrawSubset function// does not have to scan the entire mesh for all faces that are// are marked with a particular attribute id.
attributeRange.AttributeId = 0;
attributeRange.FaceStart = 0;
attributeRange.FaceCount = arrayIndices.Length / 3;
attributeRange.VertexStart = 0;
attributeRange.VertexCount = arrayVertices.Length;
mesh.VertexBuffer.SetData(arrayVertices, 0, LockFlags.None);
mesh.IndexBuffer.SetData(arrayIndices, 0, LockFlags.None);
mesh.SetAttributeTable(new AttributeRange[] { attributeRange });
return (mesh);
}
publicstaticvoid Main()
{
try
{
Form Form1 = new Form();
Application.Run(Form1);
}
catch (NotSupportedException)
{
MessageBox.Show("Your device does not have the needed 3d " +
"support to run this sample");
}
catch (DriverUnsupportedException)
{
MessageBox.Show("Your device does not have the needed 3d " +
"driver support to run this sample");
}
catch (Exception e)
{
MessageBox.Show("The sample has run into an error and " +
"needs to close: " + e.Message);
}
}
}
コードのコンパイル方法
この例では、次の名前空間への参照が必要です。
参照
概念
.NET コンパクトなフレームワーク方法を説明したトピックの検索