node.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package core
  5. import (
  6. "math"
  7. "strings"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/math32"
  10. )
  11. // INode is the interface for all node types.
  12. type INode interface {
  13. IDispatcher
  14. GetNode() *Node
  15. GetINode() INode
  16. Visible() bool
  17. SetVisible(state bool)
  18. Name() string
  19. SetName(string)
  20. Parent() INode
  21. Children() []INode
  22. IsAncestorOf(INode) bool
  23. LowestCommonAncestor(INode) INode
  24. UpdateMatrixWorld()
  25. BoundingBox() math32.Box3
  26. Render(gs *gls.GLS)
  27. Clone() INode
  28. Dispose()
  29. Position() math32.Vector3
  30. Rotation() math32.Vector3
  31. Scale() math32.Vector3
  32. }
  33. // Node events.
  34. const (
  35. OnDescendant = "core.OnDescendant" // Dispatched when a descendent is added or removed
  36. )
  37. // Node represents an object in 3D space existing within a hierarchy.
  38. type Node struct {
  39. Dispatcher // Embedded event dispatcher
  40. inode INode // The INode associated with this Node
  41. parent INode // Parent node
  42. children []INode // Children nodes
  43. name string // Optional node name
  44. loaderID string // ID used by loader
  45. visible bool // Whether the node is visible
  46. matNeedsUpdate bool // Whether the the local matrix needs to be updated because position or scale has changed
  47. rotNeedsUpdate bool // Whether the euler rotation and local matrix need to be updated because the quaternion has changed
  48. userData interface{} // Generic user data
  49. // Spatial properties
  50. position math32.Vector3 // Node position in 3D space (relative to parent)
  51. scale math32.Vector3 // Node scale (relative to parent)
  52. direction math32.Vector3 // Initial direction (relative to parent)
  53. rotation math32.Vector3 // Node rotation specified in Euler angles (relative to parent)
  54. quaternion math32.Quaternion // Node rotation specified as a Quaternion (relative to parent)
  55. // Local transform matrix stores position/rotation/scale relative to parent
  56. matrix math32.Matrix4
  57. // World transform matrix stores position/rotation/scale relative to highest ancestor (generally the scene)
  58. matrixWorld math32.Matrix4
  59. }
  60. // NewNode returns a pointer to a new Node.
  61. func NewNode() *Node {
  62. n := new(Node)
  63. n.Init(n)
  64. return n
  65. }
  66. // Init initializes the node.
  67. // Normally called by other types which embed a Node.
  68. func (n *Node) Init(inode INode) {
  69. n.Dispatcher.Initialize()
  70. n.inode = inode
  71. n.children = make([]INode, 0)
  72. n.visible = true
  73. // Initialize spatial properties
  74. n.position.Set(0, 0, 0)
  75. n.scale.Set(1, 1, 1)
  76. n.direction.Set(0, 0, 1)
  77. n.rotation.Set(0, 0, 0)
  78. n.quaternion.Set(0, 0, 0, 1)
  79. n.matrix.Identity()
  80. n.matrixWorld.Identity()
  81. // Subscribe to events
  82. n.Subscribe(OnDescendant, func(evname string, ev interface{}) {
  83. if n.parent != nil {
  84. n.parent.Dispatch(evname, ev)
  85. }
  86. })
  87. }
  88. // GetINode returns the INode associated with this Node.
  89. func (n *Node) GetINode() INode {
  90. return n.inode
  91. }
  92. // GetNode satisfies the INode interface
  93. // and returns a pointer to the embedded Node.
  94. func (n *Node) GetNode() *Node {
  95. return n
  96. }
  97. // BoundingBox satisfies the INode interface.
  98. // Computes union of own bounding box with those of all descendents.
  99. func (n *Node) BoundingBox() math32.Box3 {
  100. bbox := math32.Box3{
  101. Min: math32.Vector3{X: math.MaxFloat32, Y: math.MaxFloat32, Z: math.MaxFloat32},
  102. Max: math32.Vector3{X: -math.MaxFloat32, Y: -math.MaxFloat32, Z: -math.MaxFloat32},
  103. }
  104. for _, inode := range n.Children() {
  105. childBbox := inode.BoundingBox()
  106. bbox.Union(&childBbox)
  107. }
  108. return bbox
  109. }
  110. // Render satisfies the INode interface.
  111. func (n *Node) Render(gs *gls.GLS) {}
  112. // Dispose satisfies the INode interface.
  113. func (n *Node) Dispose() {
  114. for _, child := range n.children {
  115. child.Dispose()
  116. }
  117. }
  118. // Clone clones the Node and satisfies the INode interface.
  119. func (n *Node) Clone() INode {
  120. clone := new(Node)
  121. // TODO clone Dispatcher?
  122. clone.Dispatcher.Initialize()
  123. clone.parent = n.parent
  124. clone.name = n.name + " (Clone)" // TODO append count?
  125. clone.loaderID = n.loaderID
  126. clone.visible = n.visible
  127. clone.userData = n.userData
  128. // Update matrix world and rotation if necessary
  129. n.UpdateMatrixWorld()
  130. n.Rotation()
  131. // Clone spatial properties
  132. clone.position = n.position
  133. clone.scale = n.scale
  134. clone.direction = n.direction
  135. clone.rotation = n.rotation
  136. clone.quaternion = n.quaternion
  137. clone.matrix = n.matrix
  138. clone.matrixWorld = n.matrixWorld
  139. clone.children = make([]INode, 0)
  140. // Clone children recursively
  141. for _, child := range n.children {
  142. clone.Add(child.Clone())
  143. }
  144. return clone
  145. }
  146. // Parent returns the parent.
  147. func (n *Node) Parent() INode {
  148. return n.parent
  149. }
  150. // SetName sets the (optional) name.
  151. // The name can be used for debugging or other purposes.
  152. func (n *Node) SetName(name string) {
  153. n.name = name
  154. }
  155. // Name returns the (optional) name.
  156. func (n *Node) Name() string {
  157. return n.name
  158. }
  159. // SetLoaderID is normally used by external loaders, such as Collada,
  160. // to assign an ID to the node with the ID value in the node description.
  161. // Can be used to find other loaded nodes.
  162. func (n *Node) SetLoaderID(id string) {
  163. n.loaderID = id
  164. }
  165. // LoaderID returns an optional ID set when this node was
  166. // created by an external loader such as Collada.
  167. func (n *Node) LoaderID() string {
  168. return n.loaderID
  169. }
  170. // SetVisible sets the visibility of the node.
  171. func (n *Node) SetVisible(state bool) {
  172. n.visible = state
  173. n.matNeedsUpdate = true
  174. }
  175. // Visible returns the visibility of the node.
  176. func (n *Node) Visible() bool {
  177. return n.visible
  178. }
  179. // SetChanged sets the matNeedsUpdate flag of the node.
  180. func (n *Node) SetChanged(changed bool) {
  181. n.matNeedsUpdate = changed
  182. }
  183. // Changed returns the matNeedsUpdate flag of the node.
  184. func (n *Node) Changed() bool {
  185. return n.matNeedsUpdate
  186. }
  187. // SetUserData sets the generic user data associated to the node.
  188. func (n *Node) SetUserData(data interface{}) {
  189. n.userData = data
  190. }
  191. // UserData returns the generic user data associated to the node.
  192. func (n *Node) UserData() interface{} {
  193. return n.userData
  194. }
  195. // FindPath finds a node with the specified path starting with this node and
  196. // searching in all its children recursively.
  197. // A path is the sequence of the names from the first node to the desired node
  198. // separated by the forward slash.
  199. func (n *Node) FindPath(path string) INode {
  200. // Internal recursive function to find node
  201. var finder func(inode INode, path string) INode
  202. finder = func(inode INode, path string) INode {
  203. // Get first component of the path
  204. parts := strings.Split(path, "/")
  205. if len(parts) == 0 {
  206. return nil
  207. }
  208. first := parts[0]
  209. // Checks current node
  210. node := inode.GetNode()
  211. if node.name != first {
  212. return nil
  213. }
  214. // If the path has finished this is the desired node
  215. rest := strings.Join(parts[1:], "/")
  216. if rest == "" {
  217. return inode
  218. }
  219. // Otherwise search in this node children
  220. for _, ichild := range node.children {
  221. found := finder(ichild, rest)
  222. if found != nil {
  223. return found
  224. }
  225. }
  226. return nil
  227. }
  228. return finder(n, path)
  229. }
  230. // FindLoaderID looks in the specified node and in all its children
  231. // for a node with the specified loaderID and if found returns it.
  232. // Returns nil if not found.
  233. func (n *Node) FindLoaderID(id string) INode {
  234. var finder func(parent INode, id string) INode
  235. finder = func(parent INode, id string) INode {
  236. pnode := parent.GetNode()
  237. if pnode.loaderID == id {
  238. return parent
  239. }
  240. for _, child := range pnode.children {
  241. found := finder(child, id)
  242. if found != nil {
  243. return found
  244. }
  245. }
  246. return nil
  247. }
  248. return finder(n, id)
  249. }
  250. // Children returns the list of children.
  251. func (n *Node) Children() []INode {
  252. return n.children
  253. }
  254. // Add adds the specified node to the list of children and sets its parent pointer.
  255. // If the specified node had a parent, the specified node is removed from the original parent's list of children.
  256. func (n *Node) Add(ichild INode) *Node {
  257. setParent(n.GetINode(), ichild)
  258. n.children = append(n.children, ichild)
  259. n.Dispatch(OnDescendant, nil)
  260. return n
  261. }
  262. // AddAt adds the specified node to the list of children at the specified index and sets its parent pointer.
  263. // If the specified node had a parent, the specified node is removed from the original parent's list of children.
  264. func (n *Node) AddAt(idx int, ichild INode) *Node {
  265. // Validate position
  266. if idx < 0 || idx > len(n.children) {
  267. panic("Node.AddAt: invalid position")
  268. }
  269. setParent(n.GetINode(), ichild)
  270. // Insert child in the specified position
  271. n.children = append(n.children, nil)
  272. copy(n.children[idx+1:], n.children[idx:])
  273. n.children[idx] = ichild
  274. n.Dispatch(OnDescendant, nil)
  275. return n
  276. }
  277. // setParent is used by Add and AddAt.
  278. // It verifies that the node is not being added to itself and sets the parent pointer of the specified node.
  279. // If the specified node had a parent, the specified node is removed from the original parent's list of children.
  280. // It does not add the specified node to the list of children.
  281. func setParent(parent INode, child INode) {
  282. if parent.GetNode() == child.GetNode() {
  283. panic("Node.{Add,AddAt}: object can't be added as a child of itself")
  284. }
  285. // If the specified node already has a parent,
  286. // remove it from the original parent's list of children
  287. if child.Parent() != nil {
  288. child.Parent().GetNode().Remove(child)
  289. }
  290. child.GetNode().parent = parent
  291. }
  292. // ChildAt returns the child at the specified index.
  293. func (n *Node) ChildAt(idx int) INode {
  294. if idx < 0 || idx >= len(n.children) {
  295. return nil
  296. }
  297. return n.children[idx]
  298. }
  299. // ChildIndex returns the index of the specified child (-1 if not found).
  300. func (n *Node) ChildIndex(ichild INode) int {
  301. for idx := 0; idx < len(n.children); idx++ {
  302. if n.children[idx] == ichild {
  303. return idx
  304. }
  305. }
  306. return -1
  307. }
  308. // IsAncestorOf returns whether this node is an ancestor of the specified node. Returns true if they are the same.
  309. func (n *Node) IsAncestorOf(desc INode) bool {
  310. if desc == nil {
  311. return false
  312. }
  313. if n == desc.GetNode() {
  314. return true
  315. }
  316. for _, child := range n.Children() {
  317. res := child.IsAncestorOf(desc)
  318. if res {
  319. return res
  320. }
  321. }
  322. return false
  323. }
  324. // LowestCommonAncestor returns the common ancestor of this node and the specified node if any.
  325. func (n *Node) LowestCommonAncestor(other INode) INode {
  326. if other == nil {
  327. return nil
  328. }
  329. n1 := n.GetINode()
  330. for n1 != nil {
  331. n2 := other
  332. for n2 != nil {
  333. if n1 == n2 {
  334. return n1
  335. }
  336. n2 = n2.Parent()
  337. }
  338. n1 = n1.Parent()
  339. }
  340. return nil
  341. }
  342. // Remove removes the specified INode from the list of children.
  343. // Returns true if found or false otherwise.
  344. func (n *Node) Remove(ichild INode) bool {
  345. for pos, current := range n.children {
  346. if current == ichild {
  347. copy(n.children[pos:], n.children[pos+1:])
  348. n.children[len(n.children)-1] = nil
  349. n.children = n.children[:len(n.children)-1]
  350. ichild.GetNode().parent = nil
  351. n.Dispatch(OnDescendant, nil)
  352. return true
  353. }
  354. }
  355. return false
  356. }
  357. // RemoveAt removes the child at the specified index.
  358. func (n *Node) RemoveAt(idx int) INode {
  359. // Validate position
  360. if idx < 0 || idx >= len(n.children) {
  361. panic("Node.RemoveAt: invalid position")
  362. }
  363. child := n.children[idx]
  364. // Remove child from children list
  365. copy(n.children[idx:], n.children[idx+1:])
  366. n.children[len(n.children)-1] = nil
  367. n.children = n.children[:len(n.children)-1]
  368. n.Dispatch(OnDescendant, nil)
  369. return child
  370. }
  371. // RemoveAll removes all children.
  372. func (n *Node) RemoveAll(recurs bool) {
  373. for pos, ichild := range n.children {
  374. n.children[pos] = nil
  375. ichild.GetNode().parent = nil
  376. if recurs {
  377. ichild.GetNode().RemoveAll(recurs)
  378. }
  379. }
  380. n.children = n.children[0:0]
  381. }
  382. // DisposeChildren removes and disposes of all children.
  383. // If 'recurs' is true, call DisposeChildren on each child recursively.
  384. func (n *Node) DisposeChildren(recurs bool) {
  385. for pos, ichild := range n.children {
  386. n.children[pos] = nil
  387. ichild.GetNode().parent = nil
  388. if recurs {
  389. ichild.GetNode().DisposeChildren(true)
  390. }
  391. ichild.Dispose()
  392. }
  393. n.children = n.children[0:0]
  394. }
  395. // SetPosition sets the position.
  396. func (n *Node) SetPosition(x, y, z float32) {
  397. n.position.Set(x, y, z)
  398. n.matNeedsUpdate = true
  399. }
  400. // SetPositionVec sets the position based on the specified vector pointer.
  401. func (n *Node) SetPositionVec(vpos *math32.Vector3) {
  402. n.position = *vpos
  403. n.matNeedsUpdate = true
  404. }
  405. // SetPositionX sets the X coordinate of the position.
  406. func (n *Node) SetPositionX(x float32) {
  407. n.position.X = x
  408. n.matNeedsUpdate = true
  409. }
  410. // SetPositionY sets the Y coordinate of the position.
  411. func (n *Node) SetPositionY(y float32) {
  412. n.position.Y = y
  413. n.matNeedsUpdate = true
  414. }
  415. // SetPositionZ sets the Z coordinate of the position.
  416. func (n *Node) SetPositionZ(z float32) {
  417. n.position.Z = z
  418. n.matNeedsUpdate = true
  419. }
  420. // Position returns the position as a vector.
  421. func (n *Node) Position() math32.Vector3 {
  422. return n.position
  423. }
  424. // TranslateOnAxis translates the specified distance on the specified local axis.
  425. func (n *Node) TranslateOnAxis(axis *math32.Vector3, dist float32) {
  426. v := math32.NewVec3().Copy(axis)
  427. v.ApplyQuaternion(&n.quaternion)
  428. v.MultiplyScalar(dist)
  429. n.position.Add(v)
  430. n.matNeedsUpdate = true
  431. }
  432. // TranslateX translates the specified distance on the local X axis.
  433. func (n *Node) TranslateX(dist float32) {
  434. n.TranslateOnAxis(&math32.Vector3{1, 0, 0}, dist)
  435. }
  436. // TranslateY translates the specified distance on the local Y axis.
  437. func (n *Node) TranslateY(dist float32) {
  438. n.TranslateOnAxis(&math32.Vector3{0, 1, 0}, dist)
  439. }
  440. // TranslateZ translates the specified distance on the local Z axis.
  441. func (n *Node) TranslateZ(dist float32) {
  442. n.TranslateOnAxis(&math32.Vector3{0, 0, 1}, dist)
  443. }
  444. // SetRotation sets the global rotation in Euler angles (radians).
  445. func (n *Node) SetRotation(x, y, z float32) {
  446. n.rotation.Set(x, y, z)
  447. n.quaternion.SetFromEuler(&n.rotation)
  448. n.matNeedsUpdate = true
  449. }
  450. // SetRotationVec sets the global rotation in Euler angles (radians) based on the specified vector pointer.
  451. func (n *Node) SetRotationVec(vrot *math32.Vector3) {
  452. n.rotation = *vrot
  453. n.quaternion.SetFromEuler(&n.rotation)
  454. n.matNeedsUpdate = true
  455. }
  456. // SetRotationQuat sets the global rotation based on the specified quaternion pointer.
  457. func (n *Node) SetRotationQuat(quat *math32.Quaternion) {
  458. n.quaternion = *quat
  459. n.rotNeedsUpdate = true
  460. }
  461. // SetRotationX sets the global X rotation to the specified angle in radians.
  462. func (n *Node) SetRotationX(x float32) {
  463. if n.rotNeedsUpdate {
  464. n.rotation.SetFromQuaternion(&n.quaternion)
  465. n.rotNeedsUpdate = false
  466. }
  467. n.rotation.X = x
  468. n.quaternion.SetFromEuler(&n.rotation)
  469. n.matNeedsUpdate = true
  470. }
  471. // SetRotationY sets the global Y rotation to the specified angle in radians.
  472. func (n *Node) SetRotationY(y float32) {
  473. if n.rotNeedsUpdate {
  474. n.rotation.SetFromQuaternion(&n.quaternion)
  475. n.rotNeedsUpdate = false
  476. }
  477. n.rotation.Y = y
  478. n.quaternion.SetFromEuler(&n.rotation)
  479. n.matNeedsUpdate = true
  480. }
  481. // SetRotationZ sets the global Z rotation to the specified angle in radians.
  482. func (n *Node) SetRotationZ(z float32) {
  483. if n.rotNeedsUpdate {
  484. n.rotation.SetFromQuaternion(&n.quaternion)
  485. n.rotNeedsUpdate = false
  486. }
  487. n.rotation.Z = z
  488. n.quaternion.SetFromEuler(&n.rotation)
  489. n.matNeedsUpdate = true
  490. }
  491. // Rotation returns the current global rotation in Euler angles (radians).
  492. func (n *Node) Rotation() math32.Vector3 {
  493. if n.rotNeedsUpdate {
  494. n.rotation.SetFromQuaternion(&n.quaternion)
  495. n.rotNeedsUpdate = false
  496. }
  497. return n.rotation
  498. }
  499. // RotateOnAxis rotates around the specified local axis the specified angle in radians.
  500. func (n *Node) RotateOnAxis(axis *math32.Vector3, angle float32) {
  501. var rotQuat math32.Quaternion
  502. rotQuat.SetFromAxisAngle(axis, angle)
  503. n.QuaternionMult(&rotQuat)
  504. }
  505. // RotateX rotates around the local X axis the specified angle in radians.
  506. func (n *Node) RotateX(x float32) {
  507. n.RotateOnAxis(&math32.Vector3{1, 0, 0}, x)
  508. }
  509. // RotateY rotates around the local Y axis the specified angle in radians.
  510. func (n *Node) RotateY(y float32) {
  511. n.RotateOnAxis(&math32.Vector3{0, 1, 0}, y)
  512. }
  513. // RotateZ rotates around the local Z axis the specified angle in radians.
  514. func (n *Node) RotateZ(z float32) {
  515. n.RotateOnAxis(&math32.Vector3{0, 0, 1}, z)
  516. }
  517. // SetQuaternion sets the quaternion based on the specified quaternion unit multiples.
  518. func (n *Node) SetQuaternion(x, y, z, w float32) {
  519. n.quaternion.Set(x, y, z, w)
  520. n.rotNeedsUpdate = true
  521. }
  522. // SetQuaternionVec sets the quaternion based on the specified quaternion unit multiples vector.
  523. func (n *Node) SetQuaternionVec(q *math32.Vector4) {
  524. n.quaternion.Set(q.X, q.Y, q.Z, q.W)
  525. n.rotNeedsUpdate = true
  526. }
  527. // SetQuaternionQuat sets the quaternion based on the specified quaternion pointer.
  528. func (n *Node) SetQuaternionQuat(q *math32.Quaternion) {
  529. n.quaternion = *q
  530. n.rotNeedsUpdate = true
  531. }
  532. // QuaternionMult multiplies the current quaternion by the specified quaternion.
  533. func (n *Node) QuaternionMult(q *math32.Quaternion) {
  534. n.quaternion.Multiply(q)
  535. n.rotNeedsUpdate = true
  536. }
  537. // Quaternion returns the current quaternion.
  538. func (n *Node) Quaternion() math32.Quaternion {
  539. return n.quaternion
  540. }
  541. // LookAt rotates the node to look at the specified target position, using the specified up vector.
  542. func (n *Node) LookAt(target, up *math32.Vector3) {
  543. var worldPos math32.Vector3
  544. n.WorldPosition(&worldPos)
  545. var rotMat math32.Matrix4
  546. rotMat.LookAt(&worldPos, target, up)
  547. n.quaternion.SetFromRotationMatrix(&rotMat)
  548. n.rotNeedsUpdate = true
  549. }
  550. // SetScale sets the scale.
  551. func (n *Node) SetScale(x, y, z float32) {
  552. n.scale.Set(x, y, z)
  553. n.matNeedsUpdate = true
  554. }
  555. // SetScaleVec sets the scale based on the specified vector pointer.
  556. func (n *Node) SetScaleVec(scale *math32.Vector3) {
  557. n.scale = *scale
  558. n.matNeedsUpdate = true
  559. }
  560. // SetScaleX sets the X scale.
  561. func (n *Node) SetScaleX(sx float32) {
  562. n.scale.X = sx
  563. n.matNeedsUpdate = true
  564. }
  565. // SetScaleY sets the Y scale.
  566. func (n *Node) SetScaleY(sy float32) {
  567. n.scale.Y = sy
  568. n.matNeedsUpdate = true
  569. }
  570. // SetScaleZ sets the Z scale.
  571. func (n *Node) SetScaleZ(sz float32) {
  572. n.scale.Z = sz
  573. n.matNeedsUpdate = true
  574. }
  575. // Scale returns the current scale.
  576. func (n *Node) Scale() math32.Vector3 {
  577. return n.scale
  578. }
  579. // SetDirection sets the direction.
  580. func (n *Node) SetDirection(x, y, z float32) {
  581. n.direction.Set(x, y, z)
  582. n.matNeedsUpdate = true
  583. }
  584. // SetDirectionVec sets the direction based on a vector pointer.
  585. func (n *Node) SetDirectionVec(vdir *math32.Vector3) {
  586. n.direction = *vdir
  587. n.matNeedsUpdate = true
  588. }
  589. // Direction returns the direction.
  590. func (n *Node) Direction() math32.Vector3 {
  591. return n.direction
  592. }
  593. // SetMatrix sets the local transformation matrix.
  594. func (n *Node) SetMatrix(m *math32.Matrix4) {
  595. n.matrix = *m
  596. n.matrix.Decompose(&n.position, &n.quaternion, &n.scale)
  597. n.rotNeedsUpdate = true
  598. }
  599. // Matrix returns a copy of the local transformation matrix.
  600. func (n *Node) Matrix() math32.Matrix4 {
  601. return n.matrix
  602. }
  603. // WorldPosition updates the world matrix and sets
  604. // the specified vector to the current world position of this node.
  605. func (n *Node) WorldPosition(result *math32.Vector3) {
  606. n.UpdateMatrixWorld()
  607. result.SetFromMatrixPosition(&n.matrixWorld)
  608. }
  609. // WorldQuaternion updates the world matrix and sets
  610. // the specified quaternion to the current world quaternion of this node.
  611. func (n *Node) WorldQuaternion(result *math32.Quaternion) {
  612. var position math32.Vector3
  613. var scale math32.Vector3
  614. n.UpdateMatrixWorld()
  615. n.matrixWorld.Decompose(&position, result, &scale)
  616. }
  617. // WorldRotation updates the world matrix and sets
  618. // the specified vector to the current world rotation of this node in Euler angles.
  619. func (n *Node) WorldRotation(result *math32.Vector3) {
  620. var quaternion math32.Quaternion
  621. n.WorldQuaternion(&quaternion)
  622. result.SetFromQuaternion(&quaternion)
  623. }
  624. // WorldScale updates the world matrix and sets
  625. // the specified vector to the current world scale of this node.
  626. func (n *Node) WorldScale(result *math32.Vector3) {
  627. var position math32.Vector3
  628. var quaternion math32.Quaternion
  629. n.UpdateMatrixWorld()
  630. n.matrixWorld.Decompose(&position, &quaternion, result)
  631. }
  632. // WorldDirection updates the world matrix and sets
  633. // the specified vector to the current world direction of this node.
  634. func (n *Node) WorldDirection(result *math32.Vector3) {
  635. var quaternion math32.Quaternion
  636. n.WorldQuaternion(&quaternion)
  637. *result = n.direction
  638. result.ApplyQuaternion(&quaternion)
  639. }
  640. // MatrixWorld returns a copy of the matrix world of this node.
  641. func (n *Node) MatrixWorld() math32.Matrix4 {
  642. return n.matrixWorld
  643. }
  644. // UpdateMatrix updates (if necessary) the local transform matrix
  645. // of this node based on its position, quaternion, and scale.
  646. func (n *Node) UpdateMatrix() bool {
  647. if !n.matNeedsUpdate && !n.rotNeedsUpdate {
  648. return false
  649. }
  650. n.matrix.Compose(&n.position, &n.quaternion, &n.scale)
  651. n.matNeedsUpdate = false
  652. return true
  653. }
  654. // UpdateMatrixWorld updates this node world transform matrix and of all its children
  655. func (n *Node) UpdateMatrixWorld() {
  656. n.UpdateMatrix()
  657. if n.parent == nil {
  658. n.matrixWorld = n.matrix
  659. } else {
  660. n.matrixWorld.MultiplyMatrices(&n.parent.GetNode().matrixWorld, &n.matrix)
  661. }
  662. // Update this Node children matrices
  663. for _, ichild := range n.children {
  664. ichild.UpdateMatrixWorld()
  665. }
  666. }