Procedural Grass Shader

The other day, I put together a little procedural grass shader for my game. I’m trying to keep it PS1-esque with particular inspiration from Mega Man Legends, so nothing fancy like generating actual blades of grass. No, I’m just generating a texture.

I haven’t created a full test environment yet for it, but I intend to get around to that soon when I can grapple my brain into working with me. The output of the shader on my little test platform is:

Shader output

And the shader itself is:

Full Shader

It’s fairly simple and my first time writing shaders in like a decade. Also my first time doing a visual scripting shader. The fact that Godot’s shader editor lets you preview intermediate steps is really handy.

Anyways, let’s go over the individual portions.

The Inputs

A common method for “painting” terrain is to use vertex colors or similar to assign colors to each portion of a mesh, then have a shader convert those colors into particular materials. I am doing something somewhat similar here. I’m using the r and g channels of the vertex color to determine two types of painting.

Firstly, something I am pulling from Mega Man Legends is having two different colors of grass. Darker grass cannot be traversed, lighter grass can be. It’s a fairly simple way to communicate where the player can and cannot go so the invisible walls don’t feel quite so arbitrary.

Two colors of grass in Mega Man Legends

The red channel on the vertex color is what determines which of the two grass colors we use.

The other thing I am pulling from Mega Man Legends is the dirt paths.

Dirt paths in Mega Man Legends

Dirt paths in this shader are controlled by the green channel.

Potential future considerations may put these onto one channel.

Grass Blending

Grass blending portion of the shader

The grass blending is simple. We take the red channel input and feed it through a step function, snapping it to 0.0 or 1.0. This is then used as a weight for blending between the lighter and darker grass colors.

World Coordinates

Calculating the world coordinates of a fragment

For the next portion of the shader where we add a bit of noise, I wanted the noise to be continuous across separate meshes. To do this, I needed to calculate (and scale) the world coordinates of the fragment. This is largely self explanatory, just a simple transformation. We strip away the vertical y coordinate as it is not relevant here. At the end, we divide the world coordinates by 10 in order to stretch the noise out over a wider area and make repetition and tiling less obvious.

Dirt Blending

Dirt blending portion of the shader

The last major portion of the shader is blending the grass with the dirt. The key component is the simplex noise. We sample the simplex noise with the world coordinates we calculated earlier. We then take that and multiply it by the dirt path from the vertex color. If the green channel/dirt path input is 1, there is no dirt path and the noise is left as-is. If the green channel is 0, then there is a dirt path and the noise is overwritten with 0.

We then take the output of this and feed it into a mix between the grass color calculated earlier and the dirt color. It’s set up so that grass is at a value of 1, dirt is at a value of 0. This also means that we can use intermediate values on the green channel to have partial wear on the grass, only somewhat exposing the dirt.

Conclusion

I’m really liking this! It was a fun little project, I was able to refamiliarize myself with shader programming and figure out some techniques for blending materials together. As we go I will hopefully end up learning some more techniques to make more interesting shaders. For now, I do need to get around to implementing this in blender so I can make some sample environments to properly test the grass coloration and the dirt wear.