babylon.sceneComponent.ts 8.1 KB

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