Home > Mobile >  if else condition is not working in opengl glsl shading language
if else condition is not working in opengl glsl shading language

Time:08-27

I am using this code on glsl script

but when i use if else condition it not works , every time i run script it choose the first value of color variable which is declared before if else condition.

float color = 0.3 * clr.r   0.59 * clr.g   0.11 * clr.b; // script always choose this value instead of if else values

float maxrgb = max(max(clr.r, clr.g), clr.b);

if (maxrgb < 128.0) {
  float color = 0.21 * clr.r   0.71 * clr.g   0.80 * clr.b;
} else if (maxrgb > 128.0) {
  float color = 0.30 * clr.r   0.60 * clr.g   0.82 * clr.b;
} // the values in the condition are not working.

is am i doing something wrong ?

CodePudding user response:

You have declared the color variable twice. You have declared a new variable with the same name in scope of the if-statement. Declare the variable before the if statement, but assign new values to the existing variable in the statement:

float color = 0.3 * clr.r   0.59 * clr.g   0.11 * clr.b; 

float maxrgb = max(max(clr.r, clr.g), clr.b);

if (maxrgb < 128.0) {
    color = 0.21 * clr.r   0.71 * clr.g   0.80 * clr.b;
} else if (maxrgb > 128.0) {
    color = 0.30 * clr.r   0.60 * clr.g   0.82 * clr.b;
} 
  • Related