node.go 21 KB

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