Skip to content

Fully Destructible GPU Compute Volumetric Terrain Generator in Unity HDRP

Notifications You must be signed in to change notification settings

jedjoud10/VoxelTerrainGenerator

Repository files navigation

A procedural terrain generator that makes use of your GPU and CPU to generate volumetric and fully destructible terrain

Features

  • 3D Octree for worlds up to 32km with sub-meter voxel precision
  • GPU Voxel Generation with Async CPU readback
  • Morton encoding to improve CPU cache locality (didn't actually test but I just implemented it for the funny)
  • Multithreaded and Jobified CPU meshing implementing the Surface Nets algorithm
    • Supports vertex merging and custom materials
    • Supports custom per vertex ambient occlusion and UV pass-through data
    • Async Collision Baking using the job system and PhysX
    • Custom skirts system for meshes with different resolution to avoid gaps between chunks (kinda works)
  • Terrain editing using duplicate octree
    • Supports dynamic edits which are applied on a global scale (non-destructive)
    • Supports voxel edits which are applied on a local voxel-to-voxel scale
    • Generic dynamic edits/ voxel edits allowing you to write your own editing shapes and brushes (using Job system as well)
    • Callback for voxel edits to detect how much volume was added/removed for each material type
    • Custom frame limit to limit number of in-flight meshing jobs to reduce latency
  • GPU Based Prop Generation
    • Avoids unecessary CPU callbacks
    • Uses density and surface data to generate props
    • Multiple prop "variants" supported
    • Uses the GPU for indirect instanced rendering directly
    • Uses impostors (advanced billboards) system for props that are further away
      • Makes use of albedo, mask, and normal map data to create billboards procedurally
      • Texture captures are done automatically at the start of the frame
      • Custom camera position, rotation and scale when capturing textures
      • Handles different prop variants using texture arrays
    • Uses a compute based culler executed before indirect rendering
  • Serialization / deserialization system that supports terrain edits, terrain seed, and modified/destoyed props
    • Uses RLE and delta compression for voxel data
    • Uses RLE for prop masks
  • In editor SDF/Volume/Slice preview using unity Handles API

WIP Features to be added

  • Structure generation
  • Custom prop spawning / modifiers (using CSG)
  • Optimize rendering & voxel editing
  • Better compresion ratio for saved worlds
  • Better compression algorithms for props and voxel data
  • Better lighting effects (AO/GI)
  • Voxel Occlusion culling for props and terrain chunks
  • Fully GPU-driven voxel chunks using indirect draw
    • Maybe mess around with nvidia mesh/task shaders?
    • Compute based fallback for chunks further away, to reduce readback
  • Voxel graph / interpreter to create voxel terrains in C# or visually
    • Full world biome generation (big low-res 3d texture)
    • Per-biome localized volumetric fog
  • Multiplayer support (theoretically should be easy)
    • Just need to share seed to all clients
    • And whenever we do a new edit, send an edit "request" to all clients who need it
    • Apply delta compression for edits and possible send the whole dupe octree sometimes
    • For props since they implement INetworkSerializable you just need to share their values

Main issues

  • Still riddled with bugs
    • Editing terrain sometimes leaves gaps
    • Prop generation sometimes breaks out of nowhere
  • Terrain chunk scheduling is non-conservative. Always over-estimates the amount of chunks actually containing terrain
  • Bad performance when editing large voxel/dynamic edits (due to the dupe-octree nature of voxel edits)
  • Bad memory consumption / saved world size due to dupe-octree
  • Slow async GPU readback which causes frame time spikes when there is more than 1 request per frame
  • Billboarded prop normals don't seem to match up with their gameobject counterpart (seem fine at a distance, only noticeable at some lighting conditions)
  • Floating terrain segments (could fix by running a flood fill and seeing the parts that aren't "connected")
  • Floating props (due to low-resolution segment voxel grid)

Showcase:

image image image image image

In Editor Previews

Screenshot 2024-04-08 135853 Screenshot 2024-04-08 141419 2222image