Custom Shaders for Unit and Target Selection
Recently I’ve been blogging a lot of specific tasks that were done for my TRPG, so to continue with that theme I decided to talk about a couple custom shaders I wrote for selecting things. Here are gifs showing the two shaders I’ll be talking about:


As is typical for most repeating animations, both shaders are built around the game’s time and the sine function. Unity passes the time value in to shaders, so that gives a simple number that increases smoothly. Meanwhile, sin() returns oscillating values (between 0 and 1) that can be used for a variety of animated effects.
In the case of the dashed line shader, sine of the texture coordinates (ie. sin(uv.x) ) is used to get a smoothly varying wave along the polygon. Then step() is used on that wave value to snap to either 0 or 1. Lerping (ie. lerp() ) based on that stepped value to choose between two colors for the sharp dashes. All of those operations are done in the fragment shader, while the vertex shader offsets the uv.x texture coordinate by _Time.y
The pulsing target shader also uses sin() and _Time.y to achieve the animation, but those functions/values are used differently. Rather than only modifying the texture coordinates, this shader modifies the vertex positions. Furthermore, both the sine function and the time value are used in tandem in one line of code, rather than being used separately as in the dashed line shader. sin(_Time.y) returns a value that oscillates between 0 and 1 over time. By using that in the vertex shader, one can offset the vertex position using that oscillating value, resulting in pulsating geometry.
Both animations could be done without shaders, but run faster on the GPU than doing work on the CPU.
Discussion