babylon.node.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. module BABYLON {
  2. /**
  3. * Node is the basic class for all scene objects (Mesh, Light Camera).
  4. */
  5. export class Node {
  6. public parent: Node;
  7. public name: string;
  8. public id: string;
  9. public state = "";
  10. public animations = new Array<Animation>();
  11. public onReady: (node: Node) => void;
  12. private _childrenFlag = -1;
  13. private _isEnabled = true;
  14. private _isReady = true;
  15. public _currentRenderId = -1;
  16. private _parentRenderId = -1;
  17. public _waitingParentId: string;
  18. private _scene: Scene;
  19. public _cache;
  20. /**
  21. * @constructor
  22. * @param {string} name - the name and id to be given to this node
  23. * @param {BABYLON.Scene} the scene this node will be added to
  24. */
  25. constructor(name: string, scene: Scene) {
  26. this.name = name;
  27. this.id = name;
  28. this._scene = scene;
  29. this._initCache();
  30. }
  31. public getScene(): Scene {
  32. return this._scene;
  33. }
  34. public getEngine(): Engine {
  35. return this._scene.getEngine();
  36. }
  37. // override it in derived class
  38. public getWorldMatrix(): Matrix {
  39. return Matrix.Identity();
  40. }
  41. // override it in derived class if you add new variables to the cache
  42. // and call the parent class method
  43. public _initCache() {
  44. this._cache = {};
  45. this._cache.parent = undefined;
  46. }
  47. public updateCache(force?: boolean): void {
  48. if (!force && this.isSynchronized())
  49. return;
  50. this._cache.parent = this.parent;
  51. this._updateCache();
  52. }
  53. // override it in derived class if you add new variables to the cache
  54. // and call the parent class method if !ignoreParentClass
  55. public _updateCache(ignoreParentClass?: boolean): void {
  56. }
  57. // override it in derived class if you add new variables to the cache
  58. public _isSynchronized(): boolean {
  59. return true;
  60. }
  61. public isSynchronizedWithParent(): boolean {
  62. if (!this.parent) {
  63. return true;
  64. }
  65. if (this._parentRenderId !== this.parent._currentRenderId) {
  66. return false;
  67. }
  68. this._parentRenderId = this.parent._currentRenderId;
  69. return this.parent._currentRenderId <= this._currentRenderId && this.parent.isSynchronized();
  70. }
  71. public isSynchronized(updateCache?: boolean): boolean {
  72. var check = this.hasNewParent();
  73. check = check || !this.isSynchronizedWithParent();
  74. check = check || !this._isSynchronized();
  75. if (updateCache)
  76. this.updateCache(true);
  77. return !check;
  78. }
  79. public hasNewParent(update?: boolean): boolean {
  80. if (this._cache.parent === this.parent)
  81. return false;
  82. if (update)
  83. this._cache.parent = this.parent;
  84. return true;
  85. }
  86. /**
  87. * Is this node ready to be used/rendered
  88. * @return {boolean} is it ready
  89. */
  90. public isReady(): boolean {
  91. return this._isReady;
  92. }
  93. /**
  94. * Is this node enabled.
  95. * If the node has a parent and is enabled, the parent will be inspected as well.
  96. * @return {boolean} whether this node (and its parent) is enabled.
  97. * @see setEnabled
  98. */
  99. public isEnabled(): boolean {
  100. if (!this._isEnabled) {
  101. return false;
  102. }
  103. if (this.parent) {
  104. return this.parent.isEnabled();
  105. }
  106. return true;
  107. }
  108. /**
  109. * Set the enabled state of this node.
  110. * @param {boolean} value - the new enabled state
  111. * @see isEnabled
  112. */
  113. public setEnabled(value: boolean): void {
  114. this._isEnabled = value;
  115. }
  116. /**
  117. * Is this node a descendant of the given node.
  118. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  119. * @param {BABYLON.Node} ancestor - The parent node to inspect
  120. * @see parent
  121. */
  122. public isDescendantOf(ancestor: Node): boolean {
  123. if (this.parent) {
  124. if (this.parent === ancestor) {
  125. return true;
  126. }
  127. return this.parent.isDescendantOf(ancestor);
  128. }
  129. return false;
  130. }
  131. public _getDescendants(list: Node[], results: Node[]): void {
  132. for (var index = 0; index < list.length; index++) {
  133. var item = list[index];
  134. if (item.isDescendantOf(this)) {
  135. results.push(item);
  136. }
  137. }
  138. }
  139. /**
  140. * Will return all nodes that have this node as parent.
  141. * @return {BABYLON.Node[]} all children nodes of all types.
  142. */
  143. public getDescendants(): Node[] {
  144. var results = [];
  145. this._getDescendants(this._scene.meshes, results);
  146. this._getDescendants(this._scene.lights, results);
  147. this._getDescendants(this._scene.cameras, results);
  148. return results;
  149. }
  150. public _setReady(state: boolean): void {
  151. if (state == this._isReady) {
  152. return;
  153. }
  154. if (!state) {
  155. this._isReady = false;
  156. return;
  157. }
  158. this._isReady = true;
  159. if (this.onReady) {
  160. this.onReady(this);
  161. }
  162. }
  163. }
  164. }