- Home /
Changing Replacement Shaders at Runtime
Hi!
We are using a replacement shader that enables normal maps on terrain by simply putting a replacement shader into the Resources folder. The Shader code just starts with
Shader "Hidden/TerrainEngine/Splatmap/Lightmap-FirstPass" {
and thus overrides the standard terrain shader without further need to do something in code.
Unfortunately, this shader performs bad on integrated graphic chips, since on those vertex programs are calculated on the CPU. So for lower quality levels, we want to replace this shader at runtime with the default terrain shader (that also can be found as source code on the Unity homepage).
So now we have two Terrain shaders in the Resources folder, and I tried to set a specific one by calling:
var terrainShader = Resources.Load("Shaders/TerrainSimpleReplacement", typeof(Shader)) as Shader;
Camera.mainCamera.RenderWithShader(terrainShader, "Opaque");
The "Opaque" tag was chosen because the Subshader code looks like this:
SubShader {
Tags {
"SplatCount" = "4"
"Queue" = "Geometry-100"
"RenderType" = "Opaque"
}
But this does not work, and there are no errors logged either. It seems that the first (or last) shader found in the Resources folder on startup is used and the code above has no effect.
Is there any way to change the shader used for terrain at runtime?
Thanks & Best Regards, Johannes
Answer by Jaroslav-Stehlik · Dec 26, 2012 at 07:33 PM
The solution is:
var terrainShader = Resources.Load("Shaders/TerrainSimpleReplacement", typeof(Shader)) as Shader; Camera.mainCamera.RenderWithShader(terrainShader, "RenderType");
in the second attribute when you try to pass tag value you have to pass tag type instead of the actual value. Unity will find the correct SubShaders by the corresponding tag value in the replacementShader it self.
I had the same problem, I think this is not well documented or explained in Unity Scripting Reference...
Answer by taoa · Apr 13, 2011 at 12:35 PM
You can do that by using the 'Fallback' feature of Unity shaders.
If the shader doesn't run at all on integrated chips, then the Fallback will tell Unity what replacement shader to use instead. Unfortunately, you might be 'unlucky enough' to have your shader still run on those chips but very poorly, ending up with crap results. In this particular case, a solution would be to use the LOD feature in combination of Fallback.
Check Shader Labs in the docs for more info on this but basically, you can assign a LOD value to all your shaders (including the overloaded terrain shaders), and in one of your scripts, once you've somehow detected that you're running on an integrated chip, turn down the value of
Shader.globalMaximumLOD
to a value you've decided on, and then all the shaders that have a LOD value superior to it will be instead replaced by their Fallback alternative.
This LOD feature is very useful for if you want to give to the users a choice in the level of quality in rendering.