use asset_loader::HeightMapLoader;
pub mod asset_loader;
pub mod image;
pub mod mesh_builder;
use bevy::{
app::{App, Plugin},
asset::AssetApp,
math::{UVec2, Vec2},
mesh::Mesh,
};
pub use crate::image::ImageBufferHeightMap;
pub use crate::mesh_builder::MeshBuilder;
pub trait HeightMap: Sized {
fn h(&self, p: Vec2) -> f32;
fn build_mesh(&self, size: UVec2) -> Mesh {
let mut builder = MeshBuilder::grid(size);
builder.update_z_positions(self);
builder.build()
}
}
pub struct ValueFunctionHeightMap<H: Fn(Vec2) -> f32>(pub H);
impl<H: Fn(Vec2) -> f32> HeightMap for ValueFunctionHeightMap<H> {
fn h(&self, p: Vec2) -> f32 {
self.0(p)
}
}
pub struct HeightMapPlugin;
impl Plugin for HeightMapPlugin {
fn build(&self, app: &mut App) {
app.preregister_asset_loader::<HeightMapLoader>(HeightMapLoader::EXTENSIONS);
}
fn finish(&self, app: &mut App) {
app.init_asset_loader::<asset_loader::HeightMapLoader>();
}
}