effectLayerSceneComponent.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { Camera } from "Cameras";
  2. import { Scene } from "scene";
  3. import { Engine } from "Engine";
  4. import { AbstractMesh } from "Mesh";
  5. import { RenderTargetTexture } from "Materials";
  6. import { SceneComponentConstants, ISceneSerializableComponent } from "sceneComponent";
  7. import { _TimeToken } from "Instrumentation";
  8. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  9. import { EffectLayer } from "Layer";
  10. import { AbstractScene } from "abstractScene";
  11. import { AssetContainer } from "assetContainer";
  12. // Adds the parser to the scene parsers.
  13. AbstractScene.AddParser(SceneComponentConstants.NAME_EFFECTLAYER, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
  14. if (parsedData.effectLayers) {
  15. if (!container.effectLayers) {
  16. container.effectLayers = new Array<EffectLayer>();
  17. }
  18. for (let index = 0; index < parsedData.effectLayers.length; index++) {
  19. var effectLayer = EffectLayer.Parse(parsedData.effectLayers[index], scene, rootUrl);
  20. container.effectLayers.push(effectLayer);
  21. }
  22. }
  23. });
  24. declare module "abstractScene" {
  25. export interface AbstractScene {
  26. /**
  27. * The list of effect layers (highlights/glow) added to the scene
  28. * @see http://doc.babylonjs.com/how_to/highlight_layer
  29. * @see http://doc.babylonjs.com/how_to/glow_layer
  30. */
  31. effectLayers: Array<EffectLayer>;
  32. /**
  33. * Removes the given effect layer from this scene.
  34. * @param toRemove defines the effect layer to remove
  35. * @returns the index of the removed effect layer
  36. */
  37. removeEffectLayer(toRemove: EffectLayer): number;
  38. /**
  39. * Adds the given effect layer to this scene
  40. * @param newEffectLayer defines the effect layer to add
  41. */
  42. addEffectLayer(newEffectLayer: EffectLayer): void;
  43. }
  44. }
  45. AbstractScene.prototype.removeEffectLayer = function(toRemove: EffectLayer): number {
  46. var index = this.effectLayers.indexOf(toRemove);
  47. if (index !== -1) {
  48. this.effectLayers.splice(index, 1);
  49. }
  50. return index;
  51. };
  52. AbstractScene.prototype.addEffectLayer = function(newEffectLayer: EffectLayer): void {
  53. this.effectLayers.push(newEffectLayer);
  54. };
  55. /**
  56. * Defines the layer scene component responsible to manage any effect layers
  57. * in a given scene.
  58. */
  59. export class EffectLayerSceneComponent implements ISceneSerializableComponent {
  60. /**
  61. * The component name helpfull to identify the component in the list of scene components.
  62. */
  63. public readonly name = SceneComponentConstants.NAME_EFFECTLAYER;
  64. /**
  65. * The scene the component belongs to.
  66. */
  67. public scene: Scene;
  68. private _engine: Engine;
  69. private _renderEffects = false;
  70. private _needStencil = false;
  71. private _previousStencilState = false;
  72. /**
  73. * Creates a new instance of the component for the given scene
  74. * @param scene Defines the scene to register the component in
  75. */
  76. constructor(scene: Scene) {
  77. this.scene = scene;
  78. this._engine = scene.getEngine();
  79. scene.effectLayers = new Array<EffectLayer>();
  80. }
  81. /**
  82. * Registers the component in a given scene
  83. */
  84. public register(): void {
  85. this.scene._isReadyForMeshStage.registerStep(SceneComponentConstants.STEP_ISREADYFORMESH_EFFECTLAYER, this, this._isReadyForMesh);
  86. this.scene._cameraDrawRenderTargetStage.registerStep(SceneComponentConstants.STEP_CAMERADRAWRENDERTARGET_EFFECTLAYER, this, this._renderMainTexture);
  87. this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_EFFECTLAYER, this, this._setStencil);
  88. this.scene._afterRenderingGroupDrawStage.registerStep(SceneComponentConstants.STEP_AFTERRENDERINGGROUPDRAW_EFFECTLAYER_DRAW, this, this._drawRenderingGroup);
  89. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER, this, this._setStencilBack);
  90. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW, this, this._drawCamera);
  91. }
  92. /**
  93. * Rebuilds the elements related to this component in case of
  94. * context lost for instance.
  95. */
  96. public rebuild(): void {
  97. let layers = this.scene.effectLayers;
  98. for (let effectLayer of layers) {
  99. effectLayer._rebuild();
  100. }
  101. }
  102. /**
  103. * Serializes the component data to the specified json object
  104. * @param serializationObject The object to serialize to
  105. */
  106. public serialize(serializationObject: any): void {
  107. // Effect layers
  108. serializationObject.effectLayers = [];
  109. let layers = this.scene.effectLayers;
  110. for (let effectLayer of layers) {
  111. if (effectLayer.serialize) {
  112. serializationObject.effectLayers.push(effectLayer.serialize());
  113. }
  114. }
  115. }
  116. /**
  117. * Adds all the element from the container to the scene
  118. * @param container the container holding the elements
  119. */
  120. public addFromContainer(container: AbstractScene): void {
  121. if (!container.effectLayers) {
  122. return;
  123. }
  124. container.effectLayers.forEach((o) => {
  125. this.scene.addEffectLayer(o);
  126. });
  127. }
  128. /**
  129. * Removes all the elements in the container from the scene
  130. * @param container contains the elements to remove
  131. */
  132. public removeFromContainer(container: AbstractScene): void {
  133. if (!container.effectLayers) {
  134. return;
  135. }
  136. container.effectLayers.forEach((o) => {
  137. this.scene.removeEffectLayer(o);
  138. });
  139. }
  140. /**
  141. * Disposes the component and the associated ressources.
  142. */
  143. public dispose(): void {
  144. let layers = this.scene.effectLayers;
  145. while (layers.length) {
  146. layers[0].dispose();
  147. }
  148. }
  149. private _isReadyForMesh(mesh: AbstractMesh, hardwareInstancedRendering: boolean): boolean {
  150. let layers = this.scene.effectLayers;
  151. for (let layer of layers) {
  152. if (!layer.hasMesh(mesh)) {
  153. continue;
  154. }
  155. for (var subMesh of mesh.subMeshes) {
  156. if (!layer.isReady(subMesh, hardwareInstancedRendering)) {
  157. return false;
  158. }
  159. }
  160. }
  161. return true;
  162. }
  163. private _renderMainTexture(camera: Camera): void {
  164. this._renderEffects = false;
  165. this._needStencil = false;
  166. let layers = this.scene.effectLayers;
  167. if (layers && layers.length > 0) {
  168. this._previousStencilState = this._engine.getStencilBuffer();
  169. for (let effectLayer of layers) {
  170. if (effectLayer.shouldRender() &&
  171. (!effectLayer.camera ||
  172. (effectLayer.camera.cameraRigMode === Camera.RIG_MODE_NONE && camera === effectLayer.camera) ||
  173. (effectLayer.camera.cameraRigMode !== Camera.RIG_MODE_NONE && effectLayer.camera._rigCameras.indexOf(camera) > -1))) {
  174. this._renderEffects = true;
  175. this._needStencil = this._needStencil || effectLayer.needStencil();
  176. let renderTarget = (<RenderTargetTexture>(<any>effectLayer)._mainTexture);
  177. if (renderTarget._shouldRender()) {
  178. this.scene.incrementRenderId();
  179. renderTarget.render(false, false);
  180. }
  181. }
  182. }
  183. this.scene.incrementRenderId();
  184. }
  185. }
  186. private _setStencil() {
  187. // Activate effect Layer stencil
  188. if (this._needStencil) {
  189. this._engine.setStencilBuffer(true);
  190. }
  191. }
  192. private _setStencilBack() {
  193. // Restore effect Layer stencil
  194. if (this._needStencil) {
  195. this._engine.setStencilBuffer(this._previousStencilState);
  196. }
  197. }
  198. private _draw(renderingGroupId: number): void {
  199. if (this._renderEffects) {
  200. this._engine.setDepthBuffer(false);
  201. let layers = this.scene.effectLayers;
  202. for (let i = 0; i < layers.length; i++) {
  203. const effectLayer = layers[i];
  204. if (effectLayer.renderingGroupId === renderingGroupId) {
  205. if (effectLayer.shouldRender()) {
  206. effectLayer.render();
  207. }
  208. }
  209. }
  210. this._engine.setDepthBuffer(true);
  211. }
  212. }
  213. private _drawCamera(): void {
  214. if (this._renderEffects) {
  215. this._draw(-1);
  216. }
  217. }
  218. private _drawRenderingGroup(index: number): void {
  219. if (!this.scene._isInIntermediateRendering() && this._renderEffects) {
  220. this._draw(index);
  221. }
  222. }
  223. }