2022-02-08 12:36:37 +01:00
# Voxel
2019-11-21 22:40:08 +01:00
2020-06-11 11:54:18 +02:00
A voxel engine module for godot, focusing more on editor integration, gameplay-related features, and extendability (even from gdscript), without sacrificing too much speed.
2019-11-21 22:40:08 +01:00
2020-06-11 11:54:18 +02:00
This is an engine module! Which means that you will need to compile it into Godot! [See the compiling section here. ](#compiling )
2019-11-21 22:40:08 +01:00
2020-06-11 12:36:24 +02:00
You can find a demonstration project here: https://github.com/Relintai/the_tower
2021-02-06 11:51:50 +01:00
It supports both godot 3.2 and 4.0 (master [last tested commit ](https://github.com/godotengine/godot/commit/b7e10141197fdd9b0dbc4cfa7890329510d36540 )). Note that since 4.0 is still in very early stages I only
2020-06-20 22:08:07 +02:00
check whether it works from time to time.
2020-06-11 12:02:47 +02:00
## Optional Dependencies
2022-02-08 12:36:37 +01:00
`https://github.com/Relintai/thread_pool` : Threaded chunk generation. Without this Voxel is single threaded! \
`https://github.com/Relintai/texture_packer` : You get access to [VoxelLibraryMerger ](#voxellibrarymerger ). \
2020-08-04 10:08:31 +02:00
`https://github.com/Relintai/mesh_data_resource` : You get access to a bunch of properties, and methods that can manipulate meshes.\
2020-10-20 16:29:39 +02:00
`https://github.com/Relintai/props` : You get access to a bunch of properties, and methods that can manipulate, and use props.\
`https://github.com/Relintai/mesh_utils` : Lets you use lod levels higher than 4 by default.
2020-06-11 12:02:47 +02:00
2020-07-28 15:26:03 +02:00
## Pre-built binaries
2020-07-28 15:24:43 +02:00
You can grab a pre-built editor binary from the [Broken Seals ](https://github.com/Relintai/broken_seals/releases )
repo, should you want to. It contains all my modules.
2020-06-11 12:02:47 +02:00
2020-06-11 11:54:18 +02:00
## Usage
2019-11-21 22:40:08 +01:00
2022-02-08 12:36:37 +01:00
First create a scene, and add a VoxelWorldBlocky node into it. Create a VoxelLibrary, and assign it to the Library property.
2020-06-11 12:02:47 +02:00
Also, add a VoxelSurface into your library.
2019-11-21 22:40:08 +01:00
2020-06-11 11:54:18 +02:00
(VoxelWorldBlocky is the only one that works properly for now, this will soon be fixed!)
2019-11-21 22:40:08 +01:00
2020-06-11 12:02:47 +02:00
Tick the editable property, deselect, then select the world again, and click the insert button at the top toolbar, or press B to insert a
voxel at the inspector's camera's location.
2019-11-21 22:40:08 +01:00
2020-06-11 12:02:47 +02:00
Select the add button, and now you can just add voxels with the mouse, by clicking on the newly added voxel.
2019-11-21 22:40:08 +01:00
2022-02-08 12:36:37 +01:00
## VoxelLibrary
2019-11-21 22:40:08 +01:00
2020-06-11 11:54:18 +02:00
This class stores the materials, and the VoxelSurfaces.
2019-11-21 22:40:08 +01:00
2020-06-11 11:54:18 +02:00
Note: If you want lods, assign equal (or more) materials than your maximum lod level. If you only want one material just assign it
2020-06-11 12:08:03 +02:00
multiple times. If you don't then your meshes won't have materials (They will be white).
2020-06-11 11:54:18 +02:00
2022-02-08 12:36:37 +01:00
### VoxelLibrarySimple
2020-06-11 11:54:18 +02:00
The simplest library, just assign a material with a texture, and using the atlas_rows and atlas_culomns properties to tell the system
how the UVs should be divided.
2022-02-08 12:36:37 +01:00
### VoxelLibraryMerger
2020-06-11 11:54:18 +02:00
You will only have this if your godot also contains https://github.com/Relintai/texture_packer
You can assign any texture to your surfaces with this, and it will merge them together.
## Worlds
2020-06-11 12:08:03 +02:00
The 2 base classes:
2020-06-11 11:54:18 +02:00
2020-06-11 12:08:03 +02:00
VoxelWorld: Basic world, does not do anything until you implemnent the required virtual methods!\
VoxelWorldDefault: This adds threading, and LoD storage support to VoxelWorld. Will not create meshes for you!
2020-06-11 11:54:18 +02:00
### VoxelWorldBlocky
The most basic world. It is the Minecraft-style world.
2020-06-12 19:40:18 +02:00
### VoxelWorldMarchingCubes
2020-06-11 11:54:18 +02:00
2020-06-12 19:40:18 +02:00
A marching cubes based Voxel World. Actually it uses a modified version of the Transvoxel tables. It is UV mapped.
2020-06-11 11:54:18 +02:00
### VoxelWorldCubic
This is my own meshing algorithm, it's basicly a Minecraft style mesher that can take isolevel into account.
### Level generation
2020-06-11 12:08:03 +02:00
Assign a VoxelManLevelGenerator to the `World` 's `Level Generator` property.
2020-06-11 11:54:18 +02:00
You can write your own algorithm by implementing the ``` void _generate_chunk(chunk: VoxelChunk) virtual ``` method.
2020-06-11 12:08:03 +02:00
`VoxelManLevelGeneratorFlat` is also available, it will generate a floor for you, if you use it.
2020-06-11 11:54:18 +02:00
2020-10-06 20:07:47 +02:00
## VoxelJobs
2022-02-08 12:36:37 +01:00
Producing just a terrain mesh for a chunk is not that hard by itself. However when you start adding layers/features
2020-10-06 20:07:47 +02:00
like lod generation, collision meshes (especially since manipulating the physics server is not threadsafe),
vertex volumetric lights, props, snapping props, props with vertex lights, etc
chunk mesh generation can quicly become a serious mess.
VoxelJobs are meant to solve the issue with this complexity.
They also provide a way to easily modularize mesh generation.
### VoxelJob
Base class for jobs.
If the (thread pool)[https://github.com/Relintai/thread_pool] module is present, this is inherited from `ThreadPoolJob` ,
else it implements the same api as `ThreadPoolJob` , but it's not going to use threading.
A job has a reference to it's owner chunk.
If you implement your own jobs, when your job finishes call `next_job()` .
### VoxelLightJob
This is the job that will generate vertex light based ao, random ao, and will bake your `VoxelLight` s.
2022-02-08 12:36:37 +01:00
### VoxelTerrainJob
2020-10-06 20:07:47 +02:00
2022-02-08 12:36:37 +01:00
This will generate your terrain collider and mesh (with lods) for you, using the meshers that you add into it.
2020-10-06 20:07:47 +02:00
### VoxelPropJob
This will generate your prop meshes (with lods).
2020-06-11 11:54:18 +02:00
### Internal workings
#### VoxelWorld
Whenever you want to spawn a chunk your World will create it using the ``` VoxelChunk _create_chunk(x: int, y: int, z: int, chunk: VoxelChunk) virtual ``` method.
Since properly initializing a chunk usually takes quite a few steps that you probably don't want to repeat everywhere the `chunk`
parameter was added. This means you can just call the super `_create_chunk` methods, and you won't need to worry about your chunk
getting overridden. Like:
2020-10-24 22:18:23 +02:00
Note that `_create_chunk` is also responsible for initializing chunks if you have them stored inside a scene.
This is done by `setup_chunk(shunk)` in `VoxelWorld` .
2020-06-11 11:54:18 +02:00
```
func _create_chunk(x : int, y : int, z : int, chunk : VoxelChunk) -> VoxelChunk:
2020-10-06 20:07:47 +02:00
if !chunk:
2020-06-11 12:08:03 +02:00
chunk = MyChunk.new()
2020-06-11 11:54:18 +02:00
2020-10-24 22:18:23 +02:00
# We need to check whether or not we need to initialize jobs
2020-10-26 18:29:05 +01:00
if chunk.job_get_count() == 0:
2020-10-06 20:07:47 +02:00
# Setup a blocky (minecratf like) mesher job
2022-02-08 12:36:37 +01:00
var tj : VoxelTerrainJob = VoxelTerrainJob.new()
2020-10-06 20:07:47 +02:00
2020-10-27 19:20:08 +01:00
tj.add_mesher(VoxelMesherBlocky.new())
tj.add_liquid_mesher(VoxelMesherLiquidBlocky.new())
2020-10-06 20:07:47 +02:00
2020-10-26 18:29:05 +01:00
chunk.job_add(tj);
2020-10-06 20:07:47 +02:00
2020-06-11 11:54:18 +02:00
#setup your chunk here
2020-06-11 12:08:03 +02:00
return ._create_chunk(x, y, z, chunk)
2020-06-11 11:54:18 +02:00
```
#### VoxelChunk
2022-02-08 12:36:37 +01:00
Stores terrain data, prop data. And mesh data (VoxelChunkDefault), and the mesh generation jobs.
2020-06-11 11:54:18 +02:00
2020-10-06 20:07:47 +02:00
When it starts building meshes it will start submitting jobs to thread_pool (if present) one by one.
2020-06-11 11:54:18 +02:00
#### VoxelMesher
If you want to implement your own meshing algorithm you can do so by overriding ``` void _add_chunk(chunk: VoxelChunk) virtual ```.
VoxelMesher works similarly to SurfaceTool, so first you need to set colors, uvs, etc and then call add_vertex.
They won't get reset, so for exaple if you want all your vertices to have a certain color, you can get away with setting it only once.
## Compiling
First make sure that you can compile godot. See the official docs: https://docs.godotengine.org/en/3.2/development/compiling/index.html
1. Clone the engine if you haven't already:
2020-06-20 22:08:07 +02:00
If you want Godot 3.2:
```git clone -b 3.2 https://github.com/godotengine/godot.git godot```
If you want Godot 4.0:
2020-06-11 11:54:18 +02:00
```git clone https://github.com/godotengine/godot.git godot```
2020-06-20 22:08:07 +02:00
2020-06-11 12:14:30 +02:00
2. go into the modules folder inside the engine's directory:
2020-06-11 11:54:18 +02:00
2020-06-11 12:13:57 +02:00
```cd godot``` \
2020-06-11 11:54:18 +02:00
```cd modules```
3. clone this repository
```git clone https://github.com/Relintai/voxelman.git voxelman```
(the folder needs to be named voxelman!)
2020-06-11 12:15:05 +02:00
4. If you want the optional dependencies run these commands aswell:
2020-06-11 12:02:47 +02:00
2020-06-11 12:13:57 +02:00
```git clone https://github.com/Relintai/texture_packer.git texture_packer``` \
2020-06-11 12:02:47 +02:00
```git clone https://github.com/Relintai/mesh_data_resource.git mesh_data_resource```
5. Go up one folder
2020-06-11 11:54:18 +02:00
```cd ..```
2020-06-11 12:02:47 +02:00
6. Compile godot.
2020-06-11 11:54:18 +02:00
For example:
```scons p=x11 t=release_debug tools=yes```