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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::regionmap::RegionMapU8;
use std::fs::File;
use std::io::BufWriter;
use std::ops::{Add, Div, Neg};
use std::path::{Path, PathBuf};
use bevy::ecs::entity::Entity;
use bevy::math::Vec2;
use bevy::ecs::event::Event;
use bevy::prelude::EventReader;
use bevy::asset::{AssetServer, Assets};
use bevy::render::render_resource::{Extent3d, TextureFormat};
use bevy::render::texture::Image;
use bevy::prelude::*;
use core::fmt::{self, Display, Formatter};
use crate::regions::{RegionDataEvent, RegionPlaneMesh, RegionsData, RegionsDataMapResource};
use crate::regions_config::RegionsConfig;
use crate::regions_material::RegionsMaterialExtension;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json;
use rand::Rng;
use core::cmp::{max, min};
pub struct BevyRegionEditsPlugin {
}
impl Default for BevyRegionEditsPlugin {
fn default() -> Self {
Self {
}
}
}
impl Plugin for BevyRegionEditsPlugin {
fn build(&self, app: &mut App) {
app.add_event::<EditRegionEvent>();
app.add_event::<RegionCommandEvent>();
app.add_event::<RegionBrushEvent>();
app.add_systems(Update, apply_tool_edits); //put this in a sub plugin ?
app.add_systems(Update, apply_command_events);
}
}
#[derive(Debug, Clone)]
pub enum EditingTool {
SetRegionMap { region_index: u8 }, // height, radius, save to disk
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum BrushType {
#[default]
SetExact, // hardness ?
Smooth,
//Noise,
EyeDropper,
}
impl Display for BrushType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let label = match self {
BrushType::SetExact => "SetExact",
BrushType::Smooth => "Smooth",
// BrushType::Noise => "Noise",
BrushType::EyeDropper => "EyeDropper",
};
write!(f, "{}", label)
}
}
// entity, editToolType, coords, magnitude
#[derive(Event, Debug, Clone)]
pub struct EditRegionEvent {
pub entity: Entity, //should always be the plane
pub tool: EditingTool,
pub radius: f32,
pub brush_hardness: f32, //1.0 is full
pub coordinates: Vec2,
pub brush_type: BrushType,
}
#[derive(Event, Debug, Clone)]
pub enum RegionBrushEvent {
EyeDropRegionIndex { region_index: u8 },
// EyeDropSplatMap { r: u8, g: u8, b: u8 },
}
#[derive(Event, Debug, Clone)]
pub enum RegionCommandEvent {
SaveAll ,
}
pub fn apply_command_events(
asset_server: Res<AssetServer>,
// mut chunk_query: Query<(&Chunk, &mut ChunkData, &Parent, &Children)>, //chunks parent should have terrain data
mut images: ResMut<Assets<Image>>,
mut region_materials: ResMut<Assets<RegionsMaterialExtension>>,
mut region_maps_res: ResMut<RegionsDataMapResource>, //like height map resource
region_data_query: Query<(&RegionsData, &RegionsConfig)>,
mut ev_reader: EventReader<RegionCommandEvent>,
) {
for ev in ev_reader.read() {
let Some((region_data, region_config)) = region_data_query
.get_single().ok() else {continue};
match ev {
RegionCommandEvent::SaveAll => {
//let file_name = format!("{}.png", chunk.chunk_id);
let asset_folder_path = PathBuf::from("assets");
let region_texture_path = ®ion_config.region_texture_path;
// info!("path {:?}",region_data_path);
if let Some(region_data) =
& region_maps_res.regions_data_map
{
save_region_index_map_to_disk(
®ion_data,
asset_folder_path.join( region_texture_path ),
);
}
println!("saved region data ");
}
}
}
// Ok(())
}
pub fn apply_tool_edits(
region_data_query: Query<(&mut RegionsData, &RegionsConfig)> ,
mut region_map_data_res: ResMut<RegionsDataMapResource>,
region_plane_mesh_query: Query<(Entity, &GlobalTransform), With<RegionPlaneMesh>>,
mut ev_reader: EventReader<EditRegionEvent>,
mut evt_writer: EventWriter<RegionBrushEvent>,
mut region_data_event_writer: EventWriter<RegionDataEvent>
) {
for ev in ev_reader.read() {
eprintln!("-- {:?} -- region edit event!", &ev.tool);
let Some((region_data, region_config)) = region_data_query
.get_single().ok() else {
warn!("no regions entity found" );
continue
};
let intersected_entity = &ev.entity;
let Some((region_plane_entity, _ )) = region_plane_mesh_query.get(intersected_entity.clone()).ok() else {
warn!("region plane not intersected");
continue
} ;
//let mut chunk_entities_within_range: Vec<Entity> = Vec::new();
let plane_dimensions = region_config.boundary_dimensions.clone(); //compute me from config
let tool_coords: &Vec2 = &ev.coordinates;
info!("tool coords {:?}", tool_coords);
let average_height = 0; //for now // total_height as f32 / heights_len as f32;
// ------
let radius = &ev.radius;
let brush_type = &ev.brush_type;
info!("Region Set Exact 1 ");
let Some(region_map_data) =
&mut region_map_data_res.regions_data_map
else {
warn!("regions data map is null ");
continue
};
let mut region_index_map_changed = false;
let brush_hardness = &ev.brush_hardness;
match &ev.tool {
EditingTool::SetRegionMap { region_index } => {
let tool_coords: &Vec2 = &ev.coordinates;
let tool_coords_local: &Vec2 = &ev.coordinates;
//need to make an array of all of the data indices of the terrain that will be set .. hm ?
let img_data_length = region_map_data.len();
let radius_clone = radius.clone();
info!("Region Set Exact 2 ");
match brush_type {
BrushType::SetExact => {
for x in 0..img_data_length {
for y in 0..img_data_length {
let local_coords = Vec2::new(x as f32, y as f32);
// info!("local_coords {:?} ", local_coords);
let hardness_multiplier = get_hardness_multiplier(
tool_coords_local.distance(local_coords),
radius_clone,
*brush_hardness,
);
let original_region_index = region_map_data[y][x];
// info!("tool_coords_local {:?} ", tool_coords_local);
if tool_coords_local.distance(local_coords)
< radius_clone
{
let new_region_index = region_index.clone();
region_map_data[y][x] =
apply_hardness_multiplier(
original_region_index as f32,
new_region_index as f32,
hardness_multiplier,
)
as u8;
region_index_map_changed = true;
// info!("region_index_map_changed {:?} ",new_region_index);
}
}
}
}
BrushType::Smooth => {
for x in 0..img_data_length {
for y in 0..img_data_length {
let local_coords = Vec2::new(x as f32, y as f32);
if tool_coords_local.distance(local_coords)
< *radius
{
let hardness_multiplier =
get_hardness_multiplier(
tool_coords_local
.distance(local_coords),
radius_clone,
*brush_hardness,
);
let original_region_index = region_map_data[y][x];
// Gather heights of the current point and its neighbors within the brush radius
let new_region_index = ((average_height as f32
+ original_region_index as f32)
/ 2.0)
as u8;
region_map_data[y][x] =
apply_hardness_multiplier(
original_region_index as f32,
new_region_index as f32,
hardness_multiplier,
)
as u8;
region_index_map_changed = true;
}
}
}
}
BrushType::EyeDropper => {
// Check if the clicked coordinates are within the current chunk
let x = tool_coords_local.x as usize;
let y = tool_coords_local.y as usize;
if x < img_data_length && y < img_data_length {
let local_index_data = region_map_data[y][x];
evt_writer.send(
RegionBrushEvent::EyeDropRegionIndex {
region_index: local_index_data,
},
);
}
}
}
}
} //match
if region_index_map_changed {
region_data_event_writer.send(
RegionDataEvent::RegionMapNeedsReloadFromResourceData
);
}
}
}
fn get_hardness_multiplier(pixel_distance: f32, brush_radius: f32, brush_hardness: f32) -> f32 {
// Calculate the distance as a percentage of the radius
let distance_percent = pixel_distance / brush_radius;
let adjusted_distance_percent = f32::min(1.0, distance_percent); // Ensure it does not exceed 1
// Calculate the fade effect based on brush hardness
// When hardness is 0, this will linearly interpolate from 1 at the center to 0 at the edge
// When hardness is between 0 and 1, it adjusts the fade effect accordingly
let fade_effect = 1.0 - adjusted_distance_percent;
// Apply the brush hardness to scale the fade effect, ensuring a minimum of 0
f32::max(
0.0,
fade_effect * (1.0 + brush_hardness) - (adjusted_distance_percent * brush_hardness),
)
}
fn apply_hardness_multiplier(
original_height: f32,
new_height: f32,
hardness_multiplier: f32,
) -> f32 {
original_height + (new_height - original_height) * hardness_multiplier
}
//move this to region_map.rs ?
// outputs as R16 grayscale
pub fn save_region_index_map_to_disk<P>(
region_map_data: &RegionMapU8, // Adjusted for direct Vec<Vec<u16>> input
save_file_path: P,
) where
P: AsRef<Path>,
{
let region_map_data = region_map_data.clone();
let height = region_map_data.len();
let width = region_map_data.first().map_or(0, |row| row.len());
let file = File::create(save_file_path).expect("Failed to create file");
let ref mut w = BufWriter::new(file);
let mut encoder = png::Encoder::new(w, width as u32, height as u32);
encoder.set_color(png::ColorType::Grayscale);
encoder.set_depth(png::BitDepth::Eight); // Change to 8-bit depth
let mut writer = encoder.write_header().expect("Failed to write PNG header");
// Flatten the Vec<Vec<u8>> to a Vec<u8> for the PNG encoder
let buffer: Vec<u8> = region_map_data.iter().flatten().cloned().collect();
// Write the image data
writer
.write_image_data(&buffer)
.expect("Failed to write PNG data");
}