Texture Splatting

From Shader Forge Wiki
Jump to: navigation, search

Texture Splatting is perhaps more commonly known as Texture Blending, but that's a bit misleading since you might think we are talking about blendmodes. Nonetheless it is a common technique to render more than one texture across a single surface. Most commonly it is used for terrains, where you might want the surface to gradually change from grass to dirt. There are many different ways to achieve the effect, commonly you use either vertex colors painted into the mesh, or a separate texture which uses a different UV channel to map itself around the mesh. I will show both techniques here. Here's an example image of the effect we are about to achieve:


How it works

The most basic idea is to use a gradient between 0 to 1 and use that to interpolate how much of each texture is shown. How to get such a gradient varies, we'll get into the two most common approaches later, but for now we can use the U value of a UV coordinate node just to try the theory out! If you hook the U to the Diffuse connector of the Main node, you can see that this gives us exactly that type of gradient:


The interpolation can be done in many ways, but for now we'll use the most basic way which is using a Lerp node. Lerp stands for Linear Interpolation. You will want to add two Texture2D nodes with different textures and hook them up to the A and B inputs of the Lerp node. For the T input we want our gradient, which was U from the UV coordinate node. This will give us a basic example of linear-interpolation blending between two textures:


The brick texture is shown where the gradient is dark, and the metal texture is shown where the gradient is bright, with inbetween values showing a bit of both.

If you were to use this shader on a terrain, it wouldn't very likely yield useful results (unless you've cleverly UV mapped the mesh with this in mind, which I don't recommend as it's not a particularly effective way to achieve the effect). You'll probably want to use something such as vertex colors instead to generate the gradient.

Using Vertex colors

You could very simply add a Vertex Color node, hook one of the colors in to the T input of the Lerp node to get a basic 2-texture blend, like this:


Note that I switched the mesh from the plane into the vertex-color example mesh included in ShaderForge so that we can see the effect. If this was a terrain mesh with red painted in here and there on the vertices, you would get spots of the metal texture showing up together with the brick texture covering the rest. This is all cool and stuff, but what if we want more than two textures? We have four color channels in the vertices after all! In order to achieve this you'll want to make a chain of Lerp nodes, with the previous output being one of the inputs in the next Lerp node. See below:


The attentive reader might notice that even though there's only 4 color channels on the vertices, we are still blending 5 different textures! This is because we have the ability to default to a texture when there's no color at all, in this case our brick texture which you can see along the borders on the example model. The order of the Lerp chain matters in such a way that the next entry has priority over the last one, meaning that if you paint both full red and full green on a pixel, only the texture hooked to the G channel output of the Vertex Color node will show. The Alpha channel has highest priority, so it will render the associated texture on top of everything else. Unfortunately the model has 1 in alpha on all vertices, meaning that it would only display Texture4. That's why there's a Multiply by 0 thrown in just so that the effect would be visible in the example screenshot. You wouldn't do that in your own shader.

This vertex color based solution doesn't put any special requirements on the UV-mapping as the texture based solution below does.

Using a Texture

When using a texture for your Lerping, you'd commonly call that texture a Splatmap. You might recognize this term from Unitys built-in Terrain system. The setup is very similar to the one above:


Using this technique you can update the texture-blending separately from the model in an image manipulation program. Note that this works only if the terrain is UV-mapped with top-down directional mapping, as you generally do with heightmap based terrain. The vertex color based solution doesn't have this restriction.


This should get you started with basic texture splatting, but it's worth pointing out that using Lerp nodes isn't the most optimized of approaches, but still acceptable for most. More about this in Advanced Texture Splatting, among with other topics!