-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmerged_columns.rs
153 lines (142 loc) · 4.5 KB
/
merged_columns.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
use std::ops::Range;
use bevy::{
color::palettes::css::{BLUE, RED, WHITE},
prelude::*,
render::{mesh::Indices, render_asset::RenderAssetUsages, render_resource::PrimitiveTopology},
};
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::{quick::ResourceInspectorPlugin, InspectorOptions};
use hexx::*;
use rand::{rng, Rng};
/// Chunk colors
const COLORS: [Color; 3] = [Color::Srgba(BLUE), Color::Srgba(WHITE), Color::Srgba(RED)];
pub fn main() {
App::new()
.register_type::<MapSettings>()
.register_type::<Range<f32>>()
.init_resource::<MapSettings>()
.insert_resource(AmbientLight {
brightness: 500.,
..default()
})
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin {
enable_multipass_for_primary_context: false,
})
.add_plugins(ResourceInspectorPlugin::<MapSettings>::default())
.add_systems(Startup, setup_camera)
.add_systems(Update, setup_grid)
.run();
}
/// Egui settings
#[derive(Debug, Resource, Reflect, InspectorOptions)]
struct MapSettings {
pub hex_size: Vec2,
pub column_heights: [f32; 2],
#[inspector(min = 0, max = 50)]
pub map_radius: u32,
#[inspector(min = 1, max = 50)]
pub chunk_radius: u32,
}
#[derive(Debug, Resource)]
struct Map(pub Entity);
/// 3D camera setup
fn setup_camera(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 60.0, 60.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}
/// Hex grid setup
fn setup_grid(
settings: Res<MapSettings>,
mut map: Local<Map>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
if !settings.is_changed() {
return;
}
println!("Generating map");
if map.0 != Entity::PLACEHOLDER {
commands.entity(map.0).despawn();
}
let layout = HexLayout {
scale: settings.hex_size,
..default()
};
// Materials shouldn't be added to assets every time, this is just to keep the
// example simple
let materials = COLORS.map(|c| materials.add(c));
let map_entity = commands
.spawn((
Name::new("Chunks"),
Transform::default(),
Visibility::default(),
))
.id();
let mut rng = rng();
// For each chunk
for chunk in Hex::ZERO.range(settings.map_radius) {
// We retrieve its center chil
let center = chunk.to_higher_res(settings.chunk_radius);
// Retrieve its world pos
let pos = layout.hex_to_world_pos(center);
// Compute the color index for the chunk
let color_index = (chunk.x - chunk.y).rem_euclid(3) as usize;
// Retrieve the local children coordinates (can be cached)
let children = Hex::ZERO.range(settings.chunk_radius);
// We compute the merged mesh with all children columns
let mesh = children.fold(MeshInfo::default(), |mut mesh, c| {
let [min, max] = settings.column_heights;
let height = if min < max {
rng.random_range(min..=max)
} else {
min
};
let info = ColumnMeshBuilder::new(&layout, height)
.at(c)
.without_bottom_face()
.center_aligned()
.build();
mesh.merge_with(info);
mesh
});
let mesh = meshes.add(hex_mesh(mesh));
commands.spawn((
Name::new(format!("Chunk {} {}", chunk.x, chunk.y)),
Mesh3d(mesh),
MeshMaterial3d(materials[color_index].clone()),
Transform::from_xyz(pos.x, 0.0, pos.y),
ChildOf(map_entity),
));
}
map.0 = map_entity;
}
/// Compute a bevy mesh from a hexx mesh
fn hex_mesh(mesh_info: MeshInfo) -> Mesh {
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD,
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, mesh_info.vertices)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, mesh_info.normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, mesh_info.uvs)
.with_inserted_indices(Indices::U16(mesh_info.indices))
}
impl Default for MapSettings {
fn default() -> Self {
Self {
hex_size: Vec2::ONE,
map_radius: 10,
chunk_radius: 5,
column_heights: [5.0, 10.0],
}
}
}
impl Default for Map {
fn default() -> Self {
Self(Entity::PLACEHOLDER)
}
}