Inner workings of the Terrain System?

Hey there,

I have been researching a while now and learned more about the workings of SRP (especially URP) as well as shader dev. My goal is to eventually create a custom terrain shader which is compatible with the unity terrain system (not necessarily with Shader Graph), but I’d like to start by customizing the provided Terrain/Lit shader.

So the terrain shader only supports 4 terrain layers to be height blended and, as I’ve understood it, more than 4 terrain layers (e.g. 8) are handled by having a Pass to render each set of 4 terrain layers, which are then blended together. Is this process controlled only by the shader or internally in some scripts? If I would use my own terrain shader, or lets say just add a fifth terrain layer support to the Terrain/Lit shader, would it work or is there something controlled by the unity terrain system internally?
In general, I was able to dig a bit through the shader code (https://github.com/Unity-Technologies/Graphics/tree/master/com.unity.render-pipelines.universal/Shaders/Terrain) and understand some stuff. Also this post Shaderlab "Dependency" property? (2013) has a helpfull attempt of explaining terrain shader workings.
However are there any resources (or plans for documentation) about how everything works together?

Some tips or hints to places are much appreciated!

Cheers
Lucas

Maybe this can help UnityCsReference/Modules/Terrain at master · Unity-Technologies/UnityCsReference · GitHub ?

By default Unity draws the terrain once for every 4 textures on it, and uses a separate add pass shader for every pass after the first one, allowing you to blend with the previous layers. However, after 8 textures Unity’s shader no longer handles some cases correctly. You can add a pragma to override this though (SplatCount=8), and handle more textures internally in your shader, skipping the extra passes.

1 Like