effectLayer.d.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { Observable } from "../Misc/observable";
  2. import type { Nullable } from "../types";
  3. import type { Camera } from "../Cameras/camera";
  4. import type { Scene } from "../scene";
  5. import type { ISize } from "../Maths/math.size";
  6. import { Color4 } from "../Maths/math.color";
  7. import type { AbstractEngine } from "../Engines/abstractEngine";
  8. import type { SubMesh } from "../Meshes/subMesh";
  9. import type { AbstractMesh } from "../Meshes/abstractMesh";
  10. import type { Mesh } from "../Meshes/mesh";
  11. import type { PostProcess } from "../PostProcesses/postProcess";
  12. import type { BaseTexture } from "../Materials/Textures/baseTexture";
  13. import { RenderTargetTexture } from "../Materials/Textures/renderTargetTexture";
  14. import type { Effect } from "../Materials/effect";
  15. import { Material } from "../Materials/material";
  16. import "../Shaders/glowMapGeneration.fragment";
  17. import "../Shaders/glowMapGeneration.vertex";
  18. /**
  19. * Effect layer options. This helps customizing the behaviour
  20. * of the effect layer.
  21. */
  22. export interface IEffectLayerOptions {
  23. /**
  24. * Multiplication factor apply to the canvas size to compute the render target size
  25. * used to generated the objects (the smaller the faster). Default: 0.5
  26. */
  27. mainTextureRatio: number;
  28. /**
  29. * Enforces a fixed size texture to ensure effect stability across devices. Default: undefined
  30. */
  31. mainTextureFixedSize?: number;
  32. /**
  33. * Alpha blending mode used to apply the blur. Default depends of the implementation. Default: ALPHA_COMBINE
  34. */
  35. alphaBlendingMode: number;
  36. /**
  37. * The camera attached to the layer. Default: null
  38. */
  39. camera: Nullable<Camera>;
  40. /**
  41. * The rendering group to draw the layer in. Default: -1
  42. */
  43. renderingGroupId: number;
  44. /**
  45. * The type of the main texture. Default: TEXTURETYPE_UNSIGNED_INT
  46. */
  47. mainTextureType: number;
  48. /**
  49. * Whether or not to generate a stencil buffer. Default: false
  50. */
  51. generateStencilBuffer: boolean;
  52. }
  53. /**
  54. * The effect layer Helps adding post process effect blended with the main pass.
  55. *
  56. * This can be for instance use to generate glow or highlight effects on the scene.
  57. *
  58. * The effect layer class can not be used directly and is intented to inherited from to be
  59. * customized per effects.
  60. */
  61. export declare abstract class EffectLayer {
  62. private _vertexBuffers;
  63. private _indexBuffer;
  64. private _effectLayerOptions;
  65. private _mergeDrawWrapper;
  66. protected _scene: Scene;
  67. protected _engine: AbstractEngine;
  68. protected _maxSize: number;
  69. protected _mainTextureDesiredSize: ISize;
  70. protected _mainTexture: RenderTargetTexture;
  71. protected _shouldRender: boolean;
  72. protected _postProcesses: PostProcess[];
  73. protected _textures: BaseTexture[];
  74. protected _emissiveTextureAndColor: {
  75. texture: Nullable<BaseTexture>;
  76. color: Color4;
  77. };
  78. protected _effectIntensity: {
  79. [meshUniqueId: number]: number;
  80. };
  81. /**
  82. * The name of the layer
  83. */
  84. name: string;
  85. /**
  86. * The clear color of the texture used to generate the glow map.
  87. */
  88. neutralColor: Color4;
  89. /**
  90. * Specifies whether the highlight layer is enabled or not.
  91. */
  92. isEnabled: boolean;
  93. /**
  94. * Gets the camera attached to the layer.
  95. */
  96. get camera(): Nullable<Camera>;
  97. /**
  98. * Gets the rendering group id the layer should render in.
  99. */
  100. get renderingGroupId(): number;
  101. set renderingGroupId(renderingGroupId: number);
  102. /**
  103. * Specifies if the bounding boxes should be rendered normally or if they should undergo the effect of the layer
  104. */
  105. disableBoundingBoxesFromEffectLayer: boolean;
  106. /**
  107. * An event triggered when the effect layer has been disposed.
  108. */
  109. onDisposeObservable: Observable<EffectLayer>;
  110. /**
  111. * An event triggered when the effect layer is about rendering the main texture with the glowy parts.
  112. */
  113. onBeforeRenderMainTextureObservable: Observable<EffectLayer>;
  114. /**
  115. * An event triggered when the generated texture is being merged in the scene.
  116. */
  117. onBeforeComposeObservable: Observable<EffectLayer>;
  118. /**
  119. * An event triggered when the mesh is rendered into the effect render target.
  120. */
  121. onBeforeRenderMeshToEffect: Observable<AbstractMesh>;
  122. /**
  123. * An event triggered after the mesh has been rendered into the effect render target.
  124. */
  125. onAfterRenderMeshToEffect: Observable<AbstractMesh>;
  126. /**
  127. * An event triggered when the generated texture has been merged in the scene.
  128. */
  129. onAfterComposeObservable: Observable<EffectLayer>;
  130. /**
  131. * An event triggered when the effect layer changes its size.
  132. */
  133. onSizeChangedObservable: Observable<EffectLayer>;
  134. /**
  135. * Gets the main texture where the effect is rendered
  136. */
  137. get mainTexture(): RenderTargetTexture;
  138. /**
  139. * @internal
  140. */
  141. static _SceneComponentInitialization: (scene: Scene) => void;
  142. private _materialForRendering;
  143. /**
  144. * Sets a specific material to be used to render a mesh/a list of meshes in the layer
  145. * @param mesh mesh or array of meshes
  146. * @param material material to use by the layer when rendering the mesh(es). If undefined is passed, the specific material created by the layer will be used.
  147. */
  148. setMaterialForRendering(mesh: AbstractMesh | AbstractMesh[], material?: Material): void;
  149. /**
  150. * Gets the intensity of the effect for a specific mesh.
  151. * @param mesh The mesh to get the effect intensity for
  152. * @returns The intensity of the effect for the mesh
  153. */
  154. getEffectIntensity(mesh: AbstractMesh): number;
  155. /**
  156. * Sets the intensity of the effect for a specific mesh.
  157. * @param mesh The mesh to set the effect intensity for
  158. * @param intensity The intensity of the effect for the mesh
  159. */
  160. setEffectIntensity(mesh: AbstractMesh, intensity: number): void;
  161. /**
  162. * Instantiates a new effect Layer and references it in the scene.
  163. * @param name The name of the layer
  164. * @param scene The scene to use the layer in
  165. */
  166. constructor(
  167. /** The Friendly of the effect in the scene */
  168. name: string, scene?: Scene);
  169. /**
  170. * Get the effect name of the layer.
  171. * @returns The effect name
  172. */
  173. abstract getEffectName(): string;
  174. /**
  175. * Checks for the readiness of the element composing the layer.
  176. * @param subMesh the mesh to check for
  177. * @param useInstances specify whether or not to use instances to render the mesh
  178. * @returns true if ready otherwise, false
  179. */
  180. abstract isReady(subMesh: SubMesh, useInstances: boolean): boolean;
  181. /**
  182. * Returns whether or not the layer needs stencil enabled during the mesh rendering.
  183. * @returns true if the effect requires stencil during the main canvas render pass.
  184. */
  185. abstract needStencil(): boolean;
  186. /**
  187. * Create the merge effect. This is the shader use to blit the information back
  188. * to the main canvas at the end of the scene rendering.
  189. * @returns The effect containing the shader used to merge the effect on the main canvas
  190. */
  191. protected abstract _createMergeEffect(): Effect;
  192. /**
  193. * Creates the render target textures and post processes used in the effect layer.
  194. */
  195. protected abstract _createTextureAndPostProcesses(): void;
  196. /**
  197. * Implementation specific of rendering the generating effect on the main canvas.
  198. * @param effect The effect used to render through
  199. * @param renderNum Index of the _internalRender call (0 for the first time _internalRender is called, 1 for the second time, etc. _internalRender is called the number of times returned by _numInternalDraws())
  200. */
  201. protected abstract _internalRender(effect: Effect, renderIndex: number): void;
  202. /**
  203. * Sets the required values for both the emissive texture and and the main color.
  204. */
  205. protected abstract _setEmissiveTextureAndColor(mesh: Mesh, subMesh: SubMesh, material: Material): void;
  206. /**
  207. * Free any resources and references associated to a mesh.
  208. * Internal use
  209. * @param mesh The mesh to free.
  210. */
  211. abstract _disposeMesh(mesh: Mesh): void;
  212. /**
  213. * Serializes this layer (Glow or Highlight for example)
  214. * @returns a serialized layer object
  215. */
  216. abstract serialize?(): any;
  217. /**
  218. * Number of times _internalRender will be called. Some effect layers need to render the mesh several times, so they should override this method with the number of times the mesh should be rendered
  219. * @returns Number of times a mesh must be rendered in the layer
  220. */
  221. protected _numInternalDraws(): number;
  222. /**
  223. * Initializes the effect layer with the required options.
  224. * @param options Sets of none mandatory options to use with the layer (see IEffectLayerOptions for more information)
  225. */
  226. protected _init(options: Partial<IEffectLayerOptions>): void;
  227. /**
  228. * Generates the index buffer of the full screen quad blending to the main canvas.
  229. */
  230. private _generateIndexBuffer;
  231. /**
  232. * Generates the vertex buffer of the full screen quad blending to the main canvas.
  233. */
  234. private _generateVertexBuffer;
  235. /**
  236. * Sets the main texture desired size which is the closest power of two
  237. * of the engine canvas size.
  238. */
  239. private _setMainTextureSize;
  240. /**
  241. * Creates the main texture for the effect layer.
  242. */
  243. protected _createMainTexture(): void;
  244. /**
  245. * Adds specific effects defines.
  246. * @param defines The defines to add specifics to.
  247. */
  248. protected _addCustomEffectDefines(defines: string[]): void;
  249. /**
  250. * Checks for the readiness of the element composing the layer.
  251. * @param subMesh the mesh to check for
  252. * @param useInstances specify whether or not to use instances to render the mesh
  253. * @param emissiveTexture the associated emissive texture used to generate the glow
  254. * @returns true if ready otherwise, false
  255. */
  256. protected _isReady(subMesh: SubMesh, useInstances: boolean, emissiveTexture: Nullable<BaseTexture>): boolean;
  257. /**
  258. * Renders the glowing part of the scene by blending the blurred glowing meshes on top of the rendered scene.
  259. */
  260. render(): void;
  261. /**
  262. * Determine if a given mesh will be used in the current effect.
  263. * @param mesh mesh to test
  264. * @returns true if the mesh will be used
  265. */
  266. hasMesh(mesh: AbstractMesh): boolean;
  267. /**
  268. * Returns true if the layer contains information to display, otherwise false.
  269. * @returns true if the glow layer should be rendered
  270. */
  271. shouldRender(): boolean;
  272. /**
  273. * Returns true if the mesh should render, otherwise false.
  274. * @param mesh The mesh to render
  275. * @returns true if it should render otherwise false
  276. */
  277. protected _shouldRenderMesh(mesh: AbstractMesh): boolean;
  278. /**
  279. * Returns true if the mesh can be rendered, otherwise false.
  280. * @param mesh The mesh to render
  281. * @param material The material used on the mesh
  282. * @returns true if it can be rendered otherwise false
  283. */
  284. protected _canRenderMesh(mesh: AbstractMesh, material: Material): boolean;
  285. /**
  286. * Returns true if the mesh should render, otherwise false.
  287. * @returns true if it should render otherwise false
  288. */
  289. protected _shouldRenderEmissiveTextureForMesh(): boolean;
  290. /**
  291. * Renders the submesh passed in parameter to the generation map.
  292. * @param subMesh
  293. * @param enableAlphaMode
  294. */
  295. protected _renderSubMesh(subMesh: SubMesh, enableAlphaMode?: boolean): void;
  296. /**
  297. * Defines whether the current material of the mesh should be use to render the effect.
  298. * @param mesh defines the current mesh to render
  299. * @returns true if the mesh material should be use
  300. */
  301. protected _useMeshMaterial(mesh: AbstractMesh): boolean;
  302. /**
  303. * Rebuild the required buffers.
  304. * @internal Internal use only.
  305. */
  306. _rebuild(): void;
  307. /**
  308. * Dispose only the render target textures and post process.
  309. */
  310. private _disposeTextureAndPostProcesses;
  311. /**
  312. * Dispose the highlight layer and free resources.
  313. */
  314. dispose(): void;
  315. /**
  316. * Gets the class name of the effect layer
  317. * @returns the string with the class name of the effect layer
  318. */
  319. getClassName(): string;
  320. /**
  321. * Creates an effect layer from parsed effect layer data
  322. * @param parsedEffectLayer defines effect layer data
  323. * @param scene defines the current scene
  324. * @param rootUrl defines the root URL containing the effect layer information
  325. * @returns a parsed effect Layer
  326. */
  327. static Parse(parsedEffectLayer: any, scene: Scene, rootUrl: string): EffectLayer;
  328. }