babylon.node.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. };
  21. BABYLON.Node.prototype.updateCache = function (force) {
  22. if (!force && this.isSynchronized())
  23. return;
  24. this._cache.parent = this.parent;
  25. this._updateCache();
  26. };
  27. // override it in derived class if you add new variables to the cache
  28. // and call the parent class method if !ignoreParentClass
  29. // BABYLON.ParentClass.prototype._updateCache.call(this, ignoreParentClass)
  30. BABYLON.Node.prototype._updateCache = function (ignoreParentClass) {
  31. };
  32. // override it in derived class if you add new variables to the cache
  33. // and call the parent class method
  34. // BABYLON.ParentClass.prototype._isSynchronized.call(this)
  35. BABYLON.Node.prototype._isSynchronized = function () {
  36. return true;
  37. };
  38. BABYLON.Node.prototype.isSynchronized = function (updateCache) {
  39. var r = this.hasNewParent();
  40. r = r ? true : (this.parent && this.parent._needToSynchonizeChildren());
  41. r = r ? true : !this._isSynchronized();
  42. if(updateCache)
  43. this.updateCache(true);
  44. return !r;
  45. };
  46. BABYLON.Node.prototype.hasNewParent = function (update) {
  47. if (this._cache.parent === this.parent)
  48. return false;
  49. if(update)
  50. this._cache.parent = this.parent;
  51. return true;
  52. };
  53. BABYLON.Node.prototype._needToSynchonizeChildren = function () {
  54. return this._childrenFlag;
  55. };
  56. BABYLON.Node.prototype.isReady = function () {
  57. return this._isReady;
  58. };
  59. BABYLON.Node.prototype.isEnabled = function () {
  60. if (!this.isReady() || !this._isEnabled) {
  61. return false;
  62. }
  63. if (this.parent) {
  64. return this.parent.isEnabled();
  65. }
  66. return true;
  67. };
  68. BABYLON.Node.prototype.setEnabled = function (value) {
  69. this._isEnabled = value;
  70. };
  71. BABYLON.Node.prototype.isDescendantOf = function (ancestor) {
  72. if (this.parent) {
  73. if (this.parent === ancestor) {
  74. return true;
  75. }
  76. return this.parent.isDescendantOf(ancestor);
  77. }
  78. return false;
  79. };
  80. BABYLON.Node.prototype._getDescendants = function(list, results) {
  81. for (var index = 0; index < list.length; index++) {
  82. var item = list[index];
  83. if (item.isDescendantOf(this)) {
  84. results.push(item);
  85. }
  86. }
  87. };
  88. BABYLON.Node.prototype.getDescendants = function () {
  89. var results = [];
  90. this._getDescendants(this._scene.meshes, results);
  91. this._getDescendants(this._scene.lights, results);
  92. this._getDescendants(this._scene.cameras, results);
  93. return results;
  94. };
  95. })();