babylon.node.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /// <reference path="Tools\babylon.decorators.ts" />
  2. module BABYLON {
  3. /**
  4. * Node is the basic class for all scene objects (Mesh, Light Camera).
  5. */
  6. export class 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. @serialize()
  16. public metadata: any = null;
  17. public doNotSerialize = false;
  18. public animations = new Array<Animation>();
  19. private _ranges: { [name: string]: AnimationRange; } = {};
  20. public onReady: (node: Node) => void;
  21. private _childrenFlag = -1;
  22. private _isEnabled = true;
  23. private _isReady = true;
  24. public _currentRenderId = -1;
  25. private _parentRenderId = -1;
  26. public _waitingParentId: string;
  27. private _scene: Scene;
  28. public _cache;
  29. private _parentNode: Node;
  30. private _children: Node[];
  31. public set parent(parent: Node) {
  32. if (this._parentNode === parent) {
  33. return;
  34. }
  35. if (this._parentNode) {
  36. var index = this._parentNode._children.indexOf(this);
  37. if (index !== -1) {
  38. this._parentNode._children.splice(index, 1);
  39. }
  40. }
  41. this._parentNode = parent;
  42. if (this._parentNode) {
  43. if (!this._parentNode._children) {
  44. this._parentNode._children = new Array<Node>();
  45. }
  46. this._parentNode._children.push(this);
  47. }
  48. }
  49. public get parent(): Node {
  50. return this._parentNode;
  51. }
  52. public getClassName(): string {
  53. return "Node";
  54. }
  55. /**
  56. * An event triggered when the mesh is disposed.
  57. * @type {BABYLON.Observable}
  58. */
  59. public onDisposeObservable = new Observable<Node>();
  60. private _onDisposeObserver: Observer<Node>;
  61. public set onDispose(callback: () => void) {
  62. if (this._onDisposeObserver) {
  63. this.onDisposeObservable.remove(this._onDisposeObserver);
  64. }
  65. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  66. }
  67. /**
  68. * @constructor
  69. * @param {string} name - the name and id to be given to this node
  70. * @param {BABYLON.Scene} the scene this node will be added to
  71. */
  72. constructor(name: string, scene: Scene) {
  73. this.name = name;
  74. this.id = name;
  75. this._scene = scene || Engine.LastCreatedScene;
  76. this._initCache();
  77. }
  78. public getScene(): Scene {
  79. return this._scene;
  80. }
  81. public getEngine(): Engine {
  82. return this._scene.getEngine();
  83. }
  84. // override it in derived class
  85. public getWorldMatrix(): Matrix {
  86. return Matrix.Identity();
  87. }
  88. // override it in derived class if you add new variables to the cache
  89. // and call the parent class method
  90. public _initCache() {
  91. this._cache = {};
  92. this._cache.parent = undefined;
  93. }
  94. public updateCache(force?: boolean): void {
  95. if (!force && this.isSynchronized())
  96. return;
  97. this._cache.parent = this.parent;
  98. this._updateCache();
  99. }
  100. // override it in derived class if you add new variables to the cache
  101. // and call the parent class method if !ignoreParentClass
  102. public _updateCache(ignoreParentClass?: boolean): void {
  103. }
  104. // override it in derived class if you add new variables to the cache
  105. public _isSynchronized(): boolean {
  106. return true;
  107. }
  108. public _markSyncedWithParent() {
  109. this._parentRenderId = this.parent._currentRenderId;
  110. }
  111. public isSynchronizedWithParent(): boolean {
  112. if (!this.parent) {
  113. return true;
  114. }
  115. if (this._parentRenderId !== this.parent._currentRenderId) {
  116. return false;
  117. }
  118. return this.parent.isSynchronized();
  119. }
  120. public isSynchronized(updateCache?: boolean): boolean {
  121. var check = this.hasNewParent();
  122. check = check || !this.isSynchronizedWithParent();
  123. check = check || !this._isSynchronized();
  124. if (updateCache)
  125. this.updateCache(true);
  126. return !check;
  127. }
  128. public hasNewParent(update?: boolean): boolean {
  129. if (this._cache.parent === this.parent)
  130. return false;
  131. if (update)
  132. this._cache.parent = this.parent;
  133. return true;
  134. }
  135. /**
  136. * Is this node ready to be used/rendered
  137. * @return {boolean} is it ready
  138. */
  139. public isReady(): boolean {
  140. return this._isReady;
  141. }
  142. /**
  143. * Is this node enabled.
  144. * If the node has a parent and is enabled, the parent will be inspected as well.
  145. * @return {boolean} whether this node (and its parent) is enabled.
  146. * @see setEnabled
  147. */
  148. public isEnabled(): boolean {
  149. if (!this._isEnabled) {
  150. return false;
  151. }
  152. if (this.parent) {
  153. return this.parent.isEnabled();
  154. }
  155. return true;
  156. }
  157. /**
  158. * Set the enabled state of this node.
  159. * @param {boolean} value - the new enabled state
  160. * @see isEnabled
  161. */
  162. public setEnabled(value: boolean): void {
  163. this._isEnabled = value;
  164. }
  165. /**
  166. * Is this node a descendant of the given node.
  167. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  168. * @param {BABYLON.Node} ancestor - The parent node to inspect
  169. * @see parent
  170. */
  171. public isDescendantOf(ancestor: Node): boolean {
  172. if (this.parent) {
  173. if (this.parent === ancestor) {
  174. return true;
  175. }
  176. return this.parent.isDescendantOf(ancestor);
  177. }
  178. return false;
  179. }
  180. /**
  181. * Evaluate the list of children and determine if they should be considered as descendants considering the given criterias
  182. * @param {BABYLON.Node[]} results the result array containing the nodes matching the given criterias
  183. * @param {boolean} directDescendantsOnly 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.
  184. * @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.
  185. */
  186. public _getDescendants(results: Node[], directDescendantsOnly: boolean = false, predicate?: (node: Node) => boolean): void {
  187. if (!this._children) {
  188. return;
  189. }
  190. for (var index = 0; index < this._children.length; index++) {
  191. var item = this._children[index];
  192. if (!predicate || predicate(item)) {
  193. results.push(item);
  194. }
  195. if (!directDescendantsOnly) {
  196. item._getDescendants(results, false, predicate);
  197. }
  198. }
  199. }
  200. /**
  201. * Will return all nodes that have this node as ascendant.
  202. * @param {boolean} directDescendantsOnly 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.
  203. * @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.
  204. * @return {BABYLON.Node[]} all children nodes of all types.
  205. */
  206. public getDescendants(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): Node[] {
  207. var results = [];
  208. this._getDescendants(results, directDescendantsOnly, predicate);
  209. return results;
  210. }
  211. /**
  212. * Get all child-meshes of this node.
  213. */
  214. public getChildMeshes(directDecendantsOnly?: boolean, predicate?: (node: Node) => boolean): AbstractMesh[] {
  215. var results: Array<AbstractMesh> = [];
  216. this._getDescendants(results, directDecendantsOnly, (node: Node) => {
  217. return ((!predicate || predicate(node)) && (node instanceof AbstractMesh));
  218. });
  219. return results;
  220. }
  221. /**
  222. * Get all direct children of this node.
  223. */
  224. public getChildren(predicate?: (node: Node) => boolean): Node[] {
  225. return this.getDescendants(true, predicate);
  226. }
  227. public _setReady(state: boolean): void {
  228. if (state === this._isReady) {
  229. return;
  230. }
  231. if (!state) {
  232. this._isReady = false;
  233. return;
  234. }
  235. this._isReady = true;
  236. if (this.onReady) {
  237. this.onReady(this);
  238. }
  239. }
  240. public getAnimationByName(name: string): Animation {
  241. for (var i = 0; i < this.animations.length; i++) {
  242. var animation = this.animations[i];
  243. if (animation.name === name) {
  244. return animation;
  245. }
  246. }
  247. return null;
  248. }
  249. public createAnimationRange(name: string, from: number, to: number): void {
  250. // check name not already in use
  251. if (!this._ranges[name]) {
  252. this._ranges[name] = new AnimationRange(name, from, to);
  253. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  254. if (this.animations[i]) {
  255. this.animations[i].createRange(name, from, to);
  256. }
  257. }
  258. }
  259. }
  260. public deleteAnimationRange(name: string, deleteFrames = true): void {
  261. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  262. if (this.animations[i]) {
  263. this.animations[i].deleteRange(name, deleteFrames);
  264. }
  265. }
  266. this._ranges[name] = undefined; // said much faster than 'delete this._range[name]'
  267. }
  268. public getAnimationRange(name: string): AnimationRange {
  269. return this._ranges[name];
  270. }
  271. public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): void {
  272. var range = this.getAnimationRange(name);
  273. if (!range) {
  274. return null;
  275. }
  276. this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  277. }
  278. public serializeAnimationRanges(): any {
  279. var serializationRanges = [];
  280. for (var name in this._ranges) {
  281. var range: any = {};
  282. range.name = name;
  283. range.from = this._ranges[name].from;
  284. range.to = this._ranges[name].to;
  285. serializationRanges.push(range);
  286. }
  287. return serializationRanges;
  288. }
  289. public dispose(): void {
  290. this.parent = null;
  291. // Callback
  292. this.onDisposeObservable.notifyObservers(this);
  293. this.onDisposeObservable.clear();
  294. }
  295. public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
  296. if (parsedNode.ranges) {
  297. for (var index = 0; index < parsedNode.ranges.length; index++) {
  298. var data = parsedNode.ranges[index];
  299. node.createAnimationRange(data.name, data.from, data.to);
  300. }
  301. }
  302. }
  303. }
  304. }