-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmultiple_worlds.rs
More file actions
213 lines (185 loc) · 6.86 KB
/
multiple_worlds.rs
File metadata and controls
213 lines (185 loc) · 6.86 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::sync::Arc;
use bevy::{
light::CascadeShadowConfigBuilder,
mesh::MeshVertexBufferLayoutRef,
pbr::{MaterialPipeline, MaterialPipelineKey},
platform::collections::HashMap,
prelude::*,
render::render_resource::{
AsBindGroup, RenderPipelineDescriptor, SpecializedMeshPipelineError,
},
};
use bevy_shader::{ShaderDefVal, ShaderRef};
use bevy_voxel_world::{
prelude::*,
rendering::{vertex_layout, VOXEL_TEXTURE_SHADER_HANDLE},
};
use noise::{HybridMulti, NoiseFn, Perlin};
const RED: u8 = 0;
const GREEN: u8 = 1;
const BLUE: u8 = 2;
// This is the main world configuration. In this example, the main world is the procedural terrain.
#[derive(Resource, Clone, Default)]
struct MainWorld;
impl VoxelWorldConfig for MainWorld {
type MaterialIndex = u8;
type ChunkUserBundle = ();
fn spawning_distance(&self) -> u32 {
10
}
fn voxel_lookup_delegate(&self) -> VoxelLookupDelegate<Self::MaterialIndex> {
Box::new(move |_chunk_pos, _lod, _previous| get_voxel_fn())
}
fn texture_index_mapper(
&self,
) -> Arc<dyn Fn(Self::MaterialIndex) -> [u32; 3] + Send + Sync> {
Arc::new(|mat| match mat {
0 => [0, 0, 0],
1 => [1, 1, 1],
2 => [2, 2, 2],
3 => [3, 3, 3],
_ => [0, 0, 0],
})
}
}
// This is the second world configuration. In this example, the second world is using a custom material.
#[derive(Resource, Clone, Default)]
struct SecondWorld;
impl VoxelWorldConfig for SecondWorld {
type MaterialIndex = u8;
type ChunkUserBundle = ();
fn texture_index_mapper(&self) -> Arc<dyn Fn(u8) -> [u32; 3] + Send + Sync> {
Arc::new(|vox_mat: u8| match vox_mat {
RED => [1, 1, 1],
GREEN => [2, 2, 2],
BLUE => [3, 3, 3],
_ => [3, 3, 3],
})
}
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(MaterialPlugin::<CustomVoxelMaterial>::default())
.add_plugins(VoxelWorldPlugin::with_config(MainWorld)) // Add the main world
.add_plugins(
// Add the second world with a custom material
VoxelWorldPlugin::with_config(SecondWorld)
.with_material(CustomVoxelMaterial { _unused: 0 }),
)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, mut second_world: VoxelWorld<SecondWorld>) {
// --- Just scene setup below ---
// camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-10.0, 10.0, -10.0).looking_at(Vec3::Y * 4.0, Vec3::Y),
// This tells bevy_voxel_world to use this cameras transform to calculate spawning area
VoxelWorldCamera::<MainWorld>::default(),
VoxelWorldCamera::<SecondWorld>::default(),
));
// Sun
let cascade_shadow_config = CascadeShadowConfigBuilder { ..default() }.build();
commands.spawn((
DirectionalLight {
color: Color::srgb(0.98, 0.95, 0.82),
shadows_enabled: true,
..default()
},
Transform::from_xyz(0.0, 0.0, 0.0)
.looking_at(Vec3::new(-0.15, -0.1, 0.15), Vec3::Y),
cascade_shadow_config,
));
// Ambient light, same color as sun
commands.insert_resource(GlobalAmbientLight {
color: Color::srgb(0.98, 0.95, 0.82),
brightness: 100.0,
affects_lightmapped_meshes: true,
});
// Set some voxels in the second world
second_world.set_voxel(IVec3::new(0, 2, 0), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(1, 2, 0), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(0, 2, 1), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(0, 2, -1), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(-1, 2, 0), WorldVoxel::Solid(GREEN));
second_world.set_voxel(IVec3::new(-2, 2, 0), WorldVoxel::Solid(GREEN));
second_world.set_voxel(IVec3::new(-1, 3, 0), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(-2, 3, 0), WorldVoxel::Solid(RED));
second_world.set_voxel(IVec3::new(0, 3, 0), WorldVoxel::Solid(RED));
}
fn get_voxel_fn() -> Box<dyn FnMut(IVec3, Option<WorldVoxel>) -> WorldVoxel + Send + Sync>
{
// Set up some noise to use as the terrain height map
let mut noise = HybridMulti::<Perlin>::new(1234);
noise.octaves = 4;
noise.frequency = 1.0;
noise.lacunarity = 2.2;
noise.persistence = 0.5;
// We use this to cache the noise value for each y column so we only need
// to calculate it once per x/z coordinate
let mut cache = HashMap::<(i32, i32), f64>::new();
// Then we return this boxed closure that captures the noise and the cache
// This will get sent off to a separate thread for meshing by bevy_voxel_world
Box::new(move |pos: IVec3, _previous| {
// Sea level
if pos.y < 1 {
return WorldVoxel::Solid(3);
}
let [x, y, z] = pos.as_dvec3().to_array();
let sample = match cache.get(&(pos.x, pos.z)) {
Some(sample) => *sample,
None => {
let sample = noise.get([x / 700.0, z / 700.0]) * 5.0;
cache.insert((pos.x, pos.z), sample);
sample
}
};
// If y is less than the noise sample, we will set the voxel to solid
let is_surface = y < sample;
let is_sub_surface = y < sample - 1.0;
if is_surface && !is_sub_surface {
// Solid voxel of material type 0
WorldVoxel::Solid(0)
} else if is_sub_surface {
// Solid voxel of material type 1
WorldVoxel::Solid(1)
} else {
WorldVoxel::Air
}
})
}
// This is the custom material. You can set this up like any other material in Bevy.
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
struct CustomVoxelMaterial {
// We're not using any uniforms in this example
_unused: u32,
}
impl Material for CustomVoxelMaterial {
fn vertex_shader() -> ShaderRef {
// You can use the default shader from bevy_voxel_world for the vertex shader for simplicity
VOXEL_TEXTURE_SHADER_HANDLE.into()
}
fn fragment_shader() -> ShaderRef {
"custom_material.wgsl".into()
}
fn specialize(
_pipeline: &MaterialPipeline,
descriptor: &mut RenderPipelineDescriptor,
layout: &MeshVertexBufferLayoutRef,
_key: MaterialPipelineKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
if descriptor
.vertex
.shader_defs
.contains(&ShaderDefVal::Bool("PREPASS_PIPELINE".into(), true))
{
return Ok(());
}
// Use `vertex_layout()` from `bevy_voxel_world` to get the correct vertex layout
let vertex_layout = layout.0.get_layout(&vertex_layout())?;
descriptor.vertex.buffers = vec![vertex_layout];
Ok(())
}
}