node.go 19 KB

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