Search Unity

Define code strings in shaders before compilation

Discussion in 'Shaders' started by ErdTod, May 6, 2020.

  1. ErdTod

    ErdTod

    Joined:
    Jan 7, 2018
    Posts:
    11
    Hello!
    Is it possible to use different fucntions in unity shaders depending on some preprocessor macros? What I want to do is like that:

    Code (CSharp):
    1.  
    2.     #if defined (_LIGHTMODE_UNLIT)
    3.         #define SURF_FUNC surfUnlit
    4.         #define LIGHTING_FUNC Unlit
    5.     #else
    6.         #define SURF_FUNC surfLit
    7.         #define LIGHTING_FUNC Standard
    8.     #endif
    9.  
    10.     #pragma surface SURF_FUNC LIGHTING_FUNC vertex:vert
    11.  
    Is this possible? By defining a keyword I can choose which function to use? (the code above doesn't work, it's for example of what I want to achieve)
    Or should I do keyword definitions inside the functions themselves? (which isn't ideal, since for lit variant I just wanted to use unity default lighting)
     
    Last edited: May 6, 2020
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,023
    Hi!
    This is not possible.
    #pragma directives are not macro-expanded.
    You can use keywords inside those functions, though.
     
  3. ErdTod

    ErdTod

    Joined:
    Jan 7, 2018
    Posts:
    11
    Thanks for the response!