babylon.node.ts 3.7 KB

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