Creating a Fluid surface with Blueprints and Render Targets

Using Blueprints and Render Targets, you can create a fluid surface that simulates what happens to water when it is hit by a projectile or player. In the following guide we will learn how you can use Blueprints & Render Targets to create a fluid surface actor by creating a new set of render targets and Materials that are specifically setup to work with render targets. We will then create and setup a new Blueprint that will contain and power the fluid surface interaction when the fluid surface is touched or shot at.

Part of this guide uses the same assets created in the Creating a Height Field Painter with Blueprints and Render Targets How To guide. If you have not completed the Creating a Height Field Painter with Blueprints and Render Targets How To guide, please do so before starting this one. You can also find this example map in the Content Example project that can be downloaded from the Learn tab of the Epic Games launcher.

1 - Project Setup

In this section, we will go over how to set your project up so that you can create a fluid surface Blueprint.

The project setup for the Fluid surface is the same as for the Height Field Painter. If you have not done so, read the Project Setup section of the Creating a Height Field Painter with Blueprints and Render Targets How To guide. When completed you should now have a new Game Mode and HUD. When you run the project, you should see something similar to the image below.

In the next section we will take a look at creating all of the required assets to create a Fluid Surface Blueprint.

2 - Required Asset Creation & Setup

Before we can create the Height Field Painter Blueprint, we will need to create and setup the following asset types inside of the UE4 Content Browser. In the following sections, we will go over how each of the assets needs to be setup as well as any special properties that you will need to be aware of.

Please note that the following is not specific to any UE4 project type. The following can be accomplished in any project that uses UE4 version 4.13 or later.

  • Render Target

  • Material

  • Blueprint (Based off Actor)

Render Target Creation & Setup

  1. Right-click inside of the Content Browser and from the displayed menu, go to Create Advanced Asset > Materials & Textures and then select the Render Target option.

  2. Name the newly created Render Target, RT_Height_Filed_Painter, and then press the Save All button to save your work.

Material Creation & Setup

  1. Right-click inside of the Content Browser and from the Create Basic Asset list, select the Material option to create a new Material, naming it MAT_HeighfieldPainter.
  2. Select the MAT_HeightfieldPainter Material in the Content Browser and then right-click on it. From the displayed menu, under the Common section, select the Duplicate option to create a copy of the Material, naming it MAT_ForceSplat.
  3. Double-click on the MAT_HeighfieldPainter Material to open it up and then select the Main Material Node. In the Details panel under the Material section, setup the following parameters.
    • Shading Model: Unlit
  4. The MAT_HeighfieldPainter Material is going to use the World Position Offset input to increase the Z-height of the Static Mesh vertices it is applied to. You can copy it into your Material by hovering your mouse over the image below and then clicking on the Copy Expression Graph option, copying-and-pasting the provided Material code into MAT_HeighfieldPainter.
    Copy Expression Graph
    Do not forget to Apply and Save your Material so that it can be used.
  5. Open up the MAT_ForceSplat Material and then select the Main Material Node. In the Details panel under the Material section set up the following parameters.
    • Blend Mode: Additive
    • Shading Model: Unlit
    • Allow Negative Emissive Color: Checked
  6. Since the MAT_ForceSplat Material is going to act as our paint brush for raising the surface of our height field, it will need to have a few options that will allow you to control its size and intensity. You can copy the completed Material into your Material by hovering your mouse over the image below and then clicking on the Copy Expression Graph option, copying-and-pasting the provided Material code into MAT_ForceSplat.
    Copy Expression Graph
    Do not forget to Apply and Save your Material when done so that it can be used.

With the required assets now setup, in the next section, we will take a look at creating a Blueprint that will link all of the assets together to create the height map painter.

3 - Blueprint Setup

With the render targets and needed Materials setup and ready to use, it is now time to bring everything together using a Blueprint. In the following section, we will go over how to setup all of the various parts of the Height Field Painter Blueprint.

Blueprint Creation & Variable Setup

  1. Right-click in the Content Browser and from the menu in the Create Basic Asset section, click on the Blueprint Class option.

  2. From the Pick Parent Class window, select Actor and name it HeightFieldPainter.

  3. Double-click on the HeightFieldPainter Blueprint to open it up and then click on the Event Graph tab. When the Event Graph is open, add the following variables.

    Variable Name

    Variable Type

    Default Value

    HeightfieldRT

    Texture Render Target 2D

    N/A

    PainterMaterialInstance

    Material Instance Dynamic

    N/A

    MouseDown

    Bool

    N/A

    ForceSplatMID

    Material Instance Dynamic

    N/A

    InteractionDistance

    Float

    1000.0

    BrushSize

    Float

    0.04

    BrushStrength

    Float

    1.0

Blueprint Function Creation & Setup

Now that the Blueprint has been created with its required variables, it is now time to create some functions. In the following section, we will go over what the three functions are and how you go about setting them up.

Do not forget to Compile and Save your Blueprint so that it can be used.

  • The GetHeightRT function will get and store the height of the render target so that it can be used later. Before you start to create this function, make sure you setup the function's Inputs and Outputs with the following information.
    • Inputs:
      • Name: Index
      • Type:Integer
    • Outputs:
      • Name: Heigh RT
      • Type:Texture Render Target 2D
      Copy Node Graph
      Do not forget to add each of the three render targets to the Height RT inputs on the Return Node.
  • The GetLastHeight function will store the last known height for the fluid surface for later use. Before you start to create this function, make sure you setup the function's Inputs and Outputs with the following information.
    • Inputs:
      • Name: CurrentHeightIndex
      • Type:Integer
      • Name: NumFramesOld
      • Type:Integer
    • Outputs:
      • Name: Heigh RT
      • Type:Texture Render Target 2D
      Copy Node Graph
  • Trace From Camera is the same trace that was used in the Height Field Painter Blueprint. For this function, there are no input or output variables that need to be setup.
    Copy Node Graph

Blueprint Construction Graph Setup

The Construction Graph is used to create and set both of the Static Meshes and Materials that fluid simulation needs to function correctly. In the following section, we will go over what the three functions are, and how you go about setting them up.

Do not forget to Compile and Save your Blueprint so that it can be used.

  • In the Construction Graph, we are going to create a Static Mesh that is needed for the water surface and then create and apply a Dynamic Material Instance to that Static Mesh, which will simulate the water's surface.
    Copy Node Graph

Blueprint Event Graph Setup

The Event graph is where everything that is needed for this effect to work correctly is added and connected. In the following section, we will go over what each section of the Event Graph does and how it affects the final Blueprint that is placed in the world.

Do not forget to Compile and Save your Blueprint so that it can be used.

  • Clear all render targets of data is the part of the Blueprint that clears all of the render targets of any previous data that they might contain in order to ensure that no artifacts appear the first time the Blueprint is run.
    Copy Node Graph
    Do not forget to add each of the three render targets to the Height RT* inputs on the Return Node.
  • Check to see if Left Mouse Button is held down is the part of the Blueprint that checks to see if the user has the Left Mouse Button held down or not. If the user has the Left Mouse button held down, the MouseDown is set to true; otherwise, it is set to false.
    Copy Node Graph
  • The Take Damage Force section finds the location on the Static Mesh that was hit and applies the ForceSplat Material to that section so that it can offset the surface that is simulating an impact event.
    Copy Node Graph
  • The Begin & End Overlap section checks to see if the player is currently touching the fluid surface Actor that has been placed in the world or not.
    Copy Node Graph
  • Shoot when mouse is down is the part of the Blueprint that checks each Tick to see if the Left Mouse button is held down. When the Left Mouse button is held down, the Trace From Camera function is called, which traces from the center of the camera into the world.
    Copy Node Graph
  • Touching Character Force is the part of the Blueprint that is responsible for applying and updating the force that is applied to the fluid surface Actor when a player is touching it.
    Copy Node Graph
  • Fixed timestep, so fluid speed is framerate independent is the part of the Blueprint that makes sure that the propagation of the waves through the fluid surface has a frame rate that is not tied to the frame rate of the project. Essentially, the waves in the fluid surface will not slow down or speed up based on what is currently happening in the level.
    Copy Node Graph
  • Cycle Active heightfield is the part of the Blueprint that cycles through the various render targets for the fluid surface so that the fluid surface has a more dynamic look when shot or touched.
    Copy Node Graph
  • Apply fluid surface simulation kernel, which propagates waves is the part of the Blueprint that handles the propagation of the waves through the fluid surface Actor making it look like the waves die out over time.
    Copy Node Graph
  • Compute Surface Normal is a section of the Blueprint that applies a normal to the surface of the fluid which helps make the ripples more pronounced and easier to see.
    Copy Node Graph

Now that the Blueprints have been setup in the next section we will take a look at how to set all of this up for use in UE4.

4 - End Result

When all of the various pieces of the fluid surface Blueprint have been connected, and the fluid surface Blueprint has been compiled and saved, add it to a level. Once placed in a level, you can play the level and then interact with the fluid surface Blueprint by holding down the left mouse button to shoot at it or by walking over it with your character. Below are some videos that show both these types of interaction.

This video shows what happens when you shoot at the fluid surface Blueprint.
This video shows what happens when you walk over the fluid surface Blueprint with a Pawn-based character.
Copy Node Graph
Here is a copy of the entire completed fluid surface Blueprint.