babylon.imageProcessingPostProcess.ts 13 KB

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