babylon.node.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.attach(this);
  91. this._behaviors.push(behavior);
  92. return this;
  93. }
  94. public removeBehavior(behavior: Behavior<Node>): Node {
  95. var index = this._behaviors.indexOf(behavior);
  96. if (index === -1) {
  97. return this;
  98. }
  99. this._behaviors[index].detach();
  100. this._behaviors.splice(index, 1);
  101. return this;
  102. }
  103. public get behaviors(): Behavior<Node>[] {
  104. return this._behaviors;
  105. }
  106. public getBehaviorByName(name: string): Nullable<Behavior<Node>> {
  107. for (var behavior of this._behaviors) {
  108. if (behavior.name === name) {
  109. return behavior;
  110. }
  111. }
  112. return null;
  113. }
  114. // override it in derived class
  115. public getWorldMatrix(): Matrix {
  116. return Matrix.Identity();
  117. }
  118. // override it in derived class if you add new variables to the cache
  119. // and call the parent class method
  120. public _initCache() {
  121. this._cache = {};
  122. this._cache.parent = undefined;
  123. }
  124. public updateCache(force?: boolean): void {
  125. if (!force && this.isSynchronized())
  126. return;
  127. this._cache.parent = this.parent;
  128. this._updateCache();
  129. }
  130. // override it in derived class if you add new variables to the cache
  131. // and call the parent class method if !ignoreParentClass
  132. public _updateCache(ignoreParentClass?: boolean): void {
  133. }
  134. // override it in derived class if you add new variables to the cache
  135. public _isSynchronized(): boolean {
  136. return true;
  137. }
  138. public _markSyncedWithParent() {
  139. if (this.parent) {
  140. this._parentRenderId = this.parent._currentRenderId;
  141. }
  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 = new Array<Node>();
  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): Nullable<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] = null; // said much faster than 'delete this._range[name]'
  299. }
  300. public getAnimationRange(name: string): Nullable<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;
  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 localRange = this._ranges[name];
  314. if (!localRange) {
  315. continue;
  316. }
  317. var range: any = {};
  318. range.name = name;
  319. range.from = localRange.from;
  320. range.to = localRange.to;
  321. serializationRanges.push(range);
  322. }
  323. return serializationRanges;
  324. }
  325. public dispose(): void {
  326. this.parent = null;
  327. // Callback
  328. this.onDisposeObservable.notifyObservers(this);
  329. this.onDisposeObservable.clear();
  330. // Behaviors
  331. for (var behavior of this._behaviors) {
  332. behavior.detach();
  333. }
  334. this._behaviors = [];
  335. }
  336. public static ParseAnimationRanges(node: Node, parsedNode: any, scene: Scene): void {
  337. if (parsedNode.ranges) {
  338. for (var index = 0; index < parsedNode.ranges.length; index++) {
  339. var data = parsedNode.ranges[index];
  340. node.createAnimationRange(data.name, data.from, data.to);
  341. }
  342. }
  343. }
  344. }
  345. }