2
\$\begingroup\$

I'd like to have this in my shader:

#if SHADER_API_MOBILE
#pragma surface surf Lambert noforwardadd
#else
#pragma surface surf Lambert
#endif

But this tries to compile both #pragma surface and ignores the second one instead of respecting the #if condition.

Anyone know how to add "noforwardadd" to a shader, but only when it's compiled for mobile platforms?

Thanks!

Allen

\$\endgroup\$
2
\$\begingroup\$

Preprocessor macros don't work on pragmas, because they are parsed before any preprocessing happens. What you can do instead is to make two identical subshaders, the top one without noforwardadd and the bottom one with. If you assign different LOD thresholds to those subshaders, you can then choose between them by making mobile builds use a lower LOD value:

SubShader {
    // Desktop shader
    LOD 300

    CGPROGRAM
    #pragma surface surf Lambert
    ...
    ENDCG
} 
SubShader {
    // Mobile shader
    LOD 100

    CGPROGRAM
    #pragma surface surf Lambert noforwardadd
    ...
    ENDCG
}

With LOD set as:

#if (UNITY_IOS || UNITY_ANDROID)
    Shader.globalMaximumLOD = 200; // Only subshaders with LOD <= 200 will run
#else
    Shader.globalMaximumLOD = Int32.MaxValue;
\$\endgroup\$
  • \$\begingroup\$ Hmm, that looks like something that might get some of the benefits that I'm looking for, thanks! With this method, I suspect it will increase shader size (instead of decreasing as noforwardadd would do normally)... \$\endgroup\$ – Allen Pestaluky Jun 10 '16 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.