node.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. import { Scene } from "./scene";
  2. import { Nullable } from "./types";
  3. import { Matrix, Vector3 } from "./Maths/math.vector";
  4. import { Engine } from "./Engines/engine";
  5. import { IBehaviorAware, Behavior } from "./Behaviors/behavior";
  6. import { serialize } from "./Misc/decorators";
  7. import { Observable, Observer } from "./Misc/observable";
  8. import { EngineStore } from "./Engines/engineStore";
  9. import { _DevTools } from './Misc/devTools';
  10. import { AbstractActionManager } from './Actions/abstractActionManager';
  11. import { IInspectable } from './Misc/iInspectable';
  12. declare type Animatable = import("./Animations/animatable").Animatable;
  13. declare type AnimationPropertiesOverride = import("./Animations/animationPropertiesOverride").AnimationPropertiesOverride;
  14. declare type Animation = import("./Animations/animation").Animation;
  15. declare type AnimationRange = import("./Animations/animationRange").AnimationRange;
  16. declare type AbstractMesh = import("./Meshes/abstractMesh").AbstractMesh;
  17. /**
  18. * Defines how a node can be built from a string name.
  19. */
  20. export type NodeConstructor = (name: string, scene: Scene, options?: any) => () => Node;
  21. /**
  22. * Node is the basic class for all scene objects (Mesh, Light, Camera.)
  23. */
  24. export class Node implements IBehaviorAware<Node> {
  25. /** @hidden */
  26. public static _AnimationRangeFactory = (name: string, from: number, to: number): AnimationRange => {
  27. throw _DevTools.WarnImport("AnimationRange");
  28. }
  29. private static _NodeConstructors: { [key: string]: any } = {};
  30. /**
  31. * Add a new node constructor
  32. * @param type defines the type name of the node to construct
  33. * @param constructorFunc defines the constructor function
  34. */
  35. public static AddNodeConstructor(type: string, constructorFunc: NodeConstructor) {
  36. this._NodeConstructors[type] = constructorFunc;
  37. }
  38. /**
  39. * Returns a node constructor based on type name
  40. * @param type defines the type name
  41. * @param name defines the new node name
  42. * @param scene defines the hosting scene
  43. * @param options defines optional options to transmit to constructors
  44. * @returns the new constructor or null
  45. */
  46. public static Construct(type: string, name: string, scene: Scene, options?: any): Nullable<() => Node> {
  47. let constructorFunc = this._NodeConstructors[type];
  48. if (!constructorFunc) {
  49. return null;
  50. }
  51. return constructorFunc(name, scene, options);
  52. }
  53. /**
  54. * Gets or sets the name of the node
  55. */
  56. @serialize()
  57. public name: string;
  58. /**
  59. * Gets or sets the id of the node
  60. */
  61. @serialize()
  62. public id: string;
  63. /**
  64. * Gets or sets the unique id of the node
  65. */
  66. @serialize()
  67. public uniqueId: number;
  68. /**
  69. * Gets or sets a string used to store user defined state for the node
  70. */
  71. @serialize()
  72. public state = "";
  73. /**
  74. * Gets or sets an object used to store user defined information for the node
  75. */
  76. @serialize()
  77. public metadata: any = null;
  78. /**
  79. * For internal use only. Please do not use.
  80. */
  81. public reservedDataStore: any = null;
  82. /**
  83. * List of inspectable custom properties (used by the Inspector)
  84. * @see https://doc.babylonjs.com/how_to/debug_layer#extensibility
  85. */
  86. public inspectableCustomProperties: IInspectable[];
  87. private _doNotSerialize = false;
  88. /**
  89. * Gets or sets a boolean used to define if the node must be serialized
  90. */
  91. public get doNotSerialize() {
  92. if (this._doNotSerialize) {
  93. return true;
  94. }
  95. if (this._parentNode) {
  96. return this._parentNode.doNotSerialize;
  97. }
  98. return false;
  99. }
  100. public set doNotSerialize(value: boolean) {
  101. this._doNotSerialize = value;
  102. }
  103. /** @hidden */
  104. public _isDisposed = false;
  105. /**
  106. * Gets a list of Animations associated with the node
  107. */
  108. public animations = new Array<Animation>();
  109. protected _ranges: { [name: string]: Nullable<AnimationRange> } = {};
  110. /**
  111. * Callback raised when the node is ready to be used
  112. */
  113. public onReady: Nullable<(node: Node) => void> = null;
  114. private _isEnabled = true;
  115. private _isParentEnabled = true;
  116. private _isReady = true;
  117. /** @hidden */
  118. public _currentRenderId = -1;
  119. private _parentUpdateId = -1;
  120. /** @hidden */
  121. public _childUpdateId = -1;
  122. /** @hidden */
  123. public _waitingParentId: Nullable<string> = null;
  124. /** @hidden */
  125. public _scene: Scene;
  126. /** @hidden */
  127. public _cache: any = {};
  128. private _parentNode: Nullable<Node> = null;
  129. private _children: Nullable<Node[]> = null;
  130. /** @hidden */
  131. public _worldMatrix = Matrix.Identity();
  132. /** @hidden */
  133. public _worldMatrixDeterminant = 0;
  134. /** @hidden */
  135. public _worldMatrixDeterminantIsDirty = true;
  136. /** @hidden */
  137. private _sceneRootNodesIndex = -1;
  138. /**
  139. * Gets a boolean indicating if the node has been disposed
  140. * @returns true if the node was disposed
  141. */
  142. public isDisposed(): boolean {
  143. return this._isDisposed;
  144. }
  145. /**
  146. * Gets or sets the parent of the node (without keeping the current position in the scene)
  147. * @see https://doc.babylonjs.com/how_to/parenting
  148. */
  149. public set parent(parent: Nullable<Node>) {
  150. if (this._parentNode === parent) {
  151. return;
  152. }
  153. const previousParentNode = this._parentNode;
  154. // Remove self from list of children of parent
  155. if (this._parentNode && this._parentNode._children !== undefined && this._parentNode._children !== null) {
  156. var index = this._parentNode._children.indexOf(this);
  157. if (index !== -1) {
  158. this._parentNode._children.splice(index, 1);
  159. }
  160. if (!parent && !this._isDisposed) {
  161. this._addToSceneRootNodes();
  162. }
  163. }
  164. // Store new parent
  165. this._parentNode = parent;
  166. // Add as child to new parent
  167. if (this._parentNode) {
  168. if (this._parentNode._children === undefined || this._parentNode._children === null) {
  169. this._parentNode._children = new Array<Node>();
  170. }
  171. this._parentNode._children.push(this);
  172. if (!previousParentNode) {
  173. this._removeFromSceneRootNodes();
  174. }
  175. }
  176. // Enabled state
  177. this._syncParentEnabledState();
  178. }
  179. public get parent(): Nullable<Node> {
  180. return this._parentNode;
  181. }
  182. /** @hidden */
  183. public _addToSceneRootNodes() {
  184. if (this._sceneRootNodesIndex === -1) {
  185. this._sceneRootNodesIndex = this._scene.rootNodes.length;
  186. this._scene.rootNodes.push(this);
  187. }
  188. }
  189. /** @hidden */
  190. public _removeFromSceneRootNodes() {
  191. if (this._sceneRootNodesIndex !== -1) {
  192. const rootNodes = this._scene.rootNodes;
  193. const lastIdx = rootNodes.length - 1;
  194. rootNodes[this._sceneRootNodesIndex] = rootNodes[lastIdx];
  195. rootNodes[this._sceneRootNodesIndex]._sceneRootNodesIndex = this._sceneRootNodesIndex;
  196. this._scene.rootNodes.pop();
  197. this._sceneRootNodesIndex = -1;
  198. }
  199. }
  200. private _animationPropertiesOverride: Nullable<AnimationPropertiesOverride> = null;
  201. /**
  202. * Gets or sets the animation properties override
  203. */
  204. public get animationPropertiesOverride(): Nullable<AnimationPropertiesOverride> {
  205. if (!this._animationPropertiesOverride) {
  206. return this._scene.animationPropertiesOverride;
  207. }
  208. return this._animationPropertiesOverride;
  209. }
  210. public set animationPropertiesOverride(value: Nullable<AnimationPropertiesOverride>) {
  211. this._animationPropertiesOverride = value;
  212. }
  213. /**
  214. * Gets a string idenfifying the name of the class
  215. * @returns "Node" string
  216. */
  217. public getClassName(): string {
  218. return "Node";
  219. }
  220. /** @hidden */
  221. public readonly _isNode = true;
  222. /**
  223. * An event triggered when the mesh is disposed
  224. */
  225. public onDisposeObservable = new Observable<Node>();
  226. private _onDisposeObserver: Nullable<Observer<Node>> = null;
  227. /**
  228. * Sets a callback that will be raised when the node will be disposed
  229. */
  230. public set onDispose(callback: () => void) {
  231. if (this._onDisposeObserver) {
  232. this.onDisposeObservable.remove(this._onDisposeObserver);
  233. }
  234. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  235. }
  236. /**
  237. * Creates a new Node
  238. * @param name the name and id to be given to this node
  239. * @param scene the scene this node will be added to
  240. */
  241. constructor(name: string, scene: Nullable<Scene> = null) {
  242. this.name = name;
  243. this.id = name;
  244. this._scene = <Scene>(scene || EngineStore.LastCreatedScene);
  245. this.uniqueId = this._scene.getUniqueId();
  246. this._initCache();
  247. }
  248. /**
  249. * Gets the scene of the node
  250. * @returns a scene
  251. */
  252. public getScene(): Scene {
  253. return this._scene;
  254. }
  255. /**
  256. * Gets the engine of the node
  257. * @returns a Engine
  258. */
  259. public getEngine(): Engine {
  260. return this._scene.getEngine();
  261. }
  262. // Behaviors
  263. private _behaviors = new Array<Behavior<Node>>();
  264. /**
  265. * Attach a behavior to the node
  266. * @see http://doc.babylonjs.com/features/behaviour
  267. * @param behavior defines the behavior to attach
  268. * @param attachImmediately defines that the behavior must be attached even if the scene is still loading
  269. * @returns the current Node
  270. */
  271. public addBehavior(behavior: Behavior<Node>, attachImmediately = false): Node {
  272. var index = this._behaviors.indexOf(behavior);
  273. if (index !== -1) {
  274. return this;
  275. }
  276. behavior.init();
  277. if (this._scene.isLoading && !attachImmediately) {
  278. // We defer the attach when the scene will be loaded
  279. this._scene.onDataLoadedObservable.addOnce(() => {
  280. behavior.attach(this);
  281. });
  282. } else {
  283. behavior.attach(this);
  284. }
  285. this._behaviors.push(behavior);
  286. return this;
  287. }
  288. /**
  289. * Remove an attached behavior
  290. * @see http://doc.babylonjs.com/features/behaviour
  291. * @param behavior defines the behavior to attach
  292. * @returns the current Node
  293. */
  294. public removeBehavior(behavior: Behavior<Node>): Node {
  295. var index = this._behaviors.indexOf(behavior);
  296. if (index === -1) {
  297. return this;
  298. }
  299. this._behaviors[index].detach();
  300. this._behaviors.splice(index, 1);
  301. return this;
  302. }
  303. /**
  304. * Gets the list of attached behaviors
  305. * @see http://doc.babylonjs.com/features/behaviour
  306. */
  307. public get behaviors(): Behavior<Node>[] {
  308. return this._behaviors;
  309. }
  310. /**
  311. * Gets an attached behavior by name
  312. * @param name defines the name of the behavior to look for
  313. * @see http://doc.babylonjs.com/features/behaviour
  314. * @returns null if behavior was not found else the requested behavior
  315. */
  316. public getBehaviorByName(name: string): Nullable<Behavior<Node>> {
  317. for (var behavior of this._behaviors) {
  318. if (behavior.name === name) {
  319. return behavior;
  320. }
  321. }
  322. return null;
  323. }
  324. /**
  325. * Returns the latest update of the World matrix
  326. * @returns a Matrix
  327. */
  328. public getWorldMatrix(): Matrix {
  329. if (this._currentRenderId !== this._scene.getRenderId()) {
  330. this.computeWorldMatrix();
  331. }
  332. return this._worldMatrix;
  333. }
  334. /** @hidden */
  335. public _getWorldMatrixDeterminant(): number {
  336. if (this._worldMatrixDeterminantIsDirty) {
  337. this._worldMatrixDeterminantIsDirty = false;
  338. this._worldMatrixDeterminant = this._worldMatrix.determinant();
  339. }
  340. return this._worldMatrixDeterminant;
  341. }
  342. /**
  343. * Returns directly the latest state of the mesh World matrix.
  344. * A Matrix is returned.
  345. */
  346. public get worldMatrixFromCache(): Matrix {
  347. return this._worldMatrix;
  348. }
  349. // override it in derived class if you add new variables to the cache
  350. // and call the parent class method
  351. /** @hidden */
  352. public _initCache() {
  353. this._cache = {};
  354. this._cache.parent = undefined;
  355. }
  356. /** @hidden */
  357. public updateCache(force?: boolean): void {
  358. if (!force && this.isSynchronized()) {
  359. return;
  360. }
  361. this._cache.parent = this.parent;
  362. this._updateCache();
  363. }
  364. /** @hidden */
  365. public _getActionManagerForTrigger(trigger?: number, initialCall = true): Nullable<AbstractActionManager> {
  366. if (!this.parent) {
  367. return null;
  368. }
  369. return this.parent._getActionManagerForTrigger(trigger, false);
  370. }
  371. // override it in derived class if you add new variables to the cache
  372. // and call the parent class method if !ignoreParentClass
  373. /** @hidden */
  374. public _updateCache(ignoreParentClass?: boolean): void {
  375. }
  376. // override it in derived class if you add new variables to the cache
  377. /** @hidden */
  378. public _isSynchronized(): boolean {
  379. return true;
  380. }
  381. /** @hidden */
  382. public _markSyncedWithParent() {
  383. if (this._parentNode) {
  384. this._parentUpdateId = this._parentNode._childUpdateId;
  385. }
  386. }
  387. /** @hidden */
  388. public isSynchronizedWithParent(): boolean {
  389. if (!this._parentNode) {
  390. return true;
  391. }
  392. if (this._parentUpdateId !== this._parentNode._childUpdateId) {
  393. return false;
  394. }
  395. return this._parentNode.isSynchronized();
  396. }
  397. /** @hidden */
  398. public isSynchronized(): boolean {
  399. if (this._cache.parent != this._parentNode) {
  400. this._cache.parent = this._parentNode;
  401. return false;
  402. }
  403. if (this._parentNode && !this.isSynchronizedWithParent()) {
  404. return false;
  405. }
  406. return this._isSynchronized();
  407. }
  408. /**
  409. * Is this node ready to be used/rendered
  410. * @param completeCheck defines if a complete check (including materials and lights) has to be done (false by default)
  411. * @return true if the node is ready
  412. */
  413. public isReady(completeCheck = false): boolean {
  414. return this._isReady;
  415. }
  416. /**
  417. * Is this node enabled?
  418. * 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
  419. * @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
  420. * @return whether this node (and its parent) is enabled
  421. */
  422. public isEnabled(checkAncestors: boolean = true): boolean {
  423. if (checkAncestors === false) {
  424. return this._isEnabled;
  425. }
  426. if (!this._isEnabled) {
  427. return false;
  428. }
  429. return this._isParentEnabled;
  430. }
  431. /** @hidden */
  432. protected _syncParentEnabledState() {
  433. this._isParentEnabled = this._parentNode ? this._parentNode.isEnabled() : true;
  434. if (this._children) {
  435. this._children.forEach((c) => {
  436. c._syncParentEnabledState(); // Force children to update accordingly
  437. });
  438. }
  439. }
  440. /**
  441. * Set the enabled state of this node
  442. * @param value defines the new enabled state
  443. */
  444. public setEnabled(value: boolean): void {
  445. this._isEnabled = value;
  446. this._syncParentEnabledState();
  447. }
  448. /**
  449. * Is this node a descendant of the given node?
  450. * The function will iterate up the hierarchy until the ancestor was found or no more parents defined
  451. * @param ancestor defines the parent node to inspect
  452. * @returns a boolean indicating if this node is a descendant of the given node
  453. */
  454. public isDescendantOf(ancestor: Node): boolean {
  455. if (this.parent) {
  456. if (this.parent === ancestor) {
  457. return true;
  458. }
  459. return this.parent.isDescendantOf(ancestor);
  460. }
  461. return false;
  462. }
  463. /** @hidden */
  464. public _getDescendants(results: Node[], directDescendantsOnly: boolean = false, predicate?: (node: Node) => boolean): void {
  465. if (!this._children) {
  466. return;
  467. }
  468. for (var index = 0; index < this._children.length; index++) {
  469. var item = this._children[index];
  470. if (!predicate || predicate(item)) {
  471. results.push(item);
  472. }
  473. if (!directDescendantsOnly) {
  474. item._getDescendants(results, false, predicate);
  475. }
  476. }
  477. }
  478. /**
  479. * Will return all nodes that have this node as ascendant
  480. * @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
  481. * @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
  482. * @return all children nodes of all types
  483. */
  484. public getDescendants(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): Node[] {
  485. var results = new Array<Node>();
  486. this._getDescendants(results, directDescendantsOnly, predicate);
  487. return results;
  488. }
  489. /**
  490. * Get all child-meshes of this node
  491. * @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 (Default: false)
  492. * @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
  493. * @returns an array of AbstractMesh
  494. */
  495. public getChildMeshes(directDescendantsOnly?: boolean, predicate?: (node: Node) => boolean): AbstractMesh[] {
  496. var results: Array<AbstractMesh> = [];
  497. this._getDescendants(results, directDescendantsOnly, (node: Node) => {
  498. return ((!predicate || predicate(node)) && ((<AbstractMesh>node).cullingStrategy !== undefined));
  499. });
  500. return results;
  501. }
  502. /**
  503. * Get all direct children of this node
  504. * @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
  505. * @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 (Default: true)
  506. * @returns an array of Node
  507. */
  508. public getChildren(predicate?: (node: Node) => boolean, directDescendantsOnly = true): Node[] {
  509. return this.getDescendants(directDescendantsOnly, predicate);
  510. }
  511. /** @hidden */
  512. public _setReady(state: boolean): void {
  513. if (state === this._isReady) {
  514. return;
  515. }
  516. if (!state) {
  517. this._isReady = false;
  518. return;
  519. }
  520. if (this.onReady) {
  521. this.onReady(this);
  522. }
  523. this._isReady = true;
  524. }
  525. /**
  526. * Get an animation by name
  527. * @param name defines the name of the animation to look for
  528. * @returns null if not found else the requested animation
  529. */
  530. public getAnimationByName(name: string): Nullable<Animation> {
  531. for (var i = 0; i < this.animations.length; i++) {
  532. var animation = this.animations[i];
  533. if (animation.name === name) {
  534. return animation;
  535. }
  536. }
  537. return null;
  538. }
  539. /**
  540. * Creates an animation range for this node
  541. * @param name defines the name of the range
  542. * @param from defines the starting key
  543. * @param to defines the end key
  544. */
  545. public createAnimationRange(name: string, from: number, to: number): void {
  546. // check name not already in use
  547. if (!this._ranges[name]) {
  548. this._ranges[name] = Node._AnimationRangeFactory(name, from, to);
  549. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  550. if (this.animations[i]) {
  551. this.animations[i].createRange(name, from, to);
  552. }
  553. }
  554. }
  555. }
  556. /**
  557. * Delete a specific animation range
  558. * @param name defines the name of the range to delete
  559. * @param deleteFrames defines if animation frames from the range must be deleted as well
  560. */
  561. public deleteAnimationRange(name: string, deleteFrames = true): void {
  562. for (var i = 0, nAnimations = this.animations.length; i < nAnimations; i++) {
  563. if (this.animations[i]) {
  564. this.animations[i].deleteRange(name, deleteFrames);
  565. }
  566. }
  567. this._ranges[name] = null; // said much faster than 'delete this._range[name]'
  568. }
  569. /**
  570. * Get an animation range by name
  571. * @param name defines the name of the animation range to look for
  572. * @returns null if not found else the requested animation range
  573. */
  574. public getAnimationRange(name: string): Nullable<AnimationRange> {
  575. return this._ranges[name];
  576. }
  577. /**
  578. * Gets the list of all animation ranges defined on this node
  579. * @returns an array
  580. */
  581. public getAnimationRanges(): Nullable<AnimationRange>[] {
  582. var animationRanges: Nullable<AnimationRange>[] = [];
  583. var name: string;
  584. for (name in this._ranges) {
  585. animationRanges.push(this._ranges[name]);
  586. }
  587. return animationRanges;
  588. }
  589. /**
  590. * Will start the animation sequence
  591. * @param name defines the range frames for animation sequence
  592. * @param loop defines if the animation should loop (false by default)
  593. * @param speedRatio defines the speed factor in which to run the animation (1 by default)
  594. * @param onAnimationEnd defines a function to be executed when the animation ended (undefined by default)
  595. * @returns the object created for this animation. If range does not exist, it will return null
  596. */
  597. public beginAnimation(name: string, loop?: boolean, speedRatio?: number, onAnimationEnd?: () => void): Nullable<Animatable> {
  598. var range = this.getAnimationRange(name);
  599. if (!range) {
  600. return null;
  601. }
  602. return this._scene.beginAnimation(this, range.from, range.to, loop, speedRatio, onAnimationEnd);
  603. }
  604. /**
  605. * Serialize animation ranges into a JSON compatible object
  606. * @returns serialization object
  607. */
  608. public serializeAnimationRanges(): any {
  609. var serializationRanges = [];
  610. for (var name in this._ranges) {
  611. var localRange = this._ranges[name];
  612. if (!localRange) {
  613. continue;
  614. }
  615. var range: any = {};
  616. range.name = name;
  617. range.from = localRange.from;
  618. range.to = localRange.to;
  619. serializationRanges.push(range);
  620. }
  621. return serializationRanges;
  622. }
  623. /**
  624. * Computes the world matrix of the node
  625. * @param force defines if the cache version should be invalidated forcing the world matrix to be created from scratch
  626. * @returns the world matrix
  627. */
  628. public computeWorldMatrix(force?: boolean): Matrix {
  629. if (!this._worldMatrix) {
  630. this._worldMatrix = Matrix.Identity();
  631. }
  632. return this._worldMatrix;
  633. }
  634. /**
  635. * Releases resources associated with this node.
  636. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  637. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  638. */
  639. public dispose(doNotRecurse?: boolean, disposeMaterialAndTextures = false): void {
  640. this._isDisposed = true;
  641. if (!doNotRecurse) {
  642. const nodes = this.getDescendants(true);
  643. for (const node of nodes) {
  644. node.dispose(doNotRecurse, disposeMaterialAndTextures);
  645. }
  646. }
  647. if (!this.parent) {
  648. this._removeFromSceneRootNodes();
  649. } else {
  650. this.parent = null;
  651. }
  652. // Callback
  653. this.onDisposeObservable.notifyObservers(this);
  654. this.onDisposeObservable.clear();
  655. // Behaviors
  656. for (var behavior of this._behaviors) {
  657. behavior.detach();
  658. }
  659. this._behaviors = [];
  660. }
  661. /**
  662. * Parse animation range data from a serialization object and store them into a given node
  663. * @param node defines where to store the animation ranges
  664. * @param parsedNode defines the serialization object to read data from
  665. * @param scene defines the hosting scene
  666. */
  667. public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
  668. if (parsedNode.ranges) {
  669. for (var index = 0; index < parsedNode.ranges.length; index++) {
  670. var data = parsedNode.ranges[index];
  671. node.createAnimationRange(data.name, data.from, data.to);
  672. }
  673. }
  674. }
  675. /**
  676. * Return the minimum and maximum world vectors of the entire hierarchy under current node
  677. * @param includeDescendants Include bounding info from descendants as well (true by default)
  678. * @param predicate defines a callback function that can be customize to filter what meshes should be included in the list used to compute the bounding vectors
  679. * @returns the new bounding vectors
  680. */
  681. public getHierarchyBoundingVectors(includeDescendants = true, predicate: Nullable<(abstractMesh: AbstractMesh) => boolean> = null): { min: Vector3, max: Vector3 } {
  682. // Ensures that all world matrix will be recomputed.
  683. this.getScene().incrementRenderId();
  684. this.computeWorldMatrix(true);
  685. let min: Vector3;
  686. let max: Vector3;
  687. let thisAbstractMesh = (this as Node as AbstractMesh);
  688. if (thisAbstractMesh.getBoundingInfo && thisAbstractMesh.subMeshes) {
  689. // If this is an abstract mesh get its bounding info
  690. let boundingInfo = thisAbstractMesh.getBoundingInfo();
  691. min = boundingInfo.boundingBox.minimumWorld.clone();
  692. max = boundingInfo.boundingBox.maximumWorld.clone();
  693. } else {
  694. min = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  695. max = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  696. }
  697. if (includeDescendants) {
  698. let descendants = this.getDescendants(false);
  699. for (var descendant of descendants) {
  700. let childMesh = <AbstractMesh>descendant;
  701. childMesh.computeWorldMatrix(true);
  702. // Filters meshes based on custom predicate function.
  703. if (predicate && !predicate(childMesh)) {
  704. continue;
  705. }
  706. //make sure we have the needed params to get mix and max
  707. if (!childMesh.getBoundingInfo || childMesh.getTotalVertices() === 0) {
  708. continue;
  709. }
  710. let childBoundingInfo = childMesh.getBoundingInfo();
  711. let boundingBox = childBoundingInfo.boundingBox;
  712. var minBox = boundingBox.minimumWorld;
  713. var maxBox = boundingBox.maximumWorld;
  714. Vector3.CheckExtends(minBox, min, max);
  715. Vector3.CheckExtends(maxBox, min, max);
  716. }
  717. }
  718. return {
  719. min: min,
  720. max: max
  721. };
  722. }
  723. }