babylon.node.ts 11 KB

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