GLSL + Scenekit:不允许定义函数
GLSL + Scenekit: Function definition not allowed
我正在尝试在 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 片段。谢谢!
I added the shaders to the geometry using SCNShaderModifiers and know that they are successfully found and attached.
如果你使用SCNShaderModifiers
,你也必须遵守他们的documentation:
- Custom global functions. If your shader modifier benefits from factoring common code into functions, place their definitions here. If
you include custom functions in your snippet, you must place the
#pragma body
directive between your function definitions and the main body of the snippet.
我正在尝试在 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 片段。谢谢!
I added the shaders to the geometry using SCNShaderModifiers and know that they are successfully found and attached.
如果你使用SCNShaderModifiers
,你也必须遵守他们的documentation:
- Custom global functions. If your shader modifier benefits from factoring common code into functions, place their definitions here. If you include custom functions in your snippet, you must place the
#pragma body
directive between your function definitions and the main body of the snippet.