babylon.blurPostProcess.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. module BABYLON {
  2. /**
  3. * The Blur Post Process which blurs an image based on a kernel and direction.
  4. * Can be used twice in x and y directions to perform a guassian blur in two passes.
  5. */
  6. export class BlurPostProcess extends PostProcess {
  7. protected _kernel: number;
  8. protected _idealKernel: number;
  9. protected _packedFloat: boolean = false;
  10. private _staticDefines:string = ""
  11. /**
  12. * Sets the length in pixels of the blur sample region
  13. */
  14. public set kernel(v: number) {
  15. if (this._idealKernel === v) {
  16. return;
  17. }
  18. v = Math.max(v, 1);
  19. this._idealKernel = v;
  20. this._kernel = this._nearestBestKernel(v);
  21. this._updateParameters();
  22. }
  23. /**
  24. * Gets the length in pixels of the blur sample region
  25. */
  26. public get kernel(): number {
  27. return this._idealKernel;
  28. }
  29. /**
  30. * Sets wether or not the blur needs to unpack/repack floats
  31. */
  32. public set packedFloat(v: boolean) {
  33. if (this._packedFloat === v) {
  34. return;
  35. }
  36. this._packedFloat = v;
  37. this._updateParameters();
  38. }
  39. /**
  40. * Gets wether or not the blur is unpacking/repacking floats
  41. */
  42. public get packedFloat(): boolean {
  43. return this._packedFloat;
  44. }
  45. /**
  46. * Creates a new instance of @see BlurPostProcess
  47. * @param name The name of the effect.
  48. * @param direction The direction in which to blur the image.
  49. * @param kernel The size of the kernel to be used when computing the blur. eg. Size of 3 will blur the center pixel by 2 pixels surrounding it.
  50. * @param options The required width/height ratio to downsize to before computing the render pass. (Use 1.0 for full size)
  51. * @param camera The camera to apply the render pass to.
  52. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  53. * @param engine The engine which the post process will be applied. (default: current engine)
  54. * @param reusable If the post process can be reused on the same frame. (default: false)
  55. * @param textureType Type of textures used when performing the post process. (default: 0)
  56. */
  57. constructor(name: string, /** The direction in which to blur the image. */ public direction: Vector2, kernel: number, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode: number = Texture.BILINEAR_SAMPLINGMODE, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, defines = "") {
  58. super(name, "kernelBlur", ["delta", "direction", "cameraMinMaxZ"], ["circleOfConfusionSampler"], options, camera, samplingMode, engine, reusable, null, textureType, "kernelBlur", {varyingCount: 0, depCount: 0}, true);
  59. this._staticDefines = defines;
  60. this.onApplyObservable.add((effect: Effect) => {
  61. effect.setFloat2('delta', (1 / this.width) * this.direction.x, (1 / this.height) * this.direction.y);
  62. });
  63. this.kernel = kernel;
  64. }
  65. protected _updateParameters(): void {
  66. // Generate sampling offsets and weights
  67. let N = this._kernel;
  68. let centerIndex = (N - 1) / 2;
  69. // Generate Gaussian sampling weights over kernel
  70. let offsets = [];
  71. let weights = [];
  72. let totalWeight = 0;
  73. for (let i = 0; i < N; i++) {
  74. let u = i / (N - 1);
  75. let w = this._gaussianWeight(u * 2.0 - 1);
  76. offsets[i] = (i - centerIndex);
  77. weights[i] = w;
  78. totalWeight += w;
  79. }
  80. // Normalize weights
  81. for (let i = 0; i < weights.length; i++) {
  82. weights[i] /= totalWeight;
  83. }
  84. // Optimize: combine samples to take advantage of hardware linear sampling
  85. // Walk from left to center, combining pairs (symmetrically)
  86. let linearSamplingWeights = [];
  87. let linearSamplingOffsets = [];
  88. let linearSamplingMap = [];
  89. for (let i = 0; i <= centerIndex; i += 2) {
  90. let j = Math.min(i + 1, Math.floor(centerIndex));
  91. let singleCenterSample = i === j;
  92. if (singleCenterSample) {
  93. linearSamplingMap.push({ o: offsets[i], w: weights[i] });
  94. } else {
  95. let sharedCell = j === centerIndex;
  96. let weightLinear = (weights[i] + weights[j] * (sharedCell ? .5 : 1.));
  97. let offsetLinear = offsets[i] + 1 / (1 + weights[i] / weights[j]);
  98. if (offsetLinear === 0) {
  99. linearSamplingMap.push({ o: offsets[i], w: weights[i] });
  100. linearSamplingMap.push({ o: offsets[i + 1], w: weights[i + 1] });
  101. } else {
  102. linearSamplingMap.push({ o: offsetLinear, w: weightLinear });
  103. linearSamplingMap.push({ o: -offsetLinear, w: weightLinear });
  104. }
  105. }
  106. }
  107. for (let i = 0; i < linearSamplingMap.length; i++) {
  108. linearSamplingOffsets[i] = linearSamplingMap[i].o;
  109. linearSamplingWeights[i] = linearSamplingMap[i].w;
  110. }
  111. // Replace with optimized
  112. offsets = linearSamplingOffsets;
  113. weights = linearSamplingWeights;
  114. // Generate shaders
  115. let maxVaryingRows = this.getEngine().getCaps().maxVaryingVectors;
  116. let freeVaryingVec2 = Math.max(maxVaryingRows, 0.) - 1; // Because of sampleCenter
  117. let varyingCount = Math.min(offsets.length, freeVaryingVec2);
  118. let defines = "";
  119. defines+=this._staticDefines;
  120. for (let i = 0; i < varyingCount; i++) {
  121. defines += `#define KERNEL_OFFSET${i} ${this._glslFloat(offsets[i])}\r\n`;
  122. defines += `#define KERNEL_WEIGHT${i} ${this._glslFloat(weights[i])}\r\n`;
  123. }
  124. let depCount = 0;
  125. for (let i = freeVaryingVec2; i < offsets.length; i++) {
  126. defines += `#define KERNEL_DEP_OFFSET${depCount} ${this._glslFloat(offsets[i])}\r\n`;
  127. defines += `#define KERNEL_DEP_WEIGHT${depCount} ${this._glslFloat(weights[i])}\r\n`;
  128. depCount++;
  129. }
  130. if (this.packedFloat) {
  131. defines += `#define PACKEDFLOAT 1`;
  132. }
  133. this.updateEffect(defines, null, null, {
  134. varyingCount: varyingCount,
  135. depCount: depCount
  136. });
  137. }
  138. /**
  139. * Best kernels are odd numbers that when divided by 2, their integer part is even, so 5, 9 or 13.
  140. * Other odd kernels optimize correctly but require proportionally more samples, even kernels are
  141. * possible but will produce minor visual artifacts. Since each new kernel requires a new shader we
  142. * want to minimize kernel changes, having gaps between physical kernels is helpful in that regard.
  143. * The gaps between physical kernels are compensated for in the weighting of the samples
  144. * @param idealKernel Ideal blur kernel.
  145. * @return Nearest best kernel.
  146. */
  147. protected _nearestBestKernel(idealKernel: number): number {
  148. let v = Math.round(idealKernel);
  149. for (let k of [v, v - 1, v + 1, v - 2, v + 2]) {
  150. if (((k % 2) !== 0) && ((Math.floor(k / 2) % 2) === 0) && k > 0) {
  151. return Math.max(k, 3);
  152. }
  153. }
  154. return Math.max(v, 3);
  155. }
  156. /**
  157. * Calculates the value of a Gaussian distribution with sigma 3 at a given point.
  158. * @param x The point on the Gaussian distribution to sample.
  159. * @return the value of the Gaussian function at x.
  160. */
  161. protected _gaussianWeight(x: number): number {
  162. //reference: Engine/ImageProcessingBlur.cpp #dcc760
  163. // We are evaluating the Gaussian (normal) distribution over a kernel parameter space of [-1,1],
  164. // so we truncate at three standard deviations by setting stddev (sigma) to 1/3.
  165. // The choice of 3-sigma truncation is common but arbitrary, and means that the signal is
  166. // truncated at around 1.3% of peak strength.
  167. //the distribution is scaled to account for the difference between the actual kernel size and the requested kernel size
  168. let sigma = (1 / 3);
  169. let denominator = Math.sqrt(2.0 * Math.PI) * sigma;
  170. let exponent = -((x * x) / (2.0 * sigma * sigma));
  171. let weight = (1.0 / denominator) * Math.exp(exponent);
  172. return weight;
  173. }
  174. /**
  175. * Generates a string that can be used as a floating point number in GLSL.
  176. * @param x Value to print.
  177. * @param decimalFigures Number of decimal places to print the number to (excluding trailing 0s).
  178. * @return GLSL float string.
  179. */
  180. protected _glslFloat(x: number, decimalFigures = 8) {
  181. return x.toFixed(decimalFigures).replace(/0+$/, '');
  182. }
  183. }
  184. }