Advertisement
Guest User

Untitled

a guest
May 21st, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.98 KB | None | 0 0
  1. #version 330
  2.  
  3. // parameters
  4. uniform vec4 p;
  5. float t = p.x;
  6.  
  7. // sphere sponge
  8. float ss( vec3 pos )
  9. {
  10.     float k=1, d=-k;
  11.     for (int i=0;i<5;k*=2,i++)
  12.         d = max( d, (3.1 - length( mod(pos * k, 6) - vec3( 3 ) )) / k );
  13.     return d*.9;
  14. }
  15.  
  16. // mandelbox
  17. vec3 mb(vec3 w)
  18. {
  19.     float md = 2;
  20.     float d = md;
  21.     float mR2 = .2;
  22.     float mp;
  23.  
  24.     vec4 scaleFactor = vec4(2, 2, 2, abs(2)) / mR2;
  25.     vec4 p = vec4(w, 1), p0 = p;
  26.    
  27.     for (int i = 0; i < 6; i++)
  28.     {
  29.         md = min( md, d );
  30.         p.xyz = clamp(p.xyz, -1, 1) * 2 - p.xyz;
  31.         d = dot( p.xyz, p.xyz );
  32.         p = p * clamp(max(mR2 / d, mR2), 0, 1) * scaleFactor + p0 + mR2 / scaleFactor;
  33.     }
  34.    
  35.     // some originality in the distance calculation (?)
  36.     p0 = abs(p);
  37.     return vec3( (max(p0.x,max(p0.y,p0.z)) - 1) / p.w * .9, md, log( length(p) + 1 ) / 3 );
  38. }
  39.  
  40. // the distance estimate for the whole 3d scene
  41. vec3 dE( vec4 p )
  42. {
  43.     // dist is initially a worm sine thing intersected with a sphere sponge fractal
  44.     float dist = max( length(p.xz-vec2(0,sin(p.z/10)*2-2))-(sin(p.y/10)+1), ss( p.xyz ) );
  45.     float distWater = p.z+1.9; // distance to a plane
  46.     if (distWater < 0.1)
  47.     {
  48.         // evaluate the bump only if close enough
  49.         vec4 cst = vec4(7,2,22,5) * sin((p.x*0.011+0.5)+(p.y*0.0059+0.1));
  50.         vec4 s1 = sin( p.xyxy*cst+cst.ywxz*t/3 )/4;
  51.         distWater += s1.x*s1.y*s1.z*s1.w;
  52.     }
  53.     if (dist < p.w*4)
  54.     {
  55.         // evaluate the mandelbox only if close enough to add detail
  56.         dist = max( dist, mb( p.xyz-round(p.xyz/4)*4 ).x );
  57.     }
  58.     return dist < distWater ? vec3(dist,0,1) : vec3(distWater,0.5,0);
  59. }
  60.  
  61. // forward gradient at the position pos
  62. vec3 gn( vec4 pos, float signedDistanceAtPos )
  63. {
  64.     vec2 eps = vec2(pos.w,0);
  65.     return normalize(vec3(dE( pos+eps.xyyy ).x - signedDistanceAtPos,
  66.         dE( pos+eps.yxyy ).x - signedDistanceAtPos,
  67.         dE( pos+eps.yyxy ).x - signedDistanceAtPos));
  68. }
  69.  
  70. // noise func
  71. vec4 c4(vec4 x)
  72. {
  73.     vec4 y = (x - fract(x)) * 0.001;
  74.     return fract((y*1.172+0.5)*(y*5.911+0.1)*(y*7.352+0.9));
  75. }
  76.  
  77. float nd( vec3 p )
  78. {
  79.     vec3 q = floor( p + 0.5 );
  80.     vec3 u = sin( (p-q)*22/7 ) * 0.5 + 0.5;
  81.     vec3 v = vec3( 11, 59, 151 );
  82.     vec3 qv = q*v;
  83.     vec3 rv = qv+v;
  84.  
  85.     vec4 m = mix( c4( vec4( qv.x+qv.y+qv.z, rv.x+qv.y+qv.z, qv.x+rv.y+qv.z, rv.x+rv.y+qv.z ) ),
  86.                   c4( vec4( qv.x+qv.y+rv.z, rv.x+qv.y+rv.z, qv.x+rv.y+rv.z, rv.x+rv.y+rv.z ) ), u.z );
  87.  
  88.     vec2 o = mix( m.xy, m.zw, u.y );
  89.  
  90.     return mix(o.x,o.y,u.x);
  91. }
  92.  
  93. // 3d rotation
  94. mat3 mr( vec3 v, float f )
  95. {
  96.     float cosf = cos( f ), sinf = sin( f ), umcosf = 1-cosf;
  97.     float x=v.x,y=v.y,z=v.z;
  98.  
  99.     return mat3( cosf+umcosf*x*x, umcosf*x*y-sinf*z, umcosf*x*z+sinf*y,
  100.                 umcosf*x*y+sinf*z, cosf+umcosf*y*y, umcosf*y*z-sinf*x,
  101.                 umcosf*x*z-sinf*y, umcosf*y*z+sinf*x, cosf+umcosf*z*z );
  102. }
  103.  
  104. // shading
  105. vec3 sh( float material, vec4 rp, vec4 rd )
  106. {
  107.     vec3 resultColor;
  108.     vec3 direction = vec3(.7,-.7,0), vec = vec3(0,1,0);
  109.     vec3 lightDirection = mr( direction, t/20 ) * vec;
  110.     float lightVisi = clamp(sqrt(lightDirection.z*0.5+0.5)*2-.7,0,1);
  111.  
  112.     if (material > 0)
  113.     {
  114.         // sky
  115.         vec3 skycolor = vec3(0.1,0.2,0.4) + mix( vec3(.8,.6,.4), vec3(0.4,0.5,1), sqrt(abs(rd.z)) );
  116.         vec3 perlinPos = (rp.xyz - rd.xyz * (10+rp.z) / rd.z) / 5 + t / 4;
  117.         float perlinY=1, perlinX=0, cloud;
  118.  
  119.         for(int i=0;i<5;perlinY/=2,i++)
  120.             perlinX += nd( perlinPos * perlinY ) / perlinY;
  121.  
  122.         cloud = perlinX * perlinY * 2.6 - 1.6;
  123.  
  124.         resultColor = mix( skycolor + pow( max( dot( lightDirection, rd.xyz ), 0 ), 64 ), vec3(cloud), clamp(cloud,0,1) ) * (lightVisi*0.8+0.2) + vec3(0,0.1,0);
  125.     }
  126.     else
  127.     {
  128.         // fractal
  129.         vec3 inNormal = gn( rp, dE(rp).x );
  130.         vec3 ds = mb( vec3(rp.x,rp.y-(round(rp.y/2)*2),rp.z) ).yzy;
  131.         float lambertianplusphong = max( dot( lightDirection, inNormal ) , 0 ) +
  132.                                     pow( max( dot( reflect( lightDirection, inNormal ), rd.xyz ), 0 ), 4 );
  133.  
  134.         float ao = 1;
  135.         float eps = rp.w*3;
  136.         float k = 0.15 / eps;
  137.         float d = 2 * eps;
  138.  
  139.         for (int i=0; i<6; d+=eps, k/=2, i++)
  140.             ao -= ( d - dE(rp + vec4(inNormal,0) * d).x ) * k;
  141.  
  142.         resultColor = mix( vec3(1,0.2,0.2), vec3(1,1,0.2), sin(dot(ds,ds.yxz))*0.2+0.5 ) *
  143.                       mix( mix(.3,lambertianplusphong,lightVisi), clamp(ao, 0, 1), .6-lightVisi*.3 );
  144.     }
  145.  
  146.     return resultColor;
  147. }
  148.  
  149. // raymarching
  150.  
  151. vec4 rm( vec4 rp, vec4 rd )
  152. {
  153.     float reflected=0, t=0, hit=1, dot3=0.3;
  154.     vec4 dist=vec4(0);
  155.  
  156.     // raymarch
  157.     for (int i=0;abs(rp.x)<1.5 && t<5 && i<110;i++)
  158.     {
  159.         dist = vec4( dE( rp+=rd*(t-dist.w) ), t );
  160.  
  161.         if (rp.w > dist.x) // is the distance inferior to "cone radius" ?
  162.         {
  163.             if (dist.y > 0)
  164.             {
  165.                 // we hit the ocean, reflect the ray
  166.                 hit *= dist.y; // dist.y = amount of recursive reflections
  167.                 t = rp.w*2;
  168.                 dist.w = 0;
  169.                 rd.xyz = reflect( rd.xyz, gn( rp, dist.x ) );
  170.                 reflected = dot3;
  171.             }
  172.             else hit = 0; // we've hit the object
  173.            
  174.             if ( hit < dot3 ) t=6; // if we hit it's finished
  175.         }
  176.         t += dist.x;
  177.     }
  178.  
  179.     if (rd.z<0 && hit>dot3)
  180.     {
  181.         // raytrace the ocean...
  182.         rp -= rd * (1.9+rp.z) / rd.z;
  183.         // and reflect the ray for proper shading
  184.         rd.xyz = reflect( rd.xyz, gn( rp, dE(rp).x ) );
  185.         reflected = dot3;
  186.     }
  187.  
  188.     return vec4( mix( sh( hit, rp, rd ), vec3(0.4,0.6,0.8), reflected ), 0 );
  189. }
  190.  
  191. /** main */
  192.  
  193. void main()
  194. {
  195.     float part;
  196.     float beat = exp(p.y-t)/20;
  197.     float localt = sin( (modf( t/20, part )-0.5)*22/7 ) * 0.5 + 0.5;
  198.     part += 3;
  199.     vec2 screenUVs = (gl_FragCoord.xy-p.zw) / p.zw * vec2(1,p.w/p.z);
  200.     vec4 color=vec4(0),
  201.     randoms = (vec4( nd(vec3(part+1)), nd(vec3(part+11)), nd(vec3(part+111)), nd(vec3(part+1111)) ) - 0.5)*22/7,
  202.     randomsm1 = (vec4( nd(vec3(part)), nd(vec3(part+10)), nd(vec3(part+110)), nd(vec3(part+1110)) ) - 0.5)*22/7;
  203.  
  204.     if (abs(screenUVs.y) < 0.5)
  205.     {
  206.         // camera path is here, inside the call to raymarching
  207.         color = rm( vec4( .2+beat,t/4-13,-1.75-beat,0 ), vec4( mr( normalize(vec3(0,mix(randomsm1.xy,randoms.xy,localt))), mix(randomsm1.w,randoms.w,localt)*.7 ) * mr( vec3(0,0,1), screenUVs.x ) * mr( vec3(0,1,0), -screenUVs.y ) * normalize(vec3(-1,0,1)), 1/p.w ) );
  208.         color *= color;
  209.         color += beat*6;
  210.     }
  211.    
  212.     gl_FragColor = color;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement