SceneKit 中的自定义顶点属性

Custom vertex attributes in SceneKit

我正在使用 SceneKit 生成一些自定义几何体。这工作正常,但我正在努力寻找一种方法来包含自定义顶点属性。具体来说,我希望为每个顶点包含一个额外的 SCNVector3。

我发现了很多使用 shaderModifiers 来包含依赖于自定义 uniform 值的 GLSL/Metal 代码的示例,但还没有找到方法包括顶点 属性 。这对 SceneKit 可行吗,还是我的想法倒退了?

要制作自定义几何图形,请使用 SCNGeometrySource objects to provide the vertex position, normal, and texture coordinate attributes using a buffer or Data object full of values, then pass your geometry sources to the SCNGeometry init(sources:elements:) 初始化器。

当您创建 SCNGeometrySource 时,您将其与类似于 GL/Metal 顶点属性的 "semantic" 相关联。要创建自定义顶点属性,只需为其提供自定义语义即可。 (在 ObjC 中,SCNGeometrySourceSemantic 只是 NSString 的类型定义,因此您可以提供自己的字符串名称。在 Swift 中,SCNGeometrySource.Semantic 是一个类似枚举的结构,您可以扩展使用 String 个原始值创建新成员。)

要在自定义着色器程序中使用自定义顶点属性,请对金属着色器使用 setSemantic(_:forSymbol:options:) for GL shaders and (I think) handleBinding(ofBufferNamed:frequency:handler:)

似乎没有办法在着色器修改器中访问自定义几何源。也就是说,如果您的数据适合 float4,您可以通过 SCNGeometrySourceSemanticColor.

传递该数据

Fox 示例代码使用相同的技术来制作草叶动画。位移量烘焙成灰度顶点颜色如下图

最后,以下着色器修改器读取 _geometry.color 的值并确保将其重置为纯白色,以便存储的数据不会影响对象的最终颜色。

 float offset = _geometry.color.x * (sin(1.2 * u_time + (_geometry.position.x+ _geometry.position.z)*4.0) + 0.5) * 0.02;
_geometry.position.x += offset;
_geometry.color.xyz = vec3(1.0);