PoissonDenoiseShader.js 6.7 KB

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