babylon.node.ts 14 KB

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