babylon.node.ts 5.4 KB

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