Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/3d/gizmos/gizmo_3d_helper.cpp
21807 views
1
/**************************************************************************/
2
/* gizmo_3d_helper.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "gizmo_3d_helper.h"
32
33
#include "core/input/input.h"
34
#include "core/math/geometry_3d.h"
35
#include "editor/editor_undo_redo_manager.h"
36
#include "editor/scene/3d/node_3d_editor_plugin.h"
37
#include "scene/3d/camera_3d.h"
38
39
void Gizmo3DHelper::initialize_handle_action(const Variant &p_initial_value, const Transform3D &p_initial_transform) {
40
initial_value = p_initial_value;
41
initial_transform = p_initial_transform;
42
}
43
44
void Gizmo3DHelper::get_segment(Camera3D *p_camera, const Point2 &p_point, Vector3 *r_segment) {
45
Transform3D gt = initial_transform;
46
Transform3D gi = gt.affine_inverse();
47
48
Vector3 ray_from = p_camera->project_ray_origin(p_point);
49
Vector3 ray_dir = p_camera->project_ray_normal(p_point);
50
51
r_segment[0] = gi.xform(ray_from);
52
r_segment[1] = gi.xform(ray_from + ray_dir * 4096);
53
}
54
55
Vector<Vector3> Gizmo3DHelper::box_get_handles(const Vector3 &p_box_size) {
56
Vector<Vector3> handles;
57
for (int i = 0; i < 3; i++) {
58
Vector3 ax;
59
ax[i] = p_box_size[i] / 2;
60
handles.push_back(ax);
61
handles.push_back(-ax);
62
}
63
return handles;
64
}
65
66
String Gizmo3DHelper::box_get_handle_name(int p_id) const {
67
switch (p_id) {
68
case 0:
69
case 1:
70
return "Size X";
71
case 2:
72
case 3:
73
return "Size Y";
74
case 4:
75
case 5:
76
return "Size Z";
77
}
78
return "";
79
}
80
81
void Gizmo3DHelper::box_set_handle(const Vector3 p_segment[2], int p_id, Vector3 &r_box_size, Vector3 &r_box_position) {
82
int axis = p_id / 2;
83
int sign = p_id % 2 * -2 + 1;
84
85
Vector3 initial_size = initial_value;
86
float neg_end = initial_size[axis] * -0.5;
87
float pos_end = initial_size[axis] * 0.5;
88
89
Vector3 axis_segment[2] = { Vector3(), Vector3() };
90
axis_segment[0][axis] = 4096.0;
91
axis_segment[1][axis] = -4096.0;
92
Vector3 ra, rb;
93
Geometry3D::get_closest_points_between_segments(axis_segment[0], axis_segment[1], p_segment[0], p_segment[1], ra, rb);
94
95
// Calculate new size.
96
r_box_size = initial_size;
97
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
98
r_box_size[axis] = ra[axis] * sign * 2;
99
} else {
100
r_box_size[axis] = sign > 0 ? ra[axis] - neg_end : pos_end - ra[axis];
101
}
102
103
// Snap to grid.
104
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
105
r_box_size[axis] = Math::snapped(r_box_size[axis], Node3DEditor::get_singleton()->get_translate_snap());
106
}
107
r_box_size[axis] = MAX(r_box_size[axis], 0.001);
108
109
// Adjust position.
110
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
111
r_box_position = initial_transform.get_origin();
112
} else {
113
if (sign > 0) {
114
pos_end = neg_end + r_box_size[axis];
115
} else {
116
neg_end = pos_end - r_box_size[axis];
117
}
118
119
Vector3 offset;
120
offset[axis] = (pos_end + neg_end) * 0.5;
121
r_box_position = initial_transform.xform(offset);
122
}
123
}
124
125
void Gizmo3DHelper::box_commit_handle(const String &p_action_name, bool p_cancel, Object *p_position_object, Object *p_size_object, const StringName &p_position_property, const StringName &p_size_property) {
126
if (!p_size_object) {
127
p_size_object = p_position_object;
128
}
129
130
if (p_cancel) {
131
p_size_object->set(p_size_property, initial_value);
132
p_position_object->set(p_position_property, initial_transform.get_origin());
133
return;
134
}
135
136
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
137
ur->create_action(p_action_name);
138
ur->add_do_property(p_size_object, p_size_property, p_size_object->get(p_size_property));
139
ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));
140
ur->add_undo_property(p_size_object, p_size_property, initial_value);
141
ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());
142
ur->commit_action();
143
}
144
145
Vector<Vector3> Gizmo3DHelper::cylinder_get_handles(real_t p_height, real_t p_radius) {
146
Vector<Vector3> handles;
147
handles.push_back(Vector3(0, p_height * 0.5, 0));
148
handles.push_back(Vector3(0, p_height * -0.5, 0));
149
handles.push_back(Vector3(p_radius, 0, 0));
150
return handles;
151
}
152
153
String Gizmo3DHelper::_cylinder_or_capsule_or_cone_frustum_get_handle_name(int p_id) const {
154
if (p_id < 2) {
155
return "Height";
156
} else {
157
return "Radius";
158
}
159
}
160
161
void Gizmo3DHelper::_cylinder_or_capsule_or_cone_frustum_set_handle(const Vector3 p_segment[2], int p_id, real_t &r_height, real_t &r_radius_top, real_t &r_radius_bottom, Vector3 &r_position, bool p_is_capsule, bool p_is_frustum) {
162
ERR_FAIL_COND(p_is_capsule && p_is_frustum);
163
164
real_t initial_radius_top, initial_radius_bottom, initial_height;
165
if (p_is_frustum) {
166
initial_radius_top = initial_value.operator Vector3().x;
167
initial_radius_bottom = initial_value.operator Vector3().y;
168
initial_height = initial_value.operator Vector3().z;
169
} else {
170
initial_radius_top = initial_value.operator Vector2().x;
171
initial_radius_bottom = initial_radius_top;
172
initial_height = initial_value.operator Vector2().y;
173
}
174
175
int sign = p_id == 1 ? -1 : 1;
176
int axis = p_id >= 2 ? 0 : 1;
177
178
Vector3 axis_vector;
179
axis_vector[axis] = sign;
180
Vector3 ra, rb;
181
Geometry3D::get_closest_points_between_segments(axis_vector * -4096, axis_vector * 4096, p_segment[0], p_segment[1], ra, rb);
182
float d = axis_vector.dot(ra);
183
184
// Snap to grid.
185
if (Node3DEditor::get_singleton()->is_snap_enabled()) {
186
d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
187
}
188
189
if (p_id < 2) {
190
// Adjust height.
191
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
192
r_height = d * 2.0;
193
} else {
194
r_height = (initial_height * 0.5) + d;
195
}
196
197
if (r_height < 0.001) {
198
r_height = 0.001;
199
}
200
201
// Adjust position.
202
if (Input::get_singleton()->is_key_pressed(Key::ALT)) {
203
r_position = initial_transform.get_origin();
204
} else {
205
Vector3 offset;
206
offset[axis] = (r_height - initial_height) * 0.5 * sign;
207
r_position = initial_transform.xform(offset);
208
}
209
210
if (p_is_capsule) {
211
r_radius_top = MIN(initial_radius_top, r_height / 2.0);
212
r_radius_bottom = r_radius_top;
213
} else {
214
r_radius_top = initial_radius_top;
215
r_radius_bottom = initial_radius_bottom;
216
}
217
} else {
218
// Adjust radius.
219
if (d < 0.001) {
220
d = 0.001;
221
}
222
if (!p_is_frustum) {
223
r_radius_top = d;
224
r_radius_bottom = d;
225
} else {
226
real_t diff = d - (initial_radius_bottom + initial_radius_top) / 2.0;
227
diff = MAX(MAX(-initial_radius_bottom + 0.001, -initial_radius_top + 0.001), diff);
228
r_radius_top = initial_radius_top + diff;
229
r_radius_bottom = initial_radius_bottom + diff;
230
}
231
232
r_position = initial_transform.get_origin();
233
234
if (p_is_capsule) {
235
r_height = MAX(initial_height, r_radius_top * 2.0);
236
} else {
237
r_height = initial_height;
238
}
239
}
240
}
241
242
void Gizmo3DHelper::cylinder_commit_handle(int p_id, const String &p_radius_action_name, const String &p_height_action_name, bool p_cancel, Object *p_position_object, Object *p_height_object, Object *p_radius_object, const StringName &p_position_property, const StringName &p_height_property, const StringName &p_radius_property) {
243
if (!p_height_object) {
244
p_height_object = p_position_object;
245
}
246
if (!p_radius_object) {
247
p_radius_object = p_position_object;
248
}
249
250
if (p_cancel) {
251
p_radius_object->set(p_radius_property, initial_value.operator Vector2().x);
252
p_height_object->set(p_height_property, initial_value.operator Vector2().y);
253
p_position_object->set(p_position_property, initial_transform.get_origin());
254
return;
255
}
256
257
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
258
ur->create_action(p_id < 2 ? p_height_action_name : p_radius_action_name);
259
ur->add_do_property(p_radius_object, p_radius_property, p_radius_object->get(p_radius_property));
260
ur->add_undo_property(p_radius_object, p_radius_property, initial_value.operator Vector2().x);
261
ur->add_do_property(p_height_object, p_height_property, p_height_object->get(p_height_property));
262
ur->add_undo_property(p_height_object, p_height_property, initial_value.operator Vector2().y);
263
ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));
264
ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());
265
ur->commit_action();
266
}
267
268
Vector<Vector3> Gizmo3DHelper::cone_frustum_get_handles(real_t p_height, real_t p_radius_top, real_t p_radius_bottom) {
269
Vector<Vector3> handles;
270
handles.push_back(Vector3(0, p_height * 0.5, 0));
271
handles.push_back(Vector3(0, p_height * -0.5, 0));
272
handles.push_back(Vector3((p_radius_top + p_radius_bottom) / 2.0, 0, 0));
273
return handles;
274
}
275
276
void Gizmo3DHelper::cone_frustum_commit_handle(int p_id, const String &p_radius_action_name, const String &p_height_action_name, bool p_cancel, Object *p_position_object, Object *p_height_object, Object *p_radius_top_object, Object *p_radius_bottom_object, const StringName &p_position_property, const StringName &p_height_property, const StringName &p_radius_top_property, const StringName &p_radius_bottom_property) {
277
if (!p_height_object) {
278
p_height_object = p_position_object;
279
}
280
if (!p_radius_top_object) {
281
p_radius_top_object = p_position_object;
282
}
283
if (!p_radius_bottom_object) {
284
p_radius_bottom_object = p_position_object;
285
}
286
287
if (p_cancel) {
288
p_radius_top_object->set(p_radius_top_property, initial_value.operator Vector3().x);
289
p_radius_bottom_object->set(p_radius_bottom_property, initial_value.operator Vector3().y);
290
p_height_object->set(p_height_property, initial_value.operator Vector3().z);
291
p_position_object->set(p_position_property, initial_transform.get_origin());
292
return;
293
}
294
295
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
296
ur->create_action(p_id < 2 ? p_height_action_name : p_radius_action_name);
297
ur->add_do_property(p_radius_top_object, p_radius_top_property, p_radius_top_object->get(p_radius_top_property));
298
ur->add_do_property(p_radius_bottom_object, p_radius_bottom_property, p_radius_bottom_object->get(p_radius_bottom_property));
299
ur->add_undo_property(p_radius_top_object, p_radius_top_property, initial_value.operator Vector3().x);
300
ur->add_undo_property(p_radius_bottom_object, p_radius_bottom_property, initial_value.operator Vector3().y);
301
ur->add_do_property(p_height_object, p_height_property, p_height_object->get(p_height_property));
302
ur->add_undo_property(p_height_object, p_height_property, initial_value.operator Vector3().z);
303
ur->add_do_property(p_position_object, p_position_property, p_position_object->get(p_position_property));
304
ur->add_undo_property(p_position_object, p_position_property, initial_transform.get_origin());
305
ur->commit_action();
306
}
307
308