tirbofish/dropbear
main / crates / dropbear-engine / src / entity.rs · 21912 bytes
crates/dropbear-engine/src/entity.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use glam::{DMat4, DQuat, DVec3, Mat4, Quat, Vec3};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::Path, sync::Arc};
use crate::asset::Handle;
use crate::model::{Material, NodeTransform};
use crate::{
asset::ASSET_REGISTRY,
graphics::{Instance, SharedGraphicsContext},
model::Model,
texture::Texture,
utils::ResourceReference,
};
use egui::Ui;
use std::hash::Hash;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RotationEditorMode {
EulerDegrees,
EulerRadians,
Quaternion,
}
impl Default for RotationEditorMode {
fn default() -> Self {
Self::EulerDegrees
}
}
impl RotationEditorMode {
fn label(self) -> &'static str {
match self {
Self::EulerDegrees => "Euler (degrees)",
Self::EulerRadians => "Euler (radians)",
Self::Quaternion => "Quaternion",
}
}
}
pub fn inspect_rotation_dquat(ui: &mut Ui, id_source: impl Hash, rotation: &mut DQuat) -> bool {
let mode_id = ui.make_persistent_id(("rotation_mode", id_source));
let mut mode = ui.ctx().data_mut(|d| {
d.get_temp::<RotationEditorMode>(mode_id)
.unwrap_or_default()
});
ui.horizontal(|ui| {
ui.label("Mode");
egui::ComboBox::from_id_salt(mode_id)
.selected_text(mode.label())
.show_ui(ui, |ui| {
ui.selectable_value(
&mut mode,
RotationEditorMode::EulerDegrees,
RotationEditorMode::EulerDegrees.label(),
);
ui.selectable_value(
&mut mode,
RotationEditorMode::EulerRadians,
RotationEditorMode::EulerRadians.label(),
);
ui.selectable_value(
&mut mode,
RotationEditorMode::Quaternion,
RotationEditorMode::Quaternion.label(),
);
});
});
ui.ctx().data_mut(|d| d.insert_temp(mode_id, mode));
match mode {
RotationEditorMode::EulerDegrees => {
let euler_id = mode_id.with("euler_deg");
let last_quat_id = mode_id.with("euler_deg_last_quat");
let last_quat = ui.ctx().data(|d| d.get_temp::<DQuat>(last_quat_id));
let external_change = last_quat.map_or(true, |q| {
(q.x - rotation.x).abs() > 1e-10
|| (q.y - rotation.y).abs() > 1e-10
|| (q.z - rotation.z).abs() > 1e-10
|| (q.w - rotation.w).abs() > 1e-10
});
let stored_euler = ui.ctx().data(|d| d.get_temp::<[f64; 3]>(euler_id));
let [mut x, mut y, mut z] = if external_change || stored_euler.is_none() {
let (ex, ey, ez) = rotation.to_euler(glam::EulerRot::XYZ);
[ex.to_degrees(), ey.to_degrees(), ez.to_degrees()]
} else {
stored_euler.unwrap()
};
let mut changed = false;
let mut any_dragging = false;
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(200, 80, 80), "X:");
let rx = ui.add(
egui::DragValue::new(&mut x)
.speed(1.0)
.suffix("°")
.fixed_decimals(1),
);
changed |= rx.changed();
any_dragging |= rx.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 200, 80), "Y:");
let ry = ui.add(
egui::DragValue::new(&mut y)
.speed(1.0)
.suffix("°")
.fixed_decimals(1),
);
changed |= ry.changed();
any_dragging |= ry.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 120, 220), "Z:");
let rz = ui.add(
egui::DragValue::new(&mut z)
.speed(1.0)
.suffix("°")
.fixed_decimals(1),
);
changed |= rz.changed();
any_dragging |= rz.dragged();
});
let hint_id = mode_id.with("euler_hint_rad");
if changed {
let new_rot = DQuat::from_euler(
glam::EulerRot::XYZ,
x.to_radians(),
y.to_radians(),
z.to_radians(),
);
ui.ctx().data_mut(|d| {
d.insert_temp(euler_id, [x, y, z]);
d.insert_temp(last_quat_id, new_rot);
d.insert_temp(hint_id, [x.to_radians(), y.to_radians(), z.to_radians()]);
});
*rotation = new_rot;
} else {
ui.ctx().data_mut(|d| {
d.insert_temp(euler_id, [x, y, z]);
d.insert_temp(hint_id, [x.to_radians(), y.to_radians(), z.to_radians()]);
if !any_dragging {
d.insert_temp(last_quat_id, *rotation);
}
});
}
changed
}
RotationEditorMode::EulerRadians => {
let euler_id = mode_id.with("euler_rad");
let last_quat_id = mode_id.with("euler_rad_last_quat");
let last_quat = ui.ctx().data(|d| d.get_temp::<DQuat>(last_quat_id));
let external_change = last_quat.map_or(true, |q| {
(q.x - rotation.x).abs() > 1e-10
|| (q.y - rotation.y).abs() > 1e-10
|| (q.z - rotation.z).abs() > 1e-10
|| (q.w - rotation.w).abs() > 1e-10
});
let stored_euler = ui.ctx().data(|d| d.get_temp::<[f64; 3]>(euler_id));
let [mut x, mut y, mut z] = if external_change || stored_euler.is_none() {
let (ex, ey, ez) = rotation.to_euler(glam::EulerRot::XYZ);
[ex, ey, ez]
} else {
stored_euler.unwrap()
};
let mut changed = false;
let mut any_dragging = false;
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(200, 80, 80), "X:");
let rx = ui.add(egui::DragValue::new(&mut x).speed(0.01).fixed_decimals(3));
changed |= rx.changed();
any_dragging |= rx.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 200, 80), "Y:");
let ry = ui.add(egui::DragValue::new(&mut y).speed(0.01).fixed_decimals(3));
changed |= ry.changed();
any_dragging |= ry.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 120, 220), "Z:");
let rz = ui.add(egui::DragValue::new(&mut z).speed(0.01).fixed_decimals(3));
changed |= rz.changed();
any_dragging |= rz.dragged();
});
let hint_id = mode_id.with("euler_hint_rad");
if changed {
let new_rot = DQuat::from_euler(glam::EulerRot::XYZ, x, y, z);
ui.ctx().data_mut(|d| {
d.insert_temp(euler_id, [x, y, z]);
d.insert_temp(last_quat_id, new_rot);
d.insert_temp(hint_id, [x, y, z]);
});
*rotation = new_rot;
} else {
ui.ctx().data_mut(|d| {
d.insert_temp(euler_id, [x, y, z]);
d.insert_temp(hint_id, [x, y, z]);
if !any_dragging {
d.insert_temp(last_quat_id, *rotation);
}
});
}
changed
}
RotationEditorMode::Quaternion => {
let raw_id = mode_id.with("raw_quat");
let stored = ui.ctx().data(|d| d.get_temp::<[f64; 4]>(raw_id));
let [mut x, mut y, mut z, mut w] =
stored.unwrap_or([rotation.x, rotation.y, rotation.z, rotation.w]);
let mut changed = false;
let mut any_dragging = false;
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(200, 80, 80), "X:");
let rx = ui.add(egui::DragValue::new(&mut x).speed(0.01).fixed_decimals(4));
changed |= rx.changed();
any_dragging |= rx.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 200, 80), "Y:");
let ry = ui.add(egui::DragValue::new(&mut y).speed(0.01).fixed_decimals(4));
changed |= ry.changed();
any_dragging |= ry.dragged();
ui.colored_label(egui::Color32::from_rgb(80, 120, 220), "Z:");
let rz = ui.add(egui::DragValue::new(&mut z).speed(0.01).fixed_decimals(4));
changed |= rz.changed();
any_dragging |= rz.dragged();
ui.colored_label(egui::Color32::from_rgb(220, 180, 80), "W:");
let rw = ui.add(egui::DragValue::new(&mut w).speed(0.01).fixed_decimals(4));
changed |= rw.changed();
any_dragging |= rw.dragged();
});
if any_dragging || changed {
ui.ctx().data_mut(|d| d.insert_temp(raw_id, [x, y, z, w]));
} else {
ui.ctx().data_mut(|d| {
d.insert_temp(raw_id, [rotation.x, rotation.y, rotation.z, rotation.w])
});
}
if changed {
let q = DQuat::from_xyzw(x, y, z, w);
if q.length_squared() > 1e-12 {
*rotation = q.normalize();
}
}
changed
}
}
}
pub fn inspect_rotation_quat(ui: &mut Ui, id_source: impl Hash, rotation: &mut Quat) -> bool {
let mut dquat = rotation.as_dquat();
let changed = inspect_rotation_dquat(ui, id_source, &mut dquat);
if changed {
*rotation = dquat.as_quat();
}
changed
}
/// A type of transform that is attached to all entities. It contains the local and world transforms.
#[derive(Default, Debug, Deserialize, Serialize, Copy, PartialEq, Clone)]
pub struct EntityTransform {
local: Transform,
world: Transform,
#[serde(default)]
animation: Transform,
}
impl EntityTransform {
/// Creates a new [EntityTransform] from a local and world [Transform]
pub fn new(local: Transform, world: Transform) -> Self {
Self {
local,
world,
animation: Transform::default(),
}
}
/// Creates a new [EntityTransform] from a world [Transform] and a default local transform.
///
/// This is best for situations where a local transform is not required.
pub fn new_from_world(world: Transform) -> Self {
Self {
world,
local: Transform::default(),
animation: Transform::default(),
}
}
/// Gets a reference to the local transform
pub fn local(&self) -> &Transform {
&self.local
}
/// Gets a reference to the world transform
pub fn world(&self) -> &Transform {
&self.world
}
/// Gets a mutable reference to the local transform
pub fn local_mut(&mut self) -> &mut Transform {
&mut self.local
}
/// Gets a mutable reference to the world transform
pub fn world_mut(&mut self) -> &mut Transform {
&mut self.world
}
/// Combines both transforms into one, propagating the local transform
/// to the world transform and returning a uniform [Transform]
pub fn sync(&self) -> Transform {
let combined = self.world.matrix() * self.local.matrix() * self.animation.matrix();
let (scale, rotation, position) = combined.to_scale_rotation_translation();
Transform {
position,
rotation,
scale,
}
}
/// Applies a node transform for TRS animation as an absolute local transform.
pub fn apply_animation(&mut self, node_transform: &NodeTransform) {
self.animation.position = node_transform.translation.as_dvec3();
self.animation.rotation = node_transform.rotation.as_dquat();
self.animation.scale = node_transform.scale.as_dvec3();
}
/// Clears the animation contribution to the local transform.
pub fn clear_animation(&mut self) {
self.animation = Transform::default();
}
}
/// A type that represents a position, rotation and scale of an entity
///
/// This type is the most primitive model, as it implements most traits.
#[repr(C)]
#[derive(Debug, Clone, Deserialize, Serialize, Copy, PartialEq)]
pub struct Transform {
/// The position of the entity as [`DVec3`]
pub position: DVec3,
/// The rotation of the entity as [`DQuat`]
pub rotation: DQuat,
/// The scale of the entity as [`DVec3`]
pub scale: DVec3,
}
impl Default for Transform {
fn default() -> Self {
Self {
position: DVec3::ZERO,
rotation: DQuat::IDENTITY,
scale: DVec3::ONE,
}
}
}
impl Transform {
/// Creates a new default instance of Transform
pub fn new() -> Self {
Self::default()
}
/// Applies an offset, typically used for physics based calculations where [self.scale]
/// is not required.
pub fn with_offset(&self, translation: [f32; 3], rotation: [f32; 3]) -> Self {
let offset_pos = Vec3::from(translation).as_dvec3();
let offset_rot =
Quat::from_euler(glam::EulerRot::XYZ, rotation[0], rotation[1], rotation[2]).as_dquat();
Transform {
position: self.position + self.rotation * offset_pos,
rotation: self.rotation * offset_rot,
scale: self.scale,
}
}
/// Returns the matrix of the model
pub fn matrix(&self) -> DMat4 {
DMat4::from_scale_rotation_translation(self.scale, self.rotation, self.position)
}
/// Rotates the model on its X axis by a certain angle
pub fn rotate_x(&mut self, angle_rad: f64) {
self.rotation *= DQuat::from_euler(glam::EulerRot::XYZ, angle_rad, 0.0, 0.0);
}
/// Rotates the model on its Y axis by a certain value
pub fn rotate_y(&mut self, angle_rad: f64) {
self.rotation *= DQuat::from_euler(glam::EulerRot::XYZ, 0.0, angle_rad, 0.0);
}
/// Rotates the model on its Z axis by a certain value
pub fn rotate_z(&mut self, angle_rad: f64) {
self.rotation *= DQuat::from_euler(glam::EulerRot::XYZ, 0.0, 0.0, angle_rad);
}
/// Translates (moves) the model by a translation [`DVec3`].
///
/// Doesn't replace the position value,
/// it adds the value.
pub fn translate(&mut self, translation: DVec3) {
self.position += translation;
}
/// Scales the model by a scale value.
///
/// Doesn't replace the scale value, just multiplies.
pub fn scale(&mut self, scale: DVec3) {
self.scale *= scale;
}
pub fn inspect(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| {
ui.label("Position:");
});
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(200, 80, 80), "X:");
ui.add(
egui::DragValue::new(&mut self.position.x)
.speed(0.1)
.fixed_decimals(2),
);
ui.colored_label(egui::Color32::from_rgb(80, 200, 80), "Y:");
ui.add(
egui::DragValue::new(&mut self.position.y)
.speed(0.1)
.fixed_decimals(2),
);
ui.colored_label(egui::Color32::from_rgb(80, 120, 220), "Z:");
ui.add(
egui::DragValue::new(&mut self.position.z)
.speed(0.1)
.fixed_decimals(2),
);
});
if ui.button("Reset Position").clicked() {
self.position = DVec3::ZERO;
}
ui.add_space(4.0);
ui.horizontal(|ui| {
ui.label("Rotation:");
});
let _ = inspect_rotation_dquat(ui, "transform_rotation", &mut self.rotation);
if ui.button("Reset Rotation").clicked() {
self.rotation = DQuat::IDENTITY;
}
ui.add_space(4.0);
// Scale
ui.horizontal(|ui| {
ui.label("Scale:");
});
ui.horizontal(|ui| {
ui.colored_label(egui::Color32::from_rgb(200, 80, 80), "X:");
ui.add(
egui::DragValue::new(&mut self.scale.x)
.speed(0.01)
.fixed_decimals(3),
);
ui.colored_label(egui::Color32::from_rgb(80, 200, 80), "Y:");
ui.add(
egui::DragValue::new(&mut self.scale.y)
.speed(0.01)
.fixed_decimals(3),
);
ui.colored_label(egui::Color32::from_rgb(80, 120, 220), "Z:");
ui.add(
egui::DragValue::new(&mut self.scale.z)
.speed(0.01)
.fixed_decimals(3),
);
});
if ui.button("Reset Scale").clicked() {
self.scale = DVec3::ONE;
}
}
}
#[derive(Clone)]
/// A renderer for meshes and materials related to a model.
///
/// It includes the instances as well as a handle. The reason for a handle is so the model being rendered can be swapped
/// to something else without deleting the entire renderer. Also saves memory by rendering anything that has been loaded.
pub struct MeshRenderer {
import_scale: f32,
pub is_selected: bool,
handle: Handle<Model>,
pub instance: Instance,
previous_matrix: DMat4,
pub material_snapshot: HashMap<String, Material>,
}
impl MeshRenderer {
pub fn from_handle(model: Handle<Model>) -> Self {
let mut hm = HashMap::new();
let material_snapshot = ASSET_REGISTRY
.read()
.get_model(model)
.map(|m| m.materials.clone())
.unwrap_or_default();
for m in material_snapshot {
hm.insert(m.name.clone(), m);
}
Self {
handle: model,
instance: Instance::default(),
previous_matrix: DMat4::IDENTITY,
import_scale: 1.0,
is_selected: false,
material_snapshot: hm,
}
}
pub async fn from_path(
graphics: Arc<SharedGraphicsContext>,
path: impl AsRef<Path>,
label: Option<&str>,
) -> anyhow::Result<Self> {
let path = path.as_ref().to_path_buf();
let handle = Model::load_from_memory_raw(
graphics.clone(),
std::fs::read(&path)?,
Some(ResourceReference::from_path(&path)?),
label,
ASSET_REGISTRY.clone(),
)
.await?;
Ok(Self {
handle,
instance: Instance::default(),
import_scale: 1.0,
previous_matrix: DMat4::IDENTITY,
is_selected: false,
material_snapshot: Default::default(),
})
}
pub fn update(&mut self, transform: &Transform) {
puffin::profile_function!();
let scale = transform.scale * glam::DVec3::splat(self.import_scale as f64);
let current_matrix =
DMat4::from_scale_rotation_translation(scale, transform.rotation, transform.position);
if self.previous_matrix != current_matrix {
self.instance = Instance::from_matrix(current_matrix);
self.previous_matrix = current_matrix;
}
}
pub fn set_import_scale(&mut self, scale: f32) {
self.import_scale = scale;
}
pub fn import_scale(&self) -> f32 {
self.import_scale
}
pub fn set_model(&mut self, model: Handle<Model>) {
self.handle = model;
}
pub fn model(&self) -> Handle<Model> {
self.handle
}
pub fn mutate_material(&mut self, material_name: &str, f: impl FnOnce(&mut Material)) {
self.material_snapshot
.entry(material_name.to_string())
.and_modify(f);
}
pub fn is_texture_attached(&self, texture: Handle<Texture>) -> bool {
let registry = ASSET_REGISTRY.read();
if let Some(model) = registry.get_model(self.handle) {
for material in &model.materials {
if material.diffuse_texture == texture {
return true;
}
if material.normal_texture == Some(texture) {
return true;
}
if material.emissive_texture == Some(texture) {
return true;
}
if material.metallic_roughness_texture == Some(texture) {
return true;
}
if material.occlusion_texture == Some(texture) {
return true;
}
}
}
false
}
pub fn reset_texture_override(&mut self) {
let mut hm = HashMap::new();
let material_snapshot = ASSET_REGISTRY
.read()
.get_model(self.handle)
.map(|m| m.materials.clone())
.unwrap_or_default();
for m in material_snapshot {
hm.insert(m.name.clone(), m);
}
self.material_snapshot = hm;
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct ModelUniform {
model: [[f32; 4]; 4],
}
impl ModelUniform {
pub fn new() -> Self {
Self {
model: Mat4::IDENTITY.to_cols_array_2d(),
}
}
}