- Home /
How to modify output color of built-in Terrain Shader with more than 4 Splat Textures?
I changed the Built-in Sureface Shader to modify the output color in certain circumstances. In fact I implemented Fog of War for parts of the terrain that isn't visible by the player.
That works great as long as the terrain is using 4 splat textures. In case it's using more than 4 textures, the color modification is only applied to the first 4 textures. The other textures are rendered normally, as though nothing had happened.
I tried to modify the color using the actual surface function (SurfaceOutput.Albedo) and tried modify it using the (finalcolor) function. Both worked for the first 4 textures. But for the other textures it doesn't work.
Any Ideas?
Let me show you an example. This is a Unity terrain using a custom shader:
I added to the built-in terrain shader a finalcolor:grid function to render the hexes:
void grid (Input IN, SurfaceOutput o, inout fixed4 col) {
fixed4 grid=tex2D (_TerrainGrid, _TerrainGridOffset+IN.uv_TerrainGrid*_TerrainGridScale) * _GridVisibility;
col += grid * col * 2.0f;
}
When I use the terrain inspector to paint another texture to the terrain. For now, the terrain only contains 3 splat textures:
The result looks like this:
As you can see, the grid is still visible at the changed parts.
Then I added some other splat textures and selected the last one (in this case a water texture):
As you can see, the grid is no more visible at the parts with the water texture:
That only happens if the texture is not one of the first 4 textures.
I added some screenshots and sample code to demonstrate the behavior.
It looks like Unity is using 2 different shader for the terrain. FirstPass.shader and AddPass.shader.
You need to modify both of thous shader if you like to use more then 1 Splat-map / more then 4 textures.
Thanks fffmalzbier, modifying the add pass shader solved the problem.
Answer by Hovu96 · Apr 16, 2013 at 06:03 PM
fffmalzbier solved the problem, his answer is:
It looks like Unity is using 2 different shader for the terrain. FirstPass.shader and AddPass.shader.
You need to modify both of thous shader if you like to use more then 1 Splat-map / more then 4 textures.
Thanks!
Answer by solid_state · Apr 14, 2013 at 10:29 AM
As far as i know you can only have 4 textures per splatmap, you may however have more than one splatmap. see this thread
i don't know however if having more splatmaps affects your shader, but maybe this info helps you to figure it out
Yes, I think Unity splits the list of terrain textures into groups of four textures (called splatmaps) and then invokes the terrain shader multiple times. But this is only an assumption. But I don't understand why my modification to the shader is only applied to the first splatmap.
Is it correct that the shader is invoked multiple times?
The problem is that the splatmap is a normal texture with the channels R G B A -> four channels. For more used textures on the terrain you need at least another splatmap.