babylon.node.ts 4.1 KB

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