I want to pass a bunch of floats to the fragment shader, and access them by index.
Basically 1D Textures method mentioned in this answer: Passing a list of values to fragment shader
I try this:
javascript code
gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1);
const data = new Uint8Array([
255, 64, 192,
0, 192, 0
]);
let texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D,
0,
gl.R8,
3,
2,
0,
gl.RED,
gl.UNSIGNED_BYTE,
data);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
I access this uniform in frag shader:
uniform sampler2D u_texture;
void main() {
vec2 trans = texelFetch(u_texture, ivec2(0, 0), 0);
}
I use glTexImage2D
because I saw an example of that,
This partially works, but has two problems:
I want to get rid of all the filtering options gl.texParameteri
because I don't need them. But if I delete those I don't get the data.
This texelFetch
method returns values in the range [1, 0] (eg: 1 for 255). Why does that happen?
Basically I need an example of the mentioned method.