How do I replace if statements with ? operator for making a shader work on mobile?

421 Views Asked by At

I'm porting a custom shader to make it work on mobile phones since shaders inside mobiles can not have if statements.

I tried replacing the if statements of my code with the ? operator but I can not compile in Unity without errors. I'm pretty new to c# so it is obvious to myself I am missing something here to complete this. Any advice from more experienced programmers here? Many thanks to you all.

// The original code is commented.

I also tried to store the result inside a variable and return it but also didn't work.

/*  
if (charCol.r > .8f) 
{
    if(_monochromatic == 1)
        return float4(0, gray, 0, 1);
    else
        return col;
}
else 
{
    return col * _brightness;
}   
*/

charCol.r > .8f ? (_monochromatic == 1) ? return float4(0, gray, 0, 1) : return col : return col * _brightness;

Error message:

Shader error in 'Custom/Shader': syntax error: unexpected token 'return'

2

There are 2 best solutions below

4
Yong Shun On BEST ANSWER

You should return as below:

return charCol.r > .8f 
    ? ((_monochromatic == 1) ? float4(0, gray, 0, 1) : col) 
    : col * _brightness;

Small opinion:

Even you can return value with the ternary operator, you may get a headache if the logic is complex and difficult to maintain.

1
Caius Jard On

A ternary operator runs a test and resolves to one value if the test is true or another value if the test is false

 var age = person.Age > 21 : "adult" : "child";

The age is tested, one of the values is picked, the expression as a whole resolves to some string and age is this a string. Because the whole thing is effectively a value it can be returned

You might be tempted to nest these; it's possible but I'd say "only do it if it's simple" like:

var age = person.Age > 21 : "adult" : (person.Age > 12 ? "teenager" : "child");

This rapidly descends into a mess if you try to take it much further. Instead prefer a switch expression:

var age = person.Age switch {
  >21 => "adult",
  >12 => "teenager",
  _ => "child"
};

You can read this by imagining the compiler prefixes every line with the thing before the switch, the => is like "then" and the _ is like "else":

var age = switch {
  person.Age>21 then "adult",
  person.Age>12 then "teenager",
  else then "child"
};

And of course with anything that resolves to a value you don't have to store it in a variable before you return it.. you ca swap var age = for return on anything you see here