texelFetch in vertex shader


I want to read a texture from the vertex shader using texelFetch, but it fails to compile with the following error: ‘‘texelFetch’ : no matching overloaded function found’.

Any idea of what’s wrong?

Here is the code of my vertex shader:

uniform sampler2D tex;

attribute float vertexID;

void main() {
  int id = int(vertexID);
  ivec2 p0 = ivec2(id, 0);
  vec3 vertex = texelFetch(tex, p0, 0).rgb; // also tried with texelFetch(tex, p0);
  gl_Position = projectionMatrix * modelViewMatrix * vec4( vertex, 1.0 );

And the texture is a DataTexture, RGB with FloatType. Also interesting: it works with texture2D()…

Any help is appreciated :slight_smile:

texelFetch() does only work with WebGL 2. Are you actually using a WebGL 2 rendering context?

1 Like

I’m not telling it to, si I guess it’s using WebGL 1.0… Can I force threejs to use WebGL2?

Yes, read this for more information:


Great! Thank you a lot for your help! :slight_smile:

1 Like

You can also easily use a coordinated with float, add 0.5 to it,divide it by it’s resolution and use it regulary with texture2D.

1 Like