I am new to shaders and I'm trying to experiment with the examples present on The Book of Shaders, I'm currently stuck with Golan Levin's Cubic Bezier function:
float plotLine(vec2 uv, float y) {
return smoothstep(y - 0.02, y, uv.y) - smoothstep(y, y + 0.02, uv.y);
}
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;
// Solve for t given x (using Newton-Raphelson), then solve for y given t.
// Assume for the first guess that t = x.
float currentt = x;
int nRefinementIterations = 5;
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,1);
}
float y = yFromT (currentt, E,F,G,H);
return y;
}
// Helper functions:
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;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_resolution;
float y = circularEaseIn(uv.x);
vec3 gradient = vec3(y);
float line = plotLine(uv, y);
vec3 color = (1.0 - line) * gradient + line * lineColor;
gl_FragColor = vec4(color, 1.0);
}
and I get this mistake:
HREE.WebGLProgram:
shader error: 0 35715 false gl.getProgramInfoLog No compiled fragment shader when at least one graphics shader is attached.
ERROR:
0:147: 'xFromT' : no matching overloaded function found
can someone give me a help to understand with what am I missing;
See OpenGL ES Shading Language 1.00 Specification - 6.1 Function Definitions :
All functions must be either declared with a prototype or defined with a body before they are called.
The function has to be declared, before it is used in code. You have to move the functions xFromT
and yFromT
before cubicBezier
:
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 cubicBezier (float x, float a, float b, float c, float d){
// [...]
for (int i=0; i < nRefinementIterations; i++){
float currentx = xFromT (currentt, A,B,C,D);
// [...]
}
float y = yFromT (currentt, E,F,G,H);
// [...]
}
The other option is to declare function prototypes for xFromT
and yFromT
before cubicBezier
:
float xFromT (float t, float A, float B, float C, float D);
float yFromT (float t, float E, float F, float G, float H);
float cubicBezier (float x, float a, float b, float c, float d){
// [...]
for (int i=0; i < nRefinementIterations; i++){
float currentx = xFromT (currentt, A,B,C,D);
// [...]
}
float y = yFromT (currentt, E,F,G,H);
// [...]
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.