forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph-node.js
More file actions
1436 lines (1267 loc) · 50 KB
/
graph-node.js
File metadata and controls
1436 lines (1267 loc) · 50 KB
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
import { EventHandler } from '../core/event-handler.js';
import { Tags } from '../core/tags.js';
import { Mat3 } from '../math/mat3.js';
import { Mat4 } from '../math/mat4.js';
import { Quat } from '../math/quat.js';
import { Vec3 } from '../math/vec3.js';
var scaleCompensatePosTransform = new Mat4();
var scaleCompensatePos = new Vec3();
var scaleCompensateRot = new Quat();
var scaleCompensateRot2 = new Quat();
var scaleCompensateScale = new Vec3();
var scaleCompensateScaleForParent = new Vec3();
var tmpMat4 = new Mat4();
var tmpQuat = new Quat();
var position = new Vec3();
var invParentWtm = new Mat4();
var rotation = new Quat();
var invParentRot = new Quat();
var matrix = new Mat4();
var target = new Vec3();
var up = new Vec3();
/**
* @class
* @name GraphNode
* @augments EventHandler
* @classdesc A hierarchical scene node.
* @param {string} [name] - The non-unique name of the graph node, default is "Untitled".
* @property {string} name The non-unique name of a graph node.
* @property {Tags} tags Interface for tagging graph nodes. Tag based searches can be performed using the {@link GraphNode#findByTag} function.
*/
class GraphNode extends EventHandler {
constructor(name) {
super();
this.name = typeof name === "string" ? name : "Untitled"; // Non-unique human readable name
this.tags = new Tags(this);
this._labels = {};
// Local-space properties of transform (only first 3 are settable by the user)
this.localPosition = new Vec3(0, 0, 0);
this.localRotation = new Quat(0, 0, 0, 1);
this.localScale = new Vec3(1, 1, 1);
this.localEulerAngles = new Vec3(0, 0, 0); // Only calculated on request
// World-space properties of transform
this.position = new Vec3(0, 0, 0);
this.rotation = new Quat(0, 0, 0, 1);
this.eulerAngles = new Vec3(0, 0, 0);
this._scale = null;
this.localTransform = new Mat4();
this._dirtyLocal = false;
this._aabbVer = 0;
// _frozen flag marks the node to ignore hierarchy sync entirely (including children nodes)
// engine code automatically freezes and unfreezes objects whenever required
// segregating dynamic and stationary nodes into subhierarchies allows to reduce sync time significantly
this._frozen = false;
this.worldTransform = new Mat4();
this._dirtyWorld = false;
this.normalMatrix = new Mat3();
this._dirtyNormal = true;
this._right = null;
this._up = null;
this._forward = null;
this._parent = null;
this._children = [];
this._graphDepth = 0;
this._enabled = true;
this._enabledInHierarchy = false;
this.scaleCompensation = false;
}
/**
* @readonly
* @name GraphNode#right
* @type {Vec3}
* @description The normalized local space X-axis vector of the graph node in world space.
*/
get right() {
if (!this._right) {
this._right = new Vec3();
}
return this.getWorldTransform().getX(this._right).normalize();
}
/**
* @readonly
* @name GraphNode#up
* @type {Vec3}
* @description The normalized local space Y-axis vector of the graph node in world space.
*/
get up() {
if (!this._up) {
this._up = new Vec3();
}
return this.getWorldTransform().getY(this._up).normalize();
}
/**
* @readonly
* @name GraphNode#forward
* @type {Vec3}
* @description The normalized local space negative Z-axis vector of the graph node in world space.
*/
get forward() {
if (!this._forward) {
this._forward = new Vec3();
}
return this.getWorldTransform().getZ(this._forward).normalize().mulScalar(-1);
}
/**
* @name GraphNode#enabled
* @type {boolean}
* @description Enable or disable a GraphNode. If one of the GraphNode's parents is disabled
* there will be no other side effects. If all the parents are enabled then
* the new value will activate / deactivate all the enabled children of the GraphNode.
*/
get enabled() {
// make sure to check this._enabled too because if that
// was false when a parent was updated the _enabledInHierarchy
// flag may not have been updated for optimization purposes
return this._enabled && this._enabledInHierarchy;
}
set enabled(enabled) {
if (this._enabled !== enabled) {
this._enabled = enabled;
if (!this._parent || this._parent.enabled)
this._notifyHierarchyStateChanged(this, enabled);
}
}
/**
* @readonly
* @name GraphNode#parent
* @type {GraphNode}
* @description A read-only property to get a parent graph node.
*/
get parent() {
return this._parent;
}
/**
* @readonly
* @name GraphNode#path
* @type {string}
* @description A read-only property to get the path of the graph node relative to
* the root of the hierarchy.
*/
get path() {
var parent = this._parent;
if (parent) {
var path = this.name;
while (parent && parent._parent) {
path = parent.name + "/" + path;
parent = parent._parent;
}
return path;
}
return '';
}
/**
* @readonly
* @name GraphNode#root
* @type {GraphNode}
* @description A read-only property to get highest graph node from current node.
*/
get root() {
var parent = this._parent;
if (!parent)
return this;
while (parent._parent)
parent = parent._parent;
return parent;
}
/**
* @readonly
* @name GraphNode#children
* @type {GraphNode[]}
* @description A read-only property to get the children of this graph node.
*/
get children() {
return this._children;
}
/**
* @readonly
* @name GraphNode#graphDepth
* @type {number}
* @description A read-only property to get the depth of this child within the graph. Note that for performance reasons this is only recalculated when a node is added to a new parent, i.e. It is not recalculated when a node is simply removed from the graph.
*/
get graphDepth() {
return this._graphDepth;
}
_notifyHierarchyStateChanged(node, enabled) {
node._onHierarchyStateChanged(enabled);
var c = node._children;
for (var i = 0, len = c.length; i < len; i++) {
if (c[i]._enabled)
this._notifyHierarchyStateChanged(c[i], enabled);
}
}
/**
* @private
* @function
* @name GraphNode#_onHierarchyStateChanged
* @description Called when the enabled flag of the entity or one of its parents changes.
* @param {boolean} enabled - True if enabled in the hierarchy, false if disabled.
*/
_onHierarchyStateChanged(enabled) {
// Override in derived classes
this._enabledInHierarchy = enabled;
if (enabled && !this._frozen)
this._unfreezeParentToRoot();
}
_cloneInternal(clone) {
clone.name = this.name;
var tags = this.tags._list;
for (var i = 0; i < tags.length; i++)
clone.tags.add(tags[i]);
clone._labels = Object.assign({}, this._labels);
clone.localPosition.copy(this.localPosition);
clone.localRotation.copy(this.localRotation);
clone.localScale.copy(this.localScale);
clone.localEulerAngles.copy(this.localEulerAngles);
clone.position.copy(this.position);
clone.rotation.copy(this.rotation);
clone.eulerAngles.copy(this.eulerAngles);
clone.localTransform.copy(this.localTransform);
clone._dirtyLocal = this._dirtyLocal;
clone.worldTransform.copy(this.worldTransform);
clone._dirtyWorld = this._dirtyWorld;
clone._dirtyNormal = this._dirtyNormal;
clone._aabbVer = this._aabbVer + 1;
clone._enabled = this._enabled;
clone.scaleCompensation = this.scaleCompensation;
// false as this node is not in the hierarchy yet
clone._enabledInHierarchy = false;
}
clone() {
var clone = new GraphNode();
this._cloneInternal(clone);
return clone;
}
/**
* @function
* @name GraphNode#find
* @description Search the graph node and all of its descendants for the nodes that satisfy some search criteria.
* @param {callbacks.FindNode|string} attr - This can either be a function or a string. If it's a function, it is executed
* for each descendant node to test if node satisfies the search logic. Returning true from the function will
* include the node into the results. If it's a string then it represents the name of a field or a method of the
* node. If this is the name of a field then the value passed as the second argument will be checked for equality.
* If this is the name of a function then the return value of the function will be checked for equality against
* the valued passed as the second argument to this function.
* @param {object} [value] - If the first argument (attr) is a property name then this value will be checked against
* the value of the property.
* @returns {GraphNode[]} The array of graph nodes that match the search criteria.
* @example
* // Finds all nodes that have a model component and have `door` in their lower-cased name
* var doors = house.find(function (node) {
* return node.model && node.name.toLowerCase().indexOf('door') !== -1;
* });
* @example
* // Finds all nodes that have the name property set to 'Test'
* var entities = parent.find('name', 'Test');
*/
find(attr, value) {
var result, results = [];
var len = this._children.length;
var i, descendants;
if (attr instanceof Function) {
var fn = attr;
result = fn(this);
if (result)
results.push(this);
for (i = 0; i < len; i++) {
descendants = this._children[i].find(fn);
if (descendants.length)
results = results.concat(descendants);
}
} else {
var testValue;
if (this[attr]) {
if (this[attr] instanceof Function) {
testValue = this[attr]();
} else {
testValue = this[attr];
}
if (testValue === value)
results.push(this);
}
for (i = 0; i < len; ++i) {
descendants = this._children[i].find(attr, value);
if (descendants.length)
results = results.concat(descendants);
}
}
return results;
}
/**
* @function
* @name GraphNode#findOne
* @description Search the graph node and all of its descendants for the first node that satisfies some search criteria.
* @param {callbacks.FindNode|string} attr - This can either be a function or a string. If it's a function, it is executed
* for each descendant node to test if node satisfies the search logic. Returning true from the function will
* result in that node being returned from findOne. If it's a string then it represents the name of a field or a method of the
* node. If this is the name of a field then the value passed as the second argument will be checked for equality.
* If this is the name of a function then the return value of the function will be checked for equality against
* the valued passed as the second argument to this function.
* @param {object} [value] - If the first argument (attr) is a property name then this value will be checked against
* the value of the property.
* @returns {GraphNode} A graph node that match the search criteria.
* @example
* // Find the first node that is called `head` and has a model component
* var head = player.findOne(function (node) {
* return node.model && node.name === 'head';
* });
* @example
* // Finds the first node that has the name property set to 'Test'
* var node = parent.findOne('name', 'Test');
*/
findOne(attr, value) {
var i;
var len = this._children.length;
var result = null;
if (attr instanceof Function) {
var fn = attr;
result = fn(this);
if (result)
return this;
for (i = 0; i < len; i++) {
result = this._children[i].findOne(fn);
if (result)
return result;
}
} else {
var testValue;
if (this[attr]) {
if (this[attr] instanceof Function) {
testValue = this[attr]();
} else {
testValue = this[attr];
}
if (testValue === value) {
return this;
}
}
for (i = 0; i < len; i++) {
result = this._children[i].findOne(attr, value);
if (result !== null)
return result;
}
}
return null;
}
/**
* @function
* @name GraphNode#findByTag
* @description Return all graph nodes that satisfy the search query.
* Query can be simply a string, or comma separated strings,
* to have inclusive results of assets that match at least one query.
* A query that consists of an array of tags can be used to match graph nodes that have each tag of array.
* @param {string|string[]} query - Name of a tag or array of tags.
* @returns {GraphNode[]} A list of all graph nodes that match the query.
* @example
* // Return all graph nodes that tagged by `animal`
* var animals = node.findByTag("animal");
* @example
* // Return all graph nodes that tagged by `bird` OR `mammal`
* var birdsAndMammals = node.findByTag("bird", "mammal");
* @example
* // Return all assets that tagged by `carnivore` AND `mammal`
* var meatEatingMammals = node.findByTag(["carnivore", "mammal"]);
* @example
* // Return all assets that tagged by (`carnivore` AND `mammal`) OR (`carnivore` AND `reptile`)
* var meatEatingMammalsAndReptiles = node.findByTag(["carnivore", "mammal"], ["carnivore", "reptile"]);
*/
findByTag() {
var tags = this.tags._processArguments(arguments);
return this._findByTag(tags);
}
_findByTag(tags) {
var result = [];
var i, len = this._children.length;
var descendants;
for (i = 0; i < len; i++) {
if (this._children[i].tags._has(tags))
result.push(this._children[i]);
descendants = this._children[i]._findByTag(tags);
if (descendants.length)
result = result.concat(descendants);
}
return result;
}
/**
* @function
* @name GraphNode#findByName
* @description Get the first node found in the graph with the name. The search
* is depth first.
* @param {string} name - The name of the graph.
* @returns {GraphNode} The first node to be found matching the supplied name.
*/
findByName(name) {
if (this.name === name) return this;
for (var i = 0; i < this._children.length; i++) {
var found = this._children[i].findByName(name);
if (found !== null) return found;
}
return null;
}
/**
* @function
* @name GraphNode#findByPath
* @description Get the first node found in the graph by its full path in the graph.
* The full path has this form 'parent/child/sub-child'. The search is depth first.
* @param {string|Array} path - The full path of the {@link GraphNode} as either a string or array of {@link GraphNode} names.
* @returns {GraphNode} The first node to be found matching the supplied path.
* @example
* var path = this.entity.findByPath('child/another_child');
*/
findByPath(path) {
// if the path isn't an array, split the path in parts. Each part represents a deeper hierarchy level
var parts;
if (Array.isArray(path)) {
if (path.length === 0) return null;
parts = path;
} else {
parts = path.split('/');
}
var currentParent = this;
var result = null;
for (var i = 0, imax = parts.length; i < imax && currentParent; i++) {
var part = parts[i];
result = null;
// check all the children
var children = currentParent._children;
for (var j = 0, jmax = children.length; j < jmax; j++) {
if (children[j].name === part) {
result = children[j];
break;
}
}
// keep going deeper in the hierarchy
currentParent = result;
}
return result;
}
/**
* @function
* @name GraphNode#forEach
* @description Executes a provided function once on this graph node and all of its descendants.
* @param {callbacks.ForEach} callback - The function to execute on the graph node and each descendant.
* @param {object} [thisArg] - Optional value to use as this when executing callback function.
* @example
* // Log the path and name of each node in descendant tree starting with "parent"
* parent.forEach(function (node) {
* console.log(node.path + "/" + node.name);
* });
*/
forEach(callback, thisArg) {
callback.call(thisArg, this);
var children = this._children;
for (var i = 0; i < children.length; i++) {
children[i].forEach(callback, thisArg);
}
}
/**
* @function
* @name GraphNode#isDescendantOf
* @description Check if node is descendant of another node.
* @param {GraphNode} node - Potential ancestor of node.
* @returns {boolean} If node is descendant of another node.
* @example
* if (roof.isDescendantOf(house)) {
* // roof is descendant of house entity
* }
*/
isDescendantOf(node) {
var parent = this._parent;
while (parent) {
if (parent === node)
return true;
parent = parent._parent;
}
return false;
}
/**
* @function
* @name GraphNode#isAncestorOf
* @description Check if node is ancestor for another node.
* @param {GraphNode} node - Potential descendant of node.
* @returns {boolean} If node is ancestor for another node.
* @example
* if (body.isAncestorOf(foot)) {
* // foot is within body's hierarchy
* }
*/
isAncestorOf(node) {
return node.isDescendantOf(this);
}
/**
* @function
* @name GraphNode#getEulerAngles
* @description Get the world space rotation for the specified GraphNode in Euler angle
* form. The order of the returned Euler angles is XYZ. The value returned by this function
* should be considered read-only. In order to set the world-space rotation of the graph
* node, use {@link GraphNode#setEulerAngles}.
* @returns {Vec3} The world space rotation of the graph node in Euler angle form.
* @example
* var angles = this.entity.getEulerAngles(); // [0,0,0]
* angles[1] = 180; // rotate the entity around Y by 180 degrees
* this.entity.setEulerAngles(angles);
*/
getEulerAngles() {
this.getWorldTransform().getEulerAngles(this.eulerAngles);
return this.eulerAngles;
}
/**
* @function
* @name GraphNode#getLocalEulerAngles
* @description Get the rotation in local space for the specified GraphNode. The rotation
* is returned as euler angles in a 3-dimensional vector where the order is XYZ. The
* returned vector should be considered read-only. To update the local rotation, use
* {@link GraphNode#setLocalEulerAngles}.
* @returns {Vec3} The local space rotation of the graph node as euler angles in XYZ order.
* @example
* var angles = this.entity.getLocalEulerAngles();
* angles[1] = 180;
* this.entity.setLocalEulerAngles(angles);
*/
getLocalEulerAngles() {
this.localRotation.getEulerAngles(this.localEulerAngles);
return this.localEulerAngles;
}
/**
* @function
* @name GraphNode#getLocalPosition
* @description Get the position in local space for the specified GraphNode. The position
* is returned as a 3-dimensional vector. The returned vector should be considered read-only.
* To update the local position, use {@link GraphNode#setLocalPosition}.
* @returns {Vec3} The local space position of the graph node.
* @example
* var position = this.entity.getLocalPosition();
* position[0] += 1; // move the entity 1 unit along x.
* this.entity.setLocalPosition(position);
*/
getLocalPosition() {
return this.localPosition;
}
/**
* @function
* @name GraphNode#getLocalRotation
* @description Get the rotation in local space for the specified GraphNode. The rotation
* is returned as a quaternion. The returned quaternion should be considered read-only.
* To update the local rotation, use {@link GraphNode#setLocalRotation}.
* @returns {Quat} The local space rotation of the graph node as a quaternion.
* @example
* var rotation = this.entity.getLocalRotation();
*/
getLocalRotation() {
return this.localRotation;
}
/**
* @function
* @name GraphNode#getLocalScale
* @description Get the scale in local space for the specified GraphNode. The scale
* is returned as a 3-dimensional vector. The returned vector should be considered read-only.
* To update the local scale, use {@link GraphNode#setLocalScale}.
* @returns {Vec3} The local space scale of the graph node.
* @example
* var scale = this.entity.getLocalScale();
* scale.x = 100;
* this.entity.setLocalScale(scale);
*/
getLocalScale() {
return this.localScale;
}
/**
* @function
* @name GraphNode#getLocalTransform
* @description Get the local transform matrix for this graph node. This matrix
* is the transform relative to the node's parent's world transformation matrix.
* @returns {Mat4} The node's local transformation matrix.
* @example
* var transform = this.entity.getLocalTransform();
*/
getLocalTransform() {
if (this._dirtyLocal) {
this.localTransform.setTRS(this.localPosition, this.localRotation, this.localScale);
this._dirtyLocal = false;
}
return this.localTransform;
}
/**
* @function
* @name GraphNode#getPosition
* @description Get the world space position for the specified GraphNode. The
* value returned by this function should be considered read-only. In order to set
* the world-space position of the graph node, use {@link GraphNode#setPosition}.
* @returns {Vec3} The world space position of the graph node.
* @example
* var position = this.entity.getPosition();
* position.x = 10;
* this.entity.setPosition(position);
*/
getPosition() {
this.getWorldTransform().getTranslation(this.position);
return this.position;
}
/**
* @function
* @name GraphNode#getRotation
* @description Get the world space rotation for the specified GraphNode in quaternion
* form. The value returned by this function should be considered read-only. In order
* to set the world-space rotation of the graph node, use {@link GraphNode#setRotation}.
* @returns {Quat} The world space rotation of the graph node as a quaternion.
* @example
* var rotation = this.entity.getRotation();
*/
getRotation() {
this.rotation.setFromMat4(this.getWorldTransform());
return this.rotation;
}
/**
* @private
* @function
* @name GraphNode#getScale
* @description Get the world space scale for the specified GraphNode. The returned value
* will only be correct for graph nodes that have a non-skewed world transform (a skew can
* be introduced by the compounding of rotations and scales higher in the graph node
* hierarchy). The value returned by this function should be considered read-only. Note
* that it is not possible to set the world space scale of a graph node directly.
* @returns {Vec3} The world space scale of the graph node.
* @example
* var scale = this.entity.getScale();
*/
getScale() {
if (!this._scale) {
this._scale = new Vec3();
}
return this.getWorldTransform().getScale(this._scale);
}
/**
* @function
* @name GraphNode#getWorldTransform
* @description Get the world transformation matrix for this graph node.
* @returns {Mat4} The node's world transformation matrix.
* @example
* var transform = this.entity.getWorldTransform();
*/
getWorldTransform() {
if (!this._dirtyLocal && !this._dirtyWorld)
return this.worldTransform;
if (this._parent)
this._parent.getWorldTransform();
this._sync();
return this.worldTransform;
}
/**
* @function
* @name GraphNode#reparent
* @description Remove graph node from current parent and add as child to new parent.
* @param {GraphNode} parent - New parent to attach graph node to.
* @param {number} [index] - The child index where the child node should be placed.
*/
reparent(parent, index) {
var current = this._parent;
if (current)
current.removeChild(this);
if (parent) {
if (index >= 0) {
parent.insertChild(this, index);
} else {
parent.addChild(this);
}
}
}
/**
* @function
* @name GraphNode#setLocalEulerAngles
* @description Sets the local-space rotation of the specified graph node using euler angles.
* Eulers are interpreted in XYZ order. Eulers must be specified in degrees. This function
* has two valid signatures: you can either pass a 3D vector or 3 numbers to specify the
* local-space euler rotation.
* @param {Vec3|number} x - 3-dimensional vector holding eulers or rotation around local-space
* x-axis in degrees.
* @param {number} [y] - Rotation around local-space y-axis in degrees.
* @param {number} [z] - Rotation around local-space z-axis in degrees.
* @example
* // Set rotation of 90 degrees around y-axis via 3 numbers
* this.entity.setLocalEulerAngles(0, 90, 0);
* @example
* // Set rotation of 90 degrees around y-axis via a vector
* var angles = new pc.Vec3(0, 90, 0);
* this.entity.setLocalEulerAngles(angles);
*/
setLocalEulerAngles(x, y, z) {
if (x instanceof Vec3) {
this.localRotation.setFromEulerAngles(x.x, x.y, x.z);
} else {
this.localRotation.setFromEulerAngles(x, y, z);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
/**
* @function
* @name GraphNode#setLocalPosition
* @description Sets the local-space position of the specified graph node. This function
* has two valid signatures: you can either pass a 3D vector or 3 numbers to specify the
* local-space position.
* @param {Vec3|number} x - 3-dimensional vector holding local-space position or
* x-coordinate of local-space position.
* @param {number} [y] - Y-coordinate of local-space position.
* @param {number} [z] - Z-coordinate of local-space position.
* @example
* // Set via 3 numbers
* this.entity.setLocalPosition(0, 10, 0);
* @example
* // Set via vector
* var pos = new pc.Vec3(0, 10, 0);
* this.entity.setLocalPosition(pos);
*/
setLocalPosition(x, y, z) {
if (x instanceof Vec3) {
this.localPosition.copy(x);
} else {
this.localPosition.set(x, y, z);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
/**
* @function
* @name GraphNode#setLocalRotation
* @description Sets the local-space rotation of the specified graph node. This function
* has two valid signatures: you can either pass a quaternion or 3 numbers to specify the
* local-space rotation.
* @param {Quat|number} x - Quaternion holding local-space rotation or x-component of
* local-space quaternion rotation.
* @param {number} [y] - Y-component of local-space quaternion rotation.
* @param {number} [z] - Z-component of local-space quaternion rotation.
* @param {number} [w] - W-component of local-space quaternion rotation.
* @example
* // Set via 4 numbers
* this.entity.setLocalRotation(0, 0, 0, 1);
* @example
* // Set via quaternion
* var q = pc.Quat();
* this.entity.setLocalRotation(q);
*/
setLocalRotation(x, y, z, w) {
if (x instanceof Quat) {
this.localRotation.copy(x);
} else {
this.localRotation.set(x, y, z, w);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
/**
* @function
* @name GraphNode#setLocalScale
* @description Sets the local-space scale factor of the specified graph node. This function
* has two valid signatures: you can either pass a 3D vector or 3 numbers to specify the
* local-space scale.
* @param {Vec3|number} x - 3-dimensional vector holding local-space scale or x-coordinate
* of local-space scale.
* @param {number} [y] - Y-coordinate of local-space scale.
* @param {number} [z] - Z-coordinate of local-space scale.
* @example
* // Set via 3 numbers
* this.entity.setLocalScale(10, 10, 10);
* @example
* // Set via vector
* var scale = new pc.Vec3(10, 10, 10);
* this.entity.setLocalScale(scale);
*/
setLocalScale(x, y, z) {
if (x instanceof Vec3) {
this.localScale.copy(x);
} else {
this.localScale.set(x, y, z);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
_dirtifyLocal() {
if (!this._dirtyLocal) {
this._dirtyLocal = true;
if (!this._dirtyWorld)
this._dirtifyWorld();
}
}
_unfreezeParentToRoot() {
var p = this._parent;
while (p) {
p._frozen = false;
p = p._parent;
}
}
_dirtifyWorld() {
if (!this._dirtyWorld)
this._unfreezeParentToRoot();
this._dirtifyWorldInternal();
}
_dirtifyWorldInternal() {
if (!this._dirtyWorld) {
this._frozen = false;
this._dirtyWorld = true;
for (var i = 0; i < this._children.length; i++) {
if (!this._children[i]._dirtyWorld)
this._children[i]._dirtifyWorldInternal();
}
}
this._dirtyNormal = true;
this._aabbVer++;
}
/**
* @function
* @name GraphNode#setPosition
* @description Sets the world-space position of the specified graph node. This function
* has two valid signatures: you can either pass a 3D vector or 3 numbers to specify the
* world-space position.
* @param {Vec3|number} x - 3-dimensional vector holding world-space position or
* x-coordinate of world-space position.
* @param {number} [y] - Y-coordinate of world-space position.
* @param {number} [z] - Z-coordinate of world-space position.
* @example
* // Set via 3 numbers
* this.entity.setPosition(0, 10, 0);
* @example
* // Set via vector
* var position = new pc.Vec3(0, 10, 0);
* this.entity.setPosition(position);
*/
setPosition(x, y, z) {
if (x instanceof Vec3) {
position.copy(x);
} else {
position.set(x, y, z);
}
if (this._parent === null) {
this.localPosition.copy(position);
} else {
invParentWtm.copy(this._parent.getWorldTransform()).invert();
invParentWtm.transformPoint(position, this.localPosition);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
/**
* @function
* @name GraphNode#setRotation
* @description Sets the world-space rotation of the specified graph node. This function
* has two valid signatures: you can either pass a quaternion or 3 numbers to specify the
* world-space rotation.
* @param {Quat|number} x - Quaternion holding world-space rotation or x-component of
* world-space quaternion rotation.
* @param {number} [y] - Y-component of world-space quaternion rotation.
* @param {number} [z] - Z-component of world-space quaternion rotation.
* @param {number} [w] - W-component of world-space quaternion rotation.
* @example
* // Set via 4 numbers
* this.entity.setRotation(0, 0, 0, 1);
* @example
* // Set via quaternion
* var q = pc.Quat();
* this.entity.setRotation(q);
*/
setRotation(x, y, z, w) {
if (x instanceof Quat) {
rotation.copy(x);
} else {
rotation.set(x, y, z, w);
}
if (this._parent === null) {
this.localRotation.copy(rotation);
} else {
var parentRot = this._parent.getRotation();
invParentRot.copy(parentRot).invert();
this.localRotation.copy(invParentRot).mul(rotation);
}
if (!this._dirtyLocal)
this._dirtifyLocal();
}
/**
* @function
* @name GraphNode#setEulerAngles
* @description Sets the world-space rotation of the specified graph node using euler angles.
* Eulers are interpreted in XYZ order. Eulers must be specified in degrees. This function
* has two valid signatures: you can either pass a 3D vector or 3 numbers to specify the
* world-space euler rotation.
* @param {Vec3|number} x - 3-dimensional vector holding eulers or rotation around world-space
* x-axis in degrees.
* @param {number} [y] - Rotation around world-space y-axis in degrees.
* @param {number} [z] - Rotation around world-space z-axis in degrees.
* @example
* // Set rotation of 90 degrees around world-space y-axis via 3 numbers
* this.entity.setEulerAngles(0, 90, 0);
* @example