Debian Codegraph @

scene/3d/path_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* path_3d.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 "path_3d.h"
32
33 #include "scene/resources/mesh.h"
34
35 Path3D::Path3D() {
36 SceneTree *st = SceneTree::get_singleton();
37 if (st && st->is_debugging_paths_hint()) {
38 debug_instance = RS::get_singleton()->instance_create();
39 set_notify_transform(true);
40 _update_debug_mesh();
41 }
42 }
43
44 Path3D::~Path3D() {
45 if (debug_instance.is_valid()) {
46 ERR_FAIL_NULL(RenderingServer::get_singleton());
47 RS::get_singleton()->free_rid(debug_instance);
48 }
49 if (debug_mesh.is_valid()) {
50 ERR_FAIL_NULL(RenderingServer::get_singleton());
51 RS::get_singleton()->free_rid(debug_mesh->get_rid());
52 }
53 }
54
55 void Path3D::set_update_callback(Callable p_callback) {
56 update_callback = p_callback;
57 }
58
59 void Path3D::_notification(int p_what) {
60 switch (p_what) {
61 case NOTIFICATION_ENTER_TREE: {
62 SceneTree *st = SceneTree::get_singleton();
63 if (st && st->is_debugging_paths_hint()) {
64 _update_debug_mesh();
65 }
66 } break;
67
68 case NOTIFICATION_EXIT_TREE: {
69 SceneTree *st = SceneTree::get_singleton();
70 if (st && st->is_debugging_paths_hint()) {
71 RS::get_singleton()->instance_set_visible(debug_instance, false);
72 }
73 } break;
74
75 case NOTIFICATION_TRANSFORM_CHANGED: {
76 if (is_inside_tree()) {
77 if (debug_instance.is_valid()) {
78 RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
79 }
80
81 update_callback.call();
82 }
83 } break;
84 }
85 }
86
87 void Path3D::_update_debug_mesh() {
88 SceneTree *st = SceneTree::get_singleton();
89 if (!(st && st->is_debugging_paths_hint())) {
90 return;
91 }
92
93 if (debug_mesh.is_null()) {
94 debug_mesh.instantiate();
95 }
96
97 if (curve.is_null()) {
98 RS::get_singleton()->instance_set_visible(debug_instance, false);
99 return;
100 }
101 if (curve->get_point_count() < 2) {
102 RS::get_singleton()->instance_set_visible(debug_instance, false);
103 return;
104 }
105
106 real_t interval = 0.1;
107 const real_t length = curve->get_baked_length();
108
109 if (length <= CMP_EPSILON) {
110 RS::get_singleton()->instance_set_visible(debug_instance, false);
111 return;
112 }
113
114 const int sample_count = int(length / interval) + 2;
115 interval = length / (sample_count - 1);
116
117 Vector<Vector3> ribbon;
118 ribbon.resize(sample_count);
119 Vector3 *ribbon_ptr = ribbon.ptrw();
120
121 Vector<Vector3> bones;
122 bones.resize(sample_count * 4);
123 Vector3 *bones_ptr = bones.ptrw();
124
125 for (int i = 0; i < sample_count; i++) {
126 const Transform3D r = curve->sample_baked_with_rotation(i * interval, true, true);
127
128 const Vector3 p1 = r.origin;
129 const Vector3 side = r.basis.get_column(0);
130 const Vector3 up = r.basis.get_column(1);
131 const Vector3 forward = r.basis.get_column(2);
132
133 // Path3D as a ribbon.
134 ribbon_ptr[i] = p1;
135
136 if (i % 4 == 0) {
137 // Draw fish bone every 4 points to reduce visual noise and performance impact
138 // (compared to drawing it for every point).
139 const Vector3 p_left = p1 + (side + forward - up * 0.3) * 0.06;
140 const Vector3 p_right = p1 + (-side + forward - up * 0.3) * 0.06;
141
142 const int bone_idx = i * 4;
143
144 bones_ptr[bone_idx] = p1;
145 bones_ptr[bone_idx + 1] = p_left;
146 bones_ptr[bone_idx + 2] = p1;
147 bones_ptr[bone_idx + 3] = p_right;
148 }
149 }
150
151 Array ribbon_array;
152 ribbon_array.resize(Mesh::ARRAY_MAX);
153 ribbon_array[Mesh::ARRAY_VERTEX] = ribbon;
154
155 Array bone_array;
156 bone_array.resize(Mesh::ARRAY_MAX);
157 bone_array[Mesh::ARRAY_VERTEX] = bones;
158
159 _update_debug_path_material();
160
161 debug_mesh->clear_surfaces();
162 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINE_STRIP, ribbon_array);
163 debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, bone_array);
164 debug_mesh->surface_set_material(0, debug_material);
165 debug_mesh->surface_set_material(1, debug_material);
166
167 RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid());
168 if (is_inside_tree()) {
169 RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario());
170 RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform());
171 RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree());
172 }
173 }
174
175 void Path3D::set_debug_custom_color(const Color &p_color) {
176 debug_custom_color = p_color;
177 _update_debug_path_material();
178 }
179
180 Ref<StandardMaterial3D> Path3D::get_debug_material() {
181 return debug_material;
182 }
183
184 const Color &Path3D::get_debug_custom_color() const {
185 return debug_custom_color;
186 }
187
188 void Path3D::_update_debug_path_material() {
189 SceneTree *st = SceneTree::get_singleton();
190 if (!debug_material.is_valid()) {
191 Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);
192 debug_material = material;
193
194 material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
195 material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
196 material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
197 material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
198 material->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
199 }
200
201 Color color = debug_custom_color;
202 if (color == Color(0.0, 0.0, 0.0)) {
203 // Use the default debug path color defined in the Project Settings.
204 color = st->get_debug_paths_color();
205 }
206
207 get_debug_material()->set_albedo(color);
208 emit_signal(SNAME("debug_color_changed"));
209 }
210
211 void Path3D::_curve_changed() {
212 if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
213 update_gizmos();
214 }
215 if (is_inside_tree()) {
216 emit_signal(SNAME("curve_changed"));
217 }
218
219 // Update the configuration warnings of all children of type PathFollow
220 // previously used for PathFollowOriented (now enforced orientation is done in PathFollow). Also trigger transform update on PathFollow3Ds in deferred mode.
221 if (is_inside_tree()) {
222 for (int i = 0; i < get_child_count(); i++) {
223 PathFollow3D *child = Object::cast_to<PathFollow3D>(get_child(i));
224 if (child) {
225 child->update_configuration_warnings();
226 child->update_transform();
227 }
228 }
229 }
230 SceneTree *st = SceneTree::get_singleton();
231 if (st && st->is_debugging_paths_hint()) {
232 _update_debug_mesh();
233 }
234 }
235
236 void Path3D::set_curve(const Ref<Curve3D> &p_curve) {
237 if (curve.is_valid()) {
238 curve->disconnect_changed(callable_mp(this, &Path3D::_curve_changed));
239 }
240
241 curve = p_curve;
242
243 if (curve.is_valid()) {
244 curve->connect_changed(callable_mp(this, &Path3D::_curve_changed));
245 }
246 _curve_changed();
247 }
248
249 Ref<Curve3D> Path3D::get_curve() const {
250 return curve;
251 }
252
253 void Path3D::_bind_methods() {
254 ClassDB::bind_method(D_METHOD("set_curve", "curve"), &Path3D::set_curve);
255 ClassDB::bind_method(D_METHOD("get_curve"), &Path3D::get_curve);
256
257 ClassDB::bind_method(D_METHOD("set_debug_custom_color", "debug_custom_color"), &Path3D::set_debug_custom_color);
258 ClassDB::bind_method(D_METHOD("get_debug_custom_color"), &Path3D::get_debug_custom_color);
259
260 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve3D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve", "get_curve");
261
262 ADD_GROUP("Debug Shape", "debug_");
263 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_custom_color"), "set_debug_custom_color", "get_debug_custom_color");
264
265 ADD_SIGNAL(MethodInfo("curve_changed"));
266 ADD_SIGNAL(MethodInfo("debug_color_changed"));
267 }
268
269 void PathFollow3D::update_transform() {
270 if (!path) {
271 return;
272 }
273
274 Ref<Curve3D> c = path->get_curve();
275 if (c.is_null()) {
276 return;
277 }
278
279 real_t bl = c->get_baked_length();
280 if (bl == 0.0) {
281 return;
282 }
283
284 Transform3D t;
285
286 if (rotation_mode == ROTATION_NONE) {
287 Vector3 pos = c->sample_baked(progress, cubic);
288 t.origin = pos;
289 } else {
290 t = c->sample_baked_with_rotation(progress, cubic, false);
291 Vector3 tangent = -t.basis.get_column(2); // Retain tangent for applying tilt.
292 t = PathFollow3D::correct_posture(t, rotation_mode);
293
294 // Switch Z+ and Z- if necessary.
295 if (use_model_front) {
296 t.basis *= Basis::from_scale(Vector3(-1.0, 1.0, -1.0));
297 }
298
299 // Apply tilt *after* correct_posture().
300 if (tilt_enabled) {
301 const real_t tilt = c->sample_baked_tilt(progress);
302
303 const Basis twist(tangent, tilt);
304 t.basis = twist * t.basis;
305 }
306 }
307
308 // Apply offset and scale.
309 Vector3 scale = get_transform().basis.get_scale();
310 t.translate_local(Vector3(h_offset, v_offset, 0));
311 t.basis.scale_local(scale);
312
313 set_transform(t);
314 }
315
316 void PathFollow3D::_notification(int p_what) {
317 switch (p_what) {
318 case NOTIFICATION_ENTER_TREE: {
319 Node *parent = get_parent();
320 if (parent) {
321 path = Object::cast_to<Path3D>(parent);
322 update_transform();
323 }
324 } break;
325
326 case NOTIFICATION_EXIT_TREE: {
327 path = nullptr;
328 } break;
329 }
330 }
331
332 void PathFollow3D::set_cubic_interpolation_enabled(bool p_enabled) {
333 cubic = p_enabled;
334 }
335
336 bool PathFollow3D::is_cubic_interpolation_enabled() const {
337 return cubic;
338 }
339
340 void PathFollow3D::_validate_property(PropertyInfo &p_property) const {
341 if (!Engine::get_singleton()->is_editor_hint()) {
342 return;
343 }
344 if (p_property.name == "offset") {
345 real_t max = 10000;
346 if (path && path->get_curve().is_valid()) {
347 max = path->get_curve()->get_baked_length();
348 }
349
350 p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater";
351 }
352 }
353
354 PackedStringArray PathFollow3D::get_configuration_warnings() const {
355 PackedStringArray warnings = Node3D::get_configuration_warnings();
356
357 if (is_visible_in_tree() && is_inside_tree()) {
358 if (!Object::cast_to<Path3D>(get_parent())) {
359 warnings.push_back(RTR("PathFollow3D only works when set as a child of a Path3D node."));
360 } else {
361 Path3D *p = Object::cast_to<Path3D>(get_parent());
362 if (p->get_curve().is_valid() && !p->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) {
363 warnings.push_back(RTR("PathFollow3D's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path3D's Curve resource."));
364 }
365 }
366 }
367
368 return warnings;
369 }
370
371 Transform3D PathFollow3D::correct_posture(Transform3D p_transform, PathFollow3D::RotationMode p_rotation_mode) {
372 Transform3D t = p_transform;
373
374 // Modify frame according to rotation mode.
375 if (p_rotation_mode == PathFollow3D::ROTATION_NONE) {
376 // Clear rotation.
377 t.basis = Basis();
378 } else if (p_rotation_mode == PathFollow3D::ROTATION_ORIENTED) {
379 Vector3 tangent = -t.basis.get_column(2);
380
381 // Y-axis points up by default.
382 t.basis = Basis::looking_at(tangent);
383 } else {
384 // Lock some euler axes.
385 Vector3 euler = t.basis.get_euler_normalized(EulerOrder::YXZ);
386 if (p_rotation_mode == PathFollow3D::ROTATION_Y) {
387 // Only Y-axis allowed.
388 euler[0] = 0;
389 euler[2] = 0;
390 } else if (p_rotation_mode == PathFollow3D::ROTATION_XY) {
391 // XY allowed.
392 euler[2] = 0;
393 }
394
395 Basis locked = Basis::from_euler(euler, EulerOrder::YXZ);
396 t.basis = locked;
397 }
398
399 return t;
400 }
401
402 void PathFollow3D::_bind_methods() {
403 ClassDB::bind_method(D_METHOD("set_progress", "progress"), &PathFollow3D::set_progress);
404 ClassDB::bind_method(D_METHOD("get_progress"), &PathFollow3D::get_progress);
405
406 ClassDB::bind_method(D_METHOD("set_h_offset", "h_offset"), &PathFollow3D::set_h_offset);
407 ClassDB::bind_method(D_METHOD("get_h_offset"), &PathFollow3D::get_h_offset);
408
409 ClassDB::bind_method(D_METHOD("set_v_offset", "v_offset"), &PathFollow3D::set_v_offset);
410 ClassDB::bind_method(D_METHOD("get_v_offset"), &PathFollow3D::get_v_offset);
411
412 ClassDB::bind_method(D_METHOD("set_progress_ratio", "ratio"), &PathFollow3D::set_progress_ratio);
413 ClassDB::bind_method(D_METHOD("get_progress_ratio"), &PathFollow3D::get_progress_ratio);
414
415 ClassDB::bind_method(D_METHOD("set_rotation_mode", "rotation_mode"), &PathFollow3D::set_rotation_mode);
416 ClassDB::bind_method(D_METHOD("get_rotation_mode"), &PathFollow3D::get_rotation_mode);
417
418 ClassDB::bind_method(D_METHOD("set_cubic_interpolation", "enabled"), &PathFollow3D::set_cubic_interpolation_enabled);
419 ClassDB::bind_method(D_METHOD("get_cubic_interpolation"), &PathFollow3D::is_cubic_interpolation_enabled);
420
421 ClassDB::bind_method(D_METHOD("set_use_model_front", "enabled"), &PathFollow3D::set_use_model_front);
422 ClassDB::bind_method(D_METHOD("is_using_model_front"), &PathFollow3D::is_using_model_front);
423
424 ClassDB::bind_method(D_METHOD("set_loop", "loop"), &PathFollow3D::set_loop);
425 ClassDB::bind_method(D_METHOD("has_loop"), &PathFollow3D::has_loop);
426
427 ClassDB::bind_method(D_METHOD("set_tilt_enabled", "enabled"), &PathFollow3D::set_tilt_enabled);
428 ClassDB::bind_method(D_METHOD("is_tilt_enabled"), &PathFollow3D::is_tilt_enabled);
429
430 ClassDB::bind_static_method("PathFollow3D", D_METHOD("correct_posture", "transform", "rotation_mode"), &PathFollow3D::correct_posture);
431
432 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress", PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:m"), "set_progress", "get_progress");
433 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress_ratio", PROPERTY_HINT_RANGE, "0,1,0.0001,or_less,or_greater", PROPERTY_USAGE_EDITOR), "set_progress_ratio", "get_progress_ratio");
434 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
435 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
436 ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode", PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented"), "set_rotation_mode", "get_rotation_mode");
437 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_model_front"), "set_use_model_front", "is_using_model_front");
438 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp"), "set_cubic_interpolation", "get_cubic_interpolation");
439 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
440 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tilt_enabled"), "set_tilt_enabled", "is_tilt_enabled");
441
442 BIND_ENUM_CONSTANT(ROTATION_NONE);
443 BIND_ENUM_CONSTANT(ROTATION_Y);
444 BIND_ENUM_CONSTANT(ROTATION_XY);
445 BIND_ENUM_CONSTANT(ROTATION_XYZ);
446 BIND_ENUM_CONSTANT(ROTATION_ORIENTED);
447 }
448
449 void PathFollow3D::set_progress(real_t p_progress) {
450 ERR_FAIL_COND(!std::isfinite(p_progress));
451 if (progress == p_progress) {
452 return;
453 }
454 progress = p_progress;
455
456 if (path) {
457 if (path->get_curve().is_valid()) {
458 real_t path_length = path->get_curve()->get_baked_length();
459
460 if (loop && path_length) {
461 progress = Math::fposmod(progress, path_length);
462 if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) {
463 progress = path_length;
464 }
465 } else {
466 progress = CLAMP(progress, 0, path_length);
467 }
468 }
469
470 update_transform();
471 }
472 }
473
474 void PathFollow3D::set_h_offset(real_t p_h_offset) {
475 if (h_offset == p_h_offset) {
476 return;
477 }
478 h_offset = p_h_offset;
479 update_transform();
480 }
481
482 real_t PathFollow3D::get_h_offset() const {
483 return h_offset;
484 }
485
486 void PathFollow3D::set_v_offset(real_t p_v_offset) {
487 if (v_offset == p_v_offset) {
488 return;
489 }
490 v_offset = p_v_offset;
491 update_transform();
492 }
493
494 real_t PathFollow3D::get_v_offset() const {
495 return v_offset;
496 }
497
498 real_t PathFollow3D::get_progress() const {
499 return progress;
500 }
501
502 void PathFollow3D::set_progress_ratio(real_t p_ratio) {
503 ERR_FAIL_NULL_MSG(path, "Can only set progress ratio on a PathFollow3D that is the child of a Path3D which is itself part of the scene tree.");
504 ERR_FAIL_COND_MSG(path->get_curve().is_null(), "Can't set progress ratio on a PathFollow3D that does not have a Curve.");
505 ERR_FAIL_COND_MSG(!path->get_curve()->get_baked_length(), "Can't set progress ratio on a PathFollow3D that has a 0 length curve.");
506 set_progress(p_ratio * path->get_curve()->get_baked_length());
507 }
508
509 real_t PathFollow3D::get_progress_ratio() const {
510 if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) {
511 return get_progress() / path->get_curve()->get_baked_length();
512 } else {
513 return 0;
514 }
515 }
516
517 void PathFollow3D::set_rotation_mode(RotationMode p_rotation_mode) {
518 if (rotation_mode == p_rotation_mode) {
519 return;
520 }
521 rotation_mode = p_rotation_mode;
522
523 update_configuration_warnings();
524 update_transform();
525 }
526
527 PathFollow3D::RotationMode PathFollow3D::get_rotation_mode() const {
528 return rotation_mode;
529 }
530
531 void PathFollow3D::set_use_model_front(bool p_use_model_front) {
532 if (use_model_front == p_use_model_front) {
533 return;
534 }
535 use_model_front = p_use_model_front;
536 update_transform();
537 }
538
539 bool PathFollow3D::is_using_model_front() const {
540 return use_model_front;
541 }
542
543 void PathFollow3D::set_loop(bool p_loop) {
544 if (loop == p_loop) {
545 return;
546 }
547 loop = p_loop;
548 update_transform();
549 }
550
551 bool PathFollow3D::has_loop() const {
552 return loop;
553 }
554
555 void PathFollow3D::set_tilt_enabled(bool p_enabled) {
556 if (tilt_enabled == p_enabled) {
557 return;
558 }
559 tilt_enabled = p_enabled;
560 update_transform();
561 }
562
563 bool PathFollow3D::is_tilt_enabled() const {
564 return tilt_enabled;
565 }