Search code examples
if-statementglslfragment-shader

Compare 3 glsl vectors


I made a example code in glsl and I'am using if(diffuse.rgb > vec3(1)) and it gives a "cannot compare vector or matrices" error. and if I do this if(diffuse.rgb == vec3(1)) it runs without error, thanks.


Solution

  • Ordering comparisons and equality comparisons are not the same thing. It's easy to understand what it means for a vector to be equal/not-equal to another. But what would cause a vector to be "less than" another? Are you comparing the distance from the zero point of the space? Are you comparing each component individually? If so, what happens if some components are less while others are not less?

    GLSL has vector relational functions to deal with these varying circumstances. If the question you want to ask is "are any of these greater than 1", then you can do that with any(greaterThan(diffuse.rgb, vec3(1))). If you want to ask "are all of these greater than 1," you use all(greaterThan(diffuse.rgb, vec3(1))).