我很快制作了一个成功插入颜色的顶点/片段着色器:
#ifdef GL_ES
precision highp float;
#endif
uniform vec2 resolution;
void main(void)
{
vec2 p = gl_FragCoord.xy / resolution.xy;
float gray = 1.0 - p.x;
float red = p.y;
gl_FragColor = vec4(red, gray*red, gray*red, 1.0);
}
结果如下:
现在将其应用于四边形会产生正确的结果,因为插值实际上是在整个曲面上使用x
和y
坐标完成的。请参阅@datenwolf 的详细解释,了解为什么会这样。
编辑 1为了在功能性颜色选择器中获得全范围的颜色,可以交互地修改色调(参见https://stackoverflow.com/a/9234854/570738)。
现场在线演示:http: //goo.gl/Ivirl
#ifdef GL_ES
precision highp float;
#endif
uniform float time;
uniform vec2 resolution;
const vec4 kRGBToYPrime = vec4 (0.299, 0.587, 0.114, 0.0);
const vec4 kRGBToI = vec4 (0.596, -0.275, -0.321, 0.0);
const vec4 kRGBToQ = vec4 (0.212, -0.523, 0.311, 0.0);
const vec4 kYIQToR = vec4 (1.0, 0.956, 0.621, 0.0);
const vec4 kYIQToG = vec4 (1.0, -0.272, -0.647, 0.0);
const vec4 kYIQToB = vec4 (1.0, -1.107, 1.704, 0.0);
const float PI = 3.14159265358979323846264;
void adjustHue(inout vec4 color, float hueAdjust) {
// Convert to YIQ
float YPrime = dot (color, kRGBToYPrime);
float I = dot (color, kRGBToI);
float Q = dot (color, kRGBToQ);
// Calculate the hue and chroma
float hue = atan (Q, I);
float chroma = sqrt (I * I + Q * Q);
// Make the user's adjustments
hue += hueAdjust;
// Convert back to YIQ
Q = chroma * sin (hue);
I = chroma * cos (hue);
// Convert back to RGB
vec4 yIQ = vec4 (YPrime, I, Q, 0.0);
color.r = dot (yIQ, kYIQToR);
color.g = dot (yIQ, kYIQToG);
color.b = dot (yIQ, kYIQToB);
}
void main(void)
{
vec2 p = gl_FragCoord.xy / resolution.xy;
float gray = 1.0 - p.x;
float red = p.y;
vec4 color = vec4(red, gray*red, gray*red, 1.0);
adjustHue(color, mod(time, 2.0*PI));
gl_FragColor = color;
}
编辑 2:如果需要,用于纹理坐标的着色器(应用于纹理坐标从 0 到 1 的四边形)应该看起来像这样。未经测试。
片段着色器:
void main(void)
{
vec2 p = gl_TexCoord[0].st;
float gray = 1.0 - p.x;
float red = p.y;
gl_FragColor = vec4(red, gray*red, gray*red, 1.0);
}
一个直通顶点着色器:
void main()
{
gl_TexCoord[0]=gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}