Creating Interactive Grass in Unreal Engine 4
In this Unreal Engine 4 tutorial, you will learn how to create interactive grass by using a scene capture and particles to create a vector field. By Tommy Tran.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Creating Interactive Grass in Unreal Engine 4
20 mins
Until recently, grass in games was usually represented by a texture on the ground rather than rendering individual blades of grass. But as hardware power increases, so has our ability to render grass. You can see great examples of this in titles such as Horizon Zero Dawn and The Legend of Zelda: Breath of the Wild. In these titles, the player can walk through meadows of grass but more importantly, the grass reacts to the player.
Fortunately, creating a system to do this is not too difficult. In fact, you’re going to learn how to do it today! In this tutorial, you will learn how to:
- Create a vector field using a scene capture and particle system
- Bend grass away from the player by using the vector field
- Part 1: Painting With Render Targets
- Part 2: Deformable Snow
- Part 3: Interactive Grass (you are here!)
- Part 1: Painting With Render Targets
- Part 2: Deformable Snow
- Part 3: Interactive Grass (you are here!)
Getting Started
Start by downloading the materials for this tutorial (you can find a link at the top or bottom of this tutorial). Unzip it and navigate to InteractiveGrassStarter and open InteractiveGrass.uproject. You will see a small field of grass that will serve as the subject of this tutorial. I have also created a widget to display the scene capture’s render target.
Before we begin, make sure you’ve read our tutorial on creating snow trails as I will be skipping over some information from that tutorial. Note that this tutorial will also use capturing and projection. To save time, I have also already set up a capture Blueprint similar to the one in the snow trails tutorial.
Before we dive in, let’s look at a different method to creating interactive grass. The most common way is to send the player’s location to the grass material and then use a sphere mask to bend away grass within a certain radius of the player.
While this is a decent approach, it does not scale well if you want to add more grass-affecting actors. For every actor you add, you need to add another location parameter and sphere mask to the material. A method that scales a lot better is to use a vector field.
What is a Vector Field?
A vector field is simply a texture where each pixel represents a direction. If you’ve worked with flow maps before, they are the same thing. But instead of moving UVs, you’ll be moving vertices using the World Position Offset pin. Unlike the sphere mask approach, materials only need to sample the vector field once to get the bend direction.
Let’s look at how you can store directions into a texture. Take a look at this grid:
Let’s say that the red dot is an object you want to move. If you move it to the bottom-right corner, what vector would represent this movement? If you answered (1, 1), you are correct! As you probably know, you can also represent vectors as colors, which is how you would store them into a texture. Let’s plug this vector into Unreal’s color picker and see what color it returns.
As you can see, a direction of (1, 1) returns yellow. This means if you want to bend the grass towards the positive XY axes, you just need to use this color in the texture. Now let’s look at the colors for all the vectors.
The bottom-right quadrant looks pretty good since it has gradients on both axes. This means you can store any vector in that quadrant as a color since each vector has a unique color.
But the other three quadrants are problematic. They only have a gradient on one axis or no gradient at all. This means multiple vectors will share a color. For example, there would be no way to differentiate between the vectors (-1, 1) and (0, 1).
The reason these three quadrants don’t have unique colors for every vector is because you can only represent colors using values between 0 and 1. However, the three quadrants use negative values which are outside that range.
The solution is to remap the vectors so that they fit inside the 0 to 1 range. You can do this by multiplying the vector by 0.5 and then adding 0.5. Here’s a visualization of what that would look like:
Now, every vector has a unique color. When you need to use it for calculations, you just need to remap it back to the -1 to 1 range. Here are a few colors and what direction they represent after remapping:
- (0, 0): Negative X and Y
- (0.5, 0.5): No movement
- (0, 1): Negative X and positive Y
- (1, 0): Positive X and negative Y
Next, let’s look at how you would create a vector field in Unreal.
Creating a Vector Field
Unlike the snow trails, you won’t be capturing the shape of the objects. Instead, you will paint onto the render target using "brushes". These will simply be images of a custom vector field. I’ll refer to these as direction brushes.
Instead of drawing to the render target using Blueprints, you can use particles. The particles will display the direction brush and emit from the player. To create the vector field, you just use a scene capture and capture only the particles. The advantage of this method is that it is very easy to create trails. It also allows you to easily control properties such as trail duration and size. Particles also create semi-persistent trails since they still exist after leaving and re-entering the capture area.
Below are a few examples of direction brushes you can use and their effect on the grass. Note that the particles are not visible in the example below.
To start, let’s create the material that will display the direction brush.