babylon.node.ts 13 KB

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