node.go 21 KB

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