Skip to main content

ChunkGenerator

Trait ChunkGenerator 

Source
pub trait ChunkGenerator<V: VoxelType>:
    Send
    + Sync
    + 'static {
    // Required method
    fn generate(&self, coord: IVec3, chunk: &mut Chunk<V>);
}
Expand description

Contract for procedural chunk generation strategies.

An implementation receives an empty Chunk<V> and a chunk-grid coordinate, and fills the chunk with whatever voxel data the strategy produces. The framework stores implementations behind an std::sync::Arc and calls ChunkGenerator::generate on worker threads from Bevy’s bevy::tasks::AsyncComputeTaskPool.

§Thread safety contract

generate is called from a background thread — never from the Bevy main thread. Implementations must not:

  • access any bevy::ecs::world::World data (queries, resources, etc.),
  • hold non-Send types, or
  • perform blocking I/O on the calling thread.

The Send + Sync + 'static bounds on the trait enforce these constraints at compile time. Internal state may be freely read (shared references via Arc), but must not be mutated without interior synchronization.

§Coordinate space

coord is in chunk-grid space: each axis unit corresponds to CHUNK_SIZE world units. Convert to world space with coord.as_vec3() * CHUNK_SIZE as f32.

§Examples

use bevy::math::IVec3;
use voxel_framework::{
    chunk::Chunk,
    generator::ChunkGenerator,
    voxel::DefaultVoxel,
};

struct FlatWorldGenerator {
    ground_level: i32,
}

impl ChunkGenerator<DefaultVoxel> for FlatWorldGenerator {
    fn generate(&self, coord: IVec3, chunk: &mut Chunk<DefaultVoxel>) {
        // Fill voxels below ground_level with rock, leave the rest as air.
        if coord.y < self.ground_level {
            for x in 0..voxel_framework::chunk::CHUNK_SIZE {
                for y in 0..voxel_framework::chunk::CHUNK_SIZE {
                    for z in 0..voxel_framework::chunk::CHUNK_SIZE {
                        chunk.set(x, y, z, DefaultVoxel::Rock);
                    }
                }
            }
        }
    }
}

Required Methods§

Source

fn generate(&self, coord: IVec3, chunk: &mut Chunk<V>)

Fills chunk with voxel data for the chunk at coord.

coord is in chunk-grid space; each unit equals CHUNK_SIZE world units.

Called on a background thread. Must not access the Bevy World.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§