Shader Programming, Volume 10

Getting Started with Vertex Shader Functions

Sebastian Monroy
3 min readJul 13, 2016

We’ve explored surface shader functions and physically-based rendering in Unity. Manipulating vertices is going to open up a lot of possibilities for our shaders and we’ll get to make a lot of weird gifs along the way, so let’s get started!

Chapter 5.1

Vertex Functions, Accessing a vertex color and animating vertices in a Surface Shader

Manipulating vertex shaders is going to be exciting and awesome. No longer will we be confined by the shapes of our imported models, no longer will we be subject to their tyranny.

But first: baby steps. Let’s make a wavy plane. A kind of… snakey plane. Let’s get our snake on, on a plane.

Create a new scene and slap a plane mesh in there with a new shader and material. Assign the shader to the material, assign the material to the mesh, you know the drill.

Here’s the shader, we’ll talk about it a bit more afterward.

Essentially what this shader does is to create a sine wave value based on the current time and add it to the y value of each vertex position. The sine value is also used to modify our vertex normals in order to make its shading a little more realistic. This sort of shader would be really useful for animating a flag without having to use a skeleton structure or a hierarchy of transforms.

Just look at it. Majestic.

Chapter 5.2

Vertex Functions, Extruding your models

Let’s extrude the vertices of the soldier from the Unity camp demo. That’ll be funny. The technique we’re going to use to accomplish this is called normal extrusion. Some games use it to add more variety to their models without spending a ton of time making new content, or using a ton of space storing new content.

This is going to be a really straight-forward shader, since we won’t need to alter the normals or colors of the vertices or surfaces.

This is a very simple shader, but even so the results are pretty dramatic.

Extrusion Amount from left to right: -0.5, 0.0, 0.7

You might notice that this shader affects parts of the model that we wouldn’t necessarily want to extrude — the soldier’s helmet and pockets, for example. This technique could be improved by adding extrusion maps. That is, we could use an extra texture (or use the alpha channel of the main one) to indicate the amount of extrusion desired for any point on the model.

Here’s what the code for that might look like:

With this addition, an extruded soldier model can look like this:

You can check out the scene (\_Scenes\Ch5\extrusionmap.unity) on GitHub if you want to access the model and extrusion map texture I used.

I decided to have a little fun with this one and combine the techniques we learned in the past couple of shaders: normal extrusion and sinusoidal time-dependent values.

Heh heh hehhh heh heheh.

I like this chapter.

Next up: a snow shader!

--

--