babylon.node.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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.uniqueId = this._scene.getUniqueId();
  77. this._initCache();
  78. }
  79. public getScene(): Scene {
  80. return this._scene;
  81. }
  82. public getEngine(): Engine {
  83. return this._scene.getEngine();
  84. }
  85. // Behaviors
  86. private _behaviors = new Array<Behavior<Node>>();
  87. public addBehavior(behavior: Behavior<Node>): Node {
  88. var index = this._behaviors.indexOf(behavior);
  89. if (index !== -1) {
  90. return;
  91. }
  92. behavior.attach(this);
  93. this._behaviors.push(behavior);
  94. return this;
  95. }
  96. public removeBehavior(behavior: Behavior<Node>): Node {
  97. var index = this._behaviors.indexOf(behavior);
  98. if (index === -1) {
  99. return;
  100. }
  101. this._behaviors[index].detach();
  102. this._behaviors.splice(index, 1);
  103. return this;
  104. }
  105. public get behaviors(): Behavior<Node>[] {
  106. return this._behaviors;
  107. }
  108. public getBehaviorByName(name: string): Behavior<Node> {
  109. for (var behavior of this._behaviors) {
  110. if (behavior.name === name) {
  111. return behavior;
  112. }
  113. }
  114. return null;
  115. }
  116. // override it in derived class
  117. public getWorldMatrix(): Matrix {
  118. return Matrix.Identity();
  119. }
  120. // override it in derived class if you add new variables to the cache
  121. // and call the parent class method
  122. public _initCache() {
  123. this._cache = {};
  124. this._cache.parent = undefined;
  125. }
  126. public updateCache(force?: boolean): void {
  127. if (!force && this.isSynchronized())
  128. return;
  129. this._cache.parent = this.parent;
  130. this._updateCache();
  131. }
  132. // override it in derived class if you add new variables to the cache
  133. // and call the parent class method if !ignoreParentClass
  134. public _updateCache(ignoreParentClass?: boolean): void {
  135. }
  136. // override it in derived class if you add new variables to the cache
  137. public _isSynchronized(): boolean {
  138. return true;
  139. }
  140. public _markSyncedWithParent() {
  141. this._parentRenderId = this.parent._currentRenderId;
  142. }
  143. public isSynchronizedWithParent(): boolean {
  144. if (!this.parent) {
  145. return true;
  146. }
  147. if (this._parentRenderId !== this.parent._currentRenderId) {
  148. return false;
  149. }
  150. return this.parent.isSynchronized();
  151. }
  152. public isSynchronized(updateCache?: boolean): boolean {
  153. var check = this.hasNewParent();
  154. check = check || !this.isSynchronizedWithParent();
  155. check = check || !this._isSynchronized();
  156. if (updateCache)
  157. this.updateCache(true);
  158. return !check;
  159. }
  160. public hasNewParent(update?: boolean): boolean {
  161. if (this._cache.parent === this.parent)
  162. return false;
  163. if (update)
  164. this._cache.parent = this.parent;
  165. return true;
  166. }
  167. /**
  168. * Is this node ready to be used/rendered
  169. * @return {boolean} is it ready
  170. */
  171. public isReady(): boolean {
  172. return this._isReady;
  173. }
  174. /**
  175. * Is this node enabled.
  176. * If the node has a parent and is enabled, the parent will be inspected as well.
  177. * @return {boolean} whether this node (and its parent) is enabled.
  178. * @see setEnabled
  179. */
  180. public isEnabled(): boolean {
  181. if (!this._isEnabled) {
  182. return false;
  183. }
  184. if (this.parent) {
  185. return this.parent.isEnabled();
  186. }
  187. return true;
  188. }
  189. /**
  190. * Set the enabled state of this node.
  191. * @param {boolean} value - the new enabled state
  192. * @see isEnabled
  193. */
  194. public setEnabled(value: boolean): void {
  195. this._isEnabled = value;
  196. }
  197. /**
  198. * Is this node a descendant of the given node.
  199. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined.
  200. * @param {BABYLON.Node} ancestor - The parent node to inspect
  201. * @see parent
  202. */
  203. public isDescendantOf(ancestor: Node): boolean {
  204. if (this.parent) {
  205. if (this.parent === ancestor) {
  206. return true;
  207. }
  208. return this.parent.isDescendantOf(ancestor);
  209. }
  210. return false;
  211. }
  212. /**
  213. * Evaluate the list of children and determine if they should be considered as descendants considering the given criterias
  214. * @param {BABYLON.Node[]} results the result array containing the nodes matching the given criterias
  215. * @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.
  216. * @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.
  217. */
  218. public _getDescendants(results: Node[], directDescendantsOnly: boolean = false, predicate?: (node: Node) => boolean): void {
  219. if (!this._children) {
  220. return;
  221. }
  222. for (var index = 0; index < this._children.length; index++) {
  223. var item = this._children[index];
  224. if (!predicate || predicate(item)) {
  225. results.push(item);
  226. }
  227. if (!directDescendantsOnly) {
  228. item._getDescendants(results, false, predicate);
  229. }
  230. }
  231. }
  232. /**
  233. * Will return all nodes that have this node as ascendant.
  234. * @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.
  235. * @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.
  236. * @return {BABYLON.Node[]} all children nodes of all types.
  237. */
  238. public getDescendants(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): Node[] {
  239. var results = [];
  240. this._getDescendants(results, directDescendantsOnly, predicate);
  241. return results;
  242. }
  243. /**
  244. * Get all child-meshes of this node.
  245. */
  246. public getChildMeshes(directDecendantsOnly?: boolean, predicate?: (node: Node) => boolean): AbstractMesh[] {
  247. var results: Array<AbstractMesh> = [];
  248. this._getDescendants(results, directDecendantsOnly, (node: Node) => {
  249. return ((!predicate || predicate(node)) && (node instanceof AbstractMesh));
  250. });
  251. return results;
  252. }
  253. /**
  254. * Get all direct children of this node.
  255. */
  256. public getChildren(predicate?: (node: Node) => boolean): Node[] {
  257. return this.getDescendants(true, predicate);
  258. }
  259. public _setReady(state: boolean): void {
  260. if (state === this._isReady) {
  261. return;
  262. }
  263. if (!state) {
  264. this._isReady = false;
  265. return;
  266. }
  267. this._isReady = true;
  268. if (this.onReady) {
  269. this.onReady(this);
  270. }
  271. }
  272. public getAnimationByName(name: string): Animation {
  273. for (var i = 0; i < this.animations.length; i++) {
  274. var animation = this.animations[i];
  275. if (animation.name === name) {
  276. return animation;
  277. }
  278. }
  279. return null;
  280. }
  281. public createAnimationRange(name: string, from: number, to: number): void {
  282. // check name not already in use
  283. if (!this._ranges[name]) {
  284. this._ranges[name] = new AnimationRange(name, from, to);
  285. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  286. if (this.animations[i]) {
  287. this.animations[i].createRange(name, from, to);
  288. }
  289. }
  290. }
  291. }
  292. public deleteAnimationRange(name: string, deleteFrames = true): void {
  293. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  294. if (this.animations[i]) {
  295. this.animations[i].deleteRange(name, deleteFrames);
  296. }
  297. }
  298. this._ranges[name] = undefined; // said much faster than 'delete this._range[name]'
  299. }
  300. public getAnimationRange(name: string): AnimationRange {
  301. return this._ranges[name];
  302. }
  303. public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): void {
  304. var range = this.getAnimationRange(name);
  305. if (!range) {
  306. return null;
  307. }
  308. this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  309. }
  310. public serializeAnimationRanges(): any {
  311. var serializationRanges = [];
  312. for (var name in this._ranges) {
  313. var range: any = {};
  314. range.name = name;
  315. range.from = this._ranges[name].from;
  316. range.to = this._ranges[name].to;
  317. serializationRanges.push(range);
  318. }
  319. return serializationRanges;
  320. }
  321. public dispose(): void {
  322. this.parent = null;
  323. // Callback
  324. this.onDisposeObservable.notifyObservers(this);
  325. this.onDisposeObservable.clear();
  326. // Behaviors
  327. for (var behavior of this._behaviors) {
  328. behavior.detach();
  329. }
  330. this._behaviors = [];
  331. }
  332. public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
  333. if (parsedNode.ranges) {
  334. for (var index = 0; index < parsedNode.ranges.length; index++) {
  335. var data = parsedNode.ranges[index];
  336. node.createAnimationRange(data.name, data.from, data.to);
  337. }
  338. }
  339. }
  340. }
  341. }