GLSL+Scenekit:不允许函数定义



我正试图在Scenekit中创建一个顶点着色器。着色器已成功添加到SCNNode的几何体中,但由于出现错误,它显示为紫色。我使用SCNShaderModifiers将着色器添加到几何体中,并且知道它们已成功找到并附着。

我收到的确切错误消息是:

2020-08-31 21:26:53.977977-0700 Azure[69834:3664712] [SceneKit] Error: FATAL ERROR : failed compiling shader:
Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed: 
program_source:2901:97: error: function definition is not allowed here
float2 wavedx(float2 position, float2 direction, float speed, float frequency, float timeshift) {
                          ^
program_source:2908:49: error: function definition is not allowed here
float getwaves(float2 position, int iterations) {
^
" UserInfo={NSLocalizedDescription=Compilation failed: 
program_source:2901:97: error: function definition is not allowed here
float2 wavedx(float2 position, float2 direction, float speed, float frequency, float timeshift) {
                          ^
program_source:2908:49: error: function definition is not allowed here
float getwaves(float2 position, int iterations) {
^
}

但是,当我不使用方法时,着色器可以完美地工作。为了测试这一点,我使用了一个常规正弦着色器。以下示例起作用:

uniform float Amplitude = 0.1;
_geometry.position.z += sin(u_time + _geometry.position.x);

我试图开始工作的代码是这样的,它有以下功能:

float Time = u_time;
float DRAG_MULT = 0.048;
vec2 wavedx(vec2 position, vec2 direction, float speed, float frequency, float timeshift) {
float x = dot(direction, position) * frequency + timeshift * speed;
float wave = exp(sin(x) - 1.0);
float dx = wave * cos(x);
return vec2(wave, -dx);
}
float getwaves(vec2 position, int iterations) {

float iter = 0.0;
float phase = 6.0;
float speed = 2.0;
float weight = 1.0;
float w = 0.0;
float ws = 0.0;

for(int i = 0; i < iterations; i++){
vec2 p = vec2(sin(iter), cos(iter));
vec2 res = wavedx(position, p, speed, phase, Time);
position += normalize(p) * res.y * weight * DRAG_MULT;
w += res.x * weight;
iter += 12.0;
ws += weight;
weight = mix(weight, 0.0, 0.2);
phase *= 1.18;
speed *= 1.07;
}
return w / ws;
}

我不知道这里出了什么问题。这些都是独立的GLSL片段。谢谢

我使用SCNShaderModifiers将着色器添加到几何体中,并且知道它们已成功找到并附加。

如果您使用SCNShaderModifiers,您还必须遵守他们的文档:

  1. 自定义全局函数。如果着色器修改器受益于将公共代码分解为函数,请将它们的定义放在此处。如果如果在代码段中包含自定义函数,则必须将函数定义和代码段主体之间的#pragma body指令

最新更新