Unity Shaders: Stylized Grass
I love the look of the grass in games like Zelda Breath of the Wild and Genshin Impact.
In this post I'd like to show you a shader I've come up with to get that stylized grass look.
Since I like to keep things simple when possible, at first I've tried to model a single tuft of grass and then I've tried to "paint" it over a terrain using Unity terrain editor, but the number of tufts required to get the result I wanted was too high and it had a huge impact on performance.
To get the look that I wanted without impacting the performance, I knew that geometry shader was the only way to go but I had never touched them before so it was the perfect time to learn something new!
Basically, geometry shaders let us generate new geometry starting from the geometry coming from an object in the scene.
I've used Unity default plane which is a 20x20 triangles square.
For each triangle of the default plane, I generate a fixed number of grass blades then, by tessellating the flat plane, I can increase even more the total number of grass blades to get the look that I want.
Here's how I generate the grass blades for each triangle:
1.Get a random point inside the triangle.
In this post I'd like to show you a shader I've come up with to get that stylized grass look.
Since I like to keep things simple when possible, at first I've tried to model a single tuft of grass and then I've tried to "paint" it over a terrain using Unity terrain editor, but the number of tufts required to get the result I wanted was too high and it had a huge impact on performance.
To get the look that I wanted without impacting the performance, I knew that geometry shader was the only way to go but I had never touched them before so it was the perfect time to learn something new!
Basically, geometry shaders let us generate new geometry starting from the geometry coming from an object in the scene.
I've used Unity default plane which is a 20x20 triangles square.
For each triangle of the default plane, I generate a fixed number of grass blades then, by tessellating the flat plane, I can increase even more the total number of grass blades to get the look that I want.
Here's how I generate the grass blades for each triangle:
1.Get a random point inside the triangle.
2.Get a direction from the random point inside the triangle to one of the points of the triangle, randomly chosen.
3.Find two points at each side of the random point, along the direction I have just found.
4.Extrude the middle point along the normal vector of the triangle.
The grass blade triangle is formed by the extruded middle point and the two points on each side of starting random point.
The grass blade triangle is formed by the extruded middle point and the two points on each side of starting random point.
I've put up a repository on Github if you want to test it out.
Here's a video of a demo of the grass shader in action