Mik*_*rts 5 objective-c ios metal
如果您使用的是 iOS Metal,并且您有自定义的编程对象模型,并且(像我一样)您的模型随着 Metal2 和 iOS 11 的出现而停止工作,那么您可能已经开始研究如何以编程方式生成 MDLMesh。
Apple 文档说:“通常,您可以通过遍历 MDLAsset 对象的对象层次结构来获取网格,但您也可以从自己的顶点数据创建网格或创建参数化网格。” 不幸的是,它没有提供说明或示例代码。
您很快就会找到两个 MDLMesh 初始化调用,initWithVertexBuffer 和 initWithVertexBuffers。就像您在网上找不到示例代码或讨论一样快……至少我没有成功找到任何示例代码。
由于对于这个不经意的观察者来说应该如何完成并不是很明显,因此请求代码示例。
有很多示例使用工厂参数化方法之一创建 MDLMesh,例如立方体:
[MDLMesh newBoxWithDimensions:...
Run Code Online (Sandbox Code Playgroud)
使用其中最简单的方法,对于“平面”(矩形),我生成了一个具有最少顶点数的 1x1 矩形:
MDLMesh *mdlMesh = [MDLMesh newPlaneWithDimensions:(vector_float2){1.0, 1.0}
segments:(vector_uint2){1, 1}
geometryType:MDLGeometryTypeTriangles
allocator:metalAllocator];
Run Code Online (Sandbox Code Playgroud)
然后我使用 Xcode 调试器来研究生成的 MDLMesh 是什么样子,以此来指导我创建一个更简单的对象,一个程序化的等边三角形。
以下代码对我有用。我相信比我更了解 Metal 的人可以提供更好的解决方案。但这有望让你开始,在一些正确的方向上......
所以直到有一个新的工厂参数方法
[MDLMesh newEquilateralTriangleWithEdgeLength:...
Run Code Online (Sandbox Code Playgroud)
以下代码似乎可以解决问题...
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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1829 次 |
| 最近记录: |