Search Unity

Write a sprtie shader with ordered multiple effects which can be deactivated

Discussion in 'Shaders' started by flamingfox, Apr 4, 2017.

  1. flamingfox

    flamingfox

    Joined:
    Apr 14, 2015
    Posts:
    8
    hello, I searching since 2 days how to combine image effect in a dynamic way for a sprite shader.
    I discovered we could use shader program variant and keyword to enable or desable shader functionnnality (so image effect in my case), but I also want to order the enable effects. I haven't find a way to do that.

    is it possible to ordered shader functionnality in a shader or in a c# script ?

    shader program variant : https://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html?_ga=1.144804402.704837763.1491221193
    enable/desable shader functionnnality : https://forum.unity3d.com/threads/shader-variants-and-multiple-passes.284737/
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    There are fairly complicated and expensive ways to do this that I can think of, and there's brute force ways using a ton of variants, or you can just leave the effects as separate sprite layers rather than controlling it via the shader which will likely be faster.

    So the brute method I can think of is basically have multiple groups of the same effect, then use a KeywordEnum to select between them.

    Some bits of shader code:
    Code (CSharp):
    1. // properties
    2. [KeywordEnum(Off, One, Two, Three)] _Effect_A_Order ("Effect A Order", Float) = 0
    3. [KeywordEnum(Off, One, Two, Three)] _Effect_B_Order ("Effect B Order", Float) = 0
    4. [KeywordEnum(Off, One, Two, Three)] _Effect_C_Order ("Effect C Order", Float) = 0
    5.  
    6. // pragmas
    7. #pragma shader_feature _ _EFFECT_A_ORDER_ONE _EFFECT_A_ORDER_TWO _EFFECT_A_ORDER_THREE
    8. #pragma shader_feature _ _EFFECT_B_ORDER_ONE _EFFECT_B_ORDER_TWO _EFFECT_B_ORDER_THREE
    9. #pragma shader_feature _ _EFFECT_C_ORDER_ONE _EFFECT_C_ORDER_TWO _EFFECT_C_ORDER_THREE
    10.  
    11. // shader code
    12. // group one
    13. #if _EFFECT_A_ORDER_ONE
    14.     // do effect A code
    15. #endif
    16. #if _EFFECT_B_ORDER_ONE
    17.     // do effect B code
    18. #endif
    19. #if _EFFECT_C_ORDER_ONE
    20.     // do effect C code
    21. #endif
    22.  
    23. // group two
    24. #if _EFFECT_A_ORDER_TWO
    25.     // do effect A code
    26. #endif
    27. #if _EFFECT_B_ORDER_TWO
    28.     // do effect B code
    29. #endif
    30. #if _EFFECT_C_ORDER_TWO
    31.     // do effect C code
    32. #endif
    33.  
    34. // group three
    35. #if _EFFECT_A_ORDER_THREE
    36.     // do effect A code
    37. #endif
    38. #if _EFFECT_B_ORDER_THREE
    39.     // do effect B code
    40. #endif
    41. #if _EFFECT_C_ORDER_THREE
    42.     // do effect C code
    43. #endif
    It can obviously balloon to a pretty huge shader to manage, but does what you want. For your own personal sanity I would try to make each effect it's own function so you don't have to write it all out for each group which would be error prone, especially if you want to tweak the effect.
     
  3. flamingfox

    flamingfox

    Joined:
    Apr 14, 2015
    Posts:
    8
    Thanks a lot, I don't know we can use a enum of keyword. This will work for what I want to do.
    I planned to write all the effect functions in a .cginc and include the file, it's a lot easier to maintain like that.
    Otherwise you say about separate sprite layers, I don't understand what do you mean.
    Use different sprites like filters ? Or in a sprite renderer we can use real layers ?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    Like filters, mostly. Only really works if your effects are relatively simple and mostly just adding / blending on top of each other. If you're doing UV distortions or masks, you have to do this in a single shader, or get into much more complex rendering setups with render textures and multiple cameras.
     
  5. flamingfox

    flamingfox

    Joined:
    Apr 14, 2015
    Posts:
    8
    Ok, thanks for your help.