babylon.node.ts 5.7 KB

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