babylon.node.ts 15 KB

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