-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtextures.rs
More file actions
84 lines (73 loc) · 2.76 KB
/
textures.rs
File metadata and controls
84 lines (73 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use bevy::prelude::*;
use bevy_voxel_world::prelude::*;
use std::sync::Arc;
// Declare materials as consts for convenience
// This can also be an enum or other type, see the `textures_custom_idx.rs` example
const SNOWY_BRICK: u8 = 0;
const FULL_BRICK: u8 = 1;
const GRASS: u8 = 2;
#[derive(Resource, Clone, Default)]
struct MyMainWorld;
impl VoxelWorldConfig for MyMainWorld {
type MaterialIndex = u8;
type ChunkUserBundle = ();
fn texture_index_mapper(
&self,
) -> Arc<dyn Fn(Self::MaterialIndex) -> [u32; 3] + Send + Sync> {
Arc::new(|vox_mat| match vox_mat {
SNOWY_BRICK => [0, 1, 2],
FULL_BRICK => [2, 2, 2],
GRASS => [3, 3, 3],
_ => [3, 3, 3],
})
}
fn voxel_texture(&self) -> Option<(String, u32)> {
Some(("example_voxel_texture.png".into(), 4))
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
// We can specify a custom texture when initializing the plugin.
// This should just be a path to an image in your assets folder.
.add_plugins(VoxelWorldPlugin::with_config(MyMainWorld))
.add_systems(Startup, (setup, create_voxel_scene).chain())
.run();
}
fn setup(mut commands: Commands) {
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
// This tells bevy_voxel_world to use this cameras transform to calculate spawning area
VoxelWorldCamera::<MyMainWorld>::default(),
));
// light
commands.spawn((
PointLight {
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
}
fn create_voxel_scene(mut voxel_world: VoxelWorld<MyMainWorld>) {
// Then we can use the `u8` consts to specify the type of voxel
// 20 by 20 floor
for x in -10..10 {
for z in -10..10 {
voxel_world.set_voxel(IVec3::new(x, -1, z), WorldVoxel::Solid(GRASS));
// Grassy floor
}
}
// Some bricks
voxel_world.set_voxel(IVec3::new(0, 0, 0), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(1, 0, 0), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(0, 0, 1), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(0, 0, -1), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(-1, 0, 0), WorldVoxel::Solid(FULL_BRICK));
voxel_world.set_voxel(IVec3::new(-2, 0, 0), WorldVoxel::Solid(FULL_BRICK));
voxel_world.set_voxel(IVec3::new(-1, 1, 0), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(-2, 1, 0), WorldVoxel::Solid(SNOWY_BRICK));
voxel_world.set_voxel(IVec3::new(0, 1, 0), WorldVoxel::Solid(SNOWY_BRICK));
}