Lots of cubes! [Unreal]

Last summer I got intrigued by this article by Fabian Buchmaier, found in UE News feed. At the time, I was working on Real Time Point Clouds streaming + visualization in Unreal for a Teleoperation system (actually, still doing that!), so I was interested by the concept of using vertex shaders to move around a massive amount of elements on screen.

It was not until recently that I got the chance to try and reproduce an effect similar to what I could see in Fabian Buchmaier’s video and article. The main idea is to create a single mesh composed by a huge amount of cubes. Then, use a material in Unreal Engine which displaces the top vertices of each cube in real time, resulting in their elongation. Why a material? Because moving that insane amount of cubes without taking advantage of GPU parallelization would mean torturing your computer and yet being able to only move/display a fraction of the cubes you can mess around with using a material!

Creating the mesh

I am no Blender expert, so the following is probably not the smartest or more appropriate way to do this, BUT!
It worked, so I am sharing it here for future reference.

Requirements

This is what you need in Blender to create the required mesh! Scroll through the following gallery to see how the mesh has been created. Captions (hopefully) explain what’s going on, and what to do.

Step by step mesh!

What’s going on with vertex color and UVs?

Why do we collapse vertices in the UV map? As explained by Fabian Buchmaier, we want to displace only the cube top face, and we want to move it as a flat surface (all four top vertices moving together). We will be using the UVs to drive the displacement: one UV value will be associated to the height of a single cube. That’s why all vertices of a cube are collapsed onto the same UV value.

Still, we don’t want to move the entire cube – we want to stretch it, so the base should stay still while the top face moves up.
That’s why we use the vertex color to discriminate which vertices will move (top ones) and which ones will not move (bottom ones). This will be much clearer in the following Unreal material, anyway!

Using the mesh in Unreal

First of all, we need to import our mesh in Unreal.
This is the import settings I had to change in order for the mesh vertex colors to be imported as well:

These settings allow us to import vertex color

After that, we can move to the material creation! Now, the one I am presenting here shows a very basic example of what can be done with our mesh, which is displacing the cubes using a texture:

Material to drive our mesh displacement

So, we start from a (0, 0, 1) vector, since we only want displacement along the Z axis (note: this is fine if we are not rotating the mesh!).
Then, we multiply this vector by a texture. A single channel is enough, especially for grayscale textures!
Now, here’s the part allowing us to only move the upper vertices of each cube: we multiply everything by the vertex color. Again, picking just one channel, since it’s grayscale. This means that displacement will be multiplied by 0 (black) for the lower vertices, and 1 (white) for the upper vertices. Nice! I added a scalar parameter for overall control of our displacement power in material instances. Color is also a parameter.

As you see, the material is quite basic. If you play around, you can make the texture (and thus the mesh!) move, e.g. adding a panner block to move the texture, or even use some functions instead of the texture. Or combine them…well, starting from this material we can play around!

We have proof, material + mesh setup is working!
After some tweaking, we start to see some potential!
Looks kinda trippy, doesn’t it?

Additional note about flickering

When you heavily displace meshes vertices, you might notice some flickering. You can get rid of that by increasing the mesh bounds scale option!

dario mazzanti, 2021

Up ↑