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