babylon.node.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 _markSyncedWithParent() {
  63. this._parentRenderId = this.parent._currentRenderId;
  64. }
  65. public isSynchronizedWithParent(): boolean {
  66. if (!this.parent) {
  67. return true;
  68. }
  69. if (this._parentRenderId !== this.parent._currentRenderId) {
  70. return false;
  71. }
  72. return this.parent.isSynchronized();
  73. }
  74. public isSynchronized(updateCache?: boolean): boolean {
  75. var check = this.hasNewParent();
  76. check = check || !this.isSynchronizedWithParent();
  77. check = check || !this._isSynchronized();
  78. if (updateCache)
  79. this.updateCache(true);
  80. return !check;
  81. }
  82. public hasNewParent(update?: boolean): boolean {
  83. if (this._cache.parent === this.parent)
  84. return false;
  85. if (update)
  86. this._cache.parent = this.parent;
  87. return true;
  88. }
  89. /**
  90. * Is this node ready to be used/rendered
  91. * @return {boolean} is it ready
  92. */
  93. public isReady(): boolean {
  94. return this._isReady;
  95. }
  96. /**
  97. * Is this node enabled.
  98. * If the node has a parent and is enabled, the parent will be inspected as well.
  99. * @return {boolean} whether this node (and its parent) is enabled.
  100. * @see setEnabled
  101. */
  102. public isEnabled(): boolean {
  103. if (!this._isEnabled) {
  104. return false;
  105. }
  106. if (this.parent) {
  107. return this.parent.isEnabled();
  108. }
  109. return true;
  110. }
  111. /**
  112. * Set the enabled state of this node.
  113. * @param {boolean} value - the new enabled state
  114. * @see isEnabled
  115. */
  116. public setEnabled(value: boolean): void {
  117. this._isEnabled = value;
  118. }
  119. /**
  120. * Is this node a descendant of the given node.
  121. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  122. * @param {BABYLON.Node} ancestor - The parent node to inspect
  123. * @see parent
  124. */
  125. public isDescendantOf(ancestor: Node): boolean {
  126. if (this.parent) {
  127. if (this.parent === ancestor) {
  128. return true;
  129. }
  130. return this.parent.isDescendantOf(ancestor);
  131. }
  132. return false;
  133. }
  134. public _getDescendants(list: Node[], results: Node[]): void {
  135. for (var index = 0; index < list.length; index++) {
  136. var item = list[index];
  137. if (item.isDescendantOf(this)) {
  138. results.push(item);
  139. }
  140. }
  141. }
  142. /**
  143. * Will return all nodes that have this node as parent.
  144. * @return {BABYLON.Node[]} all children nodes of all types.
  145. */
  146. public getDescendants(): Node[] {
  147. var results = [];
  148. this._getDescendants(this._scene.meshes, results);
  149. this._getDescendants(this._scene.lights, results);
  150. this._getDescendants(this._scene.cameras, results);
  151. return results;
  152. }
  153. public _setReady(state: boolean): void {
  154. if (state == this._isReady) {
  155. return;
  156. }
  157. if (!state) {
  158. this._isReady = false;
  159. return;
  160. }
  161. this._isReady = true;
  162. if (this.onReady) {
  163. this.onReady(this);
  164. }
  165. }
  166. }
  167. }