babylon.node.js 5.7 KB

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