babylon.node.ts 4.1 KB

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