Search code examples
three.jsglslglsles

a simple compare of vec4.x > 5


I cannot compile the following fragment shader:

uniform vec3 color;
uniform sampler2D tDiffuse;
varying vec2 vUv;

void main() {
  vec4 texel = texture2D( tDiffuse, vUv );
  vec3 luma = vec3( 0.299, 0.587, 0.114 );
  float v = dot( texel.xyz, luma );
  if (texel.x > 5)
    gl_FragColor = vec4( v * color, texel.w );
  else
    gl_FragColor = texel;
}

If I change (texel.x > 5) to (1 > 5) it works fine. But somehow texel.x causes a compile error. Does anyone see an obvious problem with this code?


Solution

  • texel.x is a float, 5 is an int, you can't compare both directly.

    Try to write 5.0 instead:

    if (texel.x > 5.0)