babylon.node.js 3.5 KB

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