babylon.node.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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._ranges = {};
  16. this._childrenFlag = -1;
  17. this._isEnabled = true;
  18. this._isReady = true;
  19. this._currentRenderId = -1;
  20. this._parentRenderId = -1;
  21. this.name = name;
  22. this.id = name;
  23. this._scene = scene;
  24. this._initCache();
  25. }
  26. Node.prototype.getScene = function () {
  27. return this._scene;
  28. };
  29. Node.prototype.getEngine = function () {
  30. return this._scene.getEngine();
  31. };
  32. // override it in derived class
  33. Node.prototype.getWorldMatrix = function () {
  34. return BABYLON.Matrix.Identity();
  35. };
  36. // override it in derived class if you add new variables to the cache
  37. // and call the parent class method
  38. Node.prototype._initCache = function () {
  39. this._cache = {};
  40. this._cache.parent = undefined;
  41. };
  42. Node.prototype.updateCache = function (force) {
  43. if (!force && this.isSynchronized())
  44. return;
  45. this._cache.parent = this.parent;
  46. this._updateCache();
  47. };
  48. // override it in derived class if you add new variables to the cache
  49. // and call the parent class method if !ignoreParentClass
  50. Node.prototype._updateCache = function (ignoreParentClass) {
  51. };
  52. // override it in derived class if you add new variables to the cache
  53. Node.prototype._isSynchronized = function () {
  54. return true;
  55. };
  56. Node.prototype._markSyncedWithParent = function () {
  57. this._parentRenderId = this.parent._currentRenderId;
  58. };
  59. Node.prototype.isSynchronizedWithParent = function () {
  60. if (!this.parent) {
  61. return true;
  62. }
  63. if (this._parentRenderId !== this.parent._currentRenderId) {
  64. return false;
  65. }
  66. return this.parent.isSynchronized();
  67. };
  68. Node.prototype.isSynchronized = function (updateCache) {
  69. var check = this.hasNewParent();
  70. check = check || !this.isSynchronizedWithParent();
  71. check = check || !this._isSynchronized();
  72. if (updateCache)
  73. this.updateCache(true);
  74. return !check;
  75. };
  76. Node.prototype.hasNewParent = function (update) {
  77. if (this._cache.parent === this.parent)
  78. return false;
  79. if (update)
  80. this._cache.parent = this.parent;
  81. return true;
  82. };
  83. /**
  84. * Is this node ready to be used/rendered
  85. * @return {boolean} is it ready
  86. */
  87. Node.prototype.isReady = function () {
  88. return this._isReady;
  89. };
  90. /**
  91. * Is this node enabled.
  92. * If the node has a parent and is enabled, the parent will be inspected as well.
  93. * @return {boolean} whether this node (and its parent) is enabled.
  94. * @see setEnabled
  95. */
  96. Node.prototype.isEnabled = function () {
  97. if (!this._isEnabled) {
  98. return false;
  99. }
  100. if (this.parent) {
  101. return this.parent.isEnabled();
  102. }
  103. return true;
  104. };
  105. /**
  106. * Set the enabled state of this node.
  107. * @param {boolean} value - the new enabled state
  108. * @see isEnabled
  109. */
  110. Node.prototype.setEnabled = function (value) {
  111. this._isEnabled = value;
  112. };
  113. /**
  114. * Is this node a descendant of the given node.
  115. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  116. * @param {BABYLON.Node} ancestor - The parent node to inspect
  117. * @see parent
  118. */
  119. Node.prototype.isDescendantOf = function (ancestor) {
  120. if (this.parent) {
  121. if (this.parent === ancestor) {
  122. return true;
  123. }
  124. return this.parent.isDescendantOf(ancestor);
  125. }
  126. return false;
  127. };
  128. Node.prototype._getDescendants = function (list, results) {
  129. for (var index = 0; index < list.length; index++) {
  130. var item = list[index];
  131. if (item.isDescendantOf(this)) {
  132. results.push(item);
  133. }
  134. }
  135. };
  136. /**
  137. * Will return all nodes that have this node as parent.
  138. * @return {BABYLON.Node[]} all children nodes of all types.
  139. */
  140. Node.prototype.getDescendants = function () {
  141. var results = [];
  142. this._getDescendants(this._scene.meshes, results);
  143. this._getDescendants(this._scene.lights, results);
  144. this._getDescendants(this._scene.cameras, results);
  145. return results;
  146. };
  147. Node.prototype._setReady = function (state) {
  148. if (state === this._isReady) {
  149. return;
  150. }
  151. if (!state) {
  152. this._isReady = false;
  153. return;
  154. }
  155. this._isReady = true;
  156. if (this.onReady) {
  157. this.onReady(this);
  158. }
  159. };
  160. Node.prototype.getAnimationByName = function (name) {
  161. for (var i = 0; i < this.animations.length; i++) {
  162. var animation = this.animations[i];
  163. if (animation.name === name) {
  164. return animation;
  165. }
  166. }
  167. return null;
  168. };
  169. Node.prototype.createAnimationRange = function (name, from, to) {
  170. // check name not already in use
  171. if (!this._ranges[name]) {
  172. this._ranges[name] = new BABYLON.AnimationRange(name, from, to);
  173. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  174. if (this.animations[i]) {
  175. this.animations[i].createRange(name, from, to);
  176. }
  177. }
  178. }
  179. };
  180. Node.prototype.deleteAnimationRange = function (name, deleteFrames) {
  181. if (deleteFrames === void 0) { deleteFrames = true; }
  182. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  183. if (this.animations[i]) {
  184. this.animations[i].deleteRange(name, deleteFrames);
  185. }
  186. }
  187. this._ranges[name] = undefined; // said much faster than 'delete this._range[name]'
  188. };
  189. Node.prototype.getAnimationRange = function (name) {
  190. return this._ranges[name];
  191. };
  192. Node.prototype.beginAnimation = function (name, loop, speedRatio, onAnimationEnd) {
  193. var range = this.getAnimationRange(name);
  194. if (!range) {
  195. return null;
  196. }
  197. this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  198. };
  199. Node.prototype.serializeAnimationRanges = function () {
  200. var serializationRanges = [];
  201. for (var name in this._ranges) {
  202. var range = {};
  203. range.name = name;
  204. range.from = this._ranges[name].from;
  205. range.to = this._ranges[name].to;
  206. serializationRanges.push(range);
  207. }
  208. return serializationRanges;
  209. };
  210. Node.ParseAnimationRanges = function (node, parsedNode, scene) {
  211. if (parsedNode.ranges) {
  212. for (var index = 0; index < parsedNode.ranges.length; index++) {
  213. var data = parsedNode.ranges[index];
  214. node.createAnimationRange(data.name, data.from, data.to);
  215. }
  216. }
  217. };
  218. return Node;
  219. })();
  220. BABYLON.Node = Node;
  221. })(BABYLON || (BABYLON = {}));