Chunky Bevy
A simple and efficient chunk management system for Bevy game engine, perfect for voxel games, procedural worlds, and any application that needs spatial partitioning.
Features
- 🎯 Simple API - Easy to use chunk management with minimal boilerplate
- 🔄 Automatic Loading - Optional chunk loader component for automatic chunk spawning around entities
- 🗑️ Automatic Unloading - Configurable strategies for chunk lifecycle management (distance, limit, or hybrid)
- 💾 Persistence - Save and load chunk data with configurable storage strategies (New and not as teasted as I would like)
- 👁️ Visualization - Built-in debug visualization for chunk boundaries
- ⚡ Efficient - HashMap-based chunk lookup with O(1) access
- 🎮 Bevy Integration - First-class Bevy ECS integration with hooks and resources
Quick Start
Add to your Cargo.toml:
[]
= "0.17"
= "0.2"
Basic usage:
use *;
use *;
Features
Default Features
chunk_visualizer- Enables debug visualization of chunk boundarieschunk_loader- Enables automatic chunk loading around ChunkLoader entitieschunk_unloader- Enables automatic chunk unloading with configurable strategieschunk_saver- Enables chunk persistence with save/load functionality
Optional Features
reflect- Enables Bevy reflection for all typeschunk_info- Logs chunk spawn/despawn events
Disable default features:
= { = "0.2", = false }
Components
Chunk
Marks an entity as a chunk. Automatically registers/unregisters with ChunkManager.
ChunkPos(IVec3)
The chunk's position in chunk-space coordinates. Automatically updates the entity's Transform.
ChunkLoader(IVec3)
Automatically loads chunks in a radius around the entity. The IVec3 defines the loading radius in each direction.
Examples:
ChunkLoader(IVec3::ZERO)- Loads only the chunk the entity is inChunkLoader(IVec3::ONE)- Loads a 3x3x3 cube of chunksChunkLoader(IVec3::new(5, 0, 5))- Loads an 11x1x11 flat area
ChunkPinned
Prevents a chunk from being automatically unloaded. Useful for spawn areas or quest locations.
ChunkUnloadRadius(IVec3)
Defines the unload radius for a specific ChunkLoader. If absent, defaults to the loader's load radius.
Resources
ChunkManager
The main resource for querying and managing chunks.
Unload Strategy Resources
Chunk unloading is opt-in. Insert resources to enable different strategies:
Chunk Persistence
Save and load chunk data using the chunk_saver feature (enabled by default).
Basic Setup
use *;
use *;
use ;
Save Styles
Two storage strategies are available:
// One file per chunk (default)
new
// Multiple chunks grouped into super-chunk files
new
.with_style
PerChunk: Creates individual files like chunk_0_0_0.chunk. Best for worlds with sparse chunk distribution.
SuperChunk: Groups chunks into larger files like super_0_0_0.chunks. More efficient for dense worlds with many chunks, reducing file system overhead.
Manual Save/Load
Automatic Save/Load
!WARNING!: This feature has not been tested as well as I would like so please let me know if something is not working as you would expect!
Enable auto-save when chunks unload and auto-load when chunks spawn:
new
.with_auto_save // Save chunks before they're unloaded
.with_auto_load // Load chunk data when chunks spawn
This integrates seamlessly with ChunkLoader and unload strategies for seamless streaming worlds.
Batch Operations
For SuperChunk style, batch operations are more efficient:
// Save multiple chunks (batches writes to same super-chunk file)
registry.save_batch?;
// Load all chunks from a super-chunk file
let loaded: = registry.load_batch?;
Events
ChunkUnloadEvent
Sent when a chunk is about to be despawned. Read with MessageReader<ChunkUnloadEvent> to save data before removal.
Visualization
Enable chunk boundary visualization:
Helpers
Spawn multiple chunks at once:
use *;
Examples
Run the basic example:
Controls:
- WASD - Move camera
- Q/E - Move camera down/up
- HJKL - Move cube (chunk loader)
- Y/I - Move cube down/up
- Left Mouse Button - Look around
Run the chunk unloading example:
Controls:
- WASD - Move horizontally
- Q/E - Move down/up
- Space - Cycle through unload strategies
- Right-click + drag - Look around
Run the chunk saving examples:
# Manual save/load
# Automatic save/load with streaming
# Super-chunk batch operations
Bevy Version Compatibility
| Chunky Bevy | Bevy |
|---|---|
| 0.18 | 0.18 |
| 0.2 | 0.17 |
| 0.1 | 0.17 |
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.