Slime Mold on Unreal Engine 4 Niagara

Mori Issei
7 min readMar 4, 2020

--

An animation of agent-based slime mold particle simulation

Introduction

Slime Mold is a type of fungi that grows intelligently to collect as much food as possible. When their movement is simulated using agent-based particle simulations, it generates interesting animations. Several artists have implemented this algorithm on different platforms, but this time I decided to implement this using a brand new system of Unreal Engine 4, Niagara, and publish a tutorial. Here is the video of what this project will look like. https://youtu.be/LJaTZ8uSFcM

Screenshots available via Link

Screenshots of this project used throughout this post might be low-resolution. If this is the case for you, please download a high-resolution version of the images through this below link.

https://drive.google.com/drive/folders/15FlAmz2Wbq_lHZWIpreu-axPcycLT7KJ?usp=sharing

Overview

Slime Mold simulation requires both of the density of the particles itself and the food sources. I wish there was a way to get the density of particles in certain regions, however, Niagara does not support this currently. Thus I figured out the way to find the density value by capturing the whole particles in the same coordinate system, so it works as a positional texture. The resolution of this captured texture is set to low, to mimic the effect of blurring as discussed in the original paper.

Enable Niagara

We start with enabling Niagara Plugin. Go to the Plugin Menu from Menu > Edit > Plugins and enable Niagara and Niagara Extras.

Scene Setup

First, let’s create a new blank project by selecting New Project >Blueprint Blank. Remove all the unnecessary objects, except for the default lights.

Add a new Niagara System and Niagara Emitter from right-clicking on Content Browser thenFX > Niagara System > Create an empty system with no emitters and FX > Niagara Emitter > Directional Burst. Drag and drop the Niagara System into the scene at Location(0,0,0).

Now, we create a capturing camera and the background.

Add a Plane object into the scene and set Location(1000, 1000, 10) Scale(80, 80, 1). Create a Material with black color and assign it to the Plane.

Add a Render Target from Materials & Textures > Render Target in Content Browser.

Add a Scene Capture 2D object by using the searching bar in the Models tab. Set Location(1000, 1000, 100) Rotation(90, -90, 180) to let the capture camera face straight down. Change Projection Type > Orthographic and Ortho Width > 2000. In Texture Target, select the Render Target you just made. Change Primitive Render Mode > Use ShowOnly List and Capture Source > Final Color (LDR) in RGB. Finally, add the Plane and Niagara System to Show Only Actors list by clicking the + button and selecting each object.

Add Emitter to Niagara System

In order to add the Niagara Emitter to Niagara System, first, you need to open a Niagara Editor by double-clicking the Niagara System and drag and drop the Niagara Emitter from Content Brower to right above the play button in Timeline Tab within the Niagara Editor.

Now, open Niagara Emitter in Niagara Editor as well. Then, click the + button next to Emitter in Parameters Tab, add a float parameter, and name it Emitter.CanvasSize.

Create Niagara Scripts

Niagara requires no hard coding but only adding and connecting nodes. Niagara Module Scripts are used to modify the behavior of particles at any stage of the Particle Lifecycle. Niagara Function Scripts are defined to be used within Niagara Module Scripts as nodes. Let’s add 1 Function Script and 3Module Script needed to control the particles.

1. Rotate 2D Vector

This function rotates a 2D vector, given a degree of rotation. It’s useful to define this as a function for later use. Create a new Niagara Function Script from FX > Niagara Function Script. Name this script Rotate2dVector. In Script Details panel, add two new input parameter of typeVector 2D and float and name them Vector2D and Degrees. You can delete the default input on Graph View. Now, add nodes and connect them as in the screenshot below. To add any node, drag the circle on the right of each node and drop it on anywhere in the graph and search for a corresponding type.

2. Fit to View

This Module transfers any particle that went outside of the Canvas, back to the Canvas. Create a new Niagara Module Script from FX > Niagara Module Script. Name this script FitToView. Add Particle.Position and Emitter.CanvasSize in Map Get node. Add nodes and connect them as in the screenshot below (Hopefully, you can see them), and connect to Particle.Position in Map Set node.

3. My Color

This Module controls the color and opacity of particles. We just need to add 2 Module Parameters of type Vector3 and float and connect them to Particle.Color.

4. Change Directions

This Module implements the main algorithm of the Slime Mold. It will first define 3 candidate locations from the current position of the particle, then it will sample corresponding pixels in the combination of data and deposit textures. Finally, it will compare these and move to the direction with the highest intensity value.

First, add Particles.Position, Emitter.CanvasSizeand Particle.Velocity to Map Get. In addition, create 4 float Module Parameters and name them as below.

Add nodes and connect them as the following screenshot. Here, we determine 3 candidate sampling locations based on Sensor Angle and Sensor Distance. For each direction, the next step location is given based on Sensor Angle and Move Distance.

Next, create another Map Get node from the Input Map node, and add 4 Module Parameters as the following screenshot. For each candidate sampling location, Sample Texture 2D nodes will sample the intensity value of the texture at the corresponding pixel. This is done for both Data Texture and Captured Texture and weighted based on Data Weight and Deposit Weight respectively.

Finally, these sampled values are compared Particle.Velosity is set to the direction with the highest intensity value.

Modify Emitter Parameters

The final stage of this project is to modify the Emitter Parameters. Double-click Niagara Emitter and open Niagara Editor. Change the parameters as following screenshots and add the Modules we created.

Play!

Now, you should be able to see the cool animation of agent-based slime mold particle simulations. You can change the color and experiment with different parameters! Thank you for reading my tutorial! Hopefully, you could successfully complete the project. Let me know if you have any questions!

What’s Next?

Now that you understand the basic implementation of slime mold particle simulations using Niagara, you can be as creative as you want! Maybe adding an interactive tool can be interesting.

Limitations

The current version of Unreal Engine (4.24 at the time of writing this), does not support unique ID when the GPU Simulation option is turned on, which limits the information about other particles you can get. This is the main reason I have not been able to extend this project into 3D.

In addition, the Additive Composite Mode option does not work when Capture Source is set to RGBA. The original implementation of slime mold requires Trail, which means that slime mold extends itself by using existing paths and then branch out from it. Additive Texture, instead of overwriting it every frame, will create this effect.

Hopefully, a future release of the Unreal Engine will address these issues and limitations.

--

--

Mori Issei

B.S. in Computer Science / Computer Graphics / Creative Art