babylon.node.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. module BABYLON {
  2. export class Node {
  3. public parent: Node;
  4. public name: string;
  5. public id: string;
  6. public state = "";
  7. public animations = new Array<Animation>();
  8. public onReady: (node: Node) => void;
  9. private _childrenFlag = -1;
  10. private _isEnabled = true;
  11. private _isReady = true;
  12. public _currentRenderId = -1;
  13. public _waitingParentId: string;
  14. public _waitingActions: any;
  15. private _scene: Scene;
  16. public _cache;
  17. constructor(name: string, scene) {
  18. this.name = name;
  19. this.id = name;
  20. this._scene = scene;
  21. this._initCache();
  22. }
  23. public getScene(): Scene {
  24. return this._scene;
  25. }
  26. public getEngine(): Engine {
  27. return this._scene.getEngine();
  28. }
  29. // override it in derived class
  30. public getWorldMatrix(): Matrix {
  31. return Matrix.Identity();
  32. }
  33. // override it in derived class if you add new variables to the cache
  34. // and call the parent class method
  35. public _initCache() {
  36. this._cache = {};
  37. this._cache.parent = undefined;
  38. }
  39. public updateCache(force?: boolean): void {
  40. if (!force && this.isSynchronized())
  41. return;
  42. this._cache.parent = this.parent;
  43. this._updateCache();
  44. }
  45. // override it in derived class if you add new variables to the cache
  46. // and call the parent class method if !ignoreParentClass
  47. public _updateCache(ignoreParentClass?: boolean): void {
  48. }
  49. // override it in derived class if you add new variables to the cache
  50. public _isSynchronized(): boolean {
  51. return true;
  52. }
  53. public isSynchronizedWithParent(): boolean {
  54. return this.parent ? this.parent._currentRenderId <= this._currentRenderId : true;
  55. }
  56. public isSynchronized(updateCache?: boolean): boolean {
  57. var check = this.hasNewParent();
  58. check = check || !this.isSynchronizedWithParent();
  59. check = check || !this._isSynchronized();
  60. if (updateCache)
  61. this.updateCache(true);
  62. return !check;
  63. }
  64. public hasNewParent(update?: boolean): boolean {
  65. if (this._cache.parent === this.parent)
  66. return false;
  67. if (update)
  68. this._cache.parent = this.parent;
  69. return true;
  70. }
  71. public isReady(): boolean {
  72. return this._isReady;
  73. }
  74. public isEnabled(): boolean {
  75. if (!this._isEnabled) {
  76. return false;
  77. }
  78. if (this.parent) {
  79. return this.parent.isEnabled();
  80. }
  81. return true;
  82. }
  83. public setEnabled(value: boolean): void {
  84. this._isEnabled = value;
  85. }
  86. public isDescendantOf(ancestor: Node): boolean {
  87. if (this.parent) {
  88. if (this.parent === ancestor) {
  89. return true;
  90. }
  91. return this.parent.isDescendantOf(ancestor);
  92. }
  93. return false;
  94. }
  95. public _getDescendants(list: Node[], results: Node[]): void {
  96. for (var index = 0; index < list.length; index++) {
  97. var item = list[index];
  98. if (item.isDescendantOf(this)) {
  99. results.push(item);
  100. }
  101. }
  102. }
  103. public getDescendants(): Node[] {
  104. var results = [];
  105. this._getDescendants(this._scene.meshes, results);
  106. this._getDescendants(this._scene.lights, results);
  107. this._getDescendants(this._scene.cameras, results);
  108. return results;
  109. }
  110. public _setReady(state: boolean): void {
  111. if (state == this._isReady) {
  112. return;
  113. }
  114. if (!state) {
  115. this._isReady = false;
  116. return;
  117. }
  118. this._isReady = true;
  119. if (this.onReady) {
  120. this.onReady(this);
  121. }
  122. }
  123. }
  124. }