src/tilemap.rs 18.1 K · 560 lines · raw · history

1 use bevy::{
2 asset::RenderAssetUsages,
3 prelude::*,
4 render::render_resource::{Extent3d, TextureDimension, TextureFormat},
5 sprite_render::{AlphaMode2d, TileData, TilemapChunk, TilemapChunkTileData},
6 };
7
8 use crate::{
9 light::LightMap,
10 world::{MAX_HEIGHT, Plant, TILE_SIZE, Terrain, TileChanged, WorldMap, hash2},
11 worldgen::WorldGenerated,
12 zones::Zones,
13 };
14
15 pub const CHUNK_SIZE: u32 = 64;
16 const TEXTURE_SIZE: u32 = 16;
17 const RELIEF: f32 = 0.35;
18 const LIGHT: Vec3 = Vec3::new(-1.0, 1.0, 1.5);
19
20 type Texture = Vec<[u8; 4]>;
21
22 pub struct TilemapPlugin;
23
24 impl Plugin for TilemapPlugin {
25 fn build(&self, app: &mut App) {
26 app.add_systems(Startup, spawn_chunks).add_systems(
27 Update,
28 (
29 refresh_chunks.run_if(world_or_light_changed),
30 refresh_changed_chunks,
31 refresh_zone_chunks.run_if(resource_changed::<Zones>),
32 ),
33 );
34 }
35 }
36
37 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
38 enum Layer {
39 Terrain,
40 Zones,
41 Roofs,
42 Plants,
43 }
44
45 impl Layer {
46 fn z(self) -> f32 {
47 match self {
48 Layer::Terrain => 0.0,
49 Layer::Zones => 0.3,
50 Layer::Roofs => 0.85,
51 Layer::Plants => 0.5,
52 }
53 }
54
55 fn tiles(
56 self,
57 map: &WorldMap,
58 zones: &Zones,
59 light: &LightMap,
60 origin: UVec2,
61 ) -> Vec<Option<TileData>> {
62 match self {
63 Layer::Terrain => chunk_tiles(map, light, origin),
64 Layer::Zones => zone_tiles(zones, origin),
65 Layer::Roofs => roof_tiles(map, origin),
66 Layer::Plants => plant_tiles(map, light, origin),
67 }
68 }
69 }
70
71 #[derive(Component)]
72 struct Chunk {
73 origin: UVec2,
74 layer: Layer,
75 }
76
77 fn spawn_chunks(
78 mut commands: Commands,
79 map: Res<WorldMap>,
80 zones: Res<Zones>,
81 light: Res<LightMap>,
82 mut images: ResMut<Assets<Image>>,
83 ) {
84 let terrain = images.add(stack_tileset(
85 Terrain::ALL.iter().map(|&t| terrain_texture(t)).collect(),
86 ));
87 let plants = images.add(stack_tileset(
88 PlantLook::ALL
89 .iter()
90 .map(|&look| plant_texture(look))
91 .collect(),
92 ));
93 let zone = images.add(stack_tileset(vec![zone_texture()]));
94 let roof = images.add(stack_tileset(vec![roof_texture()]));
95 let chunks = (map.size + CHUNK_SIZE - 1) / CHUNK_SIZE;
96 for (layer, tileset) in [
97 (Layer::Terrain, terrain),
98 (Layer::Zones, zone),
99 (Layer::Plants, plants),
100 (Layer::Roofs, roof),
101 ] {
102 for cy in 0..chunks.y {
103 for cx in 0..chunks.x {
104 let origin = UVec2::new(cx, cy) * CHUNK_SIZE;
105 let center = (origin.as_vec2() + CHUNK_SIZE as f32 / 2.0) * TILE_SIZE;
106 commands.spawn((
107 Name::new(format!("{layer:?} chunk {cx},{cy}")),
108 Chunk { origin, layer },
109 TilemapChunk {
110 chunk_size: UVec2::splat(CHUNK_SIZE),
111 tile_display_size: UVec2::splat(TILE_SIZE as u32),
112 tileset: tileset.clone(),
113 alpha_mode: AlphaMode2d::Blend,
114 },
115 TilemapChunkTileData(layer.tiles(&map, &zones, &light, origin)),
116 Transform::from_translation(center.extend(layer.z())),
117 ));
118 }
119 }
120 }
121 }
122
123 fn refresh_chunks(
124 map: Res<WorldMap>,
125 zones: Res<Zones>,
126 light: Res<LightMap>,
127 mut chunks: Query<(&Chunk, &mut TilemapChunkTileData)>,
128 ) {
129 for (chunk, mut data) in &mut chunks {
130 data.0 = chunk.layer.tiles(&map, &zones, &light, chunk.origin);
131 }
132 }
133
134 fn world_or_light_changed(
135 mut generated: MessageReader<WorldGenerated>,
136 light: Res<LightMap>,
137 ) -> bool {
138 generated.read().count() > 0 || light.is_changed()
139 }
140
141 fn refresh_zone_chunks(
142 map: Res<WorldMap>,
143 zones: Res<Zones>,
144 light: Res<LightMap>,
145 mut chunks: Query<(&Chunk, &mut TilemapChunkTileData)>,
146 ) {
147 for (chunk, mut data) in &mut chunks {
148 if chunk.layer == Layer::Zones {
149 data.0 = chunk.layer.tiles(&map, &zones, &light, chunk.origin);
150 }
151 }
152 }
153
154 fn refresh_changed_chunks(
155 map: Res<WorldMap>,
156 zones: Res<Zones>,
157 light: Res<LightMap>,
158 mut changes: MessageReader<TileChanged>,
159 mut chunks: Query<(&Chunk, &mut TilemapChunkTileData)>,
160 ) {
161 let origins: Vec<UVec2> = changes
162 .read()
163 .map(|TileChanged(tile)| chunk_origin(*tile))
164 .collect();
165 for (chunk, mut data) in &mut chunks {
166 if origins.contains(&chunk.origin) {
167 data.0 = chunk.layer.tiles(&map, &zones, &light, chunk.origin);
168 }
169 }
170 }
171
172 pub fn chunk_origin(tile: UVec2) -> UVec2 {
173 tile / CHUNK_SIZE * CHUNK_SIZE
174 }
175
176 fn chunk_positions(origin: UVec2) -> impl Iterator<Item = UVec2> {
177 (0..CHUNK_SIZE).flat_map(move |y| (0..CHUNK_SIZE).map(move |x| origin + UVec2::new(x, y)))
178 }
179
180 pub fn chunk_tiles(map: &WorldMap, light: &LightMap, origin: UVec2) -> Vec<Option<TileData>> {
181 chunk_positions(origin)
182 .map(|pos| {
183 map.get(pos.as_ivec2()).map(|terrain| TileData {
184 tileset_index: terrain.index(),
185 color: tint(jitter(pos) * shade(map, pos), light.color(pos)),
186 ..default()
187 })
188 })
189 .collect()
190 }
191
192 pub fn zone_tiles(zones: &Zones, origin: UVec2) -> Vec<Option<TileData>> {
193 chunk_positions(origin)
194 .map(|pos| {
195 zones
196 .is_stockpile(pos)
197 .then(|| TileData::from_tileset_index(0))
198 })
199 .collect()
200 }
201
202 pub fn roof_tiles(map: &WorldMap, origin: UVec2) -> Vec<Option<TileData>> {
203 chunk_positions(origin)
204 .map(|pos| {
205 map.is_roofed(pos.as_ivec2())
206 .then(|| TileData::from_tileset_index(0))
207 })
208 .collect()
209 }
210
211 pub fn plant_tiles(map: &WorldMap, light: &LightMap, origin: UVec2) -> Vec<Option<TileData>> {
212 chunk_positions(origin)
213 .map(|pos| {
214 map.plant(pos.as_ivec2()).map(|plant| TileData {
215 tileset_index: PlantLook::of(plant, map.growth(pos.as_ivec2())) as u16,
216 color: tint(jitter(pos) * hillshade(map, pos), light.color(pos)),
217 ..default()
218 })
219 })
220 .collect()
221 }
222
223 fn tint(v: f32, light: Vec3) -> Color {
224 Color::srgb(v * light.x, v * light.y, v * light.z)
225 }
226
227 fn jitter(pos: UVec2) -> f32 {
228 0.92 + (hash2(pos.x, pos.y) % 9) as f32 * 0.01
229 }
230
231 fn shade(map: &WorldMap, pos: UVec2) -> f32 {
232 match map.get(pos.as_ivec2()) {
233 Some(t) if t.is_water() => 1.0,
234 _ => hillshade(map, pos) * height_brightness(map, pos),
235 }
236 }
237
238 fn height_brightness(map: &WorldMap, pos: UVec2) -> f32 {
239 0.9 + 0.2 * map.height(pos.as_ivec2()).unwrap_or(0.0) / MAX_HEIGHT
240 }
241
242 pub fn hillshade(map: &WorldMap, pos: UVec2) -> f32 {
243 let p = pos.as_ivec2();
244 let center = map.height(p).unwrap_or(0.0);
245 let h = |d: IVec2| map.height(p + d).unwrap_or(center);
246 let dx = (h(IVec2::X) - h(IVec2::NEG_X)) / 2.0;
247 let dy = (h(IVec2::Y) - h(IVec2::NEG_Y)) / 2.0;
248 let normal = Vec3::new(-dx * RELIEF, -dy * RELIEF, 1.0).normalize();
249 let light = LIGHT.normalize();
250 (normal.dot(light) / light.z).clamp(0.6, 1.25)
251 }
252
253 fn terrain_texture(terrain: Terrain) -> Texture {
254 let base = terrain.color();
255 let layer = terrain.index() as u32;
256 (0..TEXTURE_SIZE * TEXTURE_SIZE)
257 .map(|i| {
258 let speck = terrain
259 .speck_color()
260 .filter(|_| hash2(i / 2 + i / 32, layer) % 5 == 0);
261 let jitter = (hash2(i, layer) % 16) as f32 / 255.0 - 8.0 / 255.0;
262 let c = speck.unwrap_or(base);
263 Srgba::new(c.red + jitter, c.green + jitter, c.blue + jitter, 1.0).to_u8_array()
264 })
265 .collect()
266 }
267
268 fn zone_texture() -> Texture {
269 (0..TEXTURE_SIZE * TEXTURE_SIZE)
270 .map(|i| {
271 let (x, y) = (i % TEXTURE_SIZE, i / TEXTURE_SIZE);
272 let edge = x == 0 || y == 0 || x == TEXTURE_SIZE - 1 || y == TEXTURE_SIZE - 1;
273 [90, 160, 230, if edge { 150 } else { 55 }]
274 })
275 .collect()
276 }
277
278 fn roof_texture() -> Texture {
279 (0..TEXTURE_SIZE * TEXTURE_SIZE)
280 .map(|i| {
281 let (x, y) = (i % TEXTURE_SIZE, i / TEXTURE_SIZE);
282 let stripe = (x + y) % 4 == 0;
283 [110, 80, 50, if stripe { 90 } else { 45 }]
284 })
285 .collect()
286 }
287
288 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
289 pub enum PlantLook {
290 Tree,
291 Bush,
292 RipeBush,
293 Sprout,
294 YoungCrop,
295 RipeCrop,
296 }
297
298 impl PlantLook {
299 const ALL: [PlantLook; 6] = [
300 PlantLook::Tree,
301 PlantLook::Bush,
302 PlantLook::RipeBush,
303 PlantLook::Sprout,
304 PlantLook::YoungCrop,
305 PlantLook::RipeCrop,
306 ];
307
308 pub fn of(plant: Plant, growth: f32) -> Self {
309 match plant {
310 Plant::Tree => PlantLook::Tree,
311 Plant::BerryBush if plant.is_ripe(growth) => PlantLook::RipeBush,
312 Plant::BerryBush => PlantLook::Bush,
313 Plant::Crop if plant.is_ripe(growth) => PlantLook::RipeCrop,
314 Plant::Crop if growth >= SPROUT_UNTIL => PlantLook::YoungCrop,
315 Plant::Crop => PlantLook::Sprout,
316 }
317 }
318 }
319
320 pub const SPROUT_UNTIL: f32 = 0.4;
321
322 fn plant_texture(look: PlantLook) -> Texture {
323 let leaves = Srgba::rgb_u8(52, 110, 50);
324 let berry = Srgba::rgb_u8(190, 40, 90);
325 let stalk = Srgba::rgb_u8(110, 160, 60);
326 let grain = Srgba::rgb_u8(220, 190, 80);
327 match look {
328 PlantLook::Tree => tree_texture(),
329 PlantLook::Bush => blob_texture(5.0, leaves, None),
330 PlantLook::RipeBush => blob_texture(5.0, leaves, Some(berry)),
331 PlantLook::Sprout => rows_texture(3, stalk, None),
332 PlantLook::YoungCrop => rows_texture(7, stalk, None),
333 PlantLook::RipeCrop => rows_texture(10, stalk, Some(grain)),
334 }
335 }
336
337 fn blob_texture(radius: f32, color: Srgba, dots: Option<Srgba>) -> Texture {
338 (0..TEXTURE_SIZE * TEXTURE_SIZE)
339 .map(|i| {
340 let (x, y) = (i % TEXTURE_SIZE, i / TEXTURE_SIZE);
341 let d = Vec2::new(x as f32, y as f32).distance(Vec2::new(7.5, 8.5));
342 if d > radius {
343 return [0, 0, 0, 0];
344 }
345 match dots {
346 Some(dot) if hash2(x, y) % 5 == 0 => dot.to_u8_array(),
347 _ => color.to_u8_array(),
348 }
349 })
350 .collect()
351 }
352
353 fn rows_texture(height: u32, stalk: Srgba, head: Option<Srgba>) -> Texture {
354 (0..TEXTURE_SIZE * TEXTURE_SIZE)
355 .map(|i| {
356 let (x, y) = (i % TEXTURE_SIZE, i / TEXTURE_SIZE);
357 let in_row = x % 4 == 1 || x % 4 == 2;
358 let top = TEXTURE_SIZE - 2 - height;
359 if !in_row || y < top || y >= TEXTURE_SIZE - 2 {
360 return [0, 0, 0, 0];
361 }
362 match head {
363 Some(head) if y < top + 3 => head.to_u8_array(),
364 _ => stalk.to_u8_array(),
365 }
366 })
367 .collect()
368 }
369
370 fn tree_texture() -> Texture {
371 let leaves = Srgba::rgb_u8(38, 92, 42);
372 let highlight = Srgba::rgb_u8(66, 126, 58);
373 let trunk = Srgba::rgb_u8(92, 64, 40);
374 let canopy_center = Vec2::new(7.5, 6.5);
375 (0..TEXTURE_SIZE * TEXTURE_SIZE)
376 .map(|i| {
377 let (x, y) = (i % TEXTURE_SIZE, i / TEXTURE_SIZE);
378 let p = Vec2::new(x as f32, y as f32);
379 let from_center = p - canopy_center;
380 if from_center.length() <= 6.0 {
381 let lit = from_center.dot(Vec2::new(-1.0, -1.0)) > 3.0;
382 if lit { highlight } else { leaves }.to_u8_array()
383 } else if (7..=8).contains(&x) && y >= 12 && y <= 14 {
384 trunk.to_u8_array()
385 } else {
386 [0, 0, 0, 0]
387 }
388 })
389 .collect()
390 }
391
392 pub fn tileset_layer_count(textures: u32) -> u32 {
393 match textures {
394 0 | 1 => 2,
395 n if n % 6 == 0 => n + 1,
396 n => n,
397 }
398 }
399
400 fn stack_tileset(textures: Vec<Texture>) -> Image {
401 let layers = tileset_layer_count(textures.len() as u32);
402 let empty = vec![[0, 0, 0, 0]; (TEXTURE_SIZE * TEXTURE_SIZE) as usize];
403 let data = (0..layers as usize)
404 .flat_map(|i| textures.get(i).unwrap_or(&empty).clone())
405 .flatten()
406 .collect();
407 let mut image = Image::new(
408 Extent3d {
409 width: TEXTURE_SIZE,
410 height: TEXTURE_SIZE * layers,
411 depth_or_array_layers: 1,
412 },
413 TextureDimension::D2,
414 data,
415 TextureFormat::Rgba8UnormSrgb,
416 RenderAssetUsages::RENDER_WORLD,
417 );
418 image
419 .reinterpret_stacked_2d_as_array(layers)
420 .expect("tileset is a vertical stack of square layers");
421 image
422 }
423
424 #[cfg(test)]
425 mod tests {
426 use super::*;
427
428 #[test]
429 fn chunk_tiles_beyond_map_edge_are_empty() {
430 let map = WorldMap::filled(UVec2::new(CHUNK_SIZE + 2, 3), Terrain::Stone);
431 let tiles = chunk_tiles(&map, &LightMap::default(), UVec2::new(CHUNK_SIZE, 0));
432 assert_eq!(tiles.len(), (CHUNK_SIZE * CHUNK_SIZE) as usize);
433 assert!(tiles[0].is_some());
434 assert!(tiles[1].is_some());
435 assert!(tiles[2].is_none());
436 assert!(tiles[CHUNK_SIZE as usize * 3].is_none());
437 }
438
439 // Chunk entities live for the whole session. World changes (regeneration now, digging and
440 // building later) rewrite their tile data in place; TilemapChunk re-uploads it through change
441 // detection, so no entities, meshes or materials are recreated.
442 #[test]
443 fn regeneration_rewrites_tile_data_of_existing_chunks() {
444 let mut app = App::new();
445 app.insert_resource(WorldMap::filled(UVec2::splat(CHUNK_SIZE), Terrain::Grass))
446 .init_resource::<Zones>()
447 .init_resource::<LightMap>()
448 .add_message::<WorldGenerated>()
449 .add_systems(Update, refresh_chunks.run_if(on_message::<WorldGenerated>));
450 let map = app.world().resource::<WorldMap>().clone();
451 let chunk = app
452 .world_mut()
453 .spawn((
454 Chunk {
455 origin: UVec2::ZERO,
456 layer: Layer::Terrain,
457 },
458 TilemapChunkTileData(chunk_tiles(&map, &LightMap::default(), UVec2::ZERO)),
459 ))
460 .id();
461
462 *app.world_mut().resource_mut::<WorldMap>() =
463 WorldMap::filled(UVec2::splat(CHUNK_SIZE), Terrain::Stone);
464 app.world_mut().write_message(WorldGenerated);
465 app.update();
466
467 let data = app.world().get::<TilemapChunkTileData>(chunk).unwrap();
468 assert!(
469 data.0
470 .iter()
471 .all(|t| t.unwrap().tileset_index == Terrain::Stone.index())
472 );
473 }
474
475 #[test]
476 fn darkness_tints_tiles_and_fires_warm_them() {
477 let map = WorldMap::filled(UVec2::splat(CHUNK_SIZE), Terrain::Grass);
478 let fire = (
479 UVec2::new(10, 10),
480 crate::structures::StructureKind::Campfire,
481 );
482 let night = LightMap::compute(map.size, 0.0, [fire]);
483 let tiles = chunk_tiles(&map, &night, UVec2::ZERO);
484 let lum = |pos: UVec2| {
485 let c = tiles[(pos.y * CHUNK_SIZE + pos.x) as usize]
486 .unwrap()
487 .color
488 .to_srgba();
489 c.red + c.green + c.blue
490 };
491 assert!(lum(UVec2::new(10, 11)) > lum(UVec2::new(40, 40)) * 2.0);
492 }
493
494 #[test]
495 fn roof_layer_covers_roofed_tiles_only() {
496 let mut map = WorldMap::filled(UVec2::splat(4), Terrain::Grass);
497 map.set_roof(UVec2::new(2, 1), true);
498 let tiles = roof_tiles(&map, UVec2::ZERO);
499 assert!(tiles[(CHUNK_SIZE + 2) as usize].is_some());
500 assert_eq!(tiles.iter().filter(|t| t.is_some()).count(), 1);
501 }
502
503 #[test]
504 fn chunk_origin_rounds_down_to_chunk_grid() {
505 assert_eq!(chunk_origin(UVec2::new(0, 63)), UVec2::new(0, 0));
506 assert_eq!(chunk_origin(UVec2::new(64, 130)), UVec2::new(64, 128));
507 }
508
509 #[test]
510 fn tileset_has_a_layer_for_every_texture() {
511 let textures = Terrain::ALL.iter().map(|&t| terrain_texture(t)).collect();
512 let image = stack_tileset(textures);
513 assert!(image.texture_descriptor.size.depth_or_array_layers >= Terrain::ALL.len() as u32);
514 }
515
516 #[test]
517 fn plants_look_different_as_they_grow() {
518 assert_eq!(PlantLook::of(Plant::Crop, 0.1), PlantLook::Sprout);
519 assert_eq!(PlantLook::of(Plant::Crop, 0.7), PlantLook::YoungCrop);
520 assert_eq!(PlantLook::of(Plant::Crop, 1.0), PlantLook::RipeCrop);
521 assert_eq!(PlantLook::of(Plant::BerryBush, 0.5), PlantLook::Bush);
522 assert_eq!(PlantLook::of(Plant::BerryBush, 1.0), PlantLook::RipeBush);
523 }
524
525 #[test]
526 fn plant_layer_only_has_tiles_where_plants_grow() {
527 let mut map = WorldMap::filled(UVec2::splat(4), Terrain::Grass);
528 map.set_plant(UVec2::new(1, 0), Some(Plant::Tree));
529 let tiles = plant_tiles(&map, &LightMap::default(), UVec2::ZERO);
530 assert!(tiles[0].is_none());
531 assert_eq!(tiles[1].unwrap().tileset_index, PlantLook::Tree as u16);
532 }
533
534 // Light comes from the north-west (LIGHT has -x, +y), the convention for hillshaded maps.
535 #[test]
536 fn slopes_facing_the_light_are_brighter_than_slopes_facing_away() {
537 let mut map = WorldMap::filled(UVec2::new(5, 1), Terrain::Grass);
538 for (x, h) in [0.0, 5.0, 10.0, 5.0, 0.0].into_iter().enumerate() {
539 map.set_height(UVec2::new(x as u32, 0), h);
540 }
541 let flat = WorldMap::filled(UVec2::new(3, 1), Terrain::Grass);
542 let facing_west = hillshade(&map, UVec2::new(1, 0));
543 let facing_east = hillshade(&map, UVec2::new(3, 0));
544 let level = hillshade(&flat, UVec2::new(1, 0));
545 assert!(facing_west > level);
546 assert!(facing_east < level);
547 }
548
549 // wgpu's GLES backend (WebGL2) guesses the texture view dimension from the layer count:
550 // 1 layer => D2, 6 => Cube, multiples of 6 => CubeArray. The tilemap shader needs D2Array,
551 // so the tileset is padded with an unused layer to dodge those counts.
552 #[test]
553 fn tileset_layer_count_avoids_webgl_view_dimension_heuristics() {
554 for n in 0..50 {
555 let layers = tileset_layer_count(n);
556 assert!(layers >= n.max(2));
557 assert!(layers % 6 != 0, "{n} terrains -> {layers} layers");
558 }
559 }
560 }