babylon.node.ts 4.2 KB

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