abstractScene.ts 8.3 KB

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