gpt4 book ai didi

ios - Metal IOS 中的网格

转载 作者:行者123 更新时间:2023-11-28 20:53:51 24 4
gpt4 key购买 nike

我是 MTKMesh/MDLMesh 的新手,无法使用任何在线资源 Material 创建 Creating a Custom Mesh in 2D。我有顶点数据。

init(vertexBuffer: MDLMeshBuffer, vertexCount: Int,  descriptor: MDLVertexDescriptor, submeshes: [MDLSubmesh])

我希望使用这个功能。关于创建 MDLMeshBuffer、MDLVertexDescriptor、Submeshes 的任何建议

最佳答案

假设您有一个 Vertex 结构,其中包含一个表示位置的 float2 成员。您可以从这样的顶点数组 ([Vertex]) 和 16 位无符号整数索引数组 ([UInt16]) 开始。

然后你可能会做这样的事情:

let device = MTLCreateSystemDefaultDevice()!
let allocator = MTKMeshBufferAllocator(device: device)

let vertexBuffer = allocator.newBuffer(MemoryLayout<Vertex>.stride * vertices.count, type: .vertex)
let vertexMap = vertexBuffer.map()
vertexMap.bytes.assumingMemoryBound(to: Vertex.self).assign(from: vertices, count: vertices.count)

let indexBuffer = allocator.newBuffer(MemoryLayout<UInt16>.stride * indices.count, type: .index)
let indexMap = indexBuffer.map()
indexMap.bytes.assumingMemoryBound(to: UInt16.self).assign(from: indices, count: indices.count)

let submesh = MDLSubmesh(indexBuffer: indexBuffer,
indexCount: indices.count,
indexType: .uInt16,
geometryType: .triangles,
material: nil)

let vertexDescriptor = MDLVertexDescriptor()
vertexDescriptor.attributes[0] = MDLVertexAttribute(name: MDLVertexAttributePosition,
format: .float2,
offset: 0,
bufferIndex: 0)
let mdlMesh = MDLMesh(vertexBuffer: vertexBuffer,
vertexCount: vertices.count,
descriptor: vertexDescriptor,
submeshes: [submesh])

let mesh = try? MTKMesh(mesh: mdlMesh, device: device)

如果您的顶点类型更复杂,您需要相应地调整顶点描述符。

关于ios - Metal IOS 中的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com