forked from urholaukkarinen/transform-gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
284 lines (242 loc) · 10.5 KB
/
main.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
use std::f32::consts::FRAC_PI_4;
use egui::color_picker::Alpha;
use egui::{pos2, Align2, Color32, LayerId, TextStyle, Ui, Widget};
use macroquad::prelude::*;
use egui_gizmo::{
Gizmo, GizmoMode, GizmoOrientation, GizmoResult, GizmoVisuals, DEFAULT_SNAP_ANGLE,
DEFAULT_SNAP_DISTANCE,
};
const SOURCE_URL: &str = "https://github.com/urholaukkarinen/egui-gizmo/blob/main/demo/src/main.rs";
#[macroquad::main("3D")]
async fn main() {
let texture =
Texture2D::from_file_with_format(include_bytes!("../crate.png"), Some(ImageFormat::Png));
let mut camera_angle: f32 = -FRAC_PI_4;
let mut camera_y = 5.0;
let target_pos = vec3(0., 0., 0.);
let mut model_matrix = Mat4::from_scale_rotation_translation(
vec3(2.0, 2.0, 2.0),
Quat::from_axis_angle(Vec3::Y, 0.0),
target_pos,
);
let mut gizmo_mode = GizmoMode::Rotate;
let mut gizmo_orientation = GizmoOrientation::Global;
let mut last_gizmo_response = None;
let mut stroke_width = 4.0;
let mut gizmo_size = 75.0;
let mut custom_highlight_color = false;
let mut highlight_color = Color32::GOLD;
let mut x_color = Color32::from_rgb(255, 0, 148);
let mut y_color = Color32::from_rgb(148, 255, 0);
let mut z_color = Color32::from_rgb(0, 148, 255);
let mut s_color = Color32::WHITE;
let mut inactive_alpha = 0.5;
let mut highlight_alpha = 1.0;
loop {
// Rotate camera around the object with A/D
if is_key_down(KeyCode::A) {
camera_angle += 0.01;
} else if is_key_down(KeyCode::D) {
camera_angle -= 0.01;
}
// Move camera up/down with W/S
if is_key_down(KeyCode::W) {
camera_y += 0.01;
} else if is_key_down(KeyCode::S) {
camera_y -= 0.01;
}
clear_background(BLACK);
let camera = Camera3D {
position: target_pos
+ vec3(camera_angle.cos() * 5.0, camera_y, camera_angle.sin() * 5.0),
up: vec3(0., 1., 0.),
target: target_pos,
..Default::default()
};
set_camera(&camera);
draw_grid(20, 1., LIGHTGRAY, DARKGRAY);
let aspect = camera
.aspect
.unwrap_or_else(|| screen_width() / screen_height());
let projection_matrix = Mat4::perspective_rh_gl(camera.fovy, aspect, 0.01, 1000.0);
let view_matrix = Mat4::look_at_rh(camera.position, camera.target, camera.up);
egui_macroquad::ui(|egui_ctx| {
egui::Window::new("Settings")
.resizable(false)
.show(egui_ctx, |ui| {
egui::ComboBox::from_label("Mode")
.selected_text(format!("{:?}", gizmo_mode))
.show_ui(ui, |ui| {
ui.selectable_value(&mut gizmo_mode, GizmoMode::Rotate, "Rotate");
ui.selectable_value(&mut gizmo_mode, GizmoMode::Translate, "Translate");
ui.selectable_value(&mut gizmo_mode, GizmoMode::Scale, "Scale");
});
ui.end_row();
egui::ComboBox::from_label("Orientation")
.selected_text(format!("{:?}", gizmo_orientation))
.show_ui(ui, |ui| {
ui.selectable_value(
&mut gizmo_orientation,
GizmoOrientation::Global,
"Global",
);
ui.selectable_value(
&mut gizmo_orientation,
GizmoOrientation::Local,
"Local",
);
});
ui.end_row();
ui.separator();
egui::Slider::new(&mut gizmo_size, 10.0..=500.0)
.text("Gizmo size")
.ui(ui);
egui::Slider::new(&mut stroke_width, 0.1..=10.0)
.text("Stroke width")
.ui(ui);
egui::Slider::new(&mut inactive_alpha, 0.0..=1.0)
.text("Inactive alpha")
.ui(ui);
egui::Slider::new(&mut highlight_alpha, 0.0..=1.0)
.text("Highlighted alpha")
.ui(ui);
ui.horizontal(|ui| {
egui::color_picker::color_edit_button_srgba(
ui,
&mut highlight_color,
Alpha::Opaque,
);
egui::Checkbox::new(&mut custom_highlight_color, "Custom highlight color")
.ui(ui);
});
ui.horizontal(|ui| {
egui::color_picker::color_edit_button_srgba(
ui,
&mut x_color,
Alpha::Opaque,
);
egui::Label::new("X axis color").wrap(false).ui(ui);
});
ui.horizontal(|ui| {
egui::color_picker::color_edit_button_srgba(
ui,
&mut y_color,
Alpha::Opaque,
);
egui::Label::new("Y axis color").wrap(false).ui(ui);
});
ui.horizontal(|ui| {
egui::color_picker::color_edit_button_srgba(
ui,
&mut z_color,
Alpha::Opaque,
);
egui::Label::new("Z axis color").wrap(false).ui(ui);
});
ui.horizontal(|ui| {
egui::color_picker::color_edit_button_srgba(
ui,
&mut s_color,
Alpha::Opaque,
);
egui::Label::new("Screen axis color").wrap(false).ui(ui);
});
ui.end_row();
egui::Hyperlink::from_label_and_url("(source code)", SOURCE_URL).ui(ui);
});
egui::Area::new("Viewport")
.fixed_pos((0.0, 0.0))
.show(egui_ctx, |ui| {
ui.with_layer_id(LayerId::background(), |ui| {
// Snapping is enabled with ctrl key.
let snapping = ui.input().modifiers.command;
// Snap angle to use for rotation when snapping is enabled.
// Smaller snap angle is used when shift key is pressed.
let snap_angle = if ui.input().modifiers.shift {
DEFAULT_SNAP_ANGLE / 2.0
} else {
DEFAULT_SNAP_ANGLE
};
// Snap distance to use for translation when snapping is enabled.
// Smaller snap distance is used when shift key is pressed.
let snap_distance = if ui.input().modifiers.shift {
DEFAULT_SNAP_DISTANCE / 2.0
} else {
DEFAULT_SNAP_DISTANCE
};
let visuals = GizmoVisuals {
stroke_width,
x_color,
y_color,
z_color,
s_color,
inactive_alpha,
highlight_alpha,
highlight_color: if custom_highlight_color {
Some(highlight_color)
} else {
None
},
gizmo_size,
};
let gizmo = Gizmo::new("My gizmo")
.view_matrix(view_matrix.to_cols_array_2d())
.projection_matrix(projection_matrix.to_cols_array_2d())
.model_matrix(model_matrix.to_cols_array_2d())
.mode(gizmo_mode)
.orientation(gizmo_orientation)
.snapping(snapping)
.snap_angle(snap_angle)
.snap_distance(snap_distance)
.visuals(visuals);
last_gizmo_response = gizmo.interact(ui);
if let Some(gizmo_response) = last_gizmo_response {
// Response contains status of the active gizmo,
// including an updated model matrix.
model_matrix = Mat4::from_cols_array_2d(&gizmo_response.transform);
show_gizmo_status(ui, gizmo_response);
}
instructions_text(ui);
});
});
});
unsafe { get_internal_gl().quad_gl }.push_model_matrix(model_matrix);
draw_cube(
vec3(0.0, 0.0, 0.0),
vec3(1.0, 1.0, 1.0),
Some(texture),
GRAY,
);
egui_macroquad::draw();
next_frame().await
}
}
fn instructions_text(ui: &Ui) {
let rect = ui.clip_rect();
ui.painter().text(
pos2(rect.right() - 10.0, rect.bottom() - 10.0),
Align2::RIGHT_BOTTOM,
"Move camera with (A, D, W, S)\n\
Toggle snapping with Ctrl & Shift",
TextStyle::Body,
Color32::GRAY,
);
}
fn show_gizmo_status(ui: &Ui, response: GizmoResult) {
let length = Vec3::from(response.value).length();
let text = match response.mode {
GizmoMode::Rotate => format!("{:.1}°, {:.2} rad", length.to_degrees(), length),
GizmoMode::Translate | GizmoMode::Scale => format!(
"dX: {:.2}, dY: {:.2}, dZ: {:.2}",
response.value[0], response.value[1], response.value[2]
),
};
let rect = ui.clip_rect();
ui.painter().text(
pos2(rect.left() + 10.0, rect.bottom() - 10.0),
Align2::LEFT_BOTTOM,
text,
TextStyle::Heading,
Color32::WHITE,
);
}