babylon.node.ts 9.4 KB

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