我在将制服传递给场景套件的着色器修改器时遇到困难。这在使用 OpenGL 时似乎工作正常,但在使用 Metal 时则不然。
我之前曾成功使用过 SCNProgram,但想利用 SceneKit 的曲面细分器,而不是为曲面细分设置计算着色器。
但我想使用我已经用 Metal 编写的代码,而不是使用 OpenGL。
不幸的是,关于如何做到这一点的例子并不多。这个 Swift Playground 演示了如何在 OpenGL 中很好地做到这一点。
https://github.com/markpmlim/Twister/blob/master/Twister.playground/Contents.swift
//Geometry - Metal will translate the OpenGL code.
let geometryShaderModifier =
"uniform float twistFactor;\n" +
"mat4 rotationAroundX(float angle) {\n" +
"return mat4(1.0, 0.0, 0.0, 0.0,\n" +
" 0.0, cos(angle), -sin(angle), 0.0,\n" +
" 0.0, sin(angle), cos(angle), 0.0,\n" +
" 0.0, 0.0, 0.0, 1.0);\n" +
"}\n" +
"#pragma body\n" +
"float rotationAngle = _geometry.position.x * twistFactor;\n" +
"mat4 rotationMatrix = rotationAroundX(rotationAngle);\n" …Run Code Online (Sandbox Code Playgroud)