effectLayerSceneComponent.ts 9.5 KB

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