Search Unity

Question Skip shader section based on Property value

Discussion in 'Shaders' started by andyz, Jun 29, 2022.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,269
    Suppose I have something like the reflection that appears in a smooth material in the standard shader. At low smoothness there is no need to read from a reflection cubemap so it can be skipped - I can do an if statement in the shader but might only slow it or I can actually just use a simpler material but that is annoying to swap material based on property values.
    So what is the best option for simplicity and shader speed?
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Creating a Custom Material Inspector for your shader, which can then look at the smoothness value being set for example, and can disable/enable a ReflectionCubemap keyword you've defined in the shader. Using "keywords" in your shader generates shader variants, and based on those shader keywords set it will run a variant that only includes the code from that specific keyword path.
     
    andyz likes this.