babylon.imageProcessingPostProcess.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. module BABYLON {
  2. export class ImageProcessingPostProcess extends PostProcess {
  3. /**
  4. * Default configuration related to image processing available in the PBR Material.
  5. */
  6. protected _imageProcessingConfiguration: ImageProcessingConfiguration;
  7. /**
  8. * Gets the image processing configuration used either in this material.
  9. */
  10. public get imageProcessingConfiguration(): ImageProcessingConfiguration {
  11. return this._imageProcessingConfiguration;
  12. }
  13. /**
  14. * Sets the Default image processing configuration used either in the this material.
  15. *
  16. * If sets to null, the scene one is in use.
  17. */
  18. public set imageProcessingConfiguration(value: ImageProcessingConfiguration) {
  19. this._attachImageProcessingConfiguration(value);
  20. }
  21. /**
  22. * Keep track of the image processing observer to allow dispose and replace.
  23. */
  24. private _imageProcessingObserver: Observer<ImageProcessingConfiguration>;
  25. /**
  26. * Attaches a new image processing configuration to the PBR Material.
  27. * @param configuration
  28. */
  29. protected _attachImageProcessingConfiguration(configuration: ImageProcessingConfiguration): void {
  30. if (configuration === this._imageProcessingConfiguration) {
  31. return;
  32. }
  33. // Detaches observer.
  34. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  35. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  36. }
  37. // Pick the scene configuration if needed.
  38. if (!configuration) {
  39. var camera = this.getCamera();
  40. var scene = camera ? camera.getScene() : BABYLON.Engine.LastCreatedScene;
  41. this._imageProcessingConfiguration = scene.imageProcessingConfiguration;
  42. }
  43. else {
  44. this._imageProcessingConfiguration = configuration;
  45. }
  46. // Attaches observer.
  47. this._imageProcessingObserver = this._imageProcessingConfiguration.onUpdateParameters.add(conf => {
  48. this._updateParameters();
  49. });
  50. // Ensure the effect will be rebuilt.
  51. this._updateParameters();
  52. }
  53. /**
  54. * Gets Color curves setup used in the effect if colorCurvesEnabled is set to true .
  55. */
  56. public get colorCurves(): ColorCurves {
  57. return this.imageProcessingConfiguration.colorCurves;
  58. }
  59. /**
  60. * Sets Color curves setup used in the effect if colorCurvesEnabled is set to true .
  61. */
  62. public set colorCurves(value: ColorCurves) {
  63. this.imageProcessingConfiguration.colorCurves = value;
  64. }
  65. /**
  66. * Gets wether the color curves effect is enabled.
  67. */
  68. public get colorCurvesEnabled(): boolean {
  69. return this.imageProcessingConfiguration.colorCurvesEnabled;
  70. }
  71. /**
  72. * Sets wether the color curves effect is enabled.
  73. */
  74. public set colorCurvesEnabled(value: boolean) {
  75. this.imageProcessingConfiguration.colorCurvesEnabled = value;
  76. }
  77. /**
  78. * Gets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
  79. */
  80. public get colorGradingTexture(): BaseTexture {
  81. return this.imageProcessingConfiguration.colorGradingTexture;
  82. }
  83. /**
  84. * Sets Color grading LUT texture used in the effect if colorGradingEnabled is set to true.
  85. */
  86. public set colorGradingTexture(value: BaseTexture) {
  87. this.imageProcessingConfiguration.colorGradingTexture = value;
  88. }
  89. /**
  90. * Gets wether the color grading effect is enabled.
  91. */
  92. public get colorGradingEnabled(): boolean {
  93. return this.imageProcessingConfiguration.colorGradingEnabled;
  94. }
  95. /**
  96. * Gets wether the color grading effect is enabled.
  97. */
  98. public set colorGradingEnabled(value: boolean) {
  99. this.imageProcessingConfiguration.colorGradingEnabled = value;
  100. }
  101. /**
  102. * Gets exposure used in the effect.
  103. */
  104. public get exposure(): number {
  105. return this.imageProcessingConfiguration.exposure;
  106. }
  107. /**
  108. * Sets exposure used in the effect.
  109. */
  110. public set exposure(value: number) {
  111. this.imageProcessingConfiguration.exposure = value;
  112. }
  113. /**
  114. * Gets wether tonemapping is enabled or not.
  115. */
  116. public get toneMappingEnabled(): boolean {
  117. return this._imageProcessingConfiguration.toneMappingEnabled;
  118. };
  119. /**
  120. * Sets wether tonemapping is enabled or not
  121. */
  122. public set toneMappingEnabled(value: boolean) {
  123. this._imageProcessingConfiguration.toneMappingEnabled = value;
  124. };
  125. /**
  126. * Gets contrast used in the effect.
  127. */
  128. public get contrast(): number {
  129. return this.imageProcessingConfiguration.contrast;
  130. }
  131. /**
  132. * Sets contrast used in the effect.
  133. */
  134. public set contrast(value: number) {
  135. this.imageProcessingConfiguration.contrast = value;
  136. }
  137. /**
  138. * Gets Vignette stretch size.
  139. */
  140. public get vignetteStretch(): number {
  141. return this.imageProcessingConfiguration.vignetteStretch;
  142. }
  143. /**
  144. * Sets Vignette stretch size.
  145. */
  146. public set vignetteStretch(value: number) {
  147. this.imageProcessingConfiguration.vignetteStretch = value;
  148. }
  149. /**
  150. * Gets Vignette centre X Offset.
  151. */
  152. public get vignetteCentreX(): number {
  153. return this.imageProcessingConfiguration.vignetteCentreX;
  154. }
  155. /**
  156. * Sets Vignette centre X Offset.
  157. */
  158. public set vignetteCentreX(value: number) {
  159. this.imageProcessingConfiguration.vignetteCentreX = value;
  160. }
  161. /**
  162. * Gets Vignette centre Y Offset.
  163. */
  164. public get vignetteCentreY(): number {
  165. return this.imageProcessingConfiguration.vignetteCentreY;
  166. }
  167. /**
  168. * Sets Vignette centre Y Offset.
  169. */
  170. public set vignetteCentreY(value: number) {
  171. this.imageProcessingConfiguration.vignetteCentreY = value;
  172. }
  173. /**
  174. * Gets Vignette weight or intensity of the vignette effect.
  175. */
  176. public get vignetteWeight(): number {
  177. return this.imageProcessingConfiguration.vignetteWeight;
  178. }
  179. /**
  180. * Sets Vignette weight or intensity of the vignette effect.
  181. */
  182. public set vignetteWeight(value: number) {
  183. this.imageProcessingConfiguration.vignetteWeight = value;
  184. }
  185. /**
  186. * Gets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
  187. * if vignetteEnabled is set to true.
  188. */
  189. public get vignetteColor(): Color4 {
  190. return this.imageProcessingConfiguration.vignetteColor;
  191. }
  192. /**
  193. * Sets Color of the vignette applied on the screen through the chosen blend mode (vignetteBlendMode)
  194. * if vignetteEnabled is set to true.
  195. */
  196. public set vignetteColor(value: Color4) {
  197. this.imageProcessingConfiguration.vignetteColor = value;
  198. }
  199. /**
  200. * Gets Camera field of view used by the Vignette effect.
  201. */
  202. public get vignetteCameraFov(): number {
  203. return this.imageProcessingConfiguration.vignetteCameraFov;
  204. }
  205. /**
  206. * Sets Camera field of view used by the Vignette effect.
  207. */
  208. public set vignetteCameraFov(value: number) {
  209. this.imageProcessingConfiguration.vignetteCameraFov = value;
  210. }
  211. /**
  212. * Gets the vignette blend mode allowing different kind of effect.
  213. */
  214. public get vignetteBlendMode(): number {
  215. return this.imageProcessingConfiguration.vignetteBlendMode;
  216. }
  217. /**
  218. * Sets the vignette blend mode allowing different kind of effect.
  219. */
  220. public set vignetteBlendMode(value: number) {
  221. this.imageProcessingConfiguration.vignetteBlendMode = value;
  222. }
  223. /**
  224. * Gets wether the vignette effect is enabled.
  225. */
  226. public get vignetteEnabled(): boolean {
  227. return this.imageProcessingConfiguration.vignetteEnabled;
  228. }
  229. /**
  230. * Sets wether the vignette effect is enabled.
  231. */
  232. public set vignetteEnabled(value: boolean) {
  233. this.imageProcessingConfiguration.vignetteEnabled = value;
  234. }
  235. @serialize()
  236. private _fromLinearSpace = true;
  237. /**
  238. * Gets wether the input of the processing is in Gamma or Linear Space.
  239. */
  240. public get fromLinearSpace(): boolean {
  241. return this._fromLinearSpace;
  242. }
  243. /**
  244. * Sets wether the input of the processing is in Gamma or Linear Space.
  245. */
  246. public set fromLinearSpace(value: boolean) {
  247. if (this._fromLinearSpace === value) {
  248. return;
  249. }
  250. this._fromLinearSpace = value;
  251. this._updateParameters();
  252. }
  253. /**
  254. * Defines cache preventing GC.
  255. */
  256. private _defines: IImageProcessingConfigurationDefines & { FROMLINEARSPACE: boolean } = {
  257. IMAGEPROCESSING: false,
  258. VIGNETTE: false,
  259. VIGNETTEBLENDMODEMULTIPLY: false,
  260. VIGNETTEBLENDMODEOPAQUE: false,
  261. TONEMAPPING: false,
  262. CONTRAST: false,
  263. COLORCURVES: false,
  264. COLORGRADING: false,
  265. FROMLINEARSPACE: false,
  266. SAMPLER3DGREENDEPTH: false,
  267. SAMPLER3DBGRMAP: false,
  268. IMAGEPROCESSINGPOSTPROCESS: false,
  269. EXPOSURE: false,
  270. }
  271. constructor(name: string, options: number | PostProcessOptions, camera?: Camera, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
  272. super(name, "imageProcessing", [], [], options, camera, samplingMode, engine, reusable,
  273. null, textureType, "postprocess", null, true);
  274. // Setup the default processing configuration to the scene.
  275. this._attachImageProcessingConfiguration(null);
  276. this.imageProcessingConfiguration.applyByPostProcess = true;
  277. this._updateParameters();
  278. this.onApply = (effect: Effect) => {
  279. this.imageProcessingConfiguration.bind(effect, this.aspectRatio);
  280. };
  281. }
  282. public getClassName(): string {
  283. return "ImageProcessingPostProcess";
  284. }
  285. protected _updateParameters(): void {
  286. this._defines.FROMLINEARSPACE = this._fromLinearSpace;
  287. this.imageProcessingConfiguration.prepareDefines(this._defines);
  288. var defines = "";
  289. for (const define in this._defines) {
  290. if (this._defines[define]) {
  291. defines += `#define ${define};\r\n`;
  292. }
  293. }
  294. var samplers = ["textureSampler"];
  295. ImageProcessingConfiguration.PrepareSamplers(samplers, this._defines);
  296. var uniforms = ["scale"];
  297. ImageProcessingConfiguration.PrepareUniforms(uniforms, this._defines);
  298. this.updateEffect(defines, uniforms, samplers);
  299. }
  300. public dispose(camera?: Camera): void {
  301. super.dispose(camera);
  302. if (this._imageProcessingConfiguration && this._imageProcessingObserver) {
  303. this._imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingObserver);
  304. }
  305. this.imageProcessingConfiguration.applyByPostProcess = false;
  306. }
  307. }
  308. }