babylon.imageProcessingPostProcess.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. module BABYLON {
  2. export class ImageProcessingPostProcess extends PostProcess {
  3. private _colorGradingTexture: BaseTexture;
  4. public colorGradingWeight: number = 1.0;
  5. public colorCurves = new ColorCurves();
  6. private _colorCurvesEnabled = true;
  7. public cameraFov = 0.5;
  8. public vignetteStretch = 0;
  9. public vignetteCentreX = 0;
  10. public vignetteCentreY = 0;
  11. public vignetteWeight = 1.5;
  12. public vignetteColor: BABYLON.Color4 = new BABYLON.Color4(0, 0, 0, 0);
  13. private _vignetteBlendMode = ImageProcessingPostProcess.VIGNETTEMODE_MULTIPLY;
  14. private _vignetteEnabled = true;
  15. public cameraContrast = 1.0;
  16. public cameraExposure = 1.68;
  17. private _cameraToneMappingEnabled = true;
  18. private _fromLinearSpace = false;
  19. public get colorGradingTexture(): BaseTexture {
  20. return this._colorGradingTexture;
  21. }
  22. public set colorGradingTexture(value: BaseTexture) {
  23. if (this._colorGradingTexture === value) {
  24. return;
  25. }
  26. this._colorGradingTexture = value;
  27. this._updateParameters();
  28. }
  29. public get vignetteBlendMode(): number {
  30. return this._vignetteBlendMode;
  31. }
  32. public set vignetteBlendMode(value: number) {
  33. if (this._vignetteBlendMode === value) {
  34. return;
  35. }
  36. this._vignetteBlendMode = value;
  37. this._updateParameters();
  38. }
  39. public get colorCurvesEnabled(): boolean {
  40. return this._colorCurvesEnabled;
  41. }
  42. public set colorCurvesEnabled(value: boolean) {
  43. if (this._colorCurvesEnabled === value) {
  44. return;
  45. }
  46. this._colorCurvesEnabled = value;
  47. this._updateParameters();
  48. }
  49. public get vignetteEnabled(): boolean {
  50. return this._vignetteEnabled;
  51. }
  52. public set vignetteEnabled(value: boolean) {
  53. if (this._vignetteEnabled === value) {
  54. return;
  55. }
  56. this._vignetteEnabled = value;
  57. this._updateParameters();
  58. }
  59. public get fromLinearSpace(): boolean {
  60. return this._fromLinearSpace;
  61. }
  62. public set fromLinearSpace(value: boolean) {
  63. if (this._fromLinearSpace === value) {
  64. return;
  65. }
  66. this._fromLinearSpace = value;
  67. this._updateParameters();
  68. }
  69. public get cameraToneMappingEnabled(): boolean {
  70. return this._cameraToneMappingEnabled;
  71. }
  72. public set cameraToneMappingEnabled(value: boolean) {
  73. if (this._cameraToneMappingEnabled === value) {
  74. return;
  75. }
  76. this._cameraToneMappingEnabled = value;
  77. this._updateParameters();
  78. }
  79. constructor(name: string, options: number | PostProcessOptions, camera?: Camera, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
  80. super(name, "imageProcessing", [
  81. 'contrast',
  82. 'vignetteSettings1',
  83. 'vignetteSettings2',
  84. 'cameraExposureLinear',
  85. 'vCameraColorCurveNegative',
  86. 'vCameraColorCurveNeutral',
  87. 'vCameraColorCurvePositive',
  88. 'colorTransformSettings'
  89. ], ["txColorTransform"], options, camera, samplingMode, engine, reusable,
  90. null, textureType, "postprocess", null, true);
  91. this._updateParameters();
  92. this.onApply = (effect: Effect) => {
  93. let aspectRatio = this.aspectRatio;
  94. // Color
  95. if (this._colorCurvesEnabled) {
  96. ColorCurves.Bind(this.colorCurves, effect);
  97. }
  98. if (this._vignetteEnabled) {
  99. // Vignette
  100. let vignetteScaleY = Math.tan(this.cameraFov * 0.5);
  101. let vignetteScaleX = vignetteScaleY * aspectRatio;
  102. let vignetteScaleGeometricMean = Math.sqrt(vignetteScaleX * vignetteScaleY);
  103. vignetteScaleX = Tools.Mix(vignetteScaleX, vignetteScaleGeometricMean, this.vignetteStretch);
  104. vignetteScaleY = Tools.Mix(vignetteScaleY, vignetteScaleGeometricMean, this.vignetteStretch);
  105. effect.setFloat4('vignetteSettings1', vignetteScaleX, vignetteScaleY, -vignetteScaleX * this.vignetteCentreX, -vignetteScaleY * this.vignetteCentreY);
  106. let vignettePower = -2.0 * this.vignetteWeight;
  107. effect.setFloat4('vignetteSettings2', this.vignetteColor.r, this.vignetteColor.g, this.vignetteColor.b, vignettePower);
  108. }
  109. // Contrast and exposure
  110. effect.setFloat('contrast', this.cameraContrast);
  111. effect.setFloat('cameraExposureLinear', Math.pow(2.0, -this.cameraExposure) * Math.PI);
  112. // Color transform settings
  113. if (this._colorGradingTexture) {
  114. effect.setTexture('txColorTransform', this.colorGradingTexture);
  115. let textureSize = this.colorGradingTexture.getSize().height;
  116. effect.setFloat4("colorTransformSettings",
  117. (textureSize - 1) / textureSize, // textureScale
  118. 0.5 / textureSize, // textureOffset
  119. textureSize, // textureSize
  120. this.colorGradingWeight // weight
  121. );
  122. }
  123. };
  124. }
  125. protected _updateParameters(): void {
  126. var defines = "";
  127. var samplers = ["textureSampler"];
  128. if (this.colorGradingTexture) {
  129. defines = "#define COLORGRADING\r\n";
  130. samplers.push("txColorTransform");
  131. }
  132. if (this._vignetteEnabled) {
  133. defines += "#define VIGNETTE\r\n";
  134. if (this.vignetteBlendMode === ImageProcessingPostProcess._VIGNETTEMODE_MULTIPLY) {
  135. defines += "#define VIGNETTEBLENDMODEMULTIPLY\r\n";
  136. } else {
  137. defines += "#define VIGNETTEBLENDMODEOPAQUE\r\n";
  138. }
  139. }
  140. if (this.cameraToneMappingEnabled) {
  141. defines += "#define TONEMAPPING\r\n";
  142. }
  143. if (this._colorCurvesEnabled && this.colorCurves) {
  144. defines += "#define COLORCURVES\r\n";
  145. }
  146. if (this._fromLinearSpace) {
  147. defines += "#define FROMLINEARSPACE\r\n";
  148. }
  149. this.updateEffect(defines, null, samplers);
  150. }
  151. // Statics
  152. private static _VIGNETTEMODE_MULTIPLY = 0;
  153. private static _VIGNETTEMODE_OPAQUE = 1;
  154. public static get VIGNETTEMODE_MULTIPLY(): number {
  155. return ImageProcessingPostProcess._VIGNETTEMODE_MULTIPLY;
  156. }
  157. public static get VIGNETTEMODE_OPAQUE(): number {
  158. return ImageProcessingPostProcess._VIGNETTEMODE_OPAQUE;
  159. }
  160. }
  161. }