Javier-Garzo / Marching-cubes-on-Unity-3D

Terrain voxel engine with the use of Marching Cubes implemented in Unity 2020.3.17f1 (LTS).
MIT License
332 stars 43 forks source link
marching-cubes terrain-generation voxel-engine

Marching-cubes-on-Unity-3D

Terrain voxel engine with the use of Marching Cubes implemented in Unity 2020.3.17f1 (LTS). You can clone and run the project or download only the Marching_cubes folder and add to your project assets.

GifEdition



Table of contents

  1. Introduction
  2. Unity scenes
  3. Configuration
    1. Constants
    2. Managers
  4. Biome system
    1. Edit biomes
    2. Create new biomes
  5. World manager
  6. Future work
  7. References

Introduction

The unity project is a implementation of the algorithm Marching Cubes for the generation of a voxel engine for generate a random and infinite terrain. The idea is try to offer a flexible solution for developers that want integrate a free Voxel engine in his game or give a base for develop your own Marching Cube engine. Some of the actual properties of the engine:


Unity scenes

The unity project have a total of 3 different scenes:

Configuration

You can configure the project to adapt it to your necessities. You have two type configurations: constants or managers.

Constants

The configurations of the constants used internally in the engine, all indicated in the "Constants" script. You can modify the region of "Configurable variables" (don't touch other regions). You have the explanation of each variable in the script or in the below list:

Managers

Each manager (GameObjects inside Unity scene) have some parameters that you can modify for get different results, the majority of them only apply changes when new chunks are loaded if not indicate the contrary.

Mesh builder manager ("MeshBuilder"):


Noise manager ("NoiseManager"):

The noise manager also contains biomes that with parameters to modify but this is explained in the biome section.


Chunk manager ("ChunkManager"):

Biome system

The biome system allow you to generate different types of terrain generated in the infinite world. It used a value between 1 and 0 where each biome has appear range, so each biome can have 1 or 2 possible neighbors biomes.

Desert-mountains biome and the "NoiseManager" with the biomes:

Biome frontier

Ice biome and plains biome: ice-grass biomes


Edit biomes

For edit a biome you can use the TerrainViewer scene. You only need to remove all the biomes classes from the "NoiseManager" except the one which you want edit and then modify the values of the inspector.

You can use the "NoiseTerrainViewer" for use a bigger or smaller test area or apply offset to the terrain. All the changes applied in this scene update the test terrain, so you need start the scene, apply the changes to the biome, copy the component, stop the scene and paste the component values to the biome.

Example of edition of the "B_Mountains" biome: B_Mountains edition

Create custom biomes

For create the a biome you need create a class that heir from the class "Biome" and that must have the function "public override byte[] GenerateChunkData(Vector2Int vecPos, float[] biomeMerge)" used for generate the biome texturization. For this process you can copy a paste the code of some actual biome and apply the necessary changes to the code for get your custom biome.

When you have the new biome class create, use the edit biome section for edit the parameters in a real time test terrain.

For apply this to other levels just add this new custom biome to the NoiseManager in the other scenes. Like in the image we add the new biome "B_NewBIome" to the NoiseManager:



World Manager

The world manager is a optional manager (if not exist "default" world name is selected) to support multiple worlds folder system, just to allow multiple games in different worlds. You can found the prefab in "Assets > Marching Cubes > WorldManager", this prefab contain a gameobject with the manager script. This manager supports:

Future work

The priority of next update will be:

Others futures updates:

References

The Marching Cube algorithm:

Noise system:

Others: