babylon.node.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. module BABYLON {
  2. /**
  3. * Node is the basic class for all scene objects (Mesh, Light Camera).
  4. */
  5. export class Node {
  6. /**
  7. * Gets or sets the name of the node
  8. */
  9. @serialize()
  10. public name: string;
  11. /**
  12. * Gets or sets the id of the node
  13. */
  14. @serialize()
  15. public id: string;
  16. /**
  17. * Gets or sets the unique id of the node
  18. */
  19. @serialize()
  20. public uniqueId: number;
  21. /**
  22. * Gets or sets a string used to store user defined state for the node
  23. */
  24. @serialize()
  25. public state = "";
  26. /**
  27. * Gets or sets an object used to store user defined information for the node
  28. */
  29. @serialize()
  30. public metadata: any = null;
  31. /**
  32. * Gets or sets a boolean used to define if the node must be serialized
  33. */
  34. public doNotSerialize = false;
  35. /** @ignore */
  36. public _isDisposed = false;
  37. /**
  38. * Gets a list of {BABYLON.Animation} associated with the node
  39. */
  40. public animations = new Array<Animation>();
  41. private _ranges: { [name: string]: Nullable<AnimationRange> } = {};
  42. /**
  43. * Callback raised when the node is ready to be used
  44. */
  45. public onReady: (node: Node) => void;
  46. private _isEnabled = true;
  47. private _isReady = true;
  48. /** @ignore */
  49. public _currentRenderId = -1;
  50. private _parentRenderId = -1;
  51. /** @ignore */
  52. public _waitingParentId: Nullable<string>;
  53. private _scene: Scene;
  54. /** @ignore */
  55. public _cache: any;
  56. private _parentNode: Nullable<Node>;
  57. private _children: Node[];
  58. /**
  59. * Gets a boolean indicating if the node has been disposed
  60. * @returns true if the node was disposed
  61. */
  62. public isDisposed(): boolean {
  63. return this._isDisposed;
  64. }
  65. /**
  66. * Gets or sets the parent of the node
  67. */
  68. public set parent(parent: Nullable<Node>) {
  69. if (this._parentNode === parent) {
  70. return;
  71. }
  72. // Remove self from list of children of parent
  73. if (this._parentNode && this._parentNode._children !== undefined && this._parentNode._children !== null) {
  74. var index = this._parentNode._children.indexOf(this);
  75. if (index !== -1) {
  76. this._parentNode._children.splice(index, 1);
  77. }
  78. }
  79. // Store new parent
  80. this._parentNode = parent;
  81. // Add as child to new parent
  82. if (this._parentNode) {
  83. if (this._parentNode._children === undefined || this._parentNode._children === null) {
  84. this._parentNode._children = new Array<Node>();
  85. }
  86. this._parentNode._children.push(this);
  87. }
  88. }
  89. public get parent(): Nullable<Node> {
  90. return this._parentNode;
  91. }
  92. /**
  93. * Gets a string idenfifying the name of the class
  94. * @returns "Node" string
  95. */
  96. public getClassName(): string {
  97. return "Node";
  98. }
  99. /**
  100. * An event triggered when the mesh is disposed
  101. * @type {BABYLON.Observable}
  102. */
  103. public onDisposeObservable = new Observable<Node>();
  104. private _onDisposeObserver: Nullable<Observer<Node>>;
  105. /**
  106. * Sets a callback that will be raised when the node will be disposed
  107. */
  108. public set onDispose(callback: () => void) {
  109. if (this._onDisposeObserver) {
  110. this.onDisposeObservable.remove(this._onDisposeObserver);
  111. }
  112. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  113. }
  114. /**
  115. * Creates a new Node
  116. * @param {string} name - the name and id to be given to this node
  117. * @param {BABYLON.Scene} the scene this node will be added to
  118. */
  119. constructor(name: string, scene: Nullable<Scene> = null) {
  120. this.name = name;
  121. this.id = name;
  122. this._scene = <Scene>(scene || Engine.LastCreatedScene);
  123. this.uniqueId = this._scene.getUniqueId();
  124. this._initCache();
  125. }
  126. /**
  127. * Gets the scene of the node
  128. * @returns a {BABYLON.Scene}
  129. */
  130. public getScene(): Scene {
  131. return this._scene;
  132. }
  133. /**
  134. * Gets the engine of the node
  135. * @returns a {BABYLON.Engine}
  136. */
  137. public getEngine(): Engine {
  138. return this._scene.getEngine();
  139. }
  140. // Behaviors
  141. private _behaviors = new Array<Behavior<Node>>();
  142. /**
  143. * Attach a behavior to the node
  144. * @see http://doc.babylonjs.com/features/behaviour
  145. * @param behavior defines the behavior to attach
  146. * @returns the current Node
  147. */
  148. public addBehavior(behavior: Behavior<Node>): Node {
  149. var index = this._behaviors.indexOf(behavior);
  150. if (index !== -1) {
  151. return this;
  152. }
  153. behavior.init();
  154. if (this._scene.isLoading) {
  155. // We defer the attach when the scene will be loaded
  156. var observer = this._scene.onDataLoadedObservable.add(() => {
  157. behavior.attach(this);
  158. setTimeout(() => {
  159. // Need to use a timeout to avoid removing an observer while iterating the list of observers
  160. this._scene.onDataLoadedObservable.remove(observer);
  161. }, 0);
  162. });
  163. } else {
  164. behavior.attach(this);
  165. }
  166. this._behaviors.push(behavior);
  167. return this;
  168. }
  169. /**
  170. * Remove an attached behavior
  171. * @see http://doc.babylonjs.com/features/behaviour
  172. * @param behavior defines the behavior to attach
  173. * @returns the current Node
  174. */
  175. public removeBehavior(behavior: Behavior<Node>): Node {
  176. var index = this._behaviors.indexOf(behavior);
  177. if (index === -1) {
  178. return this;
  179. }
  180. this._behaviors[index].detach();
  181. this._behaviors.splice(index, 1);
  182. return this;
  183. }
  184. /**
  185. * Gets the list of attached behaviors
  186. * @see http://doc.babylonjs.com/features/behaviour
  187. */
  188. public get behaviors(): Behavior<Node>[] {
  189. return this._behaviors;
  190. }
  191. /**
  192. * Gets an attached behavior by name
  193. * @param name defines the name of the behavior to look for
  194. * @see http://doc.babylonjs.com/features/behaviour
  195. * @returns null if behavior was not found else the requested behavior
  196. */
  197. public getBehaviorByName(name: string): Nullable<Behavior<Node>> {
  198. for (var behavior of this._behaviors) {
  199. if (behavior.name === name) {
  200. return behavior;
  201. }
  202. }
  203. return null;
  204. }
  205. /**
  206. * Returns the world matrix of the node
  207. * @returns a matrix containing the node's world matrix
  208. */
  209. public getWorldMatrix(): Matrix {
  210. return Matrix.Identity();
  211. }
  212. // override it in derived class if you add new variables to the cache
  213. // and call the parent class method
  214. /** @ignore */
  215. public _initCache() {
  216. this._cache = {};
  217. this._cache.parent = undefined;
  218. }
  219. /** @ignore */
  220. public updateCache(force?: boolean): void {
  221. if (!force && this.isSynchronized())
  222. return;
  223. this._cache.parent = this.parent;
  224. this._updateCache();
  225. }
  226. // override it in derived class if you add new variables to the cache
  227. // and call the parent class method if !ignoreParentClass
  228. /** @ignore */
  229. public _updateCache(ignoreParentClass?: boolean): void {
  230. }
  231. // override it in derived class if you add new variables to the cache
  232. /** @ignore */
  233. public _isSynchronized(): boolean {
  234. return true;
  235. }
  236. /** @ignore */
  237. public _markSyncedWithParent() {
  238. if (this.parent) {
  239. this._parentRenderId = this.parent._currentRenderId;
  240. }
  241. }
  242. /** @ignore */
  243. public isSynchronizedWithParent(): boolean {
  244. if (!this.parent) {
  245. return true;
  246. }
  247. if (this._parentRenderId !== this.parent._currentRenderId) {
  248. return false;
  249. }
  250. return this.parent.isSynchronized();
  251. }
  252. /** @ignore */
  253. public isSynchronized(updateCache?: boolean): boolean {
  254. var check = this.hasNewParent();
  255. check = check || !this.isSynchronizedWithParent();
  256. check = check || !this._isSynchronized();
  257. if (updateCache)
  258. this.updateCache(true);
  259. return !check;
  260. }
  261. /** @ignore */
  262. public hasNewParent(update?: boolean): boolean {
  263. if (this._cache.parent === this.parent)
  264. return false;
  265. if (update)
  266. this._cache.parent = this.parent;
  267. return true;
  268. }
  269. /**
  270. * Is this node ready to be used/rendered
  271. * @return true if the node is ready
  272. */
  273. public isReady(): boolean {
  274. return this._isReady;
  275. }
  276. /**
  277. * Is this node enabled?
  278. * If the node has a parent, all ancestors will be checked and false will be returned if any are false (not enabled), otherwise will return true
  279. * @param checkAncestors indicates if this method should check the ancestors. The default is to check the ancestors. If set to false, the method will return the value of this node without checking ancestors
  280. * @return whether this node (and its parent) is enabled
  281. * @see setEnabled
  282. */
  283. public isEnabled(checkAncestors: boolean = true): boolean {
  284. if (checkAncestors === false) {
  285. return this._isEnabled;
  286. }
  287. if (this._isEnabled === false) {
  288. return false;
  289. }
  290. if (this.parent !== undefined && this.parent !== null) {
  291. return this.parent.isEnabled(checkAncestors);
  292. }
  293. return true;
  294. }
  295. /**
  296. * Set the enabled state of this node
  297. * @param value defines the new enabled state
  298. * @see isEnabled
  299. */
  300. public setEnabled(value: boolean): void {
  301. this._isEnabled = value;
  302. }
  303. /**
  304. * Is this node a descendant of the given node?
  305. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined
  306. * @param ancestor defines the parent node to inspect
  307. * @see parent
  308. * @returns a boolean indicating if this node is a descendant of the given node
  309. */
  310. public isDescendantOf(ancestor: Node): boolean {
  311. if (this.parent) {
  312. if (this.parent === ancestor) {
  313. return true;
  314. }
  315. return this.parent.isDescendantOf(ancestor);
  316. }
  317. return false;
  318. }
  319. /** @ignore */
  320. public _getDescendants(results: Node[], directDescendantsOnly: boolean = false, predicate?: (node: Node) => boolean): void {
  321. if (!this._children) {
  322. return;
  323. }
  324. for (var index = 0; index < this._children.length; index++) {
  325. var item = this._children[index];
  326. if (!predicate || predicate(item)) {
  327. results.push(item);
  328. }
  329. if (!directDescendantsOnly) {
  330. item._getDescendants(results, false, predicate);
  331. }
  332. }
  333. }
  334. /**
  335. * Will return all nodes that have this node as ascendant
  336. * @param directDescendantsOnly defines 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
  337. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  338. * @return all children nodes of all types
  339. */
  340. public getDescendants(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): Node[] {
  341. var results = new Array<Node>();
  342. this._getDescendants(results, directDescendantsOnly, predicate);
  343. return results;
  344. }
  345. /**
  346. * Get all child-meshes of this node
  347. * @param directDescendantsOnly defines 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
  348. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  349. * @returns an array of {BABYLON.AbstractMesh}
  350. */
  351. public getChildMeshes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): AbstractMesh[] {
  352. var results: Array<AbstractMesh> = [];
  353. this._getDescendants(results, directDescendantsOnly, (node: Node) => {
  354. return ((!predicate || predicate(node)) && (node instanceof AbstractMesh));
  355. });
  356. return results;
  357. }
  358. /**
  359. * Get all child-transformNodes of this node
  360. * @param directDescendantsOnly defines 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
  361. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  362. * @returns an array of {BABYLON.TransformNode}
  363. */
  364. public getChildTransformNodes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): TransformNode[] {
  365. var results: Array<TransformNode> = [];
  366. this._getDescendants(results, directDescendantsOnly, (node: Node) => {
  367. return ((!predicate || predicate(node)) && (node instanceof TransformNode));
  368. });
  369. return results;
  370. }
  371. /**
  372. * Get all direct children of this node
  373. * @param predicate defines an optional predicate that will be called on every evaluated child, the predicate must return true for a given child to be part of the result, otherwise it will be ignored
  374. * @returns an array of {BABYLON.Node}
  375. */
  376. public getChildren(predicate?: (node: Node) => boolean): Node[] {
  377. return this.getDescendants(true, predicate);
  378. }
  379. /** @ignore */
  380. public _setReady(state: boolean): void {
  381. if (state === this._isReady) {
  382. return;
  383. }
  384. if (!state) {
  385. this._isReady = false;
  386. return;
  387. }
  388. this._isReady = true;
  389. if (this.onReady) {
  390. this.onReady(this);
  391. }
  392. }
  393. /**
  394. * Get an animation by name
  395. * @param name defines the name of the animation to look for
  396. * @returns null if not found else the requested animation
  397. */
  398. public getAnimationByName(name: string): Nullable<Animation> {
  399. for (var i = 0; i < this.animations.length; i++) {
  400. var animation = this.animations[i];
  401. if (animation.name === name) {
  402. return animation;
  403. }
  404. }
  405. return null;
  406. }
  407. /**
  408. * Creates an animation range for this node
  409. * @param name defines the name of the range
  410. * @param from defines the starting key
  411. * @param to defines the end key
  412. */
  413. public createAnimationRange(name: string, from: number, to: number): void {
  414. // check name not already in use
  415. if (!this._ranges[name]) {
  416. this._ranges[name] = new AnimationRange(name, from, to);
  417. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  418. if (this.animations[i]) {
  419. this.animations[i].createRange(name, from, to);
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * Delete a specific animation range
  426. * @param name defines the name of the range to delete
  427. * @param deleteFrames defines if animation frames from the range must be deleted as well
  428. */
  429. public deleteAnimationRange(name: string, deleteFrames = true): void {
  430. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  431. if (this.animations[i]) {
  432. this.animations[i].deleteRange(name, deleteFrames);
  433. }
  434. }
  435. this._ranges[name] = null; // said much faster than 'delete this._range[name]'
  436. }
  437. /**
  438. * Get an animation range by name
  439. * @param name defines the name of the animation range to look for
  440. * @returns null if not found else the requested animation range
  441. */
  442. public getAnimationRange(name: string): Nullable<AnimationRange> {
  443. return this._ranges[name];
  444. }
  445. /**
  446. * Will start the animation sequence
  447. * @param name defines the range frames for animation sequence
  448. * @param loop defines if the animation should loop (false by default)
  449. * @param speedRatio defines the speed factor in which to run the animation (1 by default)
  450. * @param onAnimationEnd defines a function to be executed when the animation ended (undefined by default)
  451. * @returns the object created for this animation. If range does not exist, it will return null
  452. */
  453. public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): Nullable<Animatable> {
  454. var range = this.getAnimationRange(name);
  455. if (!range) {
  456. return null;
  457. }
  458. return this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  459. }
  460. /**
  461. * Serialize animation ranges into a JSON compatible object
  462. * @returns serialization object
  463. */
  464. public serializeAnimationRanges(): any {
  465. var serializationRanges = [];
  466. for (var name in this._ranges) {
  467. var localRange = this._ranges[name];
  468. if (!localRange) {
  469. continue;
  470. }
  471. var range: any = {};
  472. range.name = name;
  473. range.from = localRange.from;
  474. range.to = localRange.to;
  475. serializationRanges.push(range);
  476. }
  477. return serializationRanges;
  478. }
  479. /**
  480. * Computes the world matrix of the node
  481. * @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
  482. * @returns the world matrix
  483. */
  484. public computeWorldMatrix(force?: boolean): Matrix {
  485. return Matrix.Identity();
  486. }
  487. /**
  488. * Releases all associated resources
  489. */
  490. public dispose(): void {
  491. this.parent = null;
  492. // Callback
  493. this.onDisposeObservable.notifyObservers(this);
  494. this.onDisposeObservable.clear();
  495. // Behaviors
  496. for (var behavior of this._behaviors) {
  497. behavior.detach();
  498. }
  499. this._behaviors = [];
  500. this._isDisposed = true;
  501. }
  502. /**
  503. * Parse animation range data from a serialization object and store them into a given node
  504. * @param node defines where to store the animation ranges
  505. * @param parsedNode defines the serialization object to read data from
  506. * @param scene defines the hosting scene
  507. */
  508. public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
  509. if (parsedNode.ranges) {
  510. for (var index = 0; index < parsedNode.ranges.length; index++) {
  511. var data = parsedNode.ranges[index];
  512. node.createAnimationRange(data.name, data.from, data.to);
  513. }
  514. }
  515. }
  516. }
  517. }