sceneComponent.ts 8.6 KB

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