babylon.node.ts 8.6 KB

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