Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. //  Copyright (c) 2018-2019 Michele Morrone
  3. //  All rights reserved.
  4. //
  5. //  https://michelemorrone.eu - https://BrutPitt.com
  6. //
  7. //  me@michelemorrone.eu - brutpitt@gmail.com
  8. //  twitter: @BrutPitt - github: BrutPitt
  9. //  
  10. //  https://github.com/BrutPitt/glslSmartDeNoise/
  11. //
  12. //  This software is distributed under the terms of the BSD 2-Clause license
  13. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  14.  
  15. #define INV_SQRT_OF_2PI 0.39894228040143267793994605993439  // 1.0/SQRT_OF_2PI
  16. #define INV_PI 0.31830988618379067153776752674503
  17.  
  18. //  smartDeNoise - parameters
  19. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. //
  21. //  sampler2D tex     - sampler image / texture
  22. //  vec2 uv           - actual fragment coord
  23. //  float sigma  >  0 - sigma Standard Deviation
  24. //  float kSigma >= 0 - sigma coefficient
  25. //      kSigma * sigma  -->  radius of the circular kernel
  26. //  float threshold   - edge sharpening threshold
  27.  
  28. vec4 smartDeNoise(sampler2D tex, ivec2 uv, float sigma, float kSigma, float threshold)
  29. {
  30.     float radius = round(kSigma*sigma);
  31.     float radQ = radius * radius;
  32.    
  33.     float invSigmaQx2 = .5 / (sigma * sigma);      // 1.0 / (sigma^2 * 2.0)
  34.     float invSigmaQx2PI = INV_PI * invSigmaQx2;    // 1.0 / (2.0 * PI * sigma^2)
  35.    
  36.     float invThresholdSqx2 = .5 / (threshold * threshold);     // 1.0 / (sigma^2 * 2.0)
  37.     float invThresholdSqrt2PI = INV_SQRT_OF_2PI / threshold;   // 1.0 / (sqrt(2*PI) * sigma)
  38.    
  39.     vec4 centrPx = texelFetch(tex, uv, 0);
  40.    
  41.     float zBuff = 0.0;
  42.     vec4 aBuff = vec4(0.0);
  43.    
  44.     for(float x=-radius; x <= radius; x++) {
  45.         float pt = sqrt(radQ-x*x);  // pt = yRadius: have circular trend
  46.         for(float y=-pt; y <= pt; y++) {
  47.             vec2 d = vec2(x,y);
  48.  
  49.             float blurFactor = exp( -dot(d , d) * invSigmaQx2 ) * invSigmaQx2PI;
  50.            
  51.             vec4 walkPx =  texelFetch(tex, uv+ivec2(x,y), 0);
  52.  
  53.             vec4 dC = walkPx-centrPx;
  54.             float deltaFactor = exp( -dot(dC, dC) * invThresholdSqx2) * invThresholdSqrt2PI * blurFactor;
  55.                                  
  56.             zBuff += deltaFactor;
  57.             aBuff += deltaFactor*walkPx;
  58.         }
  59.     }
  60.     return aBuff/zBuff;
  61. }
  62.  
  63. //  About Standard Deviations (watch Gauss curve)
  64. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. //
  66. //  kSigma = 1*sigma cover 68% of data
  67. //  kSigma = 2*sigma cover 95% of data - but there are over 3 times
  68. //                   more points to compute
  69. //  kSigma = 3*sigma cover 99.7% of data - but needs more than double
  70. //                   the calculations of 2*sigma
  71.  
  72.  
  73. //  Optimizations (description)
  74. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. //
  76. //  fX = exp( -(x*x) * invSigmaSqx2 ) * invSigmaxSqrt2PI;
  77. //  fY = exp( -(y*y) * invSigmaSqx2 ) * invSigmaxSqrt2PI;
  78. //  where...
  79. //      invSigmaSqx2     = 1.0 / (sigma^2 * 2.0)
  80. //      invSigmaxSqrt2PI = 1.0 / (sqrt(2 * PI) * sigma)
  81. //
  82. //  now, fX*fY can be written in unique expression...
  83. //
  84. //      e^(a*X) * e^(a*Y) * c*c
  85. //
  86. //      where:
  87. //        a = invSigmaSqx2, X = (x*x), Y = (y*y), c = invSigmaxSqrt2PI
  88. //
  89. //           -[(x*x) * 1/(2 * sigma^2)]             -[(y*y) * 1/(2 * sigma^2)]
  90. //          e                                      e
  91. //  fX = -------------------------------    fY = -------------------------------
  92. //                ________                               ________
  93. //              \/ 2 * PI  * sigma                     \/ 2 * PI  * sigma
  94. //
  95. //      now with...
  96. //        a = 1/(2 * sigma^2),
  97. //        X = (x*x)
  98. //        Y = (y*y) ________
  99. //        c = 1 / \/ 2 * PI  * sigma
  100. //
  101. //      we have...
  102. //              -[aX]              -[aY]
  103. //        fX = e      * c;   fY = e      * c;
  104. //
  105. //      and...
  106. //                 -[aX + aY]    [2]     -[a(X + Y)]    [2]
  107. //        fX*fY = e           * c     = e            * c  
  108. //
  109. //      well...
  110. //
  111. //                    -[(x*x + y*y) * 1/(2 * sigma^2)]
  112. //                   e                                
  113. //        fX*fY = --------------------------------------
  114. //                                        [2]          
  115. //                          2 * PI * sigma          
  116. //      
  117. //      now with assigned constants...
  118. //
  119. //          invSigmaQx2   = 1/(2 * sigma^2)
  120. //          invSigmaQx2PI = 1/(2 * PI * sigma^2) = invSigmaQx2 * INV_PI
  121. //
  122. //      and the kernel vector
  123. //
  124. //          k = vec2(x,y)
  125. //
  126. //      we can write:
  127. //
  128. //          fXY = exp( -dot(k,k) * invSigmaQx2) * invSigmaQx2PI
  129. //
  130.  
  131. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  132. {
  133.     ivec2 texSize = textureSize(iChannel0, 0);
  134.     vec2 invTexSize = 1.0 / vec2(texSize);
  135.    
  136.     ivec2 uv = ivec2(fragCoord) % texSize;
  137.  
  138.     float szSlide = 3.0;        
  139.     vec2 mouse = iMouse.xy;
  140.     if(mouse.x <= 0.0 &&  mouse.y <= 0.0) mouse.x = iResolution.x / 2.0; // to show initial screen half splitted
  141.    
  142.     fragColor = fragCoord.x < mouse.x-szSlide  ? texelFetch(iChannel0, uv, 0)
  143.           : (fragCoord.x > mouse.x+szSlide ? smartDeNoise(iChannel0, uv, 5.0, 2.0, .100)
  144.           :  vec4(1.0));
  145.    
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement