Runtime Procedural Generation

Currently a tech demo

Overview

Developed both in Unity and Unreal, created a procedural wall generator at runtime based on splines.

The Unreal version was developed first, however, after deep engine source code examination, some PCG features aren't supported in package build. Relunctantly, I gave up on this route.

Then, the prototype was successfully built in Unity, and then optimized by moving generation logic from CPU to GPU using compute shaders.

Currently, on a RTX4060 gaming laptop, the tech demo can have nearly 30K instances updating concurrently each frame, while still keeping framerate above 60 fps.

This project is inspired by Tiny Glade

Duration

07/2025 -

Tools

Unity, C#, HLSL, Unreal

First attempt: PCG Graph in Unreal 5.6

As PCG Graph has already laid out the system, plus the better visual aesthetics in Unreal, it's a no-brainer to first try this idea in Unreal.

The working prototype was quickly developed, however, everything works fine, except for the packaged built...
After a deep digging in the engine's source code, I found out that some features supporting "Generate On Demand" is only available in the Editor.
I tried bypassing the editor check, but this feature required more than a boolean, but some other features only existing in the Editor instance.
Relunctantly, I had to give up this route.

^ Looks good in the editor, but "Generate On Demand" becomes unavailable in packaged build.

^ Debugging the feature in the packaged build, even with some tweaks, the runtime PCG doesn't produces smooth transition

Second Attempt: handcraft PCG on spline in Unity, CPU heavy

Based on Unity's spline API, I developed a mesh generator along the spline.
The transform matrices for each instance are calculated in CPU. To add more dynamics, the length of each instance are randomized.
For in-game interaction, the collider mesh is also generated at each update.
This approach works smoothly when the scene is updating below 1K instances, but the CPU overhead becomes crucial with more instances.

Third Attempt: calculations in compute shader, GPU heavy

Calculating the transform of each instance is a huge batch of parallel calculations, which is ideal for GPU to handle.
Using compute shader, I can arrange each instance's position on the spline based on their DispatchThreadID, the calculated transforms are stored in a compute buffer, which can be directly referenced by vertex shader. Therefore, calculations and reading data all happens in GPU.
I implemented my own version of spline library in HLSL, to get everything aligned with CPU version.
Currently, this version is still work in progress, features like randomized lengths and correcting shading are on the agenda.