babylon.node.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var BABYLON;
  2. (function (BABYLON) {
  3. /**
  4. * Node is the basic class for all scene objects (Mesh, Light Camera).
  5. */
  6. var Node = (function () {
  7. /**
  8. * @constructor
  9. * @param {string} name - the name and id to be given to this node
  10. * @param {BABYLON.Scene} the scene this node will be added to
  11. */
  12. function Node(name, scene) {
  13. this.state = "";
  14. this.animations = new Array();
  15. this._childrenFlag = -1;
  16. this._isEnabled = true;
  17. this._isReady = true;
  18. this._currentRenderId = -1;
  19. this.name = name;
  20. this.id = name;
  21. this._scene = scene;
  22. this._initCache();
  23. }
  24. Node.prototype.getScene = function () {
  25. return this._scene;
  26. };
  27. Node.prototype.getEngine = function () {
  28. return this._scene.getEngine();
  29. };
  30. // override it in derived class
  31. Node.prototype.getWorldMatrix = function () {
  32. return BABYLON.Matrix.Identity();
  33. };
  34. // override it in derived class if you add new variables to the cache
  35. // and call the parent class method
  36. Node.prototype._initCache = function () {
  37. this._cache = {};
  38. this._cache.parent = undefined;
  39. };
  40. Node.prototype.updateCache = function (force) {
  41. if (!force && this.isSynchronized())
  42. return;
  43. this._cache.parent = this.parent;
  44. this._updateCache();
  45. };
  46. // override it in derived class if you add new variables to the cache
  47. // and call the parent class method if !ignoreParentClass
  48. Node.prototype._updateCache = function (ignoreParentClass) {
  49. };
  50. // override it in derived class if you add new variables to the cache
  51. Node.prototype._isSynchronized = function () {
  52. return true;
  53. };
  54. Node.prototype.isSynchronizedWithParent = function () {
  55. return this.parent ? this.parent._currentRenderId <= this._currentRenderId && this.parent.isSynchronized() : true;
  56. };
  57. Node.prototype.isSynchronized = function (updateCache) {
  58. var check = this.hasNewParent();
  59. check = check || !this.isSynchronizedWithParent();
  60. check = check || !this._isSynchronized();
  61. if (updateCache)
  62. this.updateCache(true);
  63. return !check;
  64. };
  65. Node.prototype.hasNewParent = function (update) {
  66. if (this._cache.parent === this.parent)
  67. return false;
  68. if (update)
  69. this._cache.parent = this.parent;
  70. return true;
  71. };
  72. /**
  73. * Is this node ready to be used/rendered
  74. * @return {boolean} is it ready
  75. */
  76. Node.prototype.isReady = function () {
  77. return this._isReady;
  78. };
  79. /**
  80. * Is this node enabled.
  81. * If the node has a parent and is enabled, the parent will be inspected as well.
  82. * @return {boolean} whether this node (and its parent) is enabled.
  83. * @see setEnabled
  84. */
  85. Node.prototype.isEnabled = function () {
  86. if (!this._isEnabled) {
  87. return false;
  88. }
  89. if (this.parent) {
  90. return this.parent.isEnabled();
  91. }
  92. return true;
  93. };
  94. /**
  95. * Set the enabled state of this node.
  96. * @param {boolean} value - the new enabled state
  97. * @see isEnabled
  98. */
  99. Node.prototype.setEnabled = function (value) {
  100. this._isEnabled = value;
  101. };
  102. /**
  103. * Is this node a descendant of the given node.
  104. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  105. * @param {BABYLON.Node} ancestor - The parent node to inspect
  106. * @see parent
  107. */
  108. Node.prototype.isDescendantOf = function (ancestor) {
  109. if (this.parent) {
  110. if (this.parent === ancestor) {
  111. return true;
  112. }
  113. return this.parent.isDescendantOf(ancestor);
  114. }
  115. return false;
  116. };
  117. Node.prototype._getDescendants = function (list, results) {
  118. for (var index = 0; index < list.length; index++) {
  119. var item = list[index];
  120. if (item.isDescendantOf(this)) {
  121. results.push(item);
  122. }
  123. }
  124. };
  125. /**
  126. * Will return all nodes that have this node as parent.
  127. * @return {BABYLON.Node[]} all children nodes of all types.
  128. */
  129. Node.prototype.getDescendants = function () {
  130. var results = [];
  131. this._getDescendants(this._scene.meshes, results);
  132. this._getDescendants(this._scene.lights, results);
  133. this._getDescendants(this._scene.cameras, results);
  134. return results;
  135. };
  136. Node.prototype._setReady = function (state) {
  137. if (state == this._isReady) {
  138. return;
  139. }
  140. if (!state) {
  141. this._isReady = false;
  142. return;
  143. }
  144. this._isReady = true;
  145. if (this.onReady) {
  146. this.onReady(this);
  147. }
  148. };
  149. return Node;
  150. })();
  151. BABYLON.Node = Node;
  152. })(BABYLON || (BABYLON = {}));
  153. //# sourceMappingURL=babylon.node.js.map