node.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. "strings"
  7. "github.com/g3n/engine/gls"
  8. "github.com/g3n/engine/math32"
  9. )
  10. // Interface for all node types
  11. type INode interface {
  12. GetNode() *Node
  13. UpdateMatrixWorld()
  14. Raycast(*Raycaster, *[]Intersect)
  15. Render(gs *gls.GLS)
  16. Dispose()
  17. }
  18. type Node struct {
  19. Dispatcher // Embedded event dispatcher
  20. loaderID string // ID used by loader
  21. name string // Optional node name
  22. position math32.Vector3 // Node position, specified as a Vector3
  23. rotation math32.Vector3 // Node rotation, specified in Euler angles.
  24. quaternion math32.Quaternion // Node rotation, specified as a Quaternion.
  25. scale math32.Vector3 // Node scale as a Vector3
  26. direction math32.Vector3 // Initial direction
  27. matrix math32.Matrix4 // Transform matrix relative to this node parent.
  28. matrixWorld math32.Matrix4 // Transform world matrix
  29. visible bool // Visible flag
  30. changed bool // Node position/orientation/scale changed
  31. parent INode // Parent node
  32. children []INode // Array with node children
  33. userData interface{} // Generic user data
  34. }
  35. // NewNode creates and returns a pointer to a new Node
  36. func NewNode() *Node {
  37. n := new(Node)
  38. n.Init()
  39. return n
  40. }
  41. // Init initializes this Node
  42. // It is normally use by other types which embed a Node
  43. func (n *Node) Init() {
  44. n.Dispatcher.Initialize()
  45. n.position.Set(0, 0, 0)
  46. n.rotation.Set(0, 0, 0)
  47. n.quaternion.Set(0, 0, 0, 1)
  48. n.scale.Set(1, 1, 1)
  49. n.direction.Set(0, 0, 1)
  50. n.matrix.Identity()
  51. n.matrixWorld.Identity()
  52. n.children = make([]INode, 0)
  53. n.visible = true
  54. n.changed = true
  55. }
  56. // GetNode satisfies the INode interface and returns
  57. // a pointer to the embedded Node
  58. func (n *Node) GetNode() *Node {
  59. return n
  60. }
  61. // Raycast satisfies the INode interface
  62. func (n *Node) Raycast(rc *Raycaster, intersects *[]Intersect) {
  63. }
  64. // Render satisfies the INode interface
  65. func (n *Node) Render(gs *gls.GLS) {
  66. }
  67. // Dispose satisfies the INode interface
  68. func (n *Node) Dispose() {
  69. }
  70. // SetLoaderID is normally used by external loaders, such as Collada,
  71. // to assign an ID to the node with the ID value in the node description
  72. // Can be used to find other loaded nodes.
  73. func (n *Node) SetLoaderID(id string) {
  74. n.loaderID = id
  75. }
  76. // LoaderID returns an optional ID set when this node was
  77. // created by an external loader such as Collada
  78. func (n *Node) LoaderID() string {
  79. return n.loaderID
  80. }
  81. // FindPath finds a node with the specified path starting with this node and
  82. // searching in all its children recursively.
  83. // A path is the sequence of the names from the first node to the desired node
  84. // separated by the forward slash.
  85. func (n *Node) FindPath(path string) INode {
  86. // Internal recursive function to find node
  87. var finder func(inode INode, path string) INode
  88. finder = func(inode INode, path string) INode {
  89. // Get first component of the path
  90. parts := strings.Split(path, "/")
  91. if len(parts) == 0 {
  92. return nil
  93. }
  94. first := parts[0]
  95. // Checks current node
  96. node := inode.GetNode()
  97. if node.name != first {
  98. return nil
  99. }
  100. // If the path has finished this is the desired node
  101. rest := strings.Join(parts[1:], "/")
  102. if rest == "" {
  103. return inode
  104. }
  105. // Otherwise search in this node children
  106. for _, ichild := range node.children {
  107. found := finder(ichild, rest)
  108. if found != nil {
  109. return found
  110. }
  111. }
  112. return nil
  113. }
  114. return finder(n, path)
  115. }
  116. // FindLoaderID looks in the specified node and all its children
  117. // for a node with the specifid loaderID and if found returns it.
  118. // Returns nil if not found
  119. func (n *Node) FindLoaderID(id string) INode {
  120. var finder func(parent INode, id string) INode
  121. finder = func(parent INode, id string) INode {
  122. pnode := parent.GetNode()
  123. if pnode.loaderID == id {
  124. return parent
  125. }
  126. for _, child := range pnode.children {
  127. found := finder(child, id)
  128. if found != nil {
  129. return found
  130. }
  131. }
  132. return nil
  133. }
  134. return finder(n, id)
  135. }
  136. // SetChanged sets this node changed flag
  137. func (n *Node) SetChanged(changed bool) {
  138. n.changed = changed
  139. }
  140. // Changed returns this Node changed flag
  141. func (n *Node) Changed() bool {
  142. return n.changed
  143. }
  144. // SetName set an option name for the node.
  145. // This name can be used for debugging or other purposes.
  146. func (n *Node) SetName(name string) {
  147. n.name = name
  148. }
  149. // Name returns current optional name for this node
  150. func (n *Node) Name() string {
  151. return n.name
  152. }
  153. // SetPosition sets this node world position
  154. func (n *Node) SetPosition(x, y, z float32) {
  155. n.position.Set(x, y, z)
  156. n.changed = true
  157. }
  158. // SetPositionVec sets this node position from the specified vector pointer
  159. func (n *Node) SetPositionVec(vpos *math32.Vector3) {
  160. n.position = *vpos
  161. n.changed = true
  162. }
  163. // SetPositionX sets the x coordinate of this node position
  164. func (n *Node) SetPositionX(x float32) {
  165. n.position.X = x
  166. n.changed = true
  167. }
  168. // SetPositionY sets the y coordinate of this node position
  169. func (n *Node) SetPositionY(y float32) {
  170. n.position.Y = y
  171. n.changed = true
  172. }
  173. // SetPositionZ sets the z coordinate of this node position
  174. func (n *Node) SetPositionZ(z float32) {
  175. n.position.Z = z
  176. n.changed = true
  177. }
  178. // Position returns the current node position as a vector
  179. func (n *Node) Position() math32.Vector3 {
  180. return n.position
  181. }
  182. // SetRotation sets the three fields of the node rotation in radians
  183. // The node quaternion is updated
  184. func (n *Node) SetRotation(x, y, z float32) {
  185. n.rotation.Set(x, y, z)
  186. n.quaternion.SetFromEuler(&n.rotation)
  187. n.changed = true
  188. }
  189. // SetRotationX sets the x rotation angle in radians
  190. // The node quaternion is updated
  191. func (n *Node) SetRotationX(x float32) {
  192. n.rotation.X = x
  193. n.quaternion.SetFromEuler(&n.rotation)
  194. n.changed = true
  195. }
  196. // SetRotationY sets the y rotation angle in radians
  197. // The node quaternion is updated
  198. func (n *Node) SetRotationY(y float32) {
  199. n.rotation.Y = y
  200. n.quaternion.SetFromEuler(&n.rotation)
  201. n.changed = true
  202. }
  203. // SetRotationZ sets the z rotation angle in radians
  204. // The node quaternion is updated
  205. func (n *Node) SetRotationZ(z float32) {
  206. n.rotation.Z = z
  207. n.quaternion.SetFromEuler(&n.rotation)
  208. n.changed = true
  209. }
  210. // AddRotationX adds to the current rotation x coordinate in radians
  211. // The node quaternion is updated
  212. func (n *Node) AddRotationX(x float32) {
  213. n.rotation.X += x
  214. n.quaternion.SetFromEuler(&n.rotation)
  215. n.changed = true
  216. }
  217. // AddRotationY adds to the current rotation y coordinate in radians
  218. // The node quaternion is updated
  219. func (n *Node) AddRotationY(y float32) {
  220. n.rotation.Y += y
  221. n.quaternion.SetFromEuler(&n.rotation)
  222. n.changed = true
  223. }
  224. // AddRotationZ adds to the current rotation z coordinate in radians
  225. // The node quaternion is updated
  226. func (n *Node) AddRotationZ(z float32) {
  227. n.rotation.Z += z
  228. n.quaternion.SetFromEuler(&n.rotation)
  229. n.changed = true
  230. }
  231. // Rotation returns the current rotation
  232. func (n *Node) Rotation() math32.Vector3 {
  233. return n.rotation
  234. }
  235. // SetQuaternion sets this node quaternion with the specified fields
  236. func (n *Node) SetQuaternion(x, y, z, w float32) {
  237. n.quaternion.Set(x, y, z, w)
  238. n.changed = true
  239. }
  240. // SetQuaternionQuat sets this node quaternion from the specified quaternion pointer
  241. func (n *Node) SetQuaternionQuat(q *math32.Quaternion) {
  242. n.quaternion = *q
  243. n.changed = true
  244. }
  245. // QuaternionMult multiplies the quaternion by the specified quaternion
  246. func (n *Node) QuaternionMult(q *math32.Quaternion) {
  247. n.quaternion.Multiply(q)
  248. n.changed = true
  249. }
  250. // Quaternion returns the current quaternion
  251. func (n *Node) Quaternion() math32.Quaternion {
  252. return n.quaternion
  253. }
  254. // SetScale sets this node scale fields
  255. func (n *Node) SetScale(x, y, z float32) {
  256. n.scale.Set(x, y, z)
  257. n.changed = true
  258. }
  259. // SetScaleVec sets this node scale from a pointer to a Vector3
  260. func (n *Node) SetScaleVec(scale *math32.Vector3) {
  261. n.scale = *scale
  262. n.changed = true
  263. }
  264. // SetScaleX sets the X scale of this node
  265. func (n *Node) SetScaleX(sx float32) {
  266. n.scale.X = sx
  267. n.changed = true
  268. }
  269. // SetScaleY sets the Y scale of this node
  270. func (n *Node) SetScaleY(sy float32) {
  271. n.scale.Y = sy
  272. n.changed = true
  273. }
  274. // SetScaleZ sets the Z scale of this node
  275. func (n *Node) SetScaleZ(sz float32) {
  276. n.scale.Z = sz
  277. n.changed = true
  278. }
  279. // Scale returns the current scale
  280. func (n *Node) Scale() math32.Vector3 {
  281. return n.scale
  282. }
  283. // SetDirection sets this node initial direction vector
  284. func (n *Node) SetDirection(x, y, z float32) {
  285. n.direction.Set(x, y, z)
  286. n.changed = true
  287. }
  288. // SetDirectionVec sets this node initial direction vector from a pointer to Vector3
  289. func (n *Node) SetDirectionVec(vdir *math32.Vector3) {
  290. n.direction = *vdir
  291. n.changed = true
  292. }
  293. // Direction returns this node initial direction
  294. func (n *Node) Direction() math32.Vector3 {
  295. return n.direction
  296. }
  297. // SetMatrix sets this node local transformation matrix
  298. func (n *Node) SetMatrix(m *math32.Matrix4) {
  299. n.matrix = *m
  300. n.changed = true
  301. }
  302. // Matrix returns a copy of this node local transformation matrix
  303. func (n *Node) Matrix() math32.Matrix4 {
  304. return n.matrix
  305. }
  306. // SetVisible sets the node visibility state
  307. func (n *Node) SetVisible(state bool) {
  308. n.visible = state
  309. n.changed = true
  310. }
  311. // Visible returns the node visibility state
  312. func (n *Node) Visible() bool {
  313. return n.visible
  314. }
  315. // WorldPosition updates this node world matrix and gets
  316. // the current world position vector.
  317. func (n *Node) WorldPosition(result *math32.Vector3) {
  318. n.UpdateMatrixWorld()
  319. result.SetFromMatrixPosition(&n.matrixWorld)
  320. }
  321. // WorldQuaternion sets the specified result quaternion with
  322. // this node current world quaternion
  323. func (n *Node) WorldQuaternion(result *math32.Quaternion) {
  324. var position math32.Vector3
  325. var scale math32.Vector3
  326. n.UpdateMatrixWorld()
  327. n.matrixWorld.Decompose(&position, result, &scale)
  328. }
  329. // WorldRotation sets the specified result vector with
  330. // current world rotation of this node in Euler angles.
  331. func (n *Node) WorldRotation(result *math32.Vector3) {
  332. var quaternion math32.Quaternion
  333. n.WorldQuaternion(&quaternion)
  334. result.SetFromQuaternion(&quaternion)
  335. }
  336. // WorldScale sets the specified result vector with
  337. // the current world scale of this node
  338. func (n *Node) WorldScale(result *math32.Vector3) {
  339. var position math32.Vector3
  340. var quaternion math32.Quaternion
  341. n.UpdateMatrixWorld()
  342. n.matrixWorld.Decompose(&position, &quaternion, result)
  343. }
  344. // WorldDirection updates this object world matrix and sets
  345. // the current world direction.
  346. func (n *Node) WorldDirection(result *math32.Vector3) {
  347. var quaternion math32.Quaternion
  348. n.WorldQuaternion(&quaternion)
  349. *result = n.direction
  350. result.ApplyQuaternion(&quaternion)
  351. }
  352. // MatrixWorld returns a copy of this node matrix world
  353. func (n *Node) MatrixWorld() math32.Matrix4 {
  354. return n.matrixWorld
  355. }
  356. // UpdateMatrix updates this node local matrix transform from its
  357. // current position, quaternion and scale.
  358. func (n *Node) UpdateMatrix() {
  359. n.matrix.Compose(&n.position, &n.quaternion, &n.scale)
  360. }
  361. // UpdateMatrixWorld updates this node world transform matrix and of all its children
  362. func (n *Node) UpdateMatrixWorld() {
  363. n.UpdateMatrix()
  364. if n.parent == nil {
  365. n.matrixWorld = n.matrix
  366. } else {
  367. parent := n.parent.GetNode()
  368. n.matrixWorld.MultiplyMatrices(&parent.matrixWorld, &n.matrix)
  369. }
  370. // Update this Node children matrices
  371. for _, ichild := range n.children {
  372. ichild.UpdateMatrixWorld()
  373. }
  374. }
  375. // SetParent sets this node parent
  376. func (n *Node) SetParent(iparent INode) {
  377. n.parent = iparent
  378. }
  379. // Parent returns this node parent
  380. func (n *Node) Parent() INode {
  381. return n.parent
  382. }
  383. // Children returns the list of this node children
  384. func (n *Node) Children() []INode {
  385. return n.children
  386. }
  387. // Add adds the specified INode to this node list of children
  388. func (n *Node) Add(ichild INode) *Node {
  389. child := ichild.GetNode()
  390. if n == child {
  391. panic("Node.Add: object can't be added as a child of itself")
  392. }
  393. // If this child already has a parent,
  394. // removes it from this parent children list
  395. if child.parent != nil {
  396. child.parent.GetNode().Remove(ichild)
  397. }
  398. child.parent = n
  399. n.children = append(n.children, ichild)
  400. return n
  401. }
  402. // Remove removes the specified INode from this node list of children
  403. // Returns true if found or false otherwise
  404. func (n *Node) Remove(ichild INode) bool {
  405. for pos, current := range n.children {
  406. if current == ichild {
  407. copy(n.children[pos:], n.children[pos+1:])
  408. n.children[len(n.children)-1] = nil
  409. n.children = n.children[:len(n.children)-1]
  410. ichild.GetNode().parent = nil
  411. return true
  412. }
  413. }
  414. return false
  415. }
  416. // RemoveAll removes all children from this node
  417. func (n *Node) RemoveAll(recurs bool) {
  418. for pos, ichild := range n.children {
  419. n.children[pos] = nil
  420. ichild.GetNode().parent = nil
  421. if recurs {
  422. ichild.GetNode().RemoveAll(recurs)
  423. }
  424. }
  425. n.children = n.children[0:0]
  426. }
  427. // DisposeChildren removes and disposes all children of this
  428. // node and if 'recurs' is true for each of its children recursively.
  429. func (n *Node) DisposeChildren(recurs bool) {
  430. for pos, ichild := range n.children {
  431. n.children[pos] = nil
  432. ichild.GetNode().parent = nil
  433. if recurs {
  434. ichild.GetNode().DisposeChildren(true)
  435. }
  436. ichild.Dispose()
  437. }
  438. n.children = n.children[0:0]
  439. }
  440. // SetUserData sets this node associated generic user data
  441. func (n *Node) SetUserData(data interface{}) {
  442. n.userData = data
  443. }
  444. // UserData returns this node associated generic user data
  445. func (n *Node) UserData() interface{} {
  446. return n.userData
  447. }