Shaders give programmers and artists the ability to dictate how an object is drawn to the screen. The two most common shaders used to do this are vertex shaders and fragment shaders. Vertex shaders operate on single vertices while fragment shaders operate on interpolated positions outputted by the vertex shader. I'll be exploring some simple shader effects to demonstrate how vertex shaders and fragment shaders can be used in tandem to create interesting effects!
Local Space Shader Effect
The animation below demonstrates a complex pattern exhibited by a mesh without textures! This is achieved with a fragment shader, as they control how pixels on the screen are colored. We can set a fragment to a color based on the position the shader is given. However, one thing to note of is that the pattern on the mesh doesn't change as the mesh moves. This means the positions we are operating on have to be in the meshes local space.
World Space Shader Effect
The animation below demonstrates a very similar effect to the animation above. However, a crucial difference is that the pattern changes as the mesh moves through the world! This is achieved by simply outputting vertex positions in world space instead of local space.
Scaling Shader Effect
Below is an animation demonstrating a mesh being scaled by shaders. This is mainly achieved with the vertex shader, as it operates on vertices. The main input into a vertex shader is the position of a vertex from a mesh, which we can scale using a matrix over time!
Distance Shader Effect
The last effect demonstrated below is the alpha of a mesh changing based on its distance from the camera. This is achieved by caching a camera's position in a constant buffer per frame. We store it per frame instead of per draw call because I don't expect the camera to change positions in the same frame! With the camera position in the constant buffer, we can access it in the vertex shader to compute the distance a vertex is from the camera. This distance can be outputted to the fragment shader to change the alpha of the mesh!