-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnode.go
More file actions
1660 lines (1340 loc) · 56.9 KB
/
Copy pathnode.go
File metadata and controls
1660 lines (1340 loc) · 56.9 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
package tetra3d
import (
"fmt"
"math"
"slices"
"strconv"
"strings"
)
type SectorType int
const (
SectorTypeObject SectorType = iota
SectorTypeSector
SectorTypeStandalone
)
// NodeType represents a Node's type. Node types are categorized, and can be said to extend or "be of" more general types.
// For example, a ColliderSphere has a type of NodeTypeColliderSphere. That type can also be said to be NodeTypeCollider
// (because it is a collider-type object). However, it is not of type NodeTypeColliderTriangles, as that is a different category.
type NodeType uint32
const (
NodeTypeNode NodeType = 1 << iota // NodeTypeNode represents specifically a node
NodeTypeModel = 1<<iota + NodeTypeNode // NodeTypeModel represents specifically a Model
NodeTypeCamera // NodeTypeCamera represents specifically a Camera
NodeTypePath // NodeTypePath represents specifically a Path
NodeTypeGrid // NodeTypeGrid represents specifically a Grid
NodeTypeGridPoint // NodeTypeGrid represents specifically a GridPoint (note the extra underscore to ensure !NodeTypeGridPoint.Is(NodeTypeGrid))
NodeTypeCollider = 1<<iota + NodeTypeNode // Represents any generic collider object (e.g. anything that can collide)
NodeTypeColliderAABB = 1<<iota + NodeTypeCollider // Represents specifically a ColliderAABB
NodeTypeColliderCapsule // Represents specifically a ColliderCapsule
NodeTypeColliderTriangles // Represents specifically a ColliderTriangles object
NodeTypeColliderSphere // Represents specifically a ColliderSphere
NodeTypeLight = 1<<iota + NodeTypeNode // NodeTypeLight represents any generic light
NodeTypeAmbientLight = 1<<iota + NodeTypeLight // NodeTypeAmbientLight represents specifically an ambient light
NodeTypePointLight // NodeTypePointLight represents specifically a point light
NodeTypeDirectionalLight // NodeTypeDirectionalLight represents specifically a directional (sun) light
NodeTypeCubeLight // NodeTypeCubeLight represents, specifically, a cube light
NodeTypeLightVolume // NodeTypeLightVolume represents, specifically, a light volume
)
// Is returns true if a NodeType satisfies another NodeType category. A specific node type can be said to
// contain a more general one, but not vice-versa. For example, a Model (which has type NodeTypeModel) can be
// said to be a Node (NodeTypeNode), but the reverse is not true (a NodeTypeNode is not a NodeTypeModel).
func (nt NodeType) Is(other NodeType) bool {
if nt == other {
return true
}
return nt&other == other
}
func (nt NodeType) String() string {
switch nt {
case NodeTypeNode:
return "Node"
case NodeTypeModel:
return "Model"
case NodeTypeCamera:
return "Camera"
case NodeTypePath:
return "Path"
case NodeTypeGrid:
return "Grid"
case NodeTypeGridPoint:
return "GridPoint"
case NodeTypeCollider:
return "ColliderObject"
case NodeTypeColliderAABB:
return "ColliderAABB"
case NodeTypeColliderCapsule:
return "ColliderCapsule"
case NodeTypeColliderTriangles:
return "ColliderTriangles"
case NodeTypeColliderSphere:
return "ColliderSphere"
// case NodeTypeLight: // No object has "light" as a type, because it's a generic type
// return "Light"
case NodeTypeAmbientLight:
return "AmbientLight"
case NodeTypePointLight:
return "PointLight"
case NodeTypeDirectionalLight:
return "DirectionalLight"
case NodeTypeCubeLight:
return "CubeLight"
case NodeTypeLightVolume:
return "LightVolume"
}
panic("This should never happen")
}
// Represents a bit set of boolean values.
// This, for example, provides faster filtering for Nodes than checking properties' names or values.
type Bitfield uint32
func (b Bitfield) String() string {
return fmt.Sprintf("%d", b)
}
// Converts the bit of the given index to a Bitfield value (e.g. ToBitfield(0) == 2, ToBitfield(1) == 4).
func ToBitfield(index int) Bitfield {
return 2 << index
}
// Returns true if the object contains the given bit value.
func (b Bitfield) Contains(value Bitfield) bool {
if value == 0 {
return false
}
if b == value {
return true
}
return b&value == value
}
func (b Bitfield) Add(value Bitfield) Bitfield {
if int(b)+int(value) < math.MaxUint32 {
b += value
} else {
b = 0
}
return b
}
func (b Bitfield) Sub(value Bitfield) Bitfield {
if int(b)-int(value) > 0 {
b -= value
} else {
b = 0
}
return b
}
// INode represents an object that exists in 3D space and can be positioned relative to an origin point.
// By default, this origin point is {0, 0, 0} (or world origin), but Nodes can be parented
// to other Nodes to change this origin (making their movements relative and their transforms
// successive). Models and Cameras are two examples of objects that fully implement the INode interface
// by means of embedding Node.
type INode interface {
// Name returns the object's name.
Name() string
// String returns the object as a string.
String() string
// ID returns the object's unique ID.
ID() uint32
// SetName sets the object's name.
SetName(name string)
// Clone returns a clone of the specified INode implementer.
Clone() INode
// SetData sets user-customizeable data that could be usefully stored on this node.
SetData(data any)
// Data returns a pointer to user-customizeable data that could be usefully stored on this node.
Data() any
// Type returns the NodeType for this object.
Type() NodeType
setLibrary(lib *Library)
// Library returns the source Library from which this Node was instantiated. If it was created through code, this will be nil.
Library() *Library
setParent(INode)
// Parent returns the Node's parent. If the Node has no parent, this will return nil.
Parent() INode
// Unparent unparents the Node from its parent, removing it from the scenegraph.
// Beware of using this in a hierarchical loop!
Unparent()
getOwner() INode
// DescendantOf returns if a Node is a descendant child of a parent Node.
DescendantOf(parent INode) bool
// Scene looks for the Node's parents recursively to return what scene it exists in.
// If the node is not within a tree (i.e. unparented), this will return nil.
Scene() *Scene
// Root returns the root node in this tree by recursively traversing this node's hierarchy of
// parents upwards.
Root() *Node
InSceneTree() bool // Returns if this node is in the scene tree.
setCachedSceneRoot(root *Node)
cachedSceneRoot() *Node
// ReindexChild moves the child in the calling Node's children slice to the specified newPosition.
// The function returns the old index where the child Node was, or -1 if it wasn't a child of the calling Node.
// The newPosition is clamped to the size of the node's children slice.
ReindexChild(child INode, newPosition int) int
// Index returns the index of the Node in its parent's children list.
// If the node doesn't have a parent, its index will be -1.
Index() int
// Returns the Node's children as a NodeCollectionSet.
Children(recursive bool) NodeList
// Returns the number of children under the Node.
// When recursive, it traverses the entire tree underneath the Node;
// otherwise, it just returns the number of children directly beneath the Node.
ChildrenCount(recursive bool) int
// ForEachChild() runs a callback for each child in the Node's children set.
// If the callback returns false, then the execution stops with the current child.
ForEachChild(recursive bool, forEach func(node INode, index int) bool)
// AddChildren parents the provided children Nodes to the passed parent Node, inheriting its transformations and being under it in the scenegraph
// hierarchy. If the children are already parented to other Nodes, they are unparented before doing so.
AddChildren(...INode)
// RemoveChildren removes the provided children from this object.
// Beware of using this in a hierarchical loop!
RemoveChildren(...INode)
// Replaces the node with the other target node.
ReplaceWith(other INode)
// updateLocalTransform(newParent INode)
dirtyTransform()
// ClearLocalTransform clears the local transform properties (position, scale, and rotation) for the Node, reverting it to essentially an
// identity matrix (0, 0, 0 for position, 1, 1, 1 for scale, and an identity Matrix4 for rotation, indicating no rotation).
// This can be useful because by default, when you parent one Node to another, the local transform properties (position,
// scale, and rotation) are altered to keep the object in the same absolute location, even though the origin changes.
ClearLocalTransform()
// ResetWorldTransform resets the local transform properties (position, scale, and rotation) for the Node to the original transform when
// the Node was first created / cloned / instantiated in the Scene.
ResetWorldTransform()
// ResetWorldPosition resets the Node's local position to the value the Node had when
// it was first instantiated in the Scene or cloned.
ResetWorldPosition()
// ResetWorldScale resets the Node's local scale to the value the Node had when
// it was first instantiated in the Scene or cloned.
ResetWorldScale()
// ResetWorldRotation resets the Node's local rotation to the value the Node had when
// it was first instantiated in the Scene or cloned.
ResetWorldRotation()
getOriginalTransform() Matrix4
setOriginalTransform()
// SetWorldTransform sets the Node's global (world) transform to the full 4x4 transformation matrix provided.
SetWorldTransform(transform Matrix4)
// LocalRotation returns the object's local rotation Matrix4.
LocalRotation() Matrix4
// SetLocalRotation sets the object's local rotation Matrix4 (relative to any parent).
SetLocalRotation(rotation Matrix4)
// LocalPosition returns the object's local position as a Vector.
LocalPosition() Vector3
// SetLocalPosition sets the object's local position (position relative to its parent). If this object has no parent, the position should be
// relative to world origin (0, 0, 0).
SetLocalPositionVec(position Vector3)
// SetLocalPosition sets the object's local position (position relative to its parent). If this object has no parent, the position should be
// relative to world origin (0, 0, 0).
SetLocalPosition(x, y, z float32)
// LocalScale returns the object's local scale (scale relative to its parent). If this object has no parent, the scale will be absolute.
LocalScale() Vector3
// SetLocalScale sets the object's local scale (scale relative to its parent). If this object has no parent, the scale would be absolute.
// scale should be a 3D vector (i.e. X, Y, and Z components).
SetLocalScaleVec(scale Vector3)
SetLocalScale(w, h, d float32)
// WorldRotation returns an absolute rotation Matrix4 representing the object's rotation.
WorldRotation() Matrix4
// SetWorldRotation sets an object's global, world rotation to the provided rotation Matrix4.
SetWorldRotation(rotation Matrix4)
// WorldPosition returns the node's world position, taking into account its parenting hierarchy.
WorldPosition() Vector3
// SetWorldPositionVec sets the world position of the given object using the provided position vector.
// Note that this isn't as performant as setting the position locally.
SetWorldPositionVec(position Vector3)
// SetWorldPosition sets the world position of the given object using the provided position arguments.
// Note that this isn't as performant as setting the position locally.
SetWorldPosition(x, y, z float32)
// SetWorldX sets the x component of the Node's world position.
SetWorldX(x float32)
// SetWorldY sets the y component of the Node's world position.
SetWorldY(x float32)
// SetWorldZ sets the z component of the Node's world position.
SetWorldZ(x float32)
// WorldScale returns the object's absolute world scale as a 3D vector (i.e. X, Y, and Z components).
WorldScale() Vector3
// SetWorldScaleVec sets the object's absolute world scale. scale should be a 3D vector (i.e. X, Y, and Z components).
SetWorldScaleVec(scale Vector3)
// SetWorldScale sets the object's absolute world scale. scale should be three float32 values signifying the scale on the X, Y, and Z components.
SetWorldScale(x, y, z float32)
// Move moves a Node in local space by the x, y, and z values provided.
Move(x, y, z float32)
// MoveVec moves a Node in local space using the vector provided.
MoveVec(moveVec Vector3)
// Moves a Node in local space, by the object's world orientation, by the x, y, and z values provided.
MoveByOrientation(x, y, z float32)
// Moves a Node in local space, by the object's world orientation, using the vector provided.
MoveByOrientationVec(moveVec Vector3)
// Moves a Node towards the node provided by the distance given in world space.
MoveTowardsNode(target INode, distance float32)
// Linearly interpolates movement of the node towards the target location by the given percentage.
MoveTowardsNodeLerp(target INode, percentage float32)
// Moves a Node towards the node provided by the distance given in world space.
MoveTowardsVec(target Vector3, distance float32)
// Linearly interpolates movement of the node towards the target location by the given percentage.
MoveTowardsVecLerp(target Vector3, percentage float32)
// Moves a Node towards the node provided by the distance given in world space.
MoveTowards(x, y, z, distance float32)
// Linearly interpolates movement of the node towards the target location by the given percentage.
MoveTowardsLerp(x, y, z, percentage float32)
// Rotate rotates a Node on its local orientation on a vector composed of the given x, y, and z values, by the angle provided in radians.
Rotate(x, y, z, angle float32)
// RotateVec rotates a Node on its local orientation on the given vector, by the angle provided in radians.
RotateVec(vec Vector3, angle float32)
// Rotates a Node on its local orientation using a rotation Matrix4.
RotateMatrix4(mat Matrix4)
// Grow scales the object additively (i.e. calling Node.Grow(1, 0, 0) will scale it +1 on the X-axis).
Grow(x, y, z float32)
// GrowVec scales the object additively (i.e. calling Node.Grow(1, 0, 0) will scale it +1 on the X-axis).
GrowVec(vec Vector3)
// Transform returns a Matrix4 indicating the global position, rotation, and scale of the object, transforming it by any parents'.
// If there's no change between the previous Transform() call and this one, Transform() will return a cached version of the
// transform for efficiency.
Transform() Matrix4
// IsVisible returns whether the Object is visible.
IsVisible() bool
// SetVisible sets the object's visibility. If recursive is true, all recursive children of this Node will have their visibility set the same way.
SetVisible(visible, recursive bool)
// Get searches a node's hierarchy using a string to find a specified node. The path is in the format of names of nodes, separated by forward
// slashes ('/'), and is relative to the node you use to call Get. As an example of Get, if you had a cup parented to a desk, which was
// parented to a room, that was finally parented to the root of the scene, it would be found at "Room/Desk/Cup". Note also that you can use "../" to
// "go up one" in the hierarchy (so cup.Get("../") would return the Desk node).
// Since Get uses forward slashes as path separation, it would be good to avoid using forward slashes in your Node names. Also note that Get()
// trims the extra spaces from the beginning and end of Node Names, so avoid using spaces at the beginning or end of your Nodes' names.
Get(path string) INode
// Search searches a node's hierarchy using a string to find the specified Node.
Search() SearchOptions
// HierarchyAsString returns a string displaying the hierarchy of this Node, and all recursive children.
// This is a useful function to debug the layout of a node tree, for example.
HierarchyAsString() string
// Path returns a string indicating the hierarchical path to get this Node from the root. The path returned will be absolute, such that
// passing it to Get() called on the scene root node will return this node. The path returned will not contain the root node's name ("Root").
Path() string
// Properties returns this object's game Properties struct.
Properties() *Properties
// Returns if the Node contains a property that has a bit flipped that is named in the Node's Scene by the given name.
PropertiesContainsBitByName(bitfieldBitName string) bool
// IsBone returns if the Node is a "bone" (a node that was a part of an armature and so can play animations back to influence a skinned mesh).
IsBone() bool
// IsRootBone() bool
// AnimationPlayer returns the object's animation player - every object has an AnimationPlayer by default.
AnimationPlayer() *AnimationPlayer
// Sector returns the Sector this Node is in.
Sector() *Sector
sectorHierarchy() *Sector
isInVisibleSector(sectorsModels []*Model) bool
SetSectorType(sectorType SectorType)
SectorType() SectorType
// DistanceTo returns the distance between the given Nodes' centers.
// Quick syntactic sugar for Node.WorldPosition().Distance(otherNode.WorldPosition()).
DistanceTo(otherNode INode) float32
// DistanceSquared returns the squared distance between the given Nodes' centers.
// Quick syntactic sugar for Node.WorldPosition().DistanceSquared(otherNode.WorldPosition()).
DistanceSquaredTo(otherNode INode) float32
// VectorTo returns a vector from one Node to another.
// Quick syntactic sugar for other.WorldPosition().Sub(node.WorldPosition()).
VectorTo(otherNode INode) Vector3
// Callbacks returns a Node's callbacks object. This object represents the callbacks that a Node has access to when events happen.
Callbacks() *NodeCallbacks
}
// There is no zero ID; this is to make it so that systems that reference nodes by id can use 0 as an invalid reference.
var nodeID uint32 = 1
// Node represents a minimal struct that fully implements the Node interface. Model and Camera embed Node
// into their structs to automatically easily implement Node.
type Node struct {
id uint32 // Unique ID for this node
owner INode // The owner; nil if this is a Node, set to the "owning" node type otherwise (e.g. node.owner = nil, model.owner (or model.Node.owner) = model)
name string
position Vector3
scale Vector3
rotation Matrix4
originalTransform Matrix4
visible bool
data any // A place to store a pointer to something if you need it
children []INode
parent INode
cachedTransform Matrix4
isTransformDirty bool
props *Properties // Properties is an unordered set of properties, representing a means of identifying and setting game properties on Nodes.
animationPlayer *AnimationPlayer
inverseBindMatrix Matrix4 // Specifically for bones in an armature used for animating skinned meshes
isBone bool
collectionObjects []INode // Returns if the node is a collection instance
boneInfluence Matrix4
library *Library // The Library this Node was instantiated from (nil if it wasn't instantiated with a library at all)
scene *Scene
onTransformUpdate func()
sectorType SectorType
cachedSceneRootNode *Node
cachedSector *Sector
callbacks *NodeCallbacks
}
// NewNode returns a new Node.
func NewNode(name string) *Node {
nb := &Node{
id: nodeID,
name: name,
// position: NewVectorZero(),
scale: Vector3{1, 1, 1},
rotation: NewMatrix4(),
children: []INode{},
visible: true,
isTransformDirty: true,
props: NewProperties(),
// We set this just in case we call a transform property getter before setting it and caching anything
cachedTransform: NewMatrix4(),
callbacks: &NodeCallbacks{},
// originalLocalPosition: NewVectorZero(),
}
nodeID++
nb.animationPlayer = NewAnimationPlayer(nb)
return nb
}
// Callbacks returns a Node's callbacks object. This object represents the callbacks that a Node has access to when events happen.
func (node *Node) Callbacks() *NodeCallbacks {
return node.callbacks
}
// ID returns the object's unique ID.
func (node *Node) ID() uint32 {
return node.id
}
// Name returns the object's name.
func (node *Node) Name() string {
return node.name
}
// SetName sets the object's name.
func (node *Node) SetName(name string) {
node.name = name
}
// Type returns the NodeType for this object.
func (node *Node) Type() NodeType {
return NodeTypeNode
}
// Library returns the Library from which this Node was instantiated. If it was created through code, this will be nil.
func (node *Node) Library() *Library {
return node.library
}
func (node *Node) setLibrary(library *Library) {
node.library = library
}
// Clone returns a new Node.
func (node *Node) Clone() INode {
return node.clone(nil)
}
func (node *Node) clone(newOwner INode) INode {
newNode := NewNode(node.name)
newNode.owner = newOwner
newNode.scene = node.scene
newNode.position = node.position
newNode.scale = node.scale
newNode.rotation = node.rotation.Clone()
newNode.setOriginalTransform()
newNode.visible = node.visible
newNode.data = node.data
newNode.sectorType = node.sectorType
newNode.cachedSector = node.cachedSector
newNode.library = node.library
// Clone the callbacks as well.
newCallbacks := *node.callbacks
newNode.callbacks = &newCallbacks
newNode.props = node.props.Clone()
newNode.animationPlayer = node.animationPlayer.Clone()
if node.animationPlayer.RootNode == node {
newNode.animationPlayer.SetRoot(newNode)
}
newNode.children = slices.Grow(newNode.children, len(node.children))
for _, child := range node.children {
childClone := child.Clone()
childClone.setParent(newNode)
newNode.children = append(newNode.children, childClone)
}
for _, child := range newNode.children {
if model, isModel := child.(*Model); isModel && model.SkinRoot == node {
model.ReassignBones(newNode)
}
}
newNode.dirtyTransform()
newNode.isBone = node.isBone
if newNode.isBone {
newNode.inverseBindMatrix = node.inverseBindMatrix.Clone()
}
// If newOwner != nil, then that object that is cloning will call the callback
if runCallbacks && newOwner == nil && newNode.Callbacks().OnClone != nil {
newNode.Callbacks().OnClone(newNode)
}
return newNode
}
// SetData sets user-customizeable data that could be usefully stored on this node.
func (node *Node) SetData(data any) {
node.data = data
}
// Data returns a pointer to user-customizeable data that could be usefully stored on this node.
func (node *Node) Data() any {
return node.data
}
// Transform returns a Matrix4 indicating the global position, rotation, and scale of the object, transforming it by any parents'.
// If there's no change between the previous Transform() call and this one, Transform() will return a cached version of the
// transform for efficiency.
func (node *Node) Transform() Matrix4 {
// T * R * S * O
if !node.isTransformDirty {
return node.cachedTransform
}
// TODO: I think I could speed up this area considerably.
transform := NewMatrix4Scale(node.scale.X, node.scale.Y, node.scale.Z)
transform = transform.Mult(node.rotation)
transform = transform.Mult(NewMatrix4Translate(node.position.X, node.position.Y, node.position.Z))
if node.parent != nil {
transform = transform.Mult(node.parent.Transform())
}
node.cachedTransform = transform
node.isTransformDirty = false
if node.isBone {
node.boneInfluence = node.inverseBindMatrix.Mult(transform)
}
if node.onTransformUpdate != nil {
node.onTransformUpdate()
}
// We want to call child.Transform() here to ensure the children also rebuild their transforms as necessary; otherwise,
// children (i.e. ColliderAABBs) may not be rotating along with their owning Nodes (as they don't get rendered).
for _, child := range node.children {
child.Transform()
}
return transform
}
// SetWorldTransform sets the Node's global (world) transform to the full 4x4 transformation matrix provided.
func (node *Node) SetWorldTransform(transform Matrix4) {
position, scale, rotationMatrix := transform.Decompose()
node.SetWorldPositionVec(position)
node.SetWorldScaleVec(scale)
node.SetWorldRotation(rotationMatrix)
}
// dirtyTransform sets this Node and all recursive children's isTransformDirty flags to be true, indicating that they need to be
// rebuilt. This should be called when modifying the transformation properties (position, scale, rotation) of the Node.
func (node *Node) dirtyTransform() {
for _, child := range node.children {
child.dirtyTransform()
}
node.isTransformDirty = true
node.cachedSector = nil
}
// updateLocalTransform updates the local transform properties for a Node given a change in parenting. This is done so that, for example,
// parenting an object with a given postiion, scale, and rotation keeps those visual properties when parenting (by updating them to take into
// account the parent's transforms as well).
// func (node *Node) updateLocalTransform(newParent INode) {
// if newParent != nil {
// parentTransform := newParent.Transform()
// parentPos, parentScale, parentRot := parentTransform.Decompose()
// diff := node.position.Sub(parentPos)
// diff[0] /= parentScale[0]
// diff[1] /= parentScale[1]
// diff[2] /= parentScale[2]
// node.position = parentRot.Transposed().MultVec(diff)
// node.rotation = node.rotation.Mult(parentRot.Transposed())
// node.scale[0] /= parentScale[0]
// node.scale[1] /= parentScale[1]
// node.scale[2] /= parentScale[2]
// } else {
// // Reverse
// parentTransform := node.Parent().Transform()
// parentPos, parentScale, parentRot := parentTransform.Decompose()
// pr := parentRot.MultVec(node.position)
// pr[0] *= parentScale[0]
// pr[1] *= parentScale[1]
// pr[2] *= parentScale[2]
// node.position = parentPos.Add(pr)
// node.rotation = node.rotation.Mult(parentRot)
// node.scale[0] *= parentScale[0]
// node.scale[1] *= parentScale[1]
// node.scale[2] *= parentScale[2]
// }
// node.dirtyTransform()
// }
// LocalPosition returns a 3D Vector consisting of the object's local position (position relative to its parent). If this object has no parent, the position will be
// relative to world origin (0, 0, 0).
func (node *Node) LocalPosition() Vector3 {
return node.position
}
// ClearLocalTransform clears the local transform properties (position, scale, and rotation) for the Node, reverting it to essentially an
// identity matrix (0, 0, 0 for position, 1, 1, 1 for scale, and an identity Matrix4 for rotation, indicating no rotation).
// This can be useful because by default, when you parent one Node to another, the local transform properties (position,
// scale, and rotation) are altered to keep the object in the same absolute location, even though the origin changes.
func (node *Node) ClearLocalTransform() {
node.position.X = 0
node.position.Y = 0
node.position.Z = 0
node.scale.X = 1
node.scale.Y = 1
node.scale.Z = 1
node.rotation = NewMatrix4()
node.dirtyTransform()
}
// ResetWorldTransform resets the Node's world transform properties (position, scale, and rotation) for the Node to the original
// values when the Node was first instantiated in the Scene or cloned.
func (node *Node) ResetWorldTransform() {
node.SetWorldTransform(node.originalTransform)
node.dirtyTransform()
}
// ResetWorldPosition resets the Node's local position to the value the Node had when
// it was first instantiated in the Scene or cloned.
func (node *Node) ResetWorldPosition() {
p, _, _ := node.originalTransform.Decompose()
node.SetWorldPositionVec(p)
node.dirtyTransform()
}
// ResetWorldScale resets the Node's local scale to the value the Node had when
// it was first instantiated in the Scene or cloned.
func (node *Node) ResetWorldScale() {
_, s, _ := node.originalTransform.Decompose()
node.SetWorldScaleVec(s)
node.dirtyTransform()
}
// ResetWorldRotation resets the Node's local rotation to the value the Node had when
// it was first instantiated in the Scene or cloned.
func (node *Node) ResetWorldRotation() {
_, _, r := node.originalTransform.Decompose()
node.SetWorldRotation(r)
node.dirtyTransform()
}
func (node *Node) setOriginalTransform() {
node.originalTransform = node.Transform()
}
func (node *Node) getOriginalTransform() Matrix4 {
return node.originalTransform
}
// WorldPosition returns a 3D Vector consisting of the object's world position (position relative to the world origin point of {0, 0, 0}).
func (node *Node) WorldPosition() Vector3 {
return node.Transform().RowAsVector3(3) // We don't want to have to decompose if we don't have to
}
// SetLocalPosition sets the object's local position (position relative to its parent). If this object has no parent, the position should be
// relative to world origin (0, 0, 0). position should be a 3D vector (i.e. X, Y, and Z components).
func (node *Node) SetLocalPosition(x, y, z float32) {
node.position.X = x
node.position.Y = y
node.position.Z = z
node.dirtyTransform()
}
// SetLocalPositionVec sets the object's local position (position relative to its parent). If this object has no parent, the position should be
// relative to world origin (0, 0, 0). position should be a 3D vector (i.e. X, Y, and Z components).
func (node *Node) SetLocalPositionVec(position Vector3) {
node.SetLocalPosition(position.X, position.Y, position.Z)
}
// SetLocalX sets the X component of the object's local position.
func (node *Node) SetLocalX(x float32) {
node.position.X = x
node.dirtyTransform()
}
// SetLocalY sets the Y component of the object's local position.
func (node *Node) SetLocalY(y float32) {
node.position.Y = y
node.dirtyTransform()
}
// SetLocalZ sets the Z component of the object's local position.
func (node *Node) SetLocalZ(z float32) {
node.position.Z = z
node.dirtyTransform()
}
// SetWorldPositionVec sets the object's world position (position relative to the world origin point of {0, 0, 0}).
// position needs to be a 3D vector (i.e. X, Y, and Z components).
func (node *Node) SetWorldPosition(x, y, z float32) {
if node.parent != nil {
parentTransform := node.parent.Transform()
parentPos, parentScale, parentRot := parentTransform.Decompose()
pr := parentRot.Transposed().MultVec(Vector3{X: x, Y: y, Z: z}.Sub(parentPos))
pr.X /= parentScale.X
pr.Y /= parentScale.Y
pr.Z /= parentScale.Z
node.position = pr
} else {
node.position.X = x
node.position.Y = y
node.position.Z = z
}
node.dirtyTransform()
}
// SetWorldPosition sets the object's world position (position relative to the world origin point of {0, 0, 0}).
func (node *Node) SetWorldPositionVec(vec Vector3) {
node.SetWorldPosition(vec.X, vec.Y, vec.Z)
}
// SetWorldX sets the X component of the object's world position.
func (node *Node) SetWorldX(x float32) {
v := node.WorldPosition()
v.X = x
node.SetWorldPositionVec(v)
}
// SetWorldY sets the Y component of the object's world position.
func (node *Node) SetWorldY(y float32) {
v := node.WorldPosition()
v.Y = y
node.SetWorldPositionVec(v)
}
// SetWorldZ sets the Z component of the object's world position.
func (node *Node) SetWorldZ(z float32) {
v := node.WorldPosition()
v.Z = z
node.SetWorldPositionVec(v)
}
// LocalScale returns the object's local scale (scale relative to its parent). If this object has no parent, the scale will be absolute.
func (node *Node) LocalScale() Vector3 {
return node.scale
}
// SetLocalScaleVec sets the object's local scale (scale relative to its parent). If this object has no parent, the scale would be absolute.
// scale should be a 3D vector (i.e. X, Y, and Z components).
func (node *Node) SetLocalScaleVec(scale Vector3) {
node.SetLocalScale(scale.X, scale.Y, scale.Z)
}
// SetLocalScale sets the object's local scale (scale relative to its parent). If this object has no parent, the scale would be absolute.
func (node *Node) SetLocalScale(w, h, d float32) {
node.scale.X = w
node.scale.Y = h
node.scale.Z = d
node.dirtyTransform()
}
// WorldScale returns the object's absolute world scale as a 3D vector (i.e. X, Y, and Z components). Note that this is a bit slow as it
// requires decomposing the node's world transform, so you want to use node.LocalScale() if you can and performacne is a concern.
func (node *Node) WorldScale() Vector3 {
needToDecompose := false
parent := node.parent
for parent != nil {
if scale := parent.LocalScale(); scale.X != 1 || scale.Y != 1 || scale.Z != 1 {
needToDecompose = true
break
}
parent = parent.Parent()
}
// Only decompose if we absolutely need to
if needToDecompose {
return node.Transform().DecomposeScale()
}
return node.LocalScale()
}
// SetWorldScaleVec sets the object's absolute world scale. scale should be a 3D vector (i.e. X, Y, and Z components).
func (node *Node) SetWorldScaleVec(scale Vector3) {
node.SetWorldScale(scale.X, scale.Y, scale.Z)
}
// SetWorldScale sets the object's absolute world scale.
func (node *Node) SetWorldScale(w, h, d float32) {
if node.parent != nil {
parentTransform := node.parent.Transform()
_, parentScale, _ := parentTransform.Decompose()
node.scale = Vector3{
w / parentScale.X,
h / parentScale.Y,
d / parentScale.Z,
}
} else {
node.scale.X = w
node.scale.Y = h
node.scale.Z = d
}
node.dirtyTransform()
}
// LocalRotation returns the object's local rotation Matrix4.
func (node *Node) LocalRotation() Matrix4 {
return node.rotation.Clone()
}
// SetLocalRotation sets the object's local rotation Matrix4 (relative to any parent).
func (node *Node) SetLocalRotation(rotation Matrix4) {
if rotation.IsZero() {
return
}
node.rotation.Set(rotation)
node.dirtyTransform()
}
// WorldRotation returns an absolute rotation Matrix4 representing the object's rotation. Note that this is a bit slow as it
// requires decomposing the node's world transform, so you want to use node.LocalRotation() if you can and performacne is a concern.
func (node *Node) WorldRotation() Matrix4 {
needToDecompose := false
parent := node.parent
for parent != nil {
if rot := parent.LocalRotation(); !rot.IsIdentity() {
needToDecompose = true
break
}
parent = parent.Parent()
}
// Only decompose if we absolutely need to
if needToDecompose {
return node.Transform().DecomposeRotation()
}
return node.LocalRotation()
}
// SetWorldRotation sets an object's rotation to the provided rotation Matrix4.
func (node *Node) SetWorldRotation(rotation Matrix4) {
if node.parent != nil {
parentTransform := node.parent.Transform()
_, _, parentRot := parentTransform.Decompose()
node.rotation.Set(parentRot.Transposed().Mult(rotation))
} else {
node.rotation.Set(rotation)
}
node.dirtyTransform()
}
// Move moves a Node in local space by the x, y, and z values provided.
func (node *Node) Move(x, y, z float32) {
if x == 0 && y == 0 && z == 0 {
return
}
node.position.X += x
node.position.Y += y
node.position.Z += z
node.dirtyTransform()
}
// MoveVec moves a Node in local space using the vector provided.
func (node *Node) MoveVec(vec Vector3) {
node.Move(vec.X, vec.Y, vec.Z)
}
// MoveByOrientation moves a Node in local space by the x, y, and z values provided.
func (node *Node) MoveByOrientation(x, y, z float32) {
if x == 0 && y == 0 && z == 0 {
return
}
rot := node.WorldRotation()
right := rot.Right().Scale(x)
up := rot.Up().Scale(y)
forward := rot.Forward().Scale(z)
node.position.X += right.X
node.position.Y += right.Y
node.position.Z += right.Z
node.position.X += up.X
node.position.Y += up.Y
node.position.Z += up.Z
node.position.X += forward.X
node.position.Y += forward.Y
node.position.Z += forward.Z
node.dirtyTransform()
}
// MoveByOrientationVec moves a Node in local space using the vector provided.
func (node *Node) MoveByOrientationVec(vec Vector3) {
node.MoveByOrientation(vec.X, vec.Y, vec.Z)
}
// MoveTowardsNode moves the node towards the specified target Node in world space by the distance provided.
// The movement is capped to the distance to the target location.
func (node *Node) MoveTowardsNode(target INode, distance float32) {
node.SetWorldPositionVec(node.WorldPosition().MoveTowards(target.WorldPosition(), distance))
}
// Linearly interpolates the node moving towards the target vector in world space by the percentage provided.
func (node *Node) MoveTowardsNodeLerp(target INode, percentage float32) {