...
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma multi_compile_local ___ OPEN_CLOSE_EFFECT
#if defined(OPEN_CLOSE_EFFECT)
#pragma surface surf StandardSpecular fullforwardshadows vertex:vert
#endif
#if !defined(OPEN_CLOSE_EFFECT)
#pragma surface surf StandardSpecular fullforwardshadows
#endif
...
I need a vertex code to generate some random number when OPEN_CLOSE_EFFECT is ON, otherwise i don’t need vertex function.
The compiler tells me the second pragma is ignored…
Is there a way to make this work ? or will i be forced to always have a vert function with UNITY_INITIALIZE_OUTPUT(Input,o); in it ( wich seems slower than with no vert function ) ?
Thanks in advance for your answers and happy unitying !
All #pragma lines are evaluated prior to the #if and #define lines, so there’s no way to change #pragma lines this way. #pragma surface lines even more so because Surface Shaders are shader generators, and those lines are commented out in by the shader generation code so don’t even really exist by the time it’s looking at the #if statements. (The shader generator code does actually look at them, but only uses a set of default defines and hard coded #define lines, nothing from the multi_compile.)
The solution is indeed to just have a vertex:vert, and have everything inside of the custom vertex function commented out. And no, it’s not really any slower. The shader compiler will see nothing is happening and remove all the code anyway. Functions don’t really exist in compiled shaders, they’re just there in the human written shader code for organization / sanity. Actual shader code is all inlined, so it all gets dumped into a single giant “vertex function” and single giant “fragment function” that the shader compiler then strips duplicated / unused code from.
That’s not entirely true anymore since Unity 2020.1
We now run a preprocessor before figuring #pragma directives (requires Caching Preprocessor enabled in Project settings → Editor settings). However, we ignore conditional effects on pragmas if the conditional depends on user-defined keywords or a keyword that is dependent on some setting. This leaves “keywords that depend on the current build target” and regular defines in the shader itself as things that can influence #pragma directives.
thanks a lot !
As usual you give accurate and global answer
So you say i could get rid of this “UNITY_INITIALIZE_OUTPUT(Input,o);” and have a totally empty vertex func ?
I didn’t even give this a try… but i will.
thanks again !
@aleksandrk thanks for the clue but am not yet using 2020.1 and i confess i prefer staying on the well known roads for making shaders; the ones where i can expect help for
Preprocessing shader code before checking #pragma directives is not such a big change in terms of “how do I write things”. And you should be able to get help with this when required