# archetype_asset
**Fast, modular asset system with spatial preloading for Rust game engines.**
[](https://crates.io/crates/archetype_asset)
[](https://docs.rs/archetype_asset)
[](LICENSE)
## Features
- **๐ฎ GPU Abstraction** - Generic over GPU backends (Vulkan, Mock for testing)
- **โก Async Runtime Abstraction** - Works with Tokio, async-std, or custom runtimes
- **๐ฆ Smart Caching** - LRU cache with memory management and metrics
- **๐บ๏ธ Spatial Preloading** - *Unique!* Position-based asset prediction
- **๐ LOD System** - Level-of-detail mesh simplification
- **๐ฌ GLTF/GLB Loading** - Full PBR material and scene hierarchy support
## Quick Start
```rust
use archetype_asset::{AssetCache, MockGpu};
use archetype_asset::loader::gltf::load_glb_file;
// Create GPU and cache
let gpu = MockGpu::new();
let cache = AssetCache::new(gpu, 100 * 1024 * 1024); // 100MB cache
// Load a GLB model directly
let model = load_glb_file("model.glb")?;
println!("Loaded {} meshes", model.meshes.len());
// Or use the cache for automatic caching (recommended)
let cached_model = cache.get_or_load_model("model.glb")?;
```
## Feature Flags
| `gpu-mock` | Mock GPU for testing | โ
|
| `gpu-vulkan` | Vulkan GPU backend | โ |
| `runtime-mock` | Mock async runtime | โ
|
| `runtime-tokio` | Tokio async runtime | โ |
| `lod` | LOD mesh simplification | โ |
| `spatial-preload` | Spatial preloading | โ |
| `metrics` | Performance metrics | โ |
### Enable Vulkan + Tokio
```toml
[dependencies]
archetype_asset = { version = "0.1.3", features = ["gpu-vulkan", "runtime-tokio"] }
```
## API Overview
### GPU Abstraction
```rust
use archetype_asset::{GpuDevice, MockGpu, BufferUsage};
// Any GPU backend implements GpuDevice
let gpu = MockGpu::new();
let buffer = gpu.allocate_buffer(1024, BufferUsage::Vertex)?;
gpu.upload_buffer_data(&buffer, 0, &data)?;
```
### Async Runtime Abstraction
```rust
use archetype_asset::{AsyncSpawner, MockSpawner};
let spawner = MockSpawner::blocking();
spawner.spawn(async {
// Async work here
});
```
### Asset Cache
```rust
use archetype_asset::{AssetCache, MockGpu};
let cache = AssetCache::new(MockGpu::new(), 100 * 1024 * 1024);
// Load with caching (returns Arc<LoadedModel>)
let model = cache.get_or_load_model("model.glb")?;
// Check memory usage
println!("Cache using {} bytes", cache.memory_usage());
// Performance metrics
let hit_rate = cache.metrics().cache_hit_rate();
```
### Model Loading
```rust
use archetype_asset::{ModelLoader, Vertex};
// Load from bytes
let loader = ModelLoader::new();
let model = loader.load_glb(&glb_bytes)?;
// Access mesh data
for mesh in &model.meshes {
let vertices = mesh.vertices();
let vertex_count = vertices.vertices.len() / Vertex::floats_per_vertex();
println!("Mesh has {} vertices", vertex_count);
}
```
### Spatial Preloading (Unique!)
```rust
use archetype_asset::{SpatialPredictor, DistancePredictor};
use glam::Vec3;
// Predict assets based on player position
let predictor = DistancePredictor::new(100.0); // 100 unit radius
let needed_assets = predictor.predict_assets(player_position, Some(velocity));
```
### LOD System
```rust
use archetype_asset::{LodModel, DefaultSimplifier, generate_lod_levels, default_thresholds};
// Generate 4 LOD levels
let simplifier = DefaultSimplifier::new();
let lod_levels = generate_lod_levels(&mesh, 4, &simplifier);
let thresholds = default_thresholds(4);
let lod_model = LodModel::new(lod_levels, thresholds);
// Select LOD based on screen size
let mesh_to_render = lod_model.get_lod(screen_size);
```
### Texture Loading
```rust
use archetype_asset::TextureLoader;
// Synchronous loading
let loader = TextureLoader::new();
let texture = loader.load_file_sync("texture.png")?;
println!("Loaded {}x{} texture", texture.width, texture.height);
```
## Performance
Benchmarks on typical hardware:
| Cache hit check | 5.4 ns |
| Cache memory check | 0.5 ns |
| Cache creation | 291 ns |
| Mesh data allocation | 138 ns |
| Memory pool creation | 1.5 ยตs |
## License
Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for details.
## Author
**Saptak Santra**