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 | Path3D::Path3D() { |
34 | SceneTree *st = SceneTree::get_singleton(); |
35 | if (st && st->is_debugging_paths_hint()) { |
36 | debug_instance = RS::get_singleton()->instance_create(); |
37 | set_notify_transform(true); |
38 | _update_debug_mesh(); |
39 | } |
40 | } |
41 | |
42 | Path3D::~Path3D() { |
43 | if (debug_instance.is_valid()) { |
44 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
45 | RS::get_singleton()->free(debug_instance); |
46 | } |
47 | if (debug_mesh.is_valid()) { |
48 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
49 | RS::get_singleton()->free(debug_mesh->get_rid()); |
50 | } |
51 | } |
52 | |
53 | void Path3D::_notification(int p_what) { |
54 | switch (p_what) { |
55 | case NOTIFICATION_ENTER_TREE: { |
56 | SceneTree *st = SceneTree::get_singleton(); |
57 | if (st && st->is_debugging_paths_hint()) { |
58 | _update_debug_mesh(); |
59 | } |
60 | } break; |
61 | |
62 | case NOTIFICATION_EXIT_TREE: { |
63 | SceneTree *st = SceneTree::get_singleton(); |
64 | if (st && st->is_debugging_paths_hint()) { |
65 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
66 | } |
67 | } break; |
68 | |
69 | case NOTIFICATION_TRANSFORM_CHANGED: { |
70 | if (is_inside_tree() && debug_instance.is_valid()) { |
71 | RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); |
72 | } |
73 | } break; |
74 | } |
75 | } |
76 | |
77 | void Path3D::_update_debug_mesh() { |
78 | SceneTree *st = SceneTree::get_singleton(); |
79 | if (!(st && st->is_debugging_paths_hint())) { |
80 | return; |
81 | } |
82 | |
83 | if (!debug_mesh.is_valid()) { |
84 | debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh)); |
85 | } |
86 | |
87 | if (!(curve.is_valid())) { |
88 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
89 | return; |
90 | } |
91 | if (curve->get_point_count() < 2) { |
92 | RS::get_singleton()->instance_set_visible(debug_instance, false); |
93 | return; |
94 | } |
95 | |
96 | Vector<Vector3> vertex_array; |
97 | |
98 | for (int i = 1; i < curve->get_point_count(); i++) { |
99 | Vector3 line_end = curve->get_point_position(i); |
100 | Vector3 line_start = curve->get_point_position(i - 1); |
101 | vertex_array.push_back(line_start); |
102 | vertex_array.push_back(line_end); |
103 | } |
104 | |
105 | Array mesh_array; |
106 | mesh_array.resize(Mesh::ARRAY_MAX); |
107 | mesh_array[Mesh::ARRAY_VERTEX] = vertex_array; |
108 | |
109 | debug_mesh->clear_surfaces(); |
110 | debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, mesh_array); |
111 | |
112 | RS::get_singleton()->instance_set_base(debug_instance, debug_mesh->get_rid()); |
113 | RS::get_singleton()->mesh_surface_set_material(debug_mesh->get_rid(), 0, st->get_debug_paths_material()->get_rid()); |
114 | if (is_inside_tree()) { |
115 | RS::get_singleton()->instance_set_scenario(debug_instance, get_world_3d()->get_scenario()); |
116 | RS::get_singleton()->instance_set_transform(debug_instance, get_global_transform()); |
117 | RS::get_singleton()->instance_set_visible(debug_instance, is_visible_in_tree()); |
118 | } |
119 | } |
120 | |
121 | void Path3D::_curve_changed() { |
122 | if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) { |
123 | update_gizmos(); |
124 | } |
125 | if (is_inside_tree()) { |
126 | emit_signal(SNAME("curve_changed" )); |
127 | } |
128 | |
129 | // update the configuration warnings of all children of type PathFollow |
130 | // previously used for PathFollowOriented (now enforced orientation is done in PathFollow) |
131 | if (is_inside_tree()) { |
132 | for (int i = 0; i < get_child_count(); i++) { |
133 | PathFollow3D *child = Object::cast_to<PathFollow3D>(get_child(i)); |
134 | if (child) { |
135 | child->update_configuration_warnings(); |
136 | } |
137 | } |
138 | } |
139 | SceneTree *st = SceneTree::get_singleton(); |
140 | if (st && st->is_debugging_paths_hint()) { |
141 | _update_debug_mesh(); |
142 | } |
143 | } |
144 | |
145 | void Path3D::set_curve(const Ref<Curve3D> &p_curve) { |
146 | if (curve.is_valid()) { |
147 | curve->disconnect_changed(callable_mp(this, &Path3D::_curve_changed)); |
148 | } |
149 | |
150 | curve = p_curve; |
151 | |
152 | if (curve.is_valid()) { |
153 | curve->connect_changed(callable_mp(this, &Path3D::_curve_changed)); |
154 | } |
155 | _curve_changed(); |
156 | } |
157 | |
158 | Ref<Curve3D> Path3D::get_curve() const { |
159 | return curve; |
160 | } |
161 | |
162 | void Path3D::_bind_methods() { |
163 | ClassDB::bind_method(D_METHOD("set_curve" , "curve" ), &Path3D::set_curve); |
164 | ClassDB::bind_method(D_METHOD("get_curve" ), &Path3D::get_curve); |
165 | |
166 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "curve" , PROPERTY_HINT_RESOURCE_TYPE, "Curve3D" , PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_curve" , "get_curve" ); |
167 | |
168 | ADD_SIGNAL(MethodInfo("curve_changed" )); |
169 | } |
170 | |
171 | ////////////// |
172 | |
173 | void PathFollow3D::_update_transform(bool p_update_xyz_rot) { |
174 | if (!path) { |
175 | return; |
176 | } |
177 | |
178 | Ref<Curve3D> c = path->get_curve(); |
179 | if (!c.is_valid()) { |
180 | return; |
181 | } |
182 | |
183 | real_t bl = c->get_baked_length(); |
184 | if (bl == 0.0) { |
185 | return; |
186 | } |
187 | |
188 | Transform3D t; |
189 | |
190 | if (rotation_mode == ROTATION_NONE) { |
191 | Vector3 pos = c->sample_baked(progress, cubic); |
192 | t.origin = pos; |
193 | } else { |
194 | t = c->sample_baked_with_rotation(progress, cubic, false); |
195 | if (use_model_front) { |
196 | t.basis *= Basis::from_scale(Vector3(-1.0, 1.0, -1.0)); |
197 | } |
198 | Vector3 forward = t.basis.get_column(2); // Retain tangent for applying tilt |
199 | t = PathFollow3D::correct_posture(t, rotation_mode); |
200 | |
201 | // Apply tilt *after* correct_posture |
202 | if (tilt_enabled) { |
203 | const real_t tilt = c->sample_baked_tilt(progress); |
204 | |
205 | const Basis twist(forward, tilt); |
206 | t.basis = twist * t.basis; |
207 | } |
208 | } |
209 | |
210 | Vector3 scale = get_transform().basis.get_scale(); |
211 | |
212 | t.translate_local(Vector3(h_offset, v_offset, 0)); |
213 | t.basis.scale_local(scale); |
214 | |
215 | set_transform(t); |
216 | } |
217 | |
218 | void PathFollow3D::_notification(int p_what) { |
219 | switch (p_what) { |
220 | case NOTIFICATION_ENTER_TREE: { |
221 | Node *parent = get_parent(); |
222 | if (parent) { |
223 | path = Object::cast_to<Path3D>(parent); |
224 | if (path) { |
225 | _update_transform(false); |
226 | } |
227 | } |
228 | } break; |
229 | |
230 | case NOTIFICATION_EXIT_TREE: { |
231 | path = nullptr; |
232 | } break; |
233 | } |
234 | } |
235 | |
236 | void PathFollow3D::set_cubic_interpolation_enabled(bool p_enabled) { |
237 | cubic = p_enabled; |
238 | } |
239 | |
240 | bool PathFollow3D::is_cubic_interpolation_enabled() const { |
241 | return cubic; |
242 | } |
243 | |
244 | void PathFollow3D::_validate_property(PropertyInfo &p_property) const { |
245 | if (p_property.name == "offset" ) { |
246 | real_t max = 10000; |
247 | if (path && path->get_curve().is_valid()) { |
248 | max = path->get_curve()->get_baked_length(); |
249 | } |
250 | |
251 | p_property.hint_string = "0," + rtos(max) + ",0.01,or_less,or_greater" ; |
252 | } |
253 | } |
254 | |
255 | PackedStringArray PathFollow3D::get_configuration_warnings() const { |
256 | PackedStringArray warnings = Node::get_configuration_warnings(); |
257 | |
258 | if (is_visible_in_tree() && is_inside_tree()) { |
259 | if (!Object::cast_to<Path3D>(get_parent())) { |
260 | warnings.push_back(RTR("PathFollow3D only works when set as a child of a Path3D node." )); |
261 | } else { |
262 | Path3D *p = Object::cast_to<Path3D>(get_parent()); |
263 | if (p->get_curve().is_valid() && !p->get_curve()->is_up_vector_enabled() && rotation_mode == ROTATION_ORIENTED) { |
264 | warnings.push_back(RTR("PathFollow3D's ROTATION_ORIENTED requires \"Up Vector\" to be enabled in its parent Path3D's Curve resource." )); |
265 | } |
266 | } |
267 | } |
268 | |
269 | return warnings; |
270 | } |
271 | |
272 | Transform3D PathFollow3D::correct_posture(Transform3D p_transform, PathFollow3D::RotationMode p_rotation_mode) { |
273 | Transform3D t = p_transform; |
274 | |
275 | // Modify frame according to rotation mode. |
276 | if (p_rotation_mode == PathFollow3D::ROTATION_NONE) { |
277 | // Clear rotation. |
278 | t.basis = Basis(); |
279 | } else if (p_rotation_mode == PathFollow3D::ROTATION_ORIENTED) { |
280 | // Y-axis always straight up. |
281 | Vector3 up(0.0, 1.0, 0.0); |
282 | Vector3 forward = t.basis.get_column(2); |
283 | |
284 | t.basis = Basis::looking_at(-forward, up); |
285 | } else { |
286 | // Lock some euler axes. |
287 | Vector3 euler = t.basis.get_euler_normalized(EulerOrder::YXZ); |
288 | if (p_rotation_mode == PathFollow3D::ROTATION_Y) { |
289 | // Only Y-axis allowed. |
290 | euler[0] = 0; |
291 | euler[2] = 0; |
292 | } else if (p_rotation_mode == PathFollow3D::ROTATION_XY) { |
293 | // XY allowed. |
294 | euler[2] = 0; |
295 | } |
296 | |
297 | Basis locked = Basis::from_euler(euler, EulerOrder::YXZ); |
298 | t.basis = locked; |
299 | } |
300 | |
301 | return t; |
302 | } |
303 | |
304 | void PathFollow3D::_bind_methods() { |
305 | ClassDB::bind_method(D_METHOD("set_progress" , "progress" ), &PathFollow3D::set_progress); |
306 | ClassDB::bind_method(D_METHOD("get_progress" ), &PathFollow3D::get_progress); |
307 | |
308 | ClassDB::bind_method(D_METHOD("set_h_offset" , "h_offset" ), &PathFollow3D::set_h_offset); |
309 | ClassDB::bind_method(D_METHOD("get_h_offset" ), &PathFollow3D::get_h_offset); |
310 | |
311 | ClassDB::bind_method(D_METHOD("set_v_offset" , "v_offset" ), &PathFollow3D::set_v_offset); |
312 | ClassDB::bind_method(D_METHOD("get_v_offset" ), &PathFollow3D::get_v_offset); |
313 | |
314 | ClassDB::bind_method(D_METHOD("set_progress_ratio" , "ratio" ), &PathFollow3D::set_progress_ratio); |
315 | ClassDB::bind_method(D_METHOD("get_progress_ratio" ), &PathFollow3D::get_progress_ratio); |
316 | |
317 | ClassDB::bind_method(D_METHOD("set_rotation_mode" , "rotation_mode" ), &PathFollow3D::set_rotation_mode); |
318 | ClassDB::bind_method(D_METHOD("get_rotation_mode" ), &PathFollow3D::get_rotation_mode); |
319 | |
320 | ClassDB::bind_method(D_METHOD("set_cubic_interpolation" , "enabled" ), &PathFollow3D::set_cubic_interpolation_enabled); |
321 | ClassDB::bind_method(D_METHOD("get_cubic_interpolation" ), &PathFollow3D::is_cubic_interpolation_enabled); |
322 | |
323 | ClassDB::bind_method(D_METHOD("set_use_model_front" , "enabled" ), &PathFollow3D::set_use_model_front); |
324 | ClassDB::bind_method(D_METHOD("is_using_model_front" ), &PathFollow3D::is_using_model_front); |
325 | |
326 | ClassDB::bind_method(D_METHOD("set_loop" , "loop" ), &PathFollow3D::set_loop); |
327 | ClassDB::bind_method(D_METHOD("has_loop" ), &PathFollow3D::has_loop); |
328 | |
329 | ClassDB::bind_method(D_METHOD("set_tilt_enabled" , "enabled" ), &PathFollow3D::set_tilt_enabled); |
330 | ClassDB::bind_method(D_METHOD("is_tilt_enabled" ), &PathFollow3D::is_tilt_enabled); |
331 | |
332 | ClassDB::bind_static_method("PathFollow3D" , D_METHOD("correct_posture" , "transform" , "rotation_mode" ), &PathFollow3D::correct_posture); |
333 | |
334 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "progress" , PROPERTY_HINT_RANGE, "0,10000,0.01,or_less,or_greater,suffix:m" ), "set_progress" , "get_progress" ); |
335 | 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" ); |
336 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset" , PROPERTY_HINT_NONE, "suffix:m" ), "set_h_offset" , "get_h_offset" ); |
337 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset" , PROPERTY_HINT_NONE, "suffix:m" ), "set_v_offset" , "get_v_offset" ); |
338 | ADD_PROPERTY(PropertyInfo(Variant::INT, "rotation_mode" , PROPERTY_HINT_ENUM, "None,Y,XY,XYZ,Oriented" ), "set_rotation_mode" , "get_rotation_mode" ); |
339 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_model_front" ), "set_use_model_front" , "is_using_model_front" ); |
340 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "cubic_interp" ), "set_cubic_interpolation" , "get_cubic_interpolation" ); |
341 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop" ), "set_loop" , "has_loop" ); |
342 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tilt_enabled" ), "set_tilt_enabled" , "is_tilt_enabled" ); |
343 | |
344 | BIND_ENUM_CONSTANT(ROTATION_NONE); |
345 | BIND_ENUM_CONSTANT(ROTATION_Y); |
346 | BIND_ENUM_CONSTANT(ROTATION_XY); |
347 | BIND_ENUM_CONSTANT(ROTATION_XYZ); |
348 | BIND_ENUM_CONSTANT(ROTATION_ORIENTED); |
349 | } |
350 | |
351 | void PathFollow3D::set_progress(real_t p_progress) { |
352 | ERR_FAIL_COND(!isfinite(p_progress)); |
353 | progress = p_progress; |
354 | |
355 | if (path) { |
356 | if (path->get_curve().is_valid()) { |
357 | real_t path_length = path->get_curve()->get_baked_length(); |
358 | |
359 | if (loop && path_length) { |
360 | progress = Math::fposmod(progress, path_length); |
361 | if (!Math::is_zero_approx(p_progress) && Math::is_zero_approx(progress)) { |
362 | progress = path_length; |
363 | } |
364 | } else { |
365 | progress = CLAMP(progress, 0, path_length); |
366 | } |
367 | } |
368 | |
369 | _update_transform(); |
370 | } |
371 | } |
372 | |
373 | void PathFollow3D::set_h_offset(real_t p_h_offset) { |
374 | h_offset = p_h_offset; |
375 | if (path) { |
376 | _update_transform(); |
377 | } |
378 | } |
379 | |
380 | real_t PathFollow3D::get_h_offset() const { |
381 | return h_offset; |
382 | } |
383 | |
384 | void PathFollow3D::set_v_offset(real_t p_v_offset) { |
385 | v_offset = p_v_offset; |
386 | if (path) { |
387 | _update_transform(); |
388 | } |
389 | } |
390 | |
391 | real_t PathFollow3D::get_v_offset() const { |
392 | return v_offset; |
393 | } |
394 | |
395 | real_t PathFollow3D::get_progress() const { |
396 | return progress; |
397 | } |
398 | |
399 | void PathFollow3D::set_progress_ratio(real_t p_ratio) { |
400 | if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { |
401 | set_progress(p_ratio * path->get_curve()->get_baked_length()); |
402 | } |
403 | } |
404 | |
405 | real_t PathFollow3D::get_progress_ratio() const { |
406 | if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) { |
407 | return get_progress() / path->get_curve()->get_baked_length(); |
408 | } else { |
409 | return 0; |
410 | } |
411 | } |
412 | |
413 | void PathFollow3D::set_rotation_mode(RotationMode p_rotation_mode) { |
414 | rotation_mode = p_rotation_mode; |
415 | |
416 | update_configuration_warnings(); |
417 | _update_transform(); |
418 | } |
419 | |
420 | PathFollow3D::RotationMode PathFollow3D::get_rotation_mode() const { |
421 | return rotation_mode; |
422 | } |
423 | |
424 | void PathFollow3D::set_use_model_front(bool p_use_model_front) { |
425 | use_model_front = p_use_model_front; |
426 | } |
427 | |
428 | bool PathFollow3D::is_using_model_front() const { |
429 | return use_model_front; |
430 | } |
431 | |
432 | void PathFollow3D::set_loop(bool p_loop) { |
433 | loop = p_loop; |
434 | } |
435 | |
436 | bool PathFollow3D::has_loop() const { |
437 | return loop; |
438 | } |
439 | |
440 | void PathFollow3D::set_tilt_enabled(bool p_enabled) { |
441 | tilt_enabled = p_enabled; |
442 | } |
443 | |
444 | bool PathFollow3D::is_tilt_enabled() const { |
445 | return tilt_enabled; |
446 | } |
447 | |