在 swift 文件中,声明了顶点数据:
struct Vertex {
let position: vector_float4
let color: vector_float4
}
Run Code Online (Sandbox Code Playgroud)
我想访问金属文件中的这个结构,可以吗?如果是的话,如何制作?
我已经知道如何用 Objective-C 实现它,只想使用 swift 。
无法直接从 Metal 使用 Swift 结构。但是,您可以在 Objective-C 标头中声明该结构,并在 Swift 和 Metal 中使用它(通过 Objective-C 桥接标头)。有关如何执行此操作的示例,请参阅 Xcode 提供的 Metal Game 模板。与 Apple 的最佳实践建议相反,我实际上不想这样做,而是在每种语言中声明此类结构。
Metal Shading Language 中的等效结构声明是
struct Vertex {
float4 position;
float4 color;
};
Run Code Online (Sandbox Code Playgroud)
如果您在管道中使用顶点描述符(带有stage_in顶点函数的参数,而不是通过接受 avertex_id和指向Vertex结构的指针来手动获取顶点),则需要向结构添加 attribute 属性,对应于它们在顶点描述符属性数组中的索引。例如,
struct Vertex {
float4 position [[attribute(0)]];
float4 color [[attribute(1)]];
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1244 次 |
| 最近记录: |