正如我所说,许多 Opengl ES 2.0 设备都支持 GL_OES_standard_derivatives 扩展。
但对于那些不这样做的人,我做了这个解决方法:
float myFunc(vec2 p){
return p.x*p.x - p.y; // that's our function. We want derivative from it.
}
// this calculated in vertex shader
// width/height = triangle/quad width/height in px;
vec2 pixel_step = vec2(1/width, 1/height);
float current = myFunc(texCoord);
float dfdx = myFunc(texCoord + pixel_step.x) - current;
float dfdy = myFunc(texCoord + pixel_step.y) - current;
float fwidth = abs(dx) + abs(dy); // from khronos doc #http://www.khronos.org/registry/gles/extensions/OES/OES_standard_derivatives.txt
PS 我得到的结果与 glsl 内置插件非常接近,有点模糊(在我的着色器中)。为了解决这个问题,我在 1/1.75 上添加了 multiply pixel_step。如果有人知道为什么,请告诉我。