sceneComponent.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import { Scene } from "./scene";
  2. import { AbstractMesh } from "./Meshes/abstractMesh";
  3. import { SubMesh, } from "./Meshes/subMesh";
  4. import { _InstancesBatch } from "./Meshes/mesh";
  5. import { SmartArrayNoDuplicate } from "./Misc/smartArray";
  6. import { Nullable } from "./types";
  7. import { Camera } from "./Cameras/camera";
  8. import { RenderTargetTexture } from "./Materials/Textures/renderTargetTexture";
  9. import { PickingInfo } from "./Collisions/pickingInfo";
  10. import { AbstractScene } from "./abstractScene";
  11. import { IPointerEvent } from "./Events/deviceInputEvents";
  12. declare type Mesh = import("./Meshes/mesh").Mesh;
  13. declare type Effect = import("./Materials/effect").Effect;
  14. /**
  15. * Groups all the scene component constants in one place to ease maintenance.
  16. * @hidden
  17. */
  18. export class SceneComponentConstants {
  19. public static readonly NAME_EFFECTLAYER = "EffectLayer";
  20. public static readonly NAME_LAYER = "Layer";
  21. public static readonly NAME_LENSFLARESYSTEM = "LensFlareSystem";
  22. public static readonly NAME_BOUNDINGBOXRENDERER = "BoundingBoxRenderer";
  23. public static readonly NAME_PARTICLESYSTEM = "ParticleSystem";
  24. public static readonly NAME_GAMEPAD = "Gamepad";
  25. public static readonly NAME_SIMPLIFICATIONQUEUE = "SimplificationQueue";
  26. public static readonly NAME_GEOMETRYBUFFERRENDERER = "GeometryBufferRenderer";
  27. public static readonly NAME_PREPASSRENDERER = "PrePassRenderer";
  28. public static readonly NAME_DEPTHRENDERER = "DepthRenderer";
  29. public static readonly NAME_POSTPROCESSRENDERPIPELINEMANAGER = "PostProcessRenderPipelineManager";
  30. public static readonly NAME_SPRITE = "Sprite";
  31. public static readonly NAME_SUBSURFACE = "SubSurface";
  32. public static readonly NAME_OUTLINERENDERER = "Outline";
  33. public static readonly NAME_PROCEDURALTEXTURE = "ProceduralTexture";
  34. public static readonly NAME_SHADOWGENERATOR = "ShadowGenerator";
  35. public static readonly NAME_OCTREE = "Octree";
  36. public static readonly NAME_PHYSICSENGINE = "PhysicsEngine";
  37. public static readonly NAME_AUDIO = "Audio";
  38. public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
  39. public static readonly STEP_BEFOREEVALUATEACTIVEMESH_BOUNDINGBOXRENDERER = 0;
  40. public static readonly STEP_EVALUATESUBMESH_BOUNDINGBOXRENDERER = 0;
  41. public static readonly STEP_PREACTIVEMESH_BOUNDINGBOXRENDERER = 0;
  42. public static readonly STEP_CAMERADRAWRENDERTARGET_EFFECTLAYER = 1;
  43. public static readonly STEP_BEFORECAMERADRAW_PREPASS = 0;
  44. public static readonly STEP_BEFORECAMERADRAW_EFFECTLAYER = 1;
  45. public static readonly STEP_BEFORECAMERADRAW_LAYER = 2;
  46. public static readonly STEP_BEFORERENDERTARGETDRAW_PREPASS = 0;
  47. public static readonly STEP_BEFORERENDERTARGETDRAW_LAYER = 1;
  48. public static readonly STEP_BEFORERENDERINGMESH_PREPASS = 0;
  49. public static readonly STEP_BEFORERENDERINGMESH_OUTLINE = 1;
  50. public static readonly STEP_AFTERRENDERINGMESH_PREPASS = 0;
  51. public static readonly STEP_AFTERRENDERINGMESH_OUTLINE = 1;
  52. public static readonly STEP_AFTERRENDERINGGROUPDRAW_EFFECTLAYER_DRAW = 0;
  53. public static readonly STEP_AFTERRENDERINGGROUPDRAW_BOUNDINGBOXRENDERER = 1;
  54. public static readonly STEP_BEFORECAMERAUPDATE_SIMPLIFICATIONQUEUE = 0;
  55. public static readonly STEP_BEFORECAMERAUPDATE_GAMEPAD = 1;
  56. public static readonly STEP_BEFORECLEAR_PROCEDURALTEXTURE = 0;
  57. public static readonly STEP_AFTERRENDERTARGETDRAW_PREPASS = 0;
  58. public static readonly STEP_AFTERRENDERTARGETDRAW_LAYER = 1;
  59. public static readonly STEP_AFTERCAMERADRAW_PREPASS = 0;
  60. public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER = 1;
  61. public static readonly STEP_AFTERCAMERADRAW_LENSFLARESYSTEM = 2;
  62. public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW = 3;
  63. public static readonly STEP_AFTERCAMERADRAW_LAYER = 4;
  64. public static readonly STEP_AFTERRENDER_AUDIO = 0;
  65. public static readonly STEP_GATHERRENDERTARGETS_DEPTHRENDERER = 0;
  66. public static readonly STEP_GATHERRENDERTARGETS_GEOMETRYBUFFERRENDERER = 1;
  67. public static readonly STEP_GATHERRENDERTARGETS_SHADOWGENERATOR = 2;
  68. public static readonly STEP_GATHERRENDERTARGETS_POSTPROCESSRENDERPIPELINEMANAGER = 3;
  69. public static readonly STEP_GATHERACTIVECAMERARENDERTARGETS_DEPTHRENDERER = 0;
  70. public static readonly STEP_BEFORECLEARSTAGE_PREPASS = 0;
  71. public static readonly STEP_BEFORERENDERTARGETCLEARSTAGE_PREPASS = 0;
  72. public static readonly STEP_POINTERMOVE_SPRITE = 0;
  73. public static readonly STEP_POINTERDOWN_SPRITE = 0;
  74. public static readonly STEP_POINTERUP_SPRITE = 0;
  75. }
  76. /**
  77. * This represents a scene component.
  78. *
  79. * This is used to decouple the dependency the scene is having on the different workloads like
  80. * layers, post processes...
  81. */
  82. export interface ISceneComponent {
  83. /**
  84. * The name of the component. Each component must have a unique name.
  85. */
  86. name: string;
  87. /**
  88. * The scene the component belongs to.
  89. */
  90. scene: Scene;
  91. /**
  92. * Register the component to one instance of a scene.
  93. */
  94. register(): void;
  95. /**
  96. * Rebuilds the elements related to this component in case of
  97. * context lost for instance.
  98. */
  99. rebuild(): void;
  100. /**
  101. * Disposes the component and the associated ressources.
  102. */
  103. dispose(): void;
  104. }
  105. /**
  106. * This represents a SERIALIZABLE scene component.
  107. *
  108. * This extends Scene Component to add Serialization methods on top.
  109. */
  110. export interface ISceneSerializableComponent extends ISceneComponent {
  111. /**
  112. * Adds all the elements from the container to the scene
  113. * @param container the container holding the elements
  114. */
  115. addFromContainer(container: AbstractScene): void;
  116. /**
  117. * Removes all the elements in the container from the scene
  118. * @param container contains the elements to remove
  119. * @param dispose if the removed element should be disposed (default: false)
  120. */
  121. removeFromContainer(container: AbstractScene, dispose?: boolean): void;
  122. /**
  123. * Serializes the component data to the specified json object
  124. * @param serializationObject The object to serialize to
  125. */
  126. serialize(serializationObject: any): void;
  127. }
  128. /**
  129. * Strong typing of a Mesh related stage step action
  130. */
  131. export type MeshStageAction = (mesh: AbstractMesh, hardwareInstancedRendering: boolean) => boolean;
  132. /**
  133. * Strong typing of a Evaluate Sub Mesh related stage step action
  134. */
  135. export type EvaluateSubMeshStageAction = (mesh: AbstractMesh, subMesh: SubMesh) => void;
  136. /**
  137. * Strong typing of a pre active Mesh related stage step action
  138. */
  139. export type PreActiveMeshStageAction = (mesh: AbstractMesh) => void;
  140. /**
  141. * Strong typing of a Camera related stage step action
  142. */
  143. export type CameraStageAction = (camera: Camera) => void;
  144. /**
  145. * Strong typing of a Camera Frame buffer related stage step action
  146. */
  147. export type CameraStageFrameBufferAction = (camera: Camera) => boolean;
  148. /**
  149. * Strong typing of a Render Target related stage step action
  150. */
  151. export type RenderTargetStageAction = (renderTarget: RenderTargetTexture, faceIndex?: number, layer?: number) => void;
  152. /**
  153. * Strong typing of a RenderingGroup related stage step action
  154. */
  155. export type RenderingGroupStageAction = (renderingGroupId: number) => void;
  156. /**
  157. * Strong typing of a Mesh Render related stage step action
  158. */
  159. export type RenderingMeshStageAction = (mesh: Mesh, subMesh: SubMesh, batch: _InstancesBatch, effect: Nullable<Effect>) => void;
  160. /**
  161. * Strong typing of a simple stage step action
  162. */
  163. export type SimpleStageAction = () => void;
  164. /**
  165. * Strong typing of a render target action.
  166. */
  167. export type RenderTargetsStageAction = (renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>) => void;
  168. /**
  169. * Strong typing of a pointer move action.
  170. */
  171. export type PointerMoveStageAction = (unTranslatedPointerX: number, unTranslatedPointerY: number, pickResult: Nullable<PickingInfo>, isMeshPicked: boolean, element: HTMLElement) => Nullable<PickingInfo>;
  172. /**
  173. * Strong typing of a pointer up/down action.
  174. */
  175. export type PointerUpDownStageAction = (unTranslatedPointerX: number, unTranslatedPointerY: number, pickResult: Nullable<PickingInfo>, evt: IPointerEvent) => Nullable<PickingInfo>;
  176. /**
  177. * Representation of a stage in the scene (Basically a list of ordered steps)
  178. * @hidden
  179. */
  180. export class Stage<T extends Function> extends Array<{ index: number, component: ISceneComponent, action: T }> {
  181. /**
  182. * Hide ctor from the rest of the world.
  183. * @param items The items to add.
  184. */
  185. private constructor(items?: { index: number, component: ISceneComponent, action: T }[]) {
  186. super(...<any>items);
  187. }
  188. /**
  189. * Creates a new Stage.
  190. * @returns A new instance of a Stage
  191. */
  192. static Create<T extends Function>(): Stage<T> {
  193. return Object.create(Stage.prototype);
  194. }
  195. /**
  196. * Registers a step in an ordered way in the targeted stage.
  197. * @param index Defines the position to register the step in
  198. * @param component Defines the component attached to the step
  199. * @param action Defines the action to launch during the step
  200. */
  201. public registerStep(index: number, component: ISceneComponent, action: T): void {
  202. let i = 0;
  203. let maxIndex = Number.MAX_VALUE;
  204. for (; i < this.length; i++) {
  205. let step = this[i];
  206. maxIndex = step.index;
  207. if (index < maxIndex) {
  208. break;
  209. }
  210. }
  211. this.splice(i, 0, { index, component, action: action.bind(component) });
  212. }
  213. /**
  214. * Clears all the steps from the stage.
  215. */
  216. public clear(): void {
  217. this.length = 0;
  218. }
  219. }