node.go 13 KB

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