2017-10-18 4 views
2

iOS Metalを使用していて、独自のプログラムオブジェクトモデルがあり、私のようにモデルがMetal2とiOS 11の出現を止めていたら、プログラムでMDLMeshを生成する方法を検討しています。initWithVertexBuffersを使用したプログラムによるMDLMeshオブジェクトの生成

アップルのドキュメントでは、「通常、MDLAssetオブジェクトのオブジェクト階層をトラバースすることによってメッシュを取得しますが、独自の頂点データからメッシュを作成したり、パラメトリックメッシュを作成することもできます。残念ながら、それは指示やサンプルコードを提供しません。

MDLMeshの初期化呼び出しであるinitWithVertexBufferとinitWithVertexBuffersをすばやく見つけることができます。すぐに、Web上でサンプルコードやディスカッションが見つかりません...少なくとも、私は何かを見つけるのに成功しませんでした。

このカジュアルなオブザーバーには、どのように行うべきか直感的に分かりませんので、コードサンプルはここで要求されています。

+0

あなた自身の質問に答えても何も問題はありません。これはかなり一般的です。あなたはそれを先に進めて(それを受け入れられたものとしてマークするようにしてください)。 – user5226582

答えて

3

、キューブなどのファクトリパラメトリックのいずれかの方法を使用してMDLMeshを作成する例がたくさんあります:「飛行機」のために、これらの最も簡単なを使用して

[MDLMesh newBoxWithDimensions:... 

(長方形)は、I頂点の最小数を持つ1x1の矩形生成:

MDLMesh *mdlMesh = [MDLMesh newPlaneWithDimensions:(vector_float2){1.0, 1.0} 
              segments:(vector_uint2){1, 1} 
             geometryType:MDLGeometryTypeTriangles 
             allocator:metalAllocator]; 

を私はその結果のMDLMeshは、プログラム正三角形、さらに簡単なオブジェクトの私の作成を案内するための方法として、どのように見えるかを調べるためにXcodeのデバッガを使用しました。

次のコードは私に適しています。私よりもメタルに精通している方が、より良いソリューションを提供できると確信しています。しかし、これはうまくいけば

[MDLMesh newEquilateralTriangleWithEdgeLength:... 

のための新工場パラメトリック方法があるまで次のコードは、トリックを行うように見えるので...右方向のいくつかのうわべだけで、あなたが開始

を取得します...

static const float equilateralTriangleVertexData[] = 
{ 
     0.000000, 0.577350, 0.0, 
    -0.500000, -0.288675, 0.0, 
     0.500000, -0.288675, 0.0, 
}; 

static const vector_float3 equilateralTriangleVertexNormalsData[] = 
{ 
    { 0.0, 0.0, 1.0 }, 
    { 0.0, 0.0, 1.0 }, 
    { 0.0, 0.0, 1.0 }, 
}; 

static const vector_float2 equilateralTriangleVertexTexData[] = 
{ 
    { 0.50, 1.00 }, 
    { 0.00, 0.00 }, 
    { 1.00, 0.00 }, 
}; 

int numVertices = 3; 

int lenBufferForVertices_position   = sizeof(equilateralTriangleVertexData); 
int lenBufferForVertices_normal   = numVertices * sizeof(vector_float3); 
int lenBufferForVertices_textureCoordinate = numVertices * sizeof(vector_float2); 

MTKMeshBuffer *mtkMeshBufferForVertices_position   = (MTKMeshBuffer *)[metalAllocator newBuffer:lenBufferForVertices_position   type:MDLMeshBufferTypeVertex]; 
MTKMeshBuffer *mtkMeshBufferForVertices_normal   = (MTKMeshBuffer *)[metalAllocator newBuffer:lenBufferForVertices_normal   type:MDLMeshBufferTypeVertex]; 
MTKMeshBuffer *mtkMeshBufferForVertices_textureCoordinate = (MTKMeshBuffer *)[metalAllocator newBuffer:lenBufferForVertices_textureCoordinate type:MDLMeshBufferTypeVertex]; 

// Now fill the Vertex buffers with vertices. 

NSData *nsData_position   = [NSData dataWithBytes:equilateralTriangleVertexData  length:lenBufferForVertices_position]; 
NSData *nsData_normal   = [NSData dataWithBytes:equilateralTriangleVertexNormalsData length:lenBufferForVertices_normal]; 
NSData *nsData_textureCoordinate = [NSData dataWithBytes:equilateralTriangleVertexTexData  length:lenBufferForVertices_textureCoordinate]; 

[mtkMeshBufferForVertices_position   fillData:nsData_position   offset:0]; 
[mtkMeshBufferForVertices_normal   fillData:nsData_normal   offset:0]; 
[mtkMeshBufferForVertices_textureCoordinate fillData:nsData_textureCoordinate offset:0]; 

NSArray <id<MDLMeshBuffer>> *arrayOfMeshBuffers = [NSArray arrayWithObjects:mtkMeshBufferForVertices_position, mtkMeshBufferForVertices_normal, mtkMeshBufferForVertices_textureCoordinate, nil]; 

static uint16_t indices[] = 
{ 
    0, 1, 2, 
}; 

int numIndices = 3; 

int lenBufferForIndices = numIndices * sizeof(uint16_t); 
MTKMeshBuffer *mtkMeshBufferForIndices = (MTKMeshBuffer *)[metalAllocator newBuffer:lenBufferForIndices type:MDLMeshBufferTypeIndex]; 

NSData *nsData_indices = [NSData dataWithBytes:indices length:lenBufferForIndices]; 
[mtkMeshBufferForIndices fillData:nsData_indices offset:0]; 

MDLScatteringFunction *scatteringFunction = [MDLPhysicallyPlausibleScatteringFunction new]; 
MDLMaterial *material = [[MDLMaterial alloc] initWithName:@"plausibleMaterial" scatteringFunction:scatteringFunction]; 

// Not allowed to create an MTKSubmesh directly, so feed an MDLSubmesh to an MDLMesh, and then use that to load an MTKMesh, which makes the MTKSubmesh from it. 
MDLSubmesh *submesh = [[MDLSubmesh alloc] initWithName:@"summess" // Hackspeke for @"submesh" 
              indexBuffer:mtkMeshBufferForIndices 
              indexCount:numIndices 
              indexType:MDLIndexBitDepthUInt16 
              geometryType:MDLGeometryTypeTriangles 
               material:material]; 

NSArray <MDLSubmesh *> *arrayOfSubmeshes = [NSArray arrayWithObjects:submesh, nil]; 

MDLMesh *mdlMesh = [[MDLMesh alloc] initWithVertexBuffers:arrayOfMeshBuffers 
               vertexCount:numVertices 
               descriptor:mdlVertexDescriptor 
               submeshes:arrayOfSubmeshes]; 
+0

vector_float3の配列であるときにtriangleが正しく出力されなかったため、equilateralTriangleVertexDataの定義が変更されました。なぜかダンノー、それは浮動小数点作品の配列を作る。それ以外のデータは正常であると思われます。 –

関連する問題