This page looks best with JavaScript enabled

Building a Custom Unity Terrain Shader

 ·   ·  ☕ 2 min read

    We’ve built a lot of shaders, how do we attach these to our terrain though? Let’s take a look at how you can build a basic shader that integrates with Unity’s pre-built terrain tools.

    To do this we’ll need to build a shader that accepts the Splat maps and Splat’s that Unity passes in. Unity will give us a single RGBA control texture. This represents a map of 4 different textures on the terrain. Unity also configures both the Diffuse and Normal maps for each Splat we’ll be using. Each channel in our control texture maps to the strength of the splat it represents at that point on the terrain.

    By multiplying the texture lookup for each Splat by the value of the control texture that represents it (RGBA maps to Splat 0, 1, 2 and 3 respectively) we know the strength of each texture we should draw. Writing this out to our Surface Shader’s Albedo allows us to easily draw the blended maps together.

    We also get access to all the other advantages of writing our own shader. We can discard pixels or make our terrain emissive as examples.

    If you want to follow along with the Tutorial I used in this video or just want to check out the cool toon shaded terrain example you can find the source here:

    For a reference on Shader Levels you can see this Wikipedia page The number of interpolated registers increases from 10 in Shader Model 3 to 32 in Shader Model 4+.

    The World of Zero Discord Channel:

    Sam Wronski
    Sam Wronski
    Maker of things, currently helping build cloud things @ Google. World of Zero is a personal project disconnected from my professional work. Lets make something awesome together!