有大量的实例创建使用的EG的工厂参数方法之一MDLMesh,立方体:
[MDLMesh newBoxWithDimensions:...
用最简单的这些,对于一个“平面”(矩形),我生成具有最小数量的顶点的一个1x1矩形:
MDLMesh *mdlMesh = [MDLMesh newPlaneWithDimensions:(vector_float2){1.0, 1.0}
segments:(vector_uint2){1, 1}
geometryType:MDLGeometryTypeTriangles
allocator:metalAllocator];
然后我用Xcode的调试器来调查什么导致MDLMesh样子,以此来指导一个更简单的对象我的创作,一个纲领性的等边三角形。
以下代码适用于我。我敢肯定,比我更懂金属的人可以提供更好的解决方案。但希望这将让你开始,在正确的方向的一些外表...
所以,直到有用于
[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];
回答你自己的问题并没有什么错 - 这在这里很常见。你应该继续这样做(并确保将其标记为已接受)。 – user5226582