Shaders in Unity
Shaders are scripts or graphs that tell the engine how something should be visually rendered through code, they allow for very specific visual styles to be applied that wouldn’t usually be achievable, they are used often in conjunction in games to create some of usually more impressive features such as lighting and interactive elements such as grass. Naturally shader programming is quite difficult to learn as it’s a custom scripting language for graphics programming but I’ve spend the semester working with it with the intent of creating some bespoke shaders for various elements of the game
Although this started out as a grass shader (I wanted a shader that would allow grass to sway in the wind) but as we dropped grass covered levels and I’d already written the shader, I found the shader worked perfectly for the tree leaves in all of the areas allowing them to sway and move, this motion simulates wind and adds a new dynamic to the game.
Water fall shader left and tree/geometry shader below
The water shader is used for around the violin town, as it’s a seaside town I wanted the water to feel somewhat believable and I thought the best way to achieve this was to write a shader, water shaders are generally quite advanced however I found some useful information online about processes and logic that can be used and I worked with this and some experimentation to figure out how I might be able to achieve the water I was looking for.
This is heavily inspired by the Recompiled team’s work with Pseudo light shaders, I spoke to the team at EGX and their passion for writing Unity shaders was partially what inspired me to keep writing shaders. I liked the idea of creating volumetric light for the wind level to have volumetric light shining through the trees, this will generally light up the area and make it feel more magical.
Generally volumetric light is an intensive visual component and requires a lot of hardware to calculate it, this is why I fake it. I use actual objects and texture over the lighting onto them and they act as pretend rays that I can place, I simply adjust the values and change the alpha depth and I have fully customisable rays without any of the draw backs.
Link to the original design of the shader i took inspiration from
I started work on a snow shader for the layer areas in the game, this shader would allow snow build ups on the environment, I worked with tesselation to build up and raise the floor on snowy areas and assign a texture colour to this but I wasn’t happy with the result and it interfered with normal information causing visual issues. I decided to drop the idea of using a snow shader and instead used model detail and worked with Sid to develop a snow particle effect
How it was supposed to work
This is a tessellation geometry shader which means it affects the geometry in a way that tessellates and extrude it, a far more complicated version than the tree shader, although the tellesation worked I couldn’t get a normal calculation I was happy with and it would have meant UV wrapping every environmental model, this would have been a large drain of time.
How it works
First I have to calculate depth of the material to the materials or model behind it, this allows Unity to render black and white maps over the geometry to tell what is closer to the camera and what isn’t, we then use colour gradients over that to simulate water depth.
behind the water.
We generate wave like patterns and detail through noise maps or perlin noise maps and to create the white foam around the edges we simply increase this when it notices intersecting objects
We now generate the animation be moving the noise maps over the water on the Y and X axis,
I applied a distortion map over the top that acts like a normal map in a more primitive manner, we then convert these to vector values across the Y and X axis. We then set this as UV normal.
We then tweak public variables until we have the colour and motion we’re looking for.
Inspired by graphics artists and processes: https://lindenreid.wordpress.com/2017/12/15/simple-water-shader-in-unity/
How it works
The shader for trees is actually very straightforward as it’s what’s called a geometry shader (meaning to manipulate the geometry in some way), the shader simply looks for vertices on material applied objects and then I apply a linear offset so that it moves them from one place to another and then back again, when applied to entire objects it looks like ripples or wind is affecting the object.
Volumetric light placing