Advertisement
onnowhere

light test

Apr 23rd, 2019
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ad 2d crepuscular occlusion dither bayer eclipse
  3.  
  4. parent : https://www.shadertoy.com/view/4dyXWy
  5. self   : https://www.shadertoy.com/view/ltsyWl
  6.  
  7. Instead of reading from a bitmap, this uses one of 2 bayer matrix generators.
  8. Parents bufferA is a subroutine here: BuffA.mainImage() <- BA()
  9.  
  10. - no buffers (Bayer matrix bitmap is very optional)
  11. - generalize to be more parametric, maybe?
  12. */
  13.  
  14. //blurriness of borders, sets contour line smoothness
  15. // Will be scaled by shortest domain of screen resolution
  16. // [>0.]!! Set to [1.] for ideal 1:1 subbixel smoothness.
  17. // if (you use dFdx()) do [#define blur >=2.]
  18. #define blur 1.
  19.  
  20. #define DITHER          //Dithering toggle
  21. #define QUALITY     2   //0- low, 1- medium, 2- high
  22.  
  23. #define DECAY       .974
  24. #define EXPOSURE    .24
  25. #if (QUALITY==2)
  26.  #define SAMPLES    64
  27.  #define DENSITY    .97
  28.  #define WEIGHT     .25
  29. #else
  30. #if (QUALITY==1)
  31.  #define SAMPLES    32
  32.  #define DENSITY    .95
  33.  #define WEIGHT     .25
  34. #else
  35.  #define SAMPLES    16
  36.  #define DENSITY    .93
  37.  #define WEIGHT     .36
  38. #endif
  39. #endif
  40.  
  41.  
  42. //planar zoom.
  43. #define ViewZoom 3.
  44. //sub-pixel blur
  45. #define fsaa 14./min(iResolution.x,iResolution.y)
  46. //View Frame
  47. #define fra(u) (u-.5*iResolution.xy)*ViewZoom/iResolution.y
  48.  
  49. //maxiterations for bayer matrix, maximum value is number of bits of your data type?
  50. //for crepuscular ray dithering [1..3] iterations are enough
  51. //because it is basically "noisy scattering" so  any patterns in it are "just fine"
  52. #define iterBayerMat 1
  53. #define bayer2x2(a) (4-(a).x-((a).y<<1))%4
  54. //return bayer matris (bitwise operands for speed over compatibility)
  55. float GetBayerFromCoordLevel(vec2 pixelpos)
  56. {ivec2 p=ivec2(pixelpos);int a=0
  57. ;for(int i=0; i<iterBayerMat; i++
  58. ){a+=bayer2x2(p>>(iterBayerMat-1-i)&1)<<(2*i);
  59. }return float(a)/float(2<<(iterBayerMat*2-1));}
  60. //https://www.shadertoy.com/view/XtV3RG
  61.  
  62. //analytic bayer over 2 domains, is unrolled loop of GetBayerFromCoordLevel().
  63. //but in terms of reusing subroutines, which is faster,while it does not extend as nicely.
  64. float bayer2  (vec2 a){a=floor(a);return fract(dot(a,vec2(.5, a.y*.75)));}
  65. float bayer4  (vec2 a){return bayer2 (  .5*a)*.25    +bayer2(a);}
  66. float bayer8  (vec2 a){return bayer4 (  .5*a)*.25    +bayer2(a);}
  67. float bayer16 (vec2 a){return bayer4 ( .25*a)*.0625  +bayer4(a);}
  68. float bayer32 (vec2 a){return bayer8 ( .25*a)*.0625  +bayer4(a);}
  69. float bayer64 (vec2 a){return bayer8 (.125*a)*.015625+bayer8(a);}
  70. float bayer128(vec2 a){return bayer16(.125*a)*.015625+bayer8(a);}
  71. #define dither2(p)   (bayer2(  p)-.375      )
  72. #define dither4(p)   (bayer4(  p)-.46875    )
  73. #define dither8(p)   (bayer8(  p)-.4921875  )
  74. #define dither16(p)  (bayer16( p)-.498046875)
  75. #define dither32(p)  (bayer32( p)-.499511719)
  76. #define dither64(p)  (bayer64( p)-.49987793 )
  77. #define dither128(p) (bayer128(p)-.499969482)
  78. //https://www.shadertoy.com/view/4ssfWM
  79.  
  80. //3 ways to approach a bayer matrix for dithering (or for loops within permutations)
  81. float iib(vec2 u){
  82.  return dither128(u);//analytic bayer, base2
  83.  return GetBayerFromCoordLevel(u*999.);//iterative bayer
  84.  //optionally: instad just use bitmap of a bayer matrix: (LUT approach)
  85.  //return texture(iChannel1,u/iChannelResolution[1].xy).x;
  86. }
  87.  
  88.  
  89. //x is result, yz are position in normalized coords.
  90.  //This is just a quick hack for this shader only.
  91. vec3 sun( vec2 uv, vec2 position ) {   
  92. //o put op inside this function, making p not a parameter of it, because
  93.     vec2 p=fra(iMouse.zw)*.666;//damnit, the source shader has rather silly frame scaling, this irons it out a bit
  94.     //but this is a poor overwriting patch
  95.     //has something t do with averaging over 3 occluders within this function.
  96.     //this one is special, and far from general.
  97.     if(iMouse.z<=0.)p=position;
  98.     vec3 res;
  99.     float di = distance(uv, p);
  100.     res.x =  di <= .3333 ? sqrt(1. - di*3.) : 0.;
  101.     res.yz = p;
  102.     res.y /= (iResolution.x / iResolution.y);
  103.     res.yz = (res.yz+1.)*.5;
  104.     return res;}
  105.  
  106. #define SS blur/min(iResolution.x,iResolution.y)
  107.  
  108. float circle( vec2 p, float r){
  109.  return smoothstep(SS,-SS,length(p)-r);
  110.  return step(length(p)-r,.0);
  111.     return length(p) < r ? 1. : 0.;//why would you do that?
  112. }
  113.  
  114.  
  115. //[buffA of https://www.shadertoy.com/view/4dyXWy ], merged into [Image]
  116. vec4 BA(in vec2 uv, in vec2 lightpos ){  
  117.     //This buffer calculates occluders and a gaussian glowing blob.
  118.     //Just see what it returns for any uv input.
  119.     //It makes sense to buffer this as a matrix (low res 2d frame buffer)
  120.     // ,because it is being looked up multiple times
  121.     // With different offsets.
  122.     //  This is NOT doing that, for no good reason at all
  123.     //   other than brute force benchmaking a bufferless approach
  124.     uv=uv*2.-1.;
  125.     float aspect = iResolution.x / iResolution.y;
  126.    uv.x *= aspect;
  127.     vec2 m=iMouse.xy;
  128.     if(m.x<=0.)m=vec2(iResolution*.1);else m=(m/iResolution.xy)*2.-1.;//set mouse position
  129.     m.x *= aspect;
  130.     //above framing is not pretty. but thats not what is demoed here.
  131.    
  132.     float occluders=circle(uv-vec2(-.66,0), .366)-circle(uv+vec2(.75,.1),.18);
  133.     occluders+=circle(uv-vec2(.6,.2),.23);
  134.     float mouse=smoothstep(SS,-SS,abs(abs(length(uv-m)-.2)-.05)-.02);//double ring
  135.     //mouse cursor has an occluding crosshair:
  136.     float mouse2=smoothstep(SS,-SS,abs(uv.x-m.x)-.03);//vertical bar
  137.           mouse=max(mouse,mouse2);//union
  138.     //mouse=clamp((mouse-length(uv-m)*2.),.0,1.);//alpha-shade crosshair
  139.    
  140.     //transparent disk on the right
  141.     mouse2=smoothstep(SS,-SS,length(uv-m-vec2(.5,0))-.2);
  142.     mouse=max(mouse,mouse2*.5);//union
  143.        
  144.   // mouse=max(mouse,.0);//optical illusion checker!
  145.     occluders+=mouse;//-=mouse and you have a "nightmare moon spitlight"
  146.     occluders = min(occluders, 1.);
  147.     vec3 light=min(sun(uv, lightpos),1.);
  148.     float col = max(light.x - occluders, 0.);
  149.     return vec4(col,occluders,light.yz); //Gross hack to pass light pos as B and A values
  150. }
  151.  
  152. float renderLight(vec2 position, vec2 uv, vec2 fragCoord) {
  153.     vec2 coord = uv;
  154.     vec4 ic=BA(uv, position);
  155.     vec2 lightpos = ic.zw;
  156.     float occ = ic.x; //light
  157.     float obj = ic.y; //objects
  158.     float dither = iib(fragCoord);
  159.     vec2 dtc = (coord - lightpos)*(1./float(SAMPLES)*DENSITY);
  160.     float illumdecay = 1.;
  161.    
  162.     for(int i=0; i<SAMPLES; i++)    {
  163.         coord -= dtc;
  164.         #ifdef DITHER
  165.             float s = BA(coord+(dtc*dither), position).x;
  166.         #else
  167.             float s = BA(coord).x  ;    
  168.         #endif
  169.         s *= illumdecay * WEIGHT;
  170.         occ += s;
  171.         illumdecay *= DECAY;
  172.     }
  173.     return occ;
  174. }
  175.  
  176. void mainImage( out vec4 fragColor, in vec2 fragCoord ){
  177.     vec2 uv = fragCoord.xy / iResolution.xy;
  178.     float occ = renderLight(vec2(-0.5), uv, fragCoord);
  179.     occ += renderLight(vec2(0.5), uv, fragCoord);
  180.     occ += renderLight(vec2(0.5, -0.5), uv, fragCoord);
  181.        
  182.     fragColor = vec4(vec3(.5,.7,.1)*0./3.+occ*EXPOSURE,1.0);
  183. }
  184.  
  185.  
  186. /*
  187. //return 16x16 Bayer matrix, without bitwiseOP, most compatible!
  188. //likely best for old mobile hardware, likely most energy efficient.
  189. //slowest (kept small for small resolutions)
  190. float B16( vec2 _P ) {
  191.     vec2    P1 = mod( _P, 2.0 );                    // (P >> 0) & 1
  192.     vec2    P2 = floor( 0.5 * mod( _P, 4.0 ) );        // (P >> 1) & 1
  193.     vec2    P4 = floor( 0.25 * mod( _P, 8.0 ) );    // (P >> 2) & 1
  194.     vec2    P8 = floor( 0.125 * mod( _P, 16.0 ) );    // (P >> 3) & 1
  195.     return 4.0*(4.0*(4.0*B2(P1) + B2(P2)) + B2(P4)) + B2(P8);
  196. }//https://www.shadertoy.com/view/4tVSDm
  197. */
  198.  
  199. /*
  200. //A 64x64 bayer matrix LUT. FAST, static size.
  201. //best for non-mobile hardware, least energy efficient.
  202. void populatePatternTable(){    
  203.     pattern[0x00]=   0.; pattern[0x01]= 32.; pattern[0x02]=  8.; pattern[0x03]= 40.; pattern[0x04]=  2.; pattern[0x05]= 34.; pattern[0x06]= 10.; pattern[0x07]= 42.;  
  204.     pattern[0x08]=  48.; pattern[0x09]= 16.; pattern[0x0a]= 56.; pattern[0x0b]= 24.; pattern[0x0c]= 50.; pattern[0x0d]= 18.; pattern[0x0e]= 58.; pattern[0x0f]= 26.;  
  205.     pattern[0x10]=  12.; pattern[0x11]= 44.; pattern[0x12]=  4.; pattern[0x13]= 36.; pattern[0x14]= 14.; pattern[0x15]= 46.; pattern[0x16]=  6.; pattern[0x17]= 38.;  
  206.     pattern[0x10]=  60.; pattern[0x19]= 28.; pattern[0x1a]= 52.; pattern[0x1b]= 20.; pattern[0x1c]= 62.; pattern[0x1d]= 30.; pattern[0x1e]= 54.; pattern[0x1f]= 22.;  
  207.     pattern[0x20]=   3.; pattern[0x21]= 35.; pattern[0x22]= 11.; pattern[0x23]= 43.; pattern[0x24]=  1.; pattern[0x25]= 33.; pattern[0x26]=  9.; pattern[0x27]= 41.;  
  208.     pattern[0x20]=  51.; pattern[0x29]= 19.; pattern[0x2a]= 59.; pattern[0x2b]= 27.; pattern[0x2c]= 49.; pattern[0x2d]= 17.; pattern[0x2e]= 57.; pattern[0x2f]= 25.;
  209.     pattern[0x30]=  15.; pattern[0x31]= 47.; pattern[0x32]=  7.; pattern[0x33]= 39.; pattern[0x34]= 13.; pattern[0x35]= 45.; pattern[0x36]=  5.; pattern[0x37]= 37.;
  210.     pattern[0x30]=  63.; pattern[0x39]= 31.; pattern[0x3a]= 55.; pattern[0x3b]= 23.; pattern[0x3c]= 61.; pattern[0x3d]= 29.; pattern[0x3e]= 53.; pattern[0x3f]= 21.;
  211. }//www.efg2.com/Lab/Library/ImageProcessing/DHALF.TXT
  212. //https://www.shadertoy.com/view/MlByzh
  213. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement