abstractScene.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { Scene } from "./scene";
  2. import { Nullable } from "./types";
  3. import { AbstractMesh } from "./Meshes/abstractMesh";
  4. import { TransformNode } from "./Meshes/transformNode";
  5. import { Geometry } from "./Meshes/geometry";
  6. import { Skeleton } from "./Bones/skeleton";
  7. import { MorphTargetManager } from "./Morph/morphTargetManager";
  8. import { AssetContainer } from "./assetContainer";
  9. import { IParticleSystem } from "./Particles/IParticleSystem";
  10. import { AnimationGroup } from "./Animations/animationGroup";
  11. import { BaseTexture } from "./Materials/Textures/baseTexture";
  12. import { Material } from "./Materials/material";
  13. import { MultiMaterial } from "./Materials/multiMaterial";
  14. import { AbstractActionManager } from "./Actions/abstractActionManager";
  15. import { Camera } from "./Cameras/camera";
  16. import { Light } from "./Lights/light";
  17. import { Node } from "./node";
  18. declare type Animation = import("./Animations/animation").Animation;
  19. /**
  20. * Defines how the parser contract is defined.
  21. * These parsers are used to parse a list of specific assets (like particle systems, etc..)
  22. */
  23. export type BabylonFileParser = (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => void;
  24. /**
  25. * Defines how the individual parser contract is defined.
  26. * These parser can parse an individual asset
  27. */
  28. export type IndividualBabylonFileParser = (parsedData: any, scene: Scene, rootUrl: string) => any;
  29. /**
  30. * Base class of the scene acting as a container for the different elements composing a scene.
  31. * This class is dynamically extended by the different components of the scene increasing
  32. * flexibility and reducing coupling
  33. */
  34. export abstract class AbstractScene {
  35. /**
  36. * Stores the list of available parsers in the application.
  37. */
  38. private static _BabylonFileParsers: { [key: string]: BabylonFileParser } = {};
  39. /**
  40. * Stores the list of available individual parsers in the application.
  41. */
  42. private static _IndividualBabylonFileParsers: { [key: string]: IndividualBabylonFileParser } = {};
  43. /**
  44. * Adds a parser in the list of available ones
  45. * @param name Defines the name of the parser
  46. * @param parser Defines the parser to add
  47. */
  48. public static AddParser(name: string, parser: BabylonFileParser): void {
  49. this._BabylonFileParsers[name] = parser;
  50. }
  51. /**
  52. * Gets a general parser from the list of avaialble ones
  53. * @param name Defines the name of the parser
  54. * @returns the requested parser or null
  55. */
  56. public static GetParser(name: string): Nullable<BabylonFileParser> {
  57. if (this._BabylonFileParsers[name]) {
  58. return this._BabylonFileParsers[name];
  59. }
  60. return null;
  61. }
  62. /**
  63. * Adds n individual parser in the list of available ones
  64. * @param name Defines the name of the parser
  65. * @param parser Defines the parser to add
  66. */
  67. public static AddIndividualParser(name: string, parser: IndividualBabylonFileParser): void {
  68. this._IndividualBabylonFileParsers[name] = parser;
  69. }
  70. /**
  71. * Gets an individual parser from the list of avaialble ones
  72. * @param name Defines the name of the parser
  73. * @returns the requested parser or null
  74. */
  75. public static GetIndividualParser(name: string): Nullable<IndividualBabylonFileParser> {
  76. if (this._IndividualBabylonFileParsers[name]) {
  77. return this._IndividualBabylonFileParsers[name];
  78. }
  79. return null;
  80. }
  81. /**
  82. * Parser json data and populate both a scene and its associated container object
  83. * @param jsonData Defines the data to parse
  84. * @param scene Defines the scene to parse the data for
  85. * @param container Defines the container attached to the parsing sequence
  86. * @param rootUrl Defines the root url of the data
  87. */
  88. public static Parse(jsonData: any, scene: Scene, container: AssetContainer, rootUrl: string): void {
  89. for (let parserName in this._BabylonFileParsers) {
  90. if (this._BabylonFileParsers.hasOwnProperty(parserName)) {
  91. this._BabylonFileParsers[parserName](jsonData, scene, container, rootUrl);
  92. }
  93. }
  94. }
  95. /**
  96. * Gets the list of root nodes (ie. nodes with no parent)
  97. */
  98. public rootNodes = new Array<Node>();
  99. /** All of the cameras added to this scene
  100. * @see http://doc.babylonjs.com/babylon101/cameras
  101. */
  102. public cameras = new Array<Camera>();
  103. /**
  104. * All of the lights added to this scene
  105. * @see http://doc.babylonjs.com/babylon101/lights
  106. */
  107. public lights = new Array<Light>();
  108. /**
  109. * All of the (abstract) meshes added to this scene
  110. */
  111. public meshes = new Array<AbstractMesh>();
  112. /**
  113. * The list of skeletons added to the scene
  114. * @see http://doc.babylonjs.com/how_to/how_to_use_bones_and_skeletons
  115. */
  116. public skeletons = new Array<Skeleton>();
  117. /**
  118. * All of the particle systems added to this scene
  119. * @see http://doc.babylonjs.com/babylon101/particles
  120. */
  121. public particleSystems = new Array<IParticleSystem>();
  122. /**
  123. * Gets a list of Animations associated with the scene
  124. */
  125. public animations: Animation[] = [];
  126. /**
  127. * All of the animation groups added to this scene
  128. * @see http://doc.babylonjs.com/how_to/group
  129. */
  130. public animationGroups = new Array<AnimationGroup>();
  131. /**
  132. * All of the multi-materials added to this scene
  133. * @see http://doc.babylonjs.com/how_to/multi_materials
  134. */
  135. public multiMaterials = new Array<MultiMaterial>();
  136. /**
  137. * All of the materials added to this scene
  138. * In the context of a Scene, it is not supposed to be modified manually.
  139. * Any addition or removal should be done using the addMaterial and removeMaterial Scene methods.
  140. * Note also that the order of the Material within the array is not significant and might change.
  141. * @see http://doc.babylonjs.com/babylon101/materials
  142. */
  143. public materials = new Array<Material>();
  144. /**
  145. * The list of morph target managers added to the scene
  146. * @see http://doc.babylonjs.com/how_to/how_to_dynamically_morph_a_mesh
  147. */
  148. public morphTargetManagers = new Array<MorphTargetManager>();
  149. /**
  150. * The list of geometries used in the scene.
  151. */
  152. public geometries = new Array<Geometry>();
  153. /**
  154. * All of the tranform nodes added to this scene
  155. * In the context of a Scene, it is not supposed to be modified manually.
  156. * Any addition or removal should be done using the addTransformNode and removeTransformNode Scene methods.
  157. * Note also that the order of the TransformNode wihin the array is not significant and might change.
  158. * @see http://doc.babylonjs.com/how_to/transformnode
  159. */
  160. public transformNodes = new Array<TransformNode>();
  161. /**
  162. * ActionManagers available on the scene.
  163. */
  164. public actionManagers = new Array<AbstractActionManager>();
  165. /**
  166. * Textures to keep.
  167. */
  168. public textures = new Array<BaseTexture>();
  169. /**
  170. * Environment texture for the scene
  171. */
  172. public environmentTexture: Nullable<BaseTexture> = null;
  173. /**
  174. * @returns all meshes, lights, cameras, transformNodes and bones
  175. */
  176. public getNodes(): Array<Node> {
  177. let nodes = new Array<Node>();
  178. nodes = nodes.concat(this.meshes);
  179. nodes = nodes.concat(this.lights);
  180. nodes = nodes.concat(this.cameras);
  181. nodes = nodes.concat(this.transformNodes); // dummies
  182. this.skeletons.forEach((skeleton) => nodes = nodes.concat(skeleton.bones));
  183. return nodes;
  184. }
  185. }