babylon.node.js 3.6 KB

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