我是着色器新手,我正在尝试尝试《着色器之书》中提供的示例,但我目前仍坚持使用 Golan Levin 的三次贝塞尔函数:
const vec3 lineColor = vec3(0.0, 1.0, 0.0);
float slopeFromT (float t, float A, float B, float C){
float dtdx = 1.0/(3.0*A*t*t + 2.0*B*t + C);
return dtdx;
}
float xFromT (float t, float A, float B, float C, float D){
float x = A*(t*t*t) + B*(t*t) + C*t + D;
return x;
}
float yFromT (float t, float E, float F, float G, float H){
float y = E*(t*t*t) + F*(t*t) + G*t + H;
return y;
}
float plotLine(vec2 uv, float y) {
return smoothstep(y - 0.02, y, uv.y) - smoothstep(y, y + 0.02, uv.y);
}
float constrain(float y, float a, float b);
float cubicBezier (float x, float a, float b, float c, float d){
float y0a = 0.00; // initial y
float x0a = 0.00; // initial x
float y1a = b; // 1st influence y
float x1a = a; // 1st influence x
float y2a = d; // 2nd influence y
float x2a = c; // 2nd influence x
float y3a = 1.00; // final y
float x3a = 1.00; // final x
float A = x3a - 3.0*x2a + 3.0*x1a - x0a;
float B = 3.0*x2a - 6.0*x1a + 3.0*x0a;
float C = 3.0*x1a - 3.0*x0a;
float D = x0a;
float E = y3a - 3.0*y2a + 3.0*y1a - y0a;
float F = 3.0*y2a - 6.0*y1a + 3.0*y0a;
float G = 3.0*y1a - 3.0*y0a;
float H = y0a;
float currentt = x;
int nRefinementIterations = 100.0;
for (int i=0; i < nRefinementIterations; i++){
float currentx = xFromT (currentt, A,B,C,D);
float currentslope = slopeFromT (currentt, A,B,C);
currentt -= (currentx - x)*(currentslope);
currentt = constrain(currentt, 0.0, 1.0);
}
float y = yFromT (currentt, E,F,G,H);
return y;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float y = cubicBezier(uv.x, 0.500, 0.100, 0.340, 0.873);
vec3 gradient = vec3(y);
float line = plotLine(uv, y);
vec3 color = (1.0 - line) * gradient + line * lineColor;
gl_FragColor = vec4(color, 1.0);
}
我得到了这个错误:
HREE.WebGL程序:
shader error: 0 35715 false gl.getProgramInfoLog No compiled fragment shader when at least one graphics shader is attached.
ERROR:
ERROR: 0:161: '=' : cannot convert from 'const float' to 'highp int'
有人可以帮助我了解我缺少什么吗?
最佳答案
在 GLSL ES 1.00 中,浮点值不会自动转换为整数值。
100.0
是一个浮点值,但 nRefinementIterations
的数据类型是 int
。您必须通过整数值初始化 nRefinementIterations
:
int nRefinementIterations = 100.0;
int nRefinementIterations = 100;
注意,整数值也可以由浮点值构造:
int nRefinementIterations = int(100.0);
关于javascript - 错误或:ERROR: 0:161: '=' : cannot convert from 'const float' to 'highp int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59683100/