Advertisement
Guest User

Code

a guest
Jan 5th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. float sdPlane( in vec3 p, in vec4 s )
  2. {
  3.     return p.y + s.w;
  4. }
  5.  
  6. float sdSphere( in vec3 p, in float s )
  7. {
  8.     return sqrt(p.x*p.x+p.y*p.y+p.z*p.z)-s;
  9. }
  10.  
  11. float sdBox( vec3 p, vec3 b )
  12. {
  13.   vec3 d = abs(p) - b;
  14.   return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
  15. }
  16.  
  17.  
  18. vec2 opU( in vec2 d1, in vec2 d2 )
  19. {
  20.     return (d1.x<d2.x) ? d1 : d2;
  21. }
  22.  
  23. //----------------------------------------------------------------------
  24.  
  25. vec2 map( in vec3 pos )
  26. {
  27.     vec2 res = vec2( sdPlane(pos,vec4(0.0,1.0,0.0,0.0)), 13.0 );
  28.  
  29.     res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.40,-2.0), 0.40 ), 0.0 ) );
  30.     res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.25,-1.0), 0.25 ), 0.0 ) );
  31.     res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.40, 0.0), 0.40 ), 1.0 ) );
  32.     res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.25, 1.0), 0.25 ), 1.0 ) );
  33.     res = opU( res, vec2( sdSphere( pos-vec3(-2.0,0.40, 2.0), 0.40 ), 2.0 ) );
  34.     res = opU( res, vec2( sdSphere( pos-vec3(-1.0,0.25,-2.0), 0.25 ), 2.0 ) );
  35.     res = opU( res, vec2( sdSphere( pos-vec3(-1.0,0.40,-1.0), 0.40 ), 3.0 ) );
  36.     res = opU( res, vec2( sdSphere( pos-vec3(-1.0,0.25, 0.0), 0.25 ), 3.0 ) );
  37.     res = opU( res, vec2( sdSphere( pos-vec3(-1.0,0.40, 1.0), 0.40 ), 4.0 ) );
  38.     res = opU( res, vec2( sdSphere( pos-vec3(-1.0,0.25, 2.0), 0.25 ), 4.0 ) );
  39.     res = opU( res, vec2( sdSphere( pos-vec3( 0.0,0.40,-2.0), 0.40 ), 5.0 ) );
  40.     res = opU( res, vec2( sdSphere( pos-vec3( 0.0,0.25,-1.0), 0.25 ), 5.0 ) );
  41.     res = opU( res, vec2( sdSphere( pos-vec3( 0.0,0.40, 0.0), 0.40 ), 6.0 ) );
  42.     res = opU( res, vec2( sdSphere( pos-vec3( 0.0,0.25, 1.0), 0.25 ), 6.0 ) );
  43.     res = opU( res, vec2( sdSphere( pos-vec3( 0.0,0.40, 2.0), 0.40 ), 7.0 ) );
  44.     res = opU( res, vec2( sdSphere( pos-vec3( 1.0,0.25,-2.0), 0.25 ), 7.0 ) );
  45.     res = opU( res, vec2( sdSphere( pos-vec3( 1.0,0.40,-1.0), 0.40 ), 8.0 ) );
  46.     res = opU( res, vec2( sdSphere( pos-vec3( 1.0,0.25, 0.0), 0.25 ), 8.0 ) );
  47.     res = opU( res, vec2( sdSphere( pos-vec3( 1.0,0.40, 1.0), 0.40 ), 9.0 ) );
  48.     res = opU( res, vec2( sdSphere( pos-vec3( 1.0,0.25, 2.0), 0.25 ), 9.0 ) );
  49.     res = opU( res, vec2( sdSphere( pos-vec3( 2.0,0.40,-2.0), 0.40 ), 10.0 ) );
  50.     res = opU( res, vec2( sdSphere( pos-vec3( 2.0,0.25,-1.0), 0.25 ), 10.0 ) );
  51.     res = opU( res, vec2( sdSphere( pos-vec3( 2.0,0.40, 0.0), 0.40 ), 11.0 ) );
  52.     res = opU( res, vec2( sdSphere( pos-vec3( 2.0,0.25, 1.0), 0.25 ), 11.0 ) );
  53.     res = opU( res, vec2( sdSphere( pos-vec3( 2.0,0.40, 2.0), 0.40 ), 12.0 ) );
  54.    
  55.     return res;
  56. }
  57.  
  58. vec2 castRay( in vec3 ro, in vec3 rd, in float tmin, in float tmax )
  59. {
  60.     float t = tmin;
  61.     float m = 0.0;
  62.     for( int i=0; i<=63; i++ )
  63.     {
  64.         vec2 res = map( ro+rd*t );
  65.         if( res.x<0.0 || t>tmax ) break;
  66.         t += res.x;
  67.         m = res.y;
  68.     }
  69.  
  70.     if( t>tmax ) m=-1.0;
  71.     return vec2( t, m );
  72. }
  73.  
  74.  
  75. float shadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
  76. {
  77.     float t = mint;
  78.     for( int i=0; i<64; i++ )
  79.     {
  80.         vec2 res = map( ro+rd*t );
  81.         if( res.x<0.0 || t>tmax ) break;
  82.         t += res.x;
  83.     }
  84.  
  85.     if( t>tmax ) return 1.0;
  86.     return 0.0;
  87. }
  88.  
  89. float calcOcclusionArlo( in vec3 pos, in vec3 nor )
  90. {
  91.     float occ = 0.0;
  92.     for( int i=0; i<8; i++ )
  93.     {
  94.         float h = 0.005 + 0.25*float(i)/7.0;
  95.         vec3 dir = normalize( sin( float(i)*73.4 + vec3(0.0,2.1,4.2) ));
  96.         dir = normalize( nor + dir );
  97.         occ += (h-map( pos + h*dir ).x);
  98.     }
  99.     return clamp( 1.0 - 9.0*occ/8.0, 0.0, 1.0 );    
  100. }
  101.  
  102. vec3 calcNormal( in vec3 pos )
  103. {
  104.     vec3 eps = vec3( 0.001, 0.0, 0.0 );
  105.     vec3 nor = vec3(
  106.         map(pos+eps.xyy).x - map(pos-eps.xyy).x,
  107.         map(pos+eps.yxy).x - map(pos-eps.yxy).x,
  108.         map(pos+eps.yyx).x - map(pos-eps.yyx).x );
  109.     return normalize(nor);
  110. }
  111.  
  112. vec3 render( in vec3 ro, in vec3 rd )
  113. {
  114.     vec3 col = vec3(0.7, 0.9, 1.0) + rd.y*vec3(0.8,0.8,0.8);
  115.     vec2 res = castRay(ro,rd, 0.0, 20.0);
  116.     float t = res.x;
  117.     float m = res.y;
  118.     if( m>-0.5 )
  119.     {
  120.         vec3  pos = ro + t*rd;
  121.         vec3  nor = calcNormal( pos );
  122.         vec3  ref = reflect( rd, nor );
  123.         float ang = 5.0*atan( pos.x, pos.z );
  124.         float coa = cos(ang);
  125.  
  126.              if( m< 0.5 ) col = vec3(1.00, 0.00, 0.00);
  127.         else if( m< 1.5 ) col = vec3(0.92, 0.03, 0.06);
  128.         else if( m< 2.5 ) col = vec3(0.84, 0.06, 0.12);
  129.         else if( m< 3.5 ) col = vec3(0.76, 0.09, 0.18);
  130.         else if( m< 4.5 ) col = vec3(0.68, 0.12, 0.24);
  131.         else if( m< 5.5 ) col = vec3(0.60, 0.15, 0.30);
  132.         else if( m< 6.5 ) col = vec3(0.52, 0.18, 0.36);
  133.         else if( m< 7.5 ) col = vec3(0.44, 0.21, 0.42);
  134.         else if( m< 8.5 ) col = vec3(0.36, 0.24, 0.48);
  135.         else if( m< 9.5 ) col = vec3(0.28, 0.27, 0.54);
  136.         else if( m<10.5 ) col = vec3(0.20, 0.30, 0.60);
  137.         else if( m<11.5 ) col = vec3(0.12, 0.33, 0.66);
  138.         else if( m<12.5 ) col = vec3(0.04, 0.36, 0.72);
  139.         else
  140.         {
  141.             if( mod( mod(floor(pos.x),2.0) + mod(floor(pos.z),2.0), 2.0 ) < 0.5 )
  142.             {
  143.                 col = vec3(0.4, 0.4, 0.4);
  144.             }
  145.             else
  146.             {
  147.                 col = vec3(0.5, 0.5, 0.5);
  148.                 vec2 uv = 256.0*0.1*pos.xz + vec2(37.0,17.0);
  149.                 col *= texture( iChannel0, (floor(uv)+ 0.5)/256.0, -100.0 ).y;
  150.             }
  151.  
  152.             if( max( abs(pos.x), abs(pos.z)) < 1.5 )
  153.             {
  154.                 vec2 q = mod(pos.xz+0.5,1.0)-0.5;
  155.                 col *= smoothstep( 0.0, 0.5, sqrt(q.x*q.x+q.y*q.y) );
  156.             }
  157.  
  158.             col *= pow(abs(tan(ang)*coa),2.0) + pow(abs(coa),2.0);
  159.         }
  160.        
  161.  
  162.         // lighitng        
  163.         vec3  lig = normalize( vec3(-0.6, 0.7, -0.5) );
  164.         float amb = 0.5+0.5*nor.y; if( amb<0.0 ) amb=0.0; else if( amb>1.0 ) amb=1.0;
  165.         float dif = dot( normalize(nor), lig ); if( dif<0.0 ) dif=0.0; else if( dif>1.0 ) dif=1.0;
  166.         float spe = pow(clamp( dot(normalize(ref), lig ), 0.0, 1.0 ),16.0);
  167.        
  168.         dif *= shadow( pos, lig, 0.01, 2.5 );
  169.  
  170.         vec3 lin = vec3(0.0, 0.0, 0.0);
  171.         lin += 1.20*dif*vec3(1.00,0.85,0.55);
  172.         lin += 0.50*amb*vec3(0.50,0.70,1.00);
  173.         col = col*lin;
  174.         col += 1.0*spe*dif*vec3(1.0,1.0,1.0);
  175.     }
  176.  
  177.     return col;
  178. }
  179.  
  180. vec3 cross_product( in vec3 a, in vec3 b )
  181. {
  182.     return vec3( a.y*b.z - a.z*b.y,
  183.                 -a.x*b.z + a.z*b.x,
  184.                  a.x*b.y - a.y*b.x );
  185. }
  186.  
  187. mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
  188. {
  189.     vec3 cw = normalize(ta-ro);
  190.     vec3 cp = vec3(sin(cr), cos(cr),0.0);
  191.     vec3 cu = normalize( cross_product(cw,cp) );
  192.     vec3 cv = normalize( cross_product(cu,cw) );
  193.     return mat3( cu, cv, cw );
  194. }
  195.  
  196. void mainImage( out vec4 fragColor, in vec2 fragCoord )
  197. {
  198.     vec2 q = fragCoord.xy/iResolution.xy;
  199.     vec2 p = -1.0+2.0*q;
  200.     p.x *= iResolution.x/iResolution.y;
  201.          
  202.     float time = 50.0;
  203.  
  204.     // camera  
  205.     vec3 ro = vec3( 3.0*cos(0.1*time), 2.0, 3.0*sin(0.1*time) );
  206.     vec3 ta = vec3( 0.0, -0.2, 0.0 );
  207.    
  208.     // camera-to-world transformation
  209.     mat3 ca = setCamera( ro, ta, 6.283185 );
  210.    
  211.     // ray direction
  212.     vec3 rd = ca * normalize( vec3(p.xy,2.0) );
  213.  
  214.     // render  
  215.     vec3 col = render( ro, rd );
  216.  
  217.     col = pow( col, vec3(0.5) );
  218.    
  219.     fragColor=vec4( col, 1.0 );
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement