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.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Using the Show-Only List

Before you can add to the show-only list, you need a way to get all grass-affecting actors. One way to do this is to use tags. Tags are simply strings you can assign to actors and components. You can then use the Get All Actors With Tag node to get all actors with a certain tag.

Since the player actor should affect the grass, it will need a tag. To add a tag, first click the Class Defaults button. Afterwards, create a new tag under Actor\Tags and name it GrassAffector.

unreal engine grass

Since the show-only list only accepts components, you’ll need to add tags to the grass-affecting components too. Select the GrassParticles component and then add a new tag located under the Tags section. Name it GrassAffector as well (you don’t have to use the tag). Do the same for the Background component.

unreal engine grass

Now you need to add all the grass-affecting components to the capture’s show-only list. Click Compile and then close BP_Mannequin. Afterwards, open Blueprints\BP_Capture. Go to Event BeginPlay and add the highlighted nodes. Make sure you set the indicated pins as well.

unreal engine grass

This will loop over all actors with the GrassAffector tag. It will then check if the actor has any components with the same tag and add them to the show-only list.

Next you need to tell the scene capture to use the show-only list. Select the SceneCapture component and then go to the Scene Capture section. Set Primitive Render Mode to Use ShowOnly List.

unreal engine grass

Click Compile and then close the Blueprint. If you press Play, you’ll see that the render target now only captures the particles and background plane.

unreal engine grass

Up next is the section you’ve been waiting for. It’s time to make the grass bend!

Bending Grass

First, you need to project the render target onto the grass. Go to the Materials folder and open M_Grass. Afterwards, create the nodes below. Make sure to set the texture to RT_Capture.

unreal engine grass

Since you have remapped the colors to the 0 to 1 range, you need to remap it back to -1 to 1 before using it. To do this, add the highlighted nodes:

unreal engine grass

Now that you have your bend direction, you need some way to rotate the grass towards that direction. Luckily, there is already a node for this called RotateAboutAxis. Go ahead and create one.

unreal engine grass

Let’s start with the NormalizedRotationAxis pin. As its name suggests, this is the axis in which the vertex will rotate around. To calculate this, you simply need to cross product the bend direction with (0, 0, -1). To do this, add the highlighted nodes:

unreal engine grass

Next is RotationAngle which is how much the vertex should rotate around the pivot. By default, it expects a value between 0 and 1 where 0 is 0 degrees and 1 and 360 degrees. To get the rotation angle, you can use the length of the bend direction multiplied by a maximum rotation.

unreal engine grass

Multiplying by a max rotation of 0.2 means the maximum rotation angle is 72 degrees.

Calculating the PivotPoint is a bit trickier because one grass mesh contains multiple blades of grass. This means you cannot use something like the Object Position node since that would return the same point for all grass blades.

Ideally, you would use your 3D package to store pivot points inside UV channels. But for this tutorial, we will just approximate a pivot point. The way to do this is to simply move down from the top by a certain offset.

unreal engine grass

To do this, add the highlighted nodes:

unreal engine grass

In this tutorial, the grass is about 80 units tall which is why I’ve set PivotOffset to that value.

Next, you will need to perform two masks. The first mask will make sure the root of the grass blade doesn’t move. The second mask will make sure grass outside of the capture area isn’t affected by the vector field.

Masking

For this tutorial, I’ve set up the grass’s vertex colors so that the bottom vertices are black and the top vertices are white.

unreal engine grass

To mask out the roots, simply multiply the result of RotateAboutAxis with a Vertex Color node.

unreal engine grass

To mask out grass outside of the capture area, multiply the previous result with the highlighted node:

unreal engine grass

Click Apply and then close the material. Press Play and start running around to make trails in the grass!

unreal engine grass

Where to Go From Here?

You can download the completed project using the link at the top or bottom of this tutorial.

While the method in this tutorial works great for simple things like grass, it can fall short when using it on objects that require a bit more dynamism. A way to alleviate this is to use physics versions of your foliage. Check out this post on interactive foliage for more information.

Finally, I’d like to thank Deathrey from the Unreal Engine community for coming up with the particle method!

If there are any effects you’d like to me cover, let me know in the comments below!

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.