- Home /
Replacing Shader At Runtime.
Hi Everyone, I would like to know if it is possible to change a single shader from Standard to a custom shader at run time for example; Changing from Standard to a legacy or Standard to toon.
In my project I have a Standard surface shader that I want to replace with a Standard-no fog shader. I have tried setting different materials on objects with renderers but that leads to keeping track of all the objects that needs a material change. Also changing materials on a large amount of game objects can be quite performance heavy.
Thanks in Advance.
Answer by RLin · Apr 25, 2018 at 11:48 PM
There are multiple ways to do this.
1) You could keep a list of all materials that need their shader changed, but, as you said, you would prefer not to do this.
2) You could use Camera.RenderWithShader and use the tags to exclude materials that don’t need to change.
3) You can use custom shader LODs and fallback shaders. Have the standard shader be, for example, at a LOD of 100, toon at 50, and legacy at 25 and have toon be a fallback of standard and legacy be a fallback of toon. Reduce maximum shader LOD to change these shaders to their fallbacks. Note that you will probably have to change the LODs of every shader that shouldn’t change for this to work correctly and you will not be able to use shader LODs for other uses.
I personally would use method 1) or 2).