babylon.node.ts 13 KB

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