babylon.node.ts 12 KB

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