-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathfrustum_cull_test.rs
219 lines (200 loc) · 7.52 KB
/
frustum_cull_test.rs
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
214
215
216
217
218
219
//! Displays a map large enough for frustum culling to take place and configures
//! `LogPlugin` to display related traces.
use bevy::{
ecs::system::Resource, input::common_conditions::input_just_pressed, log::LogPlugin, prelude::*,
};
use bevy_ecs_tilemap::{prelude::*, FrustumCulling};
mod helpers;
// We want to trigger render chunking, which has minimum size 64x64.
const MAP_SIDE_LENGTH_X: u32 = 64;
const MAP_SIDE_LENGTH_Y: u32 = 64;
const TILE_SIZE_SQUARE: TilemapTileSize = TilemapTileSize { x: 50.0, y: 50.0 };
const TILE_SIZE_ISO: TilemapTileSize = TilemapTileSize { x: 100.0, y: 50.0 };
const TILE_SIZE_HEX_ROW: TilemapTileSize = TilemapTileSize { x: 50.0, y: 58.0 };
const TILE_SIZE_HEX_COL: TilemapTileSize = TilemapTileSize { x: 58.0, y: 50.0 };
const GRID_SIZE_SQUARE: TilemapGridSize = TilemapGridSize { x: 50.0, y: 50.0 };
const GRID_SIZE_HEX_ROW: TilemapGridSize = TilemapGridSize { x: 50.0, y: 58.0 };
const GRID_SIZE_HEX_COL: TilemapGridSize = TilemapGridSize { x: 58.0, y: 50.0 };
const GRID_SIZE_ISO: TilemapGridSize = TilemapGridSize { x: 100.0, y: 50.0 };
#[derive(Resource)]
pub struct TextureHandles {
hex_row: Handle<Image>,
hex_col: Handle<Image>,
square: Handle<Image>,
iso: Handle<Image>,
}
impl FromWorld for TextureHandles {
fn from_world(world: &mut World) -> Self {
let asset_server = world.resource::<AssetServer>();
Self {
hex_row: asset_server.load("bw-tile-hex-row.png"),
hex_col: asset_server.load("bw-tile-hex-col.png"),
square: asset_server.load("bw-tile-square.png"),
iso: asset_server.load("bw-tile-iso.png"),
}
}
}
#[derive(Component)]
pub struct MapTypeLabel;
fn spawn_scene(mut commands: Commands, texture_handles: Res<TextureHandles>) {
commands.spawn(Camera2d);
let map_size = TilemapSize {
// Render chunks are of size 64, so let's create two render chunks
x: MAP_SIDE_LENGTH_X * 2,
y: MAP_SIDE_LENGTH_Y,
};
let mut tile_storage = TileStorage::empty(map_size);
let tilemap_entity = commands.spawn_empty().id();
fill_tilemap(
TileTextureIndex(0),
map_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
);
commands.entity(tilemap_entity).insert(TilemapBundle {
grid_size: GRID_SIZE_SQUARE,
size: map_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handles.square.clone()),
tile_size: TILE_SIZE_SQUARE,
map_type: TilemapType::Square,
// This is the default value, but we provide it explicitly for demonstration
// purposes.
frustum_culling: FrustumCulling(true),
..default()
});
}
fn spawn_ui(mut commands: Commands) {
commands
.spawn((
Node {
position_type: PositionType::Absolute,
padding: UiRect::all(Val::Px(12.0)),
..default()
},
BackgroundColor(Color::BLACK.with_alpha(0.75)),
))
.with_children(|parent| {
parent
.spawn(Text::new(concat!(
"wasd: move camera\n",
"zx: zoom\n",
"space: change map type\n\n",
"map type: "
)))
.with_children(|parent| {
parent.spawn((TextSpan::default(), MapTypeLabel));
});
});
}
// Switches the map type when the user presses space.
fn switch_map_type(
mut tilemap_query: Query<(
&mut TilemapType,
&mut TilemapGridSize,
&mut TilemapTexture,
&mut TilemapTileSize,
)>,
texture_handles: Res<TextureHandles>,
) {
let Ok((mut map_type, mut grid_size, mut map_texture, mut tile_size)) =
tilemap_query.get_single_mut()
else {
return;
};
let next_type = match *map_type {
TilemapType::Square => TilemapType::Isometric(IsoCoordSystem::Diamond),
TilemapType::Isometric(IsoCoordSystem::Diamond) => {
TilemapType::Isometric(IsoCoordSystem::Staggered)
}
TilemapType::Isometric(IsoCoordSystem::Staggered) => {
TilemapType::Hexagon(HexCoordSystem::Row)
}
TilemapType::Hexagon(HexCoordSystem::Row) => TilemapType::Hexagon(HexCoordSystem::RowEven),
TilemapType::Hexagon(HexCoordSystem::RowEven) => {
TilemapType::Hexagon(HexCoordSystem::RowOdd)
}
TilemapType::Hexagon(HexCoordSystem::RowOdd) => {
TilemapType::Hexagon(HexCoordSystem::Column)
}
TilemapType::Hexagon(HexCoordSystem::Column) => {
TilemapType::Hexagon(HexCoordSystem::ColumnEven)
}
TilemapType::Hexagon(HexCoordSystem::ColumnEven) => {
TilemapType::Hexagon(HexCoordSystem::ColumnOdd)
}
TilemapType::Hexagon(HexCoordSystem::ColumnOdd) => TilemapType::Square,
};
*map_type = next_type;
match next_type {
TilemapType::Square => {
*map_texture = TilemapTexture::Single(texture_handles.square.clone());
*tile_size = TILE_SIZE_SQUARE;
*grid_size = GRID_SIZE_SQUARE;
}
TilemapType::Isometric(IsoCoordSystem::Diamond)
| TilemapType::Isometric(IsoCoordSystem::Staggered) => {
*map_texture = TilemapTexture::Single(texture_handles.iso.clone());
*tile_size = TILE_SIZE_ISO;
*grid_size = GRID_SIZE_ISO;
}
TilemapType::Hexagon(HexCoordSystem::Row)
| TilemapType::Hexagon(HexCoordSystem::RowEven)
| TilemapType::Hexagon(HexCoordSystem::RowOdd) => {
*map_texture = TilemapTexture::Single(texture_handles.hex_row.clone());
*tile_size = TILE_SIZE_HEX_ROW;
*grid_size = GRID_SIZE_HEX_ROW;
}
TilemapType::Hexagon(HexCoordSystem::Column)
| TilemapType::Hexagon(HexCoordSystem::ColumnEven)
| TilemapType::Hexagon(HexCoordSystem::ColumnOdd) => {
*map_texture = TilemapTexture::Single(texture_handles.hex_col.clone());
*tile_size = TILE_SIZE_HEX_COL;
*grid_size = GRID_SIZE_HEX_COL;
}
}
}
fn update_map_type_label(
type_query: Query<&TilemapType, Changed<TilemapType>>,
mut label_query: Query<&mut TextSpan, With<MapTypeLabel>>,
) {
let Ok(map_type) = type_query.get_single() else {
return;
};
let mut label = label_query.single_mut();
label.0 = format!("{:?}", map_type);
}
fn main() {
App::new()
.add_plugins(
DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Frustum cull test"),
..default()
}),
..default()
})
.set(ImagePlugin::default_nearest())
.set(LogPlugin {
// For debugging / demonstrating frustum culling, we'll want to see trace-level
// logs for `bevy_ecs_tilemap`.
filter: "info,bevy_ecs_tilemap=trace".into(),
..default()
}),
)
.add_plugins(TilemapPlugin)
.init_resource::<TextureHandles>()
.add_systems(Startup, (spawn_scene, spawn_ui))
.add_systems(
Update,
(
helpers::camera::movement,
switch_map_type.run_if(input_just_pressed(KeyCode::Space)),
update_map_type_label,
)
.chain(),
)
.run();
}