sub*_*754 0 opengl opengl-es glsl webgl fragment-shader
我正在渲染一个纹理,我被困在需要从某些特定索引中选择值以更新当前索引的地方。
对于 EG :
float someColor = texture2D(u_image, vTexCoord).r; //assume u_image is 10*10 image
if (someColor.r > 0.5) {
someColor = someColorPalette[(zr*zcols)+(zc-1)]; //someColorPalette is another texture
//where (zr*zcols)+(zc-1) is getting the pixel value from some index using the calculation
}
Run Code Online (Sandbox Code Playgroud)
`
在上面的代码片段中,我知道这someColorPalette[(zr*zcols)+(zc-1)]是一个 CPU 语句,在 opengl 中不起作用。任何人都可以建议一些替代解决方案来读取纹理索引吗?
GLSL ES 1.0 从纹理中提取特定的纹素使用
vec2 resolutionOfTexture = vec2(someWidth, someHeight)
int pixelX = ?
int pixelY = ?
vec2 uv = (vec2(pixelX, pixelY) + .5) / resolutionOfTexture;
vec4 color = texture(someTexture, uv);
Run Code Online (Sandbox Code Playgroud)
您需要添加 .5 来定位纹素的中心,否则您定位的是边缘并且可能会得到错误的像素。
当然,resolutionOfTexture与pixelX和pixelY都可以制服或什么
对于 GLSL ES 3.0 使用
vec4 color = texelFetch(someTexture, ivec2(pixelX, pixelY), 0);
Run Code Online (Sandbox Code Playgroud)
如果你想要一个线性索引,那么在 GLSL ES 1.0
pixelX = mod(someIndex, resolutionOfTexture.x);
pixelY = floor(someIndex / resolutionOfTexture.x);
Run Code Online (Sandbox Code Playgroud)
在 GLSL ES 3.0 中
ivec2 texSize = textureSize(someTexture, 0);
pixelX = someIndex % texSize.x;
pixelY = someIndex / texSize.x;
Run Code Online (Sandbox Code Playgroud)