babylon.node.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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._markSyncedWithParent = function () {
  56. this._parentRenderId = this.parent._currentRenderId;
  57. };
  58. Node.prototype.isSynchronizedWithParent = function () {
  59. if (!this.parent) {
  60. return true;
  61. }
  62. if (this._parentRenderId !== this.parent._currentRenderId) {
  63. return false;
  64. }
  65. return this.parent.isSynchronized();
  66. };
  67. Node.prototype.isSynchronized = function (updateCache) {
  68. var check = this.hasNewParent();
  69. check = check || !this.isSynchronizedWithParent();
  70. check = check || !this._isSynchronized();
  71. if (updateCache)
  72. this.updateCache(true);
  73. return !check;
  74. };
  75. Node.prototype.hasNewParent = function (update) {
  76. if (this._cache.parent === this.parent)
  77. return false;
  78. if (update)
  79. this._cache.parent = this.parent;
  80. return true;
  81. };
  82. /**
  83. * Is this node ready to be used/rendered
  84. * @return {boolean} is it ready
  85. */
  86. Node.prototype.isReady = function () {
  87. return this._isReady;
  88. };
  89. /**
  90. * Is this node enabled.
  91. * If the node has a parent and is enabled, the parent will be inspected as well.
  92. * @return {boolean} whether this node (and its parent) is enabled.
  93. * @see setEnabled
  94. */
  95. Node.prototype.isEnabled = function () {
  96. if (!this._isEnabled) {
  97. return false;
  98. }
  99. if (this.parent) {
  100. return this.parent.isEnabled();
  101. }
  102. return true;
  103. };
  104. /**
  105. * Set the enabled state of this node.
  106. * @param {boolean} value - the new enabled state
  107. * @see isEnabled
  108. */
  109. Node.prototype.setEnabled = function (value) {
  110. this._isEnabled = value;
  111. };
  112. /**
  113. * Is this node a descendant of the given node.
  114. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  115. * @param {BABYLON.Node} ancestor - The parent node to inspect
  116. * @see parent
  117. */
  118. Node.prototype.isDescendantOf = function (ancestor) {
  119. if (this.parent) {
  120. if (this.parent === ancestor) {
  121. return true;
  122. }
  123. return this.parent.isDescendantOf(ancestor);
  124. }
  125. return false;
  126. };
  127. Node.prototype._getDescendants = function (list, results) {
  128. for (var index = 0; index < list.length; index++) {
  129. var item = list[index];
  130. if (item.isDescendantOf(this)) {
  131. results.push(item);
  132. }
  133. }
  134. };
  135. /**
  136. * Will return all nodes that have this node as parent.
  137. * @return {BABYLON.Node[]} all children nodes of all types.
  138. */
  139. Node.prototype.getDescendants = function () {
  140. var results = [];
  141. this._getDescendants(this._scene.meshes, results);
  142. this._getDescendants(this._scene.lights, results);
  143. this._getDescendants(this._scene.cameras, results);
  144. return results;
  145. };
  146. Node.prototype._setReady = function (state) {
  147. if (state == this._isReady) {
  148. return;
  149. }
  150. if (!state) {
  151. this._isReady = false;
  152. return;
  153. }
  154. this._isReady = true;
  155. if (this.onReady) {
  156. this.onReady(this);
  157. }
  158. };
  159. return Node;
  160. })();
  161. BABYLON.Node = Node;
  162. })(BABYLON || (BABYLON = {}));
  163. //# sourceMappingURL=babylon.node.js.map