effectLayerSceneComponent.ts 9.6 KB

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