daily pastebin goal
20%
SHARE
TWEET

gpu shader graphing

a guest Jul 6th, 2011 463 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //note: shadertoy-pluggable, http://www.iquilezles.org/apps/shadertoy/
  2.  
  3. #ifdef GL_ES
  4. precision highp float;
  5. #endif
  6.  
  7. uniform float time;
  8. uniform vec2 resolution;
  9.  
  10. float aspect = resolution.x / resolution.y;
  11.  
  12. float function( float x ) {
  13.   return sin(x*x*x)*sin(x);
  14. //  return sin(x*x*x)*sin(x) + 0.1*sin(x*x);
  15. //  return sin(x);
  16. }
  17.  
  18. //note: does one sample per x, thresholds on distance in y
  19. float discreteEval( vec2 uv ) {
  20.   const float threshold = 0.015;
  21.   float x = uv.x;
  22.   float fx = function( x );
  23.   float dist = abs( uv.y - fx );
  24.   float hit = step( dist, threshold );
  25.   return hit;
  26. }
  27.  
  28. //note: samples graph by checking multiple samples being above / below function
  29. //original from http://blog.hvidtfeldts.net/index.php/2011/07/plotting-high-frequency-functions-using-a-gpu/
  30. float stochEval( vec2 uv ) {
  31.   const int samples = 255; //note: on AMD requires 255+ samples, should be ~50
  32.   const float fsamples = float(samples);
  33.   vec2 maxdist = 0.075 * vec2( aspect, 1.0 );
  34.   vec2 stepsize = maxdist / vec2(samples);
  35.   float count = 0.0;
  36.   vec2 initial_offset = - 0.5 * fsamples * stepsize;
  37.   uv += initial_offset;
  38.   for ( int ii = 0; ii<samples; ii++ ) {
  39.     float i = float(ii);
  40.     float fx = function( uv.x + i*stepsize.x );
  41.     for ( int jj = 0; jj<samples; jj++ ) {
  42.       float j = float(jj);
  43.       float diff =  fx - float(uv.y + j*stepsize.y);
  44.       count = count + step(0.0, diff) * 2.0 - 1.0;
  45.     }
  46.   }
  47.   return 1.0 - abs( count ) / float(samples*samples);
  48. }
  49.  
  50. //note: averages distances over multiple samples along x, result is identical to superEval
  51. float distAvgEval( vec2 uv ) {
  52.   const int samples = 255; //note: on AMD requires 255+ samples, should be ~50
  53.   const float fsamples = float(samples);
  54.   vec2 maxdist = 0.075 * vec2( aspect, 1.0 );
  55.   vec2 halfmaxdist = 0.5 * maxdist;
  56.   float stepsize = maxdist.x / fsamples;
  57.   float initial_offset_x = -0.5*fsamples * stepsize;
  58.   uv.x += initial_offset_x;
  59.   float hit = 0.0;
  60.   for( int i=0; i<samples; ++i ) {
  61.     float x = uv.x + stepsize * float(i);
  62.     float y = uv.y;
  63.     float fx = function( x );
  64.     float dist = ( y - fx );
  65.     float vt = clamp( dist / halfmaxdist.y -1.0, -1.0, 1.0 );
  66.     hit += vt;
  67.   }
  68.   return 1.0 - abs(hit) / fsamples;
  69. }
  70.  
  71. //note: does multiple thresholded samples
  72. float proxyEval( vec2 uv ) {
  73.   const int samples = 255; //note: on AMD requires 255+ samples, should be ~50
  74.   const float fsamples = float(samples);
  75.   vec2 maxdist = vec2(0.05) * vec2( aspect, 1.0 );
  76.   vec2 halfmaxdist = vec2(0.5) * maxdist;
  77.   float stepsize = maxdist.x / fsamples;
  78.   float initial_offset_x = -0.5 * fsamples * stepsize;
  79.   uv.x += initial_offset_x;
  80.   float hit = 0.0;
  81.   for( int i=0; i<samples; ++i ) {
  82.     float x = uv.x + stepsize * float(i);
  83.     float y = uv.y;
  84.     float fx = function( x );
  85.     float dist = abs( y - fx );
  86.     hit += step( dist, halfmaxdist.y );
  87.   }
  88.   const float arbitraryFactor = 3.5; //note: to increase intensity
  89.   const float arbitraryExp = 0.95;
  90.   return arbitraryFactor * pow( hit / fsamples, arbitraryExp );
  91. }
  92.  
  93.  
  94. void main(void)
  95. {
  96.   vec2 uv_norm = gl_FragCoord.xy / resolution.xy;
  97.   vec4 dim = vec4( -2.0, 12.0, -3.0, 3.0 );
  98.   uv_norm = (uv_norm ) * ( dim.yw - dim.xz ) + dim.xz;
  99.  
  100.   //float hitStoch = stochEval( uv_norm - vec2(0,2) );
  101.   float hitDiscr = discreteEval( uv_norm  + vec2(0,2) );
  102.   float hitProximity = proxyEval( uv_norm - vec2(0,2) );
  103.   float hitDistAvgStoch = distAvgEval( uv_norm - vec2(0,0) );
  104.  
  105.  gl_FragColor = vec4( hitDistAvgStoch
  106.                     , 0.8*hitProximity + 0.5*hitDiscr
  107.                     , hitDiscr + 0.2*hitProximity
  108.                     , 1.0);
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top