0
\$\begingroup\$

As titled

I am sampling from a texture and if the color is somehow gray [vec3(.8), vec3(.9)] and an uniform is 1 I need to substitute that color with another one

I am not a glsl veteran but I am pretty sure there is a more elegant and compact (without mentioning faster) way than this:

vec3 textureColor = texture(texture0, oUV);

if(settings.w == 1 && textureColor.r > .8 && textureColor.r < .9
                    && textureColor.g > .8 && textureColor.g < .9
                    && textureColor.b > .8 && textureColor.b < .9)
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

How about:

vec3 delta = abs(textureColor - vec3(0.85, 0.85, 0.85)); // Get delta from middle vec3

if (delta.r <= 0.05) && (delta.g <= 0.05) && (delta.b <= 0.05)

Performance needs to be profiled, but it's sure shorter to write

\$\endgroup\$
1
  • \$\begingroup\$ Sounds nice, +1, lets give just sometime to see if someone shows up with a better one, otherwise I will accept your :) \$\endgroup\$ Commented Aug 21, 2014 at 9:01
1
\$\begingroup\$

You can use the inbuilt GLSL lessThan() and greaterThan() functions (supported in all GLSL versions). These give two boolean vectors as a result.

https://www.opengl.org/sdk/docs/man/html/greaterThan.xhtml http://www.opengl.org/sdk/docs/man4/html/lessThan.xhtml

Then, you can use 'equal' (https://www.opengl.org/sdk/docs/man/html/equal.xhtml) on those results.

I would still do the first version though, even if its a bit more code, its perfectly understandable. :)

\$\endgroup\$
1
  • \$\begingroup\$ I toke the other one since I like it more, anyway thanks for your suggest, +1 \$\endgroup\$ Commented Aug 25, 2014 at 13:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.