babylon.sceneComponent.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 STEP_ISREADYFORMESH_EFFECTLAYER = 0;
  13. public static readonly STEP_BEFOREEVALUATEACTIVEMESH_BOUNDINGBOXRENDERER = 0;
  14. public static readonly STEP_EVALUATESUBMESH_BOUNDINGBOXRENDERER = 0;
  15. public static readonly STEP_ACTIVEMESH_BOUNDINGBOXRENDERER = 0;
  16. public static readonly STEP_CAMERADRAWRENDERTARGET_EFFECTLAYER = 1;
  17. public static readonly STEP_BEFORECAMERADRAW_EFFECTLAYER = 0;
  18. public static readonly STEP_BEFORECAMERADRAW_LAYER = 1;
  19. public static readonly STEP_AFTERRENDERINGGROUPDRAW_EFFECTLAYER_DRAW = 0;
  20. public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER = 0;
  21. public static readonly STEP_AFTERCAMERADRAW_LENSFLARESYSTEM = 1;
  22. public static readonly STEP_AFTERCAMERADRAW_BOUNDINGBOXRENDERER = 2;
  23. public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW = 3;
  24. public static readonly STEP_AFTERCAMERADRAW_LAYER = 4;
  25. }
  26. /**
  27. * This represents a scene component.
  28. *
  29. * This is used to decouple the dependency the scene is having on the different workloads like
  30. * layers, post processes...
  31. */
  32. export interface ISceneComponent {
  33. /**
  34. * The name of the component. Each component must have a unique name.
  35. */
  36. name: string;
  37. /**
  38. * The scene the component belongs to.
  39. */
  40. scene: Scene;
  41. /**
  42. * Register the component to one instance of a scene.
  43. */
  44. register(): void;
  45. /**
  46. * Rebuilds the elements related to this component in case of
  47. * context lost for instance.
  48. */
  49. rebuild(): void;
  50. /**
  51. * Disposes the component and the associated ressources.
  52. */
  53. dispose(): void;
  54. }
  55. /**
  56. * This represents a SERIALIZABLE scene component.
  57. *
  58. * This extends Scene Component to add Serialization methods on top.
  59. */
  60. export interface ISceneSerializableComponent extends ISceneComponent {
  61. /**
  62. * Adds all the element from the container to the scene
  63. * @param container the container holding the elements
  64. */
  65. addFromContainer(container: AbstractScene): void;
  66. /**
  67. * Removes all the elements in the container from the scene
  68. * @param container contains the elements to remove
  69. */
  70. removeFromContainer(container: AbstractScene): void;
  71. /**
  72. * Serializes the component data to the specified json object
  73. * @param serializationObject The object to serialize to
  74. */
  75. serialize(serializationObject: any): void;
  76. }
  77. /**
  78. * Strong typing of a Mesh related stage step action
  79. */
  80. export type MeshStageAction = (mesh: AbstractMesh, hardwareInstancedRendering: boolean) => boolean;
  81. /**
  82. * Strong typing of a Evaluate Sub Mesh related stage step action
  83. */
  84. export type EvaluateSubMeshStageAction = (mesh: AbstractMesh, subMesh: SubMesh) => void;
  85. /**
  86. * Strong typing of a Active Mesh related stage step action
  87. */
  88. export type ActiveMeshStageAction = (sourceMesh: AbstractMesh, mesh: AbstractMesh) => void;
  89. /**
  90. * Strong typing of a Camera related stage step action
  91. */
  92. export type CameraStageAction = (camera: Camera) => void;
  93. /**
  94. * Strong typing of a RenderingGroup related stage step action
  95. */
  96. export type RenderingGroupStageAction = (renderingGroupId: number) => void;
  97. /**
  98. * Strong typing of a simple stage step action
  99. */
  100. export type SimpleStageAction = () => void;
  101. /**
  102. * Repressentation of a stage in the scene (Basically a list of ordered steps)
  103. * @hidden
  104. */
  105. export class Stage<T extends Function> extends Array<{ index: number, component: ISceneComponent, action: T }> {
  106. /**
  107. * Hide ctor from the rest of the world.
  108. * @param items The items to add.
  109. */
  110. private constructor(items?: { index: number, component: ISceneComponent, action: T }[]) {
  111. super(...<any>items)
  112. }
  113. /**
  114. * Creates a new Stage.
  115. * @returns A new instance of a Stage
  116. */
  117. static Create<T extends Function>(): Stage<T> {
  118. return Object.create(Stage.prototype);
  119. }
  120. /**
  121. * Registers a step in an ordered way in the targeted stage.
  122. * @param index Defines the position to register the step in
  123. * @param component Defines the component attached to the step
  124. * @param action Defines the action to launch during the step
  125. */
  126. public registerStep(index: number, component: ISceneComponent, action: T): void {
  127. let i = 0;
  128. let maxIndex = Number.MAX_VALUE;
  129. for (; i < this.length && i < maxIndex; i++) {
  130. let step = this[i];
  131. maxIndex = step.index;
  132. }
  133. this.splice(i, 0, { index, component, action: action.bind(component) });
  134. }
  135. /**
  136. * Clears all the steps from the stage.
  137. */
  138. public clear(): void {
  139. this.length = 0;
  140. }
  141. }
  142. }