Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.18 KB | None | 0 0
  1.  
  2. #ifdef GL_ES
  3. precision highp float;
  4. #endif
  5.  
  6. uniform float time;
  7. uniform vec2 resolution;
  8.  
  9. float function( float x ) {
  10.   return sin(x*x*x)*sin(x);
  11. //  return sin(x*x*x)*sin(x) + 0.1*sin(x*x);
  12. //  return sin(x);
  13. }
  14.  
  15.  
  16. float superEval( vec2 uv ) {
  17.   const int samples = 255; //note: amd-bug: doesn't work below 255 iterations (probably because it doesn't unroll?)
  18.   float max_jit = 0.05;
  19.   float stepsize = max_jit / float(samples);
  20.   vec2 jitter = vec2(stepsize) * vec2( resolution.x / resolution.y, 1.0 );
  21.   float count = 0.0;
  22.   vec2 initial_offset = -vec2(samples)*0.5*jitter.xy;
  23.   uv = uv + initial_offset;
  24.   for ( int ii = 0; ii<samples; ii++ ) {
  25.     float i = float(ii);
  26.     float fx = function( uv.x + i*jitter.x );
  27.     for ( int jj = 0; jj<samples; jj++ ) {
  28.       float j = float(jj);
  29.       float diff =  fx - float(uv.y + j*jitter.y);
  30.       count = count + step(0.0, diff) * 2.0 - 1.0;
  31.     }
  32.   }
  33.   return 1.0 - abs( count ) / float(samples*samples);
  34. }
  35.  
  36. float discreteEval( vec2 uv ) {
  37.   const float threshold = 0.015;
  38.   float x = uv.x;
  39.   float fx = function( x );
  40.   float dist = abs( uv.y - fx );
  41.   float hit = step( dist, threshold );
  42.   return hit;
  43. }
  44.  
  45. float stochDistEval( vec2 uv ) {
  46.   const int samples = 255;
  47.   const float fsamples = float(samples);
  48.   vec2 maxdist = vec2( 0.075, 0.075 );
  49.   vec2 halfmaxdist = 0.5 * maxdist;
  50.   float stepsize = maxdist.x / fsamples;
  51.   float initial_offset_x = -0.5*fsamples * stepsize;
  52.   uv.x += initial_offset_x;
  53.   float hit = 0.0;
  54.   for( int i=0; i<samples; ++i ) {
  55.     float x = uv.x + stepsize * float(i);
  56.     float y = uv.y;
  57.     float fx = function( x );
  58.     float dist = ( y - fx );
  59.     float vt = clamp( dist / halfmaxdist.y -1.0, -1.0, 1.0 );
  60.     hit += vt;
  61.   }
  62.   return 1.0 - abs(hit) / fsamples;
  63. }
  64.  
  65.  
  66.  
  67. void main(void)
  68. {
  69.   vec2 uv_norm = gl_FragCoord.xy / resolution.xy;
  70.   vec4 dim = vec4( -2.0, 12.0, -3.0, 3.0 );
  71.   uv_norm = (uv_norm ) * ( dim.yw - dim.xz ) + dim.xz;
  72.  
  73.   float hitDiscr = discreteEval( uv_norm  + vec2(0,2) );
  74.   float hitStoch = superEval( uv_norm - vec2(0,2) );
  75.   float hitDistStoch = stochDistEval( uv_norm - vec2(0,0) );
  76.  
  77.  gl_FragColor = vec4( hitDistStoch, hitStoch, hitDiscr, 1.0);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement