-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
node_3d.cpp
1239 lines (1037 loc) · 40.5 KB
/
node_3d.cpp
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* node_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "node_3d.h"
#include "scene/3d/visual_instance_3d.h"
#include "scene/main/viewport.h"
#include "scene/property_utils.h"
/*
possible algorithms:
Algorithm 1: (current)
definition of invalidation: global is invalid
1) If a node sets a LOCAL, it produces an invalidation of everything above
. a) If above is invalid, don't keep invalidating upwards
2) If a node sets a GLOBAL, it is converted to LOCAL (and forces validation of everything pending below)
drawback: setting/reading globals is useful and used very often, and using affine inverses is slow
---
Algorithm 2: (no longer current)
definition of invalidation: NONE dirty, LOCAL dirty, GLOBAL dirty
1) If a node sets a LOCAL, it must climb the tree and set it as GLOBAL dirty
. a) marking GLOBALs as dirty up all the tree must be done always
2) If a node sets a GLOBAL, it marks local as dirty, and that's all?
//is clearing the dirty state correct in this case?
drawback: setting a local down the tree forces many tree walks often
--
future: no idea
*/
Node3DGizmo::Node3DGizmo() {
}
void Node3D::_notify_dirty() {
#ifdef TOOLS_ENABLED
if ((!data.gizmos.is_empty() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) {
#else
if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) {
#endif
get_tree()->xform_change_list.add(&xform_change);
}
}
void Node3D::_update_local_transform() const {
// This function is called when the local transform (data.local_transform) is dirty and the right value is contained in the Euler rotation and scale.
data.local_transform.basis.set_euler_scale(data.euler_rotation, data.scale, data.euler_rotation_order);
_clear_dirty_bits(DIRTY_LOCAL_TRANSFORM);
}
void Node3D::_update_rotation_and_scale() const {
// This function is called when the Euler rotation (data.euler_rotation) is dirty and the right value is contained in the local transform
data.scale = data.local_transform.basis.get_scale();
data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order);
_clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE);
}
void Node3D::_propagate_transform_changed_deferred() {
if (is_inside_tree() && !xform_change.in_list()) {
get_tree()->xform_change_list.add(&xform_change);
}
}
void Node3D::_propagate_transform_changed(Node3D *p_origin) {
if (!is_inside_tree()) {
return;
}
for (Node3D *&E : data.children) {
if (E->data.top_level) {
continue; //don't propagate to a top_level
}
E->_propagate_transform_changed(p_origin);
}
#ifdef TOOLS_ENABLED
if ((!data.gizmos.is_empty() || data.notify_transform) && !data.ignore_notification && !xform_change.in_list()) {
#else
if (data.notify_transform && !data.ignore_notification && !xform_change.in_list()) {
#endif
if (likely(is_accessible_from_caller_thread())) {
get_tree()->xform_change_list.add(&xform_change);
} else {
// This should very rarely happen, but if it does at least make sure the notification is received eventually.
callable_mp(this, &Node3D::_propagate_transform_changed_deferred).call_deferred();
}
}
_set_dirty_bits(DIRTY_GLOBAL_TRANSFORM);
}
void Node3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
ERR_MAIN_THREAD_GUARD;
ERR_FAIL_NULL(get_tree());
Node *p = get_parent();
if (p) {
data.parent = Object::cast_to<Node3D>(p);
}
if (data.parent) {
data.C = data.parent->data.children.push_back(this);
} else {
data.C = nullptr;
}
if (data.top_level && !Engine::get_singleton()->is_editor_hint()) {
if (data.parent) {
if (!data.top_level) {
data.local_transform = data.parent->get_global_transform() * get_transform();
} else {
data.local_transform = get_transform();
}
_replace_dirty_mask(DIRTY_EULER_ROTATION_AND_SCALE); // As local transform was updated, rot/scale should be dirty.
}
}
_set_dirty_bits(DIRTY_GLOBAL_TRANSFORM); // Global is always dirty upon entering a scene.
_notify_dirty();
notification(NOTIFICATION_ENTER_WORLD);
_update_visibility_parent(true);
} break;
case NOTIFICATION_EXIT_TREE: {
ERR_MAIN_THREAD_GUARD;
notification(NOTIFICATION_EXIT_WORLD, true);
if (xform_change.in_list()) {
get_tree()->xform_change_list.remove(&xform_change);
}
if (data.C) {
data.parent->data.children.erase(data.C);
}
data.parent = nullptr;
data.C = nullptr;
_update_visibility_parent(true);
} break;
case NOTIFICATION_ENTER_WORLD: {
ERR_MAIN_THREAD_GUARD;
data.inside_world = true;
data.viewport = nullptr;
Node *parent = get_parent();
while (parent && !data.viewport) {
data.viewport = Object::cast_to<Viewport>(parent);
parent = parent->get_parent();
}
ERR_FAIL_NULL(data.viewport);
if (get_script_instance()) {
get_script_instance()->call(SNAME("_enter_world"));
}
#ifdef TOOLS_ENABLED
if (is_part_of_edited_scene()) {
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_request_gizmo_for_id"), get_instance_id());
}
#endif
} break;
case NOTIFICATION_EXIT_WORLD: {
ERR_MAIN_THREAD_GUARD;
#ifdef TOOLS_ENABLED
clear_gizmos();
#endif
if (get_script_instance()) {
get_script_instance()->call(SNAME("_exit_world"));
}
data.viewport = nullptr;
data.inside_world = false;
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
for (int i = 0; i < data.gizmos.size(); i++) {
data.gizmos.write[i]->transform();
}
#endif
} break;
}
}
void Node3D::set_basis(const Basis &p_basis) {
ERR_THREAD_GUARD;
set_transform(Transform3D(p_basis, data.local_transform.origin));
}
void Node3D::set_quaternion(const Quaternion &p_quaternion) {
ERR_THREAD_GUARD;
if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
// We need the scale part, so if these are dirty, update it
data.scale = data.local_transform.basis.get_scale();
_clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE);
}
data.local_transform.basis = Basis(p_quaternion, data.scale);
// Rotscale should not be marked dirty because that would cause precision loss issues with the scale. Instead reconstruct rotation now.
data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order);
_replace_dirty_mask(DIRTY_NONE);
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
Vector3 Node3D::get_global_position() const {
ERR_READ_THREAD_GUARD_V(Vector3());
return get_global_transform().get_origin();
}
Basis Node3D::get_global_basis() const {
ERR_READ_THREAD_GUARD_V(Basis());
return get_global_transform().get_basis();
}
void Node3D::set_global_position(const Vector3 &p_position) {
ERR_THREAD_GUARD;
Transform3D transform = get_global_transform();
transform.set_origin(p_position);
set_global_transform(transform);
}
void Node3D::set_global_basis(const Basis &p_basis) {
ERR_THREAD_GUARD;
Transform3D transform = get_global_transform();
transform.set_basis(p_basis);
set_global_transform(transform);
}
Vector3 Node3D::get_global_rotation() const {
ERR_READ_THREAD_GUARD_V(Vector3());
return get_global_transform().get_basis().get_euler();
}
Vector3 Node3D::get_global_rotation_degrees() const {
ERR_READ_THREAD_GUARD_V(Vector3());
Vector3 radians = get_global_rotation();
return Vector3(Math::rad_to_deg(radians.x), Math::rad_to_deg(radians.y), Math::rad_to_deg(radians.z));
}
void Node3D::set_global_rotation(const Vector3 &p_euler_rad) {
ERR_THREAD_GUARD;
Transform3D transform = get_global_transform();
transform.basis = Basis::from_euler(p_euler_rad) * Basis::from_scale(transform.basis.get_scale());
set_global_transform(transform);
}
void Node3D::set_global_rotation_degrees(const Vector3 &p_euler_degrees) {
ERR_THREAD_GUARD;
Vector3 radians(Math::deg_to_rad(p_euler_degrees.x), Math::deg_to_rad(p_euler_degrees.y), Math::deg_to_rad(p_euler_degrees.z));
set_global_rotation(radians);
}
void Node3D::set_transform(const Transform3D &p_transform) {
ERR_THREAD_GUARD;
data.local_transform = p_transform;
_replace_dirty_mask(DIRTY_EULER_ROTATION_AND_SCALE); // Make rot/scale dirty.
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
Basis Node3D::get_basis() const {
ERR_READ_THREAD_GUARD_V(Basis());
return get_transform().basis;
}
Quaternion Node3D::get_quaternion() const {
return get_transform().basis.get_rotation_quaternion();
}
void Node3D::set_global_transform(const Transform3D &p_transform) {
ERR_THREAD_GUARD;
Transform3D xform = (data.parent && !data.top_level)
? data.parent->get_global_transform().affine_inverse() * p_transform
: p_transform;
set_transform(xform);
}
Transform3D Node3D::get_transform() const {
ERR_READ_THREAD_GUARD_V(Transform3D());
if (_test_dirty_bits(DIRTY_LOCAL_TRANSFORM)) {
// This update can happen if needed over multiple threads.
_update_local_transform();
}
return data.local_transform;
}
Transform3D Node3D::get_global_transform() const {
ERR_FAIL_COND_V(!is_inside_tree(), Transform3D());
/* Due to how threads work at scene level, while this global transform won't be able to be changed from outside a thread,
* it is possible that multiple threads can access it while it's dirty from previous work. Due to this, we must ensure that
* the dirty/update process is thread safe by utilizing atomic copies.
*/
uint32_t dirty = _read_dirty_mask();
if (dirty & DIRTY_GLOBAL_TRANSFORM) {
if (dirty & DIRTY_LOCAL_TRANSFORM) {
_update_local_transform(); // Update local transform atomically.
}
Transform3D new_global;
if (data.parent && !data.top_level) {
new_global = data.parent->get_global_transform() * data.local_transform;
} else {
new_global = data.local_transform;
}
if (data.disable_scale) {
new_global.basis.orthonormalize();
}
data.global_transform = new_global;
_clear_dirty_bits(DIRTY_GLOBAL_TRANSFORM);
}
return data.global_transform;
}
#ifdef TOOLS_ENABLED
Transform3D Node3D::get_global_gizmo_transform() const {
return get_global_transform();
}
Transform3D Node3D::get_local_gizmo_transform() const {
return get_transform();
}
#endif
Node3D *Node3D::get_parent_node_3d() const {
ERR_READ_THREAD_GUARD_V(nullptr); // This can't be changed on threads anyway.
if (data.top_level) {
return nullptr;
}
return Object::cast_to<Node3D>(get_parent());
}
Transform3D Node3D::get_relative_transform(const Node *p_parent) const {
ERR_READ_THREAD_GUARD_V(Transform3D());
if (p_parent == this) {
return Transform3D();
}
ERR_FAIL_NULL_V(data.parent, Transform3D());
if (p_parent == data.parent) {
return get_transform();
} else {
return data.parent->get_relative_transform(p_parent) * get_transform();
}
}
void Node3D::set_position(const Vector3 &p_position) {
ERR_THREAD_GUARD;
data.local_transform.origin = p_position;
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
void Node3D::set_rotation_edit_mode(RotationEditMode p_mode) {
ERR_THREAD_GUARD;
if (data.rotation_edit_mode == p_mode) {
return;
}
bool transform_changed = false;
if (data.rotation_edit_mode == ROTATION_EDIT_MODE_BASIS && !_test_dirty_bits(DIRTY_LOCAL_TRANSFORM)) {
data.local_transform.orthogonalize();
transform_changed = true;
}
data.rotation_edit_mode = p_mode;
if (p_mode == ROTATION_EDIT_MODE_EULER && _test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
// If going to Euler mode, ensure that vectors are _not_ dirty, else the retrieved value may be wrong.
// Otherwise keep what is there, so switching back and forth between modes does not break the vectors.
_update_rotation_and_scale();
}
if (transform_changed) {
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
notify_property_list_changed();
}
Node3D::RotationEditMode Node3D::get_rotation_edit_mode() const {
ERR_READ_THREAD_GUARD_V(ROTATION_EDIT_MODE_EULER);
return data.rotation_edit_mode;
}
void Node3D::set_rotation_order(EulerOrder p_order) {
ERR_THREAD_GUARD;
if (data.euler_rotation_order == p_order) {
return;
}
ERR_FAIL_INDEX(int32_t(p_order), 6);
bool transform_changed = false;
uint32_t dirty = _read_dirty_mask();
if ((dirty & DIRTY_EULER_ROTATION_AND_SCALE)) {
_update_rotation_and_scale();
} else if ((dirty & DIRTY_LOCAL_TRANSFORM)) {
data.euler_rotation = Basis::from_euler(data.euler_rotation, data.euler_rotation_order).get_euler_normalized(p_order);
transform_changed = true;
} else {
_set_dirty_bits(DIRTY_LOCAL_TRANSFORM);
transform_changed = true;
}
data.euler_rotation_order = p_order;
if (transform_changed) {
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
notify_property_list_changed(); // Will change the rotation property.
}
EulerOrder Node3D::get_rotation_order() const {
ERR_READ_THREAD_GUARD_V(EulerOrder::XYZ);
return data.euler_rotation_order;
}
void Node3D::set_rotation(const Vector3 &p_euler_rad) {
ERR_THREAD_GUARD;
if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
// Update scale only if rotation and scale are dirty, as rotation will be overridden.
data.scale = data.local_transform.basis.get_scale();
_clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE);
}
data.euler_rotation = p_euler_rad;
_replace_dirty_mask(DIRTY_LOCAL_TRANSFORM);
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
void Node3D::set_rotation_degrees(const Vector3 &p_euler_degrees) {
ERR_THREAD_GUARD;
Vector3 radians(Math::deg_to_rad(p_euler_degrees.x), Math::deg_to_rad(p_euler_degrees.y), Math::deg_to_rad(p_euler_degrees.z));
set_rotation(radians);
}
void Node3D::set_scale(const Vector3 &p_scale) {
ERR_THREAD_GUARD;
if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
// Update rotation only if rotation and scale are dirty, as scale will be overridden.
data.euler_rotation = data.local_transform.basis.get_euler_normalized(data.euler_rotation_order);
_clear_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE);
}
data.scale = p_scale;
_replace_dirty_mask(DIRTY_LOCAL_TRANSFORM);
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
Vector3 Node3D::get_position() const {
ERR_READ_THREAD_GUARD_V(Vector3());
return data.local_transform.origin;
}
Vector3 Node3D::get_rotation() const {
ERR_READ_THREAD_GUARD_V(Vector3());
if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
_update_rotation_and_scale();
}
return data.euler_rotation;
}
Vector3 Node3D::get_rotation_degrees() const {
ERR_READ_THREAD_GUARD_V(Vector3());
Vector3 radians = get_rotation();
return Vector3(Math::rad_to_deg(radians.x), Math::rad_to_deg(radians.y), Math::rad_to_deg(radians.z));
}
Vector3 Node3D::get_scale() const {
ERR_READ_THREAD_GUARD_V(Vector3());
if (_test_dirty_bits(DIRTY_EULER_ROTATION_AND_SCALE)) {
_update_rotation_and_scale();
}
return data.scale;
}
void Node3D::update_gizmos() {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
if (!is_inside_world()) {
return;
}
if (data.gizmos.is_empty()) {
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_request_gizmo_for_id"), get_instance_id());
return;
}
if (data.gizmos_dirty) {
return;
}
data.gizmos_dirty = true;
callable_mp(this, &Node3D::_update_gizmos).call_deferred();
#endif
}
void Node3D::set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform) {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
if (!is_inside_world()) {
return;
}
if (is_part_of_edited_scene()) {
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_set_subgizmo_selection"), this, p_gizmo, p_id, p_transform);
}
#endif
}
void Node3D::clear_subgizmo_selection() {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
if (!is_inside_world()) {
return;
}
if (data.gizmos.is_empty()) {
return;
}
if (is_part_of_edited_scene()) {
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFERRED, SceneStringName(_spatial_editor_group), SNAME("_clear_subgizmo_selection"), this);
}
#endif
}
void Node3D::add_gizmo(Ref<Node3DGizmo> p_gizmo) {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
if (data.gizmos_disabled || p_gizmo.is_null()) {
return;
}
data.gizmos.push_back(p_gizmo);
if (p_gizmo.is_valid() && is_inside_world()) {
p_gizmo->create();
if (is_visible_in_tree()) {
p_gizmo->redraw();
}
p_gizmo->transform();
}
#endif
}
void Node3D::remove_gizmo(Ref<Node3DGizmo> p_gizmo) {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
int idx = data.gizmos.find(p_gizmo);
if (idx != -1) {
p_gizmo->free();
data.gizmos.remove_at(idx);
}
#endif
}
void Node3D::clear_gizmos() {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
for (int i = 0; i < data.gizmos.size(); i++) {
data.gizmos.write[i]->free();
}
data.gizmos.clear();
#endif
}
TypedArray<Node3DGizmo> Node3D::get_gizmos_bind() const {
ERR_THREAD_GUARD_V(TypedArray<Node3DGizmo>());
TypedArray<Node3DGizmo> ret;
#ifdef TOOLS_ENABLED
for (int i = 0; i < data.gizmos.size(); i++) {
ret.push_back(Variant(data.gizmos[i].ptr()));
}
#endif
return ret;
}
Vector<Ref<Node3DGizmo>> Node3D::get_gizmos() const {
ERR_THREAD_GUARD_V(Vector<Ref<Node3DGizmo>>());
#ifdef TOOLS_ENABLED
return data.gizmos;
#else
return Vector<Ref<Node3DGizmo>>();
#endif
}
void Node3D::_replace_dirty_mask(uint32_t p_mask) const {
if (is_group_processing()) {
data.dirty.mt.set(p_mask);
} else {
data.dirty.st = p_mask;
}
}
void Node3D::_set_dirty_bits(uint32_t p_bits) const {
if (is_group_processing()) {
data.dirty.mt.bit_or(p_bits);
} else {
data.dirty.st |= p_bits;
}
}
void Node3D::_clear_dirty_bits(uint32_t p_bits) const {
if (is_group_processing()) {
data.dirty.mt.bit_and(~p_bits);
} else {
data.dirty.st &= ~p_bits;
}
}
void Node3D::_update_gizmos() {
#ifdef TOOLS_ENABLED
if (data.gizmos_disabled || !is_inside_world() || !data.gizmos_dirty) {
data.gizmos_dirty = false;
return;
}
data.gizmos_dirty = false;
for (int i = 0; i < data.gizmos.size(); i++) {
if (is_visible_in_tree()) {
data.gizmos.write[i]->redraw();
} else {
data.gizmos.write[i]->clear();
}
}
#endif
}
void Node3D::set_disable_gizmos(bool p_enabled) {
ERR_THREAD_GUARD;
#ifdef TOOLS_ENABLED
data.gizmos_disabled = p_enabled;
if (!p_enabled) {
clear_gizmos();
}
#endif
}
void Node3D::reparent(Node *p_parent, bool p_keep_global_transform) {
ERR_THREAD_GUARD;
if (p_keep_global_transform) {
Transform3D temp = get_global_transform();
Node::reparent(p_parent);
set_global_transform(temp);
} else {
Node::reparent(p_parent);
}
}
void Node3D::set_disable_scale(bool p_enabled) {
ERR_THREAD_GUARD;
data.disable_scale = p_enabled;
}
bool Node3D::is_scale_disabled() const {
ERR_READ_THREAD_GUARD_V(false);
return data.disable_scale;
}
void Node3D::set_as_top_level(bool p_enabled) {
ERR_THREAD_GUARD;
if (data.top_level == p_enabled) {
return;
}
if (is_inside_tree()) {
if (p_enabled) {
set_transform(get_global_transform());
} else if (data.parent) {
set_transform(data.parent->get_global_transform().affine_inverse() * get_global_transform());
}
}
data.top_level = p_enabled;
}
void Node3D::set_as_top_level_keep_local(bool p_enabled) {
ERR_THREAD_GUARD;
if (data.top_level == p_enabled) {
return;
}
data.top_level = p_enabled;
_propagate_transform_changed(this);
}
bool Node3D::is_set_as_top_level() const {
ERR_READ_THREAD_GUARD_V(false);
return data.top_level;
}
Ref<World3D> Node3D::get_world_3d() const {
ERR_READ_THREAD_GUARD_V(Ref<World3D>()); // World3D can only be set from main thread, so it's safe to obtain on threads.
ERR_FAIL_COND_V(!is_inside_world(), Ref<World3D>());
ERR_FAIL_NULL_V(data.viewport, Ref<World3D>());
return data.viewport->find_world_3d();
}
void Node3D::_propagate_visibility_changed() {
notification(NOTIFICATION_VISIBILITY_CHANGED);
emit_signal(SceneStringName(visibility_changed));
#ifdef TOOLS_ENABLED
if (!data.gizmos.is_empty()) {
data.gizmos_dirty = true;
_update_gizmos();
}
#endif
for (Node3D *c : data.children) {
if (!c || !c->data.visible) {
continue;
}
c->_propagate_visibility_changed();
}
}
void Node3D::show() {
ERR_MAIN_THREAD_GUARD;
set_visible(true);
}
void Node3D::hide() {
ERR_MAIN_THREAD_GUARD;
set_visible(false);
}
void Node3D::set_visible(bool p_visible) {
ERR_MAIN_THREAD_GUARD;
if (data.visible == p_visible) {
return;
}
data.visible = p_visible;
if (!is_inside_tree()) {
return;
}
_propagate_visibility_changed();
}
bool Node3D::is_visible() const {
ERR_READ_THREAD_GUARD_V(false);
return data.visible;
}
bool Node3D::is_visible_in_tree() const {
ERR_READ_THREAD_GUARD_V(false); // Since visibility can only be changed from main thread, this is safe to call.
const Node3D *s = this;
while (s) {
if (!s->data.visible) {
return false;
}
s = s->data.parent;
}
return true;
}
void Node3D::rotate_object_local(const Vector3 &p_axis, real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.rotate_local(p_axis, p_angle);
set_transform(t);
}
void Node3D::rotate(const Vector3 &p_axis, real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.rotate(p_axis, p_angle);
set_transform(t);
}
void Node3D::rotate_x(real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.rotate(Vector3(1, 0, 0), p_angle);
set_transform(t);
}
void Node3D::rotate_y(real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.rotate(Vector3(0, 1, 0), p_angle);
set_transform(t);
}
void Node3D::rotate_z(real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.rotate(Vector3(0, 0, 1), p_angle);
set_transform(t);
}
void Node3D::translate(const Vector3 &p_offset) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.translate_local(p_offset);
set_transform(t);
}
void Node3D::translate_object_local(const Vector3 &p_offset) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
Transform3D s;
s.translate_local(p_offset);
set_transform(t * s);
}
void Node3D::scale(const Vector3 &p_ratio) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.scale(p_ratio);
set_transform(t);
}
void Node3D::scale_object_local(const Vector3 &p_scale) {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.basis.scale_local(p_scale);
set_transform(t);
}
void Node3D::global_rotate(const Vector3 &p_axis, real_t p_angle) {
ERR_THREAD_GUARD;
Transform3D t = get_global_transform();
t.basis.rotate(p_axis, p_angle);
set_global_transform(t);
}
void Node3D::global_scale(const Vector3 &p_scale) {
ERR_THREAD_GUARD;
Transform3D t = get_global_transform();
t.basis.scale(p_scale);
set_global_transform(t);
}
void Node3D::global_translate(const Vector3 &p_offset) {
ERR_THREAD_GUARD;
Transform3D t = get_global_transform();
t.origin += p_offset;
set_global_transform(t);
}
void Node3D::orthonormalize() {
ERR_THREAD_GUARD;
Transform3D t = get_transform();
t.orthonormalize();
set_transform(t);
}
void Node3D::set_identity() {
ERR_THREAD_GUARD;
set_transform(Transform3D());
}
void Node3D::look_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
ERR_THREAD_GUARD;
ERR_FAIL_COND_MSG(!is_inside_tree(), "Node not inside tree. Use look_at_from_position() instead.");
Vector3 origin = get_global_transform().origin;
look_at_from_position(origin, p_target, p_up, p_use_model_front);
}
void Node3D::look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up, bool p_use_model_front) {
ERR_THREAD_GUARD;
ERR_FAIL_COND_MSG(p_pos.is_equal_approx(p_target), "Node origin and target are in the same position, look_at() failed.");
ERR_FAIL_COND_MSG(p_up.is_zero_approx(), "The up vector can't be zero, look_at() failed.");
ERR_FAIL_COND_MSG(p_up.cross(p_target - p_pos).is_zero_approx(), "Up vector and direction between node origin and target are aligned, look_at() failed.");
Vector3 forward = p_target - p_pos;
Basis lookat_basis = Basis::looking_at(forward, p_up, p_use_model_front);
Vector3 original_scale = get_scale();
set_global_transform(Transform3D(lookat_basis, p_pos));
set_scale(original_scale);
}
Vector3 Node3D::to_local(Vector3 p_global) const {
ERR_READ_THREAD_GUARD_V(Vector3());
return get_global_transform().affine_inverse().xform(p_global);
}
Vector3 Node3D::to_global(Vector3 p_local) const {
ERR_READ_THREAD_GUARD_V(Vector3());
return get_global_transform().xform(p_local);
}
void Node3D::set_notify_transform(bool p_enabled) {
ERR_THREAD_GUARD;
data.notify_transform = p_enabled;
}
bool Node3D::is_transform_notification_enabled() const {
ERR_READ_THREAD_GUARD_V(false);
return data.notify_transform;
}
void Node3D::set_notify_local_transform(bool p_enabled) {
ERR_THREAD_GUARD;
data.notify_local_transform = p_enabled;
}
bool Node3D::is_local_transform_notification_enabled() const {
ERR_READ_THREAD_GUARD_V(false);
return data.notify_local_transform;
}
void Node3D::force_update_transform() {
ERR_THREAD_GUARD;
ERR_FAIL_COND(!is_inside_tree());
if (!xform_change.in_list()) {
return; //nothing to update
}
get_tree()->xform_change_list.remove(&xform_change);
notification(NOTIFICATION_TRANSFORM_CHANGED);
}
void Node3D::_update_visibility_parent(bool p_update_root) {
RID new_parent;