Shader Programming, Volume 5

Texture Mapping and RTS Unit Terrain Highlighting

Sebastian Monroy
3 min readJun 24, 2016

In the previous section we covered how to create a transparent holographic shader. Let’s see what else we can do with surface shaders, and how we might be able to interact with our shaders via script.

Chapter 2.8

Surface Shaders and Texture Mapping, Packing and blending textures

Apparently this chapter is out of date…

I made the texture packing shader above, which blends 4 textures using the RGBA values at every point of another blend texture, but was greeted by an error :

“Too many texture interpolators would be used for ForwardBase pass (11 out of max 10) at line 19.”

I tried a few things to make it work but it doesn’t seem this technique is used all that often anyway and decided to move on instead of dwelling on it. If anyone else knows a clever way to resolve this I’d love to know the solution!

Chapter 2.9

Surface Shaders and Texture Mapping, Creating a circle around your terrain

The intention of this shader is to draw a circle, around a unit in an RTS for example, that conforms to objects of arbitrary complexity. Drawing a flat circle is easy and doesn’t require custom shaders — you can just apply a texture to a quad — but if you tried to draw a flat circle on geometry as complex as terrain you’d likely see it clipping through hills or hovering above ground, among other gross things.

Start by creating some terrain. Give it some hills and valleys but don’t go overboard.

Here’s the new shader:

The way the shader works is that it takes in the parameters of our circle (its color, radius, width, and center position) and determines whether the current world position of any point on the terrain texture is within _Radius + _RadiusWidth distance away from our given center position, _Center. If it is, it assigns the color of that point on the terrain to _Color. Otherwise, it just uses the terrain’s usual color as determined by its texture.

Make a new Radius material and attach the new shader to it. Under Terrain Settings on the terrain object you made earlier, change the Material property to the new Radius material so that any changes to the parameters of the Radius.shader affect the terrain’s material. I forgot this bit the first go round so don’t be a chump like me.

You’ll notice I included the Radius.cs script as well. This is the first time we’ve encountered it in this book, but an important thing about Unity shaders is that you can access their input variables from script. In this case, I’ve attached this script to a sphere GameObject and used its position as the input vector for the _Center parameter on our new shader. Through this script it is also possible to alter the radius and color of the circle drawn by our shader.

Make sure the Albedo (_MainTex) parameter on the Radius material is set to the texture that the terrain is using! That’s the thing we’re drawing a red circle on, after all. If your terrain is using a different texture the red circle will not appear.

The result is pretty impressive, given how simple this shader is. Our shaders are starting to get kind of good!

That’s it for today. We just finished Chapter 2, by the way. :) The focus of the next chapter is Lighting Models. We’ll start by making a toon shader.

--

--