【发布时间】:2023-04-09 12:36:01
【问题描述】:
有没有办法使用 GLSL(片段着色器)有效地改变 2D OpenGL 纹理的色调?
有人有代码吗?
更新:这是来自 user1118321 建议的代码:
uniform sampler2DRect texture;
const mat3 rgb2yiq = mat3(0.299, 0.587, 0.114, 0.595716, -0.274453, -0.321263, 0.211456, -0.522591, 0.311135);
const mat3 yiq2rgb = mat3(1.0, 0.9563, 0.6210, 1.0, -0.2721, -0.6474, 1.0, -1.1070, 1.7046);
uniform float hue;
void main() {
vec3 yColor = rgb2yiq * texture2DRect(texture, gl_TexCoord[0].st).rgb;
float originalHue = atan(yColor.b, yColor.g);
float finalHue = originalHue + hue;
float chroma = sqrt(yColor.b*yColor.b+yColor.g*yColor.g);
vec3 yFinalColor = vec3(yColor.r, chroma * cos(finalHue), chroma * sin(finalHue));
gl_FragColor = vec4(yiq2rgb*yFinalColor, 1.0);
}
这是与参考比较的结果:
我尝试在 atan 中用 Q 切换 I,但结果即使在 0° 左右也是错误的
你有什么提示吗?
如果需要比较,这是未修改的原始图像:
【问题讨论】:
-
这是 RGB 三元组围绕 (1,1,1) 向量的旋转 - 您可以将其表示为着色器中的矩阵乘法
-
如果您的色相变化是恒定的,那么您可以跳过 atan、sqrt、sin 和 cos。您正在做的是将它们转换为极坐标,添加角度,然后转换回来。您可以使用 2x2 旋转矩阵进行此数学运算。如果您不需要在 yiq 空间中进行任何其他数学运算,您可以预先计算整个变换。将您的 rgb2yiq 与 2d 旋转(填充为 3x3)相乘,然后与 yiq2rgb 相乘以获得一个完成整个过程的大 3x3 矩阵。就像@Flexo 所说,这只是围绕 (1,1,1) 向量的旋转。
标签: opengl filter glsl hsv hue