babylon.node.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Node = function (scene) {
  5. this._scene = scene;
  6. BABYLON.Node.prototype._initCache.call(this);
  7. };
  8. // Properties
  9. BABYLON.Node.prototype.parent = null;
  10. BABYLON.Node.prototype._childrenFlag = -1;
  11. BABYLON.Node.prototype._isReady = true;
  12. BABYLON.Node.prototype._isEnabled = true;
  13. // Cache
  14. BABYLON.Node.prototype._cache = null;
  15. // override it in derived class if you add new variables to the cache
  16. // and call it in the constructor of your class like this
  17. // BABYLON.YourClass.prototype._initCache.call(this)
  18. // DO NOT call parent class method
  19. BABYLON.Node.prototype._initCache = function () {
  20. this._cache = {};
  21. this._cache.parent = undefined;
  22. };
  23. BABYLON.Node.prototype.updateCache = function (force) {
  24. if (!force && this.isSynchronized())
  25. return;
  26. this._cache.parent = this.parent;
  27. this._updateCache();
  28. };
  29. // override it in derived class if you add new variables to the cache
  30. // and call the parent class method if !ignoreParentClass
  31. // BABYLON.ParentClass.prototype._updateCache.call(this, ignoreParentClass)
  32. BABYLON.Node.prototype._updateCache = function (ignoreParentClass) {
  33. };
  34. // override it in derived class if you add new variables to the cache
  35. // and call the parent class method
  36. // BABYLON.ParentClass.prototype._isSynchronized.call(this)
  37. BABYLON.Node.prototype._isSynchronized = function () {
  38. return true;
  39. };
  40. BABYLON.Node.prototype._syncChildFlag = function() {
  41. this._childrenFlag = this.parent ? this.parent._childrenFlag : this._scene.getRenderId();
  42. };
  43. BABYLON.Node.prototype.isSynchronizedWithParent = function() {
  44. return this.parent ? !this.parent._needToSynchonizeChildren(this._childrenFlag) : true;
  45. };
  46. BABYLON.Node.prototype.isSynchronized = function (updateCache) {
  47. var check = this.hasNewParent();
  48. check = check ? true : !this.isSynchronizedWithParent();
  49. check = check ? true : !this._isSynchronized();
  50. if (updateCache)
  51. this.updateCache(true);
  52. return !check;
  53. };
  54. BABYLON.Node.prototype.hasNewParent = function (update) {
  55. if (this._cache.parent === this.parent)
  56. return false;
  57. if (update)
  58. this._cache.parent = this.parent;
  59. return true;
  60. };
  61. BABYLON.Node.prototype._needToSynchonizeChildren = function (childFlag) {
  62. return this._childrenFlag != childFlag;
  63. };
  64. BABYLON.Node.prototype.isReady = function () {
  65. return this._isReady;
  66. };
  67. BABYLON.Node.prototype.isEnabled = function () {
  68. if (!this.isReady() || !this._isEnabled) {
  69. return false;
  70. }
  71. if (this.parent) {
  72. return this.parent.isEnabled();
  73. }
  74. return true;
  75. };
  76. BABYLON.Node.prototype.setEnabled = function (value) {
  77. this._isEnabled = value;
  78. };
  79. BABYLON.Node.prototype.isDescendantOf = function (ancestor) {
  80. if (this.parent) {
  81. if (this.parent === ancestor) {
  82. return true;
  83. }
  84. return this.parent.isDescendantOf(ancestor);
  85. }
  86. return false;
  87. };
  88. BABYLON.Node.prototype._getDescendants = function (list, results) {
  89. for (var index = 0; index < list.length; index++) {
  90. var item = list[index];
  91. if (item.isDescendantOf(this)) {
  92. results.push(item);
  93. }
  94. }
  95. };
  96. BABYLON.Node.prototype.getDescendants = function () {
  97. var results = [];
  98. this._getDescendants(this._scene.meshes, results);
  99. this._getDescendants(this._scene.lights, results);
  100. this._getDescendants(this._scene.cameras, results);
  101. return results;
  102. };
  103. })();