WebGL 着色器中的十六进制到 RGB 值

Ale*_*oth 5 html c opengl-es webgl fragment-shader

我正在开发一个应用程序,我想在其中获取单个整数输入(基本上是一种颜色),并使用 WebGL 着色器为具有给定输入的框着色。我最初计划使用轮班和掩码的组合来做到这一点,如下所示:

uniform int u_color;

float rValue = (((u_color) >> 16) & 0xFF) / 255.0;
float gValue = (((u_color) >> 8) & 0xFF) / 255.0;
float bValue = ((u_color) & 0xFF) / 255.0;
gl_FragColor = vec4(rValue, gValue, bValue, 1.0);
Run Code Online (Sandbox Code Playgroud)

所以给定 int 0xFF33CC, red=1.0, green=0.2, blue=0.8

但是,我遇到了一个问题,发现 WebGL 着色器无法执行按位移位。

我想知道如何能够从给定的整数有效地生成正确的 FragColor,如果这可能的话。

编辑:经过一些反复试验,感谢@Jongware,我想出了一个解决方案

uniform int u_color;
float rValue = float(u_color / 256 / 256);
float gValue = float(u_color / 256 - int(rValue * 256.0));
float bValue = float(u_color - int(rValue * 256.0 * 256.0) - int(gValue * 256.0));
gl_FragColor = vec4(rValue / 255.0, gValue / 255.0, bValue / 255.0, 1.0);
Run Code Online (Sandbox Code Playgroud)

除了清理之外,这段代码非常适合这项工作,但是我总是对与上述方法不同的任何其他方法感兴趣。

chu*_*ica 7

要添加到您自己的答案中,请考虑继续使用整数数学直到最后。

uniform int u_color;
unsigned rIntValue = (u_color / 256 / 256) % 256;
unsigned gIntValue = (u_color / 256      ) % 256;
unsigned bIntValue = (u_color            ) % 256;
gl_FragColor = vec4(rIntValue / 255.0f, gIntValue / 255.0f, bIntValue / 255.0f, 1.0);
Run Code Online (Sandbox Code Playgroud)