babylon.node.js 9.9 KB

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