PoissonDenoiseShader.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {
  2. Matrix4,
  3. Vector2,
  4. Vector3,
  5. } from 'three';
  6. /**
  7. * References:
  8. * https://openaccess.thecvf.com/content/WACV2021/papers/Khademi_Self-Supervised_Poisson-Gaussian_Denoising_WACV_2021_paper.pdf
  9. * https://arxiv.org/pdf/2206.01856.pdf
  10. */
  11. const PoissonDenoiseShader = {
  12. name: 'PoissonDenoiseShader',
  13. defines: {
  14. 'SAMPLES': 16,
  15. 'SAMPLE_VECTORS': generatePdSamplePointInitializer( 16, 2, 1 ),
  16. 'NORMAL_VECTOR_TYPE': 1,
  17. 'DEPTH_VALUE_SOURCE': 0,
  18. },
  19. uniforms: {
  20. 'tDiffuse': { value: null },
  21. 'tNormal': { value: null },
  22. 'tDepth': { value: null },
  23. 'tNoise': { value: null },
  24. 'resolution': { value: new Vector2() },
  25. 'cameraProjectionMatrixInverse': { value: new Matrix4() },
  26. 'lumaPhi': { value: 5. },
  27. 'depthPhi': { value: 5. },
  28. 'normalPhi': { value: 5. },
  29. 'radius': { value: 4. },
  30. 'index': { value: 0 }
  31. },
  32. vertexShader: /* glsl */`
  33. varying vec2 vUv;
  34. void main() {
  35. vUv = uv;
  36. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  37. }`,
  38. fragmentShader: /* glsl */`
  39. varying vec2 vUv;
  40. uniform sampler2D tDiffuse;
  41. uniform sampler2D tNormal;
  42. uniform sampler2D tDepth;
  43. uniform sampler2D tNoise;
  44. uniform vec2 resolution;
  45. uniform mat4 cameraProjectionMatrixInverse;
  46. uniform float lumaPhi;
  47. uniform float depthPhi;
  48. uniform float normalPhi;
  49. uniform float radius;
  50. uniform int index;
  51. #include <common>
  52. #include <packing>
  53. #ifndef SAMPLE_LUMINANCE
  54. #define SAMPLE_LUMINANCE dot(vec3(0.2125, 0.7154, 0.0721), a)
  55. #endif
  56. #ifndef FRAGMENT_OUTPUT
  57. #define FRAGMENT_OUTPUT vec4(denoised, 1.)
  58. #endif
  59. float getLuminance(const in vec3 a) {
  60. return SAMPLE_LUMINANCE;
  61. }
  62. const vec3 poissonDisk[SAMPLES] = SAMPLE_VECTORS;
  63. vec3 getViewPosition(const in vec2 screenPosition, const in float depth) {
  64. vec4 clipSpacePosition = vec4(vec3(screenPosition, depth) * 2.0 - 1.0, 1.0);
  65. vec4 viewSpacePosition = cameraProjectionMatrixInverse * clipSpacePosition;
  66. return viewSpacePosition.xyz / viewSpacePosition.w;
  67. }
  68. float getDepth(const vec2 uv) {
  69. #if DEPTH_VALUE_SOURCE == 1
  70. return textureLod(tDepth, uv.xy, 0.0).a;
  71. #else
  72. return textureLod(tDepth, uv.xy, 0.0).r;
  73. #endif
  74. }
  75. float fetchDepth(const ivec2 uv) {
  76. #if DEPTH_VALUE_SOURCE == 1
  77. return texelFetch(tDepth, uv.xy, 0).a;
  78. #else
  79. return texelFetch(tDepth, uv.xy, 0).r;
  80. #endif
  81. }
  82. vec3 computeNormalFromDepth(const vec2 uv) {
  83. vec2 size = vec2(textureSize(tDepth, 0));
  84. ivec2 p = ivec2(uv * size);
  85. float c0 = fetchDepth(p);
  86. float l2 = fetchDepth(p - ivec2(2, 0));
  87. float l1 = fetchDepth(p - ivec2(1, 0));
  88. float r1 = fetchDepth(p + ivec2(1, 0));
  89. float r2 = fetchDepth(p + ivec2(2, 0));
  90. float b2 = fetchDepth(p - ivec2(0, 2));
  91. float b1 = fetchDepth(p - ivec2(0, 1));
  92. float t1 = fetchDepth(p + ivec2(0, 1));
  93. float t2 = fetchDepth(p + ivec2(0, 2));
  94. float dl = abs((2.0 * l1 - l2) - c0);
  95. float dr = abs((2.0 * r1 - r2) - c0);
  96. float db = abs((2.0 * b1 - b2) - c0);
  97. float dt = abs((2.0 * t1 - t2) - c0);
  98. vec3 ce = getViewPosition(uv, c0).xyz;
  99. vec3 dpdx = (dl < dr) ? ce - getViewPosition((uv - vec2(1.0 / size.x, 0.0)), l1).xyz
  100. : -ce + getViewPosition((uv + vec2(1.0 / size.x, 0.0)), r1).xyz;
  101. vec3 dpdy = (db < dt) ? ce - getViewPosition((uv - vec2(0.0, 1.0 / size.y)), b1).xyz
  102. : -ce + getViewPosition((uv + vec2(0.0, 1.0 / size.y)), t1).xyz;
  103. return normalize(cross(dpdx, dpdy));
  104. }
  105. vec3 getViewNormal(const vec2 uv) {
  106. #if NORMAL_VECTOR_TYPE == 2
  107. return normalize(textureLod(tNormal, uv, 0.).rgb);
  108. #elif NORMAL_VECTOR_TYPE == 1
  109. return unpackRGBToNormal(textureLod(tNormal, uv, 0.).rgb);
  110. #else
  111. return computeNormalFromDepth(uv);
  112. #endif
  113. }
  114. void denoiseSample(in vec3 center, in vec3 viewNormal, in vec3 viewPos, in vec2 sampleUv, inout vec3 denoised, inout float totalWeight) {
  115. vec4 sampleTexel = textureLod(tDiffuse, sampleUv, 0.0);
  116. float sampleDepth = getDepth(sampleUv);
  117. vec3 sampleNormal = getViewNormal(sampleUv);
  118. vec3 neighborColor = sampleTexel.rgb;
  119. vec3 viewPosSample = getViewPosition(sampleUv, sampleDepth);
  120. float normalDiff = dot(viewNormal, sampleNormal);
  121. float normalSimilarity = pow(max(normalDiff, 0.), normalPhi);
  122. float lumaDiff = abs(getLuminance(neighborColor) - getLuminance(center));
  123. float lumaSimilarity = max(1.0 - lumaDiff / lumaPhi, 0.0);
  124. float depthDiff = abs(dot(viewPos - viewPosSample, viewNormal));
  125. float depthSimilarity = max(1. - depthDiff / depthPhi, 0.);
  126. float w = lumaSimilarity * depthSimilarity * normalSimilarity;
  127. denoised += w * neighborColor;
  128. totalWeight += w;
  129. }
  130. void main() {
  131. float depth = getDepth(vUv.xy);
  132. vec3 viewNormal = getViewNormal(vUv);
  133. if (depth == 1. || dot(viewNormal, viewNormal) == 0.) {
  134. discard;
  135. return;
  136. }
  137. vec4 texel = textureLod(tDiffuse, vUv, 0.0);
  138. vec3 center = texel.rgb;
  139. vec3 viewPos = getViewPosition(vUv, depth);
  140. vec2 noiseResolution = vec2(textureSize(tNoise, 0));
  141. vec2 noiseUv = vUv * resolution / noiseResolution;
  142. vec4 noiseTexel = textureLod(tNoise, noiseUv, 0.0);
  143. vec2 noiseVec = vec2(sin(noiseTexel[index % 4] * 2. * PI), cos(noiseTexel[index % 4] * 2. * PI));
  144. mat2 rotationMatrix = mat2(noiseVec.x, -noiseVec.y, noiseVec.x, noiseVec.y);
  145. float totalWeight = 1.0;
  146. vec3 denoised = texel.rgb;
  147. for (int i = 0; i < SAMPLES; i++) {
  148. vec3 sampleDir = poissonDisk[i];
  149. vec2 offset = rotationMatrix * (sampleDir.xy * (1. + sampleDir.z * (radius - 1.)) / resolution);
  150. vec2 sampleUv = vUv + offset;
  151. denoiseSample(center, viewNormal, viewPos, sampleUv, denoised, totalWeight);
  152. }
  153. if (totalWeight > 0.) {
  154. denoised /= totalWeight;
  155. }
  156. gl_FragColor = FRAGMENT_OUTPUT;
  157. }`
  158. };
  159. function generatePdSamplePointInitializer( samples, rings, radiusExponent ) {
  160. const poissonDisk = generateDenoiseSamples(
  161. samples,
  162. rings,
  163. radiusExponent,
  164. );
  165. let glslCode = 'vec3[SAMPLES](';
  166. for ( let i = 0; i < samples; i ++ ) {
  167. const sample = poissonDisk[ i ];
  168. glslCode += `vec3(${sample.x}, ${sample.y}, ${sample.z})${( i < samples - 1 ) ? ',' : ')'}`;
  169. }
  170. return glslCode;
  171. }
  172. function generateDenoiseSamples( numSamples, numRings, radiusExponent ) {
  173. const samples = [];
  174. for ( let i = 0; i < numSamples; i ++ ) {
  175. const angle = 2 * Math.PI * numRings * i / numSamples;
  176. const radius = Math.pow( i / ( numSamples - 1 ), radiusExponent );
  177. samples.push( new Vector3( Math.cos( angle ), Math.sin( angle ), radius ) );
  178. }
  179. return samples;
  180. }
  181. export { generatePdSamplePointInitializer, PoissonDenoiseShader };