node.go 21 KB

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