Advertisement
Guest User

Untitled

a guest
May 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // first proper ray-marching shader
  2. // mostly cobbled together from youtube and blogs~
  3. //
  4. // proper phong shading - ambient, diffuse, and specular lighting
  5. // but no reflections or see-through objects *yet*
  6.  
  7. #version 410 core
  8.  
  9. uniform float fGlobalTime; // in seconds
  10. uniform vec2 v2Resolution; // viewport resolution (in pixels)
  11.  
  12. uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
  13. uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
  14. uniform sampler1D texFFTIntegrated; // this is continually increasing
  15.  
  16. layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything
  17.  
  18.  
  19. #define MAX_STEPS 100
  20. #define MAX_DIST 100.
  21. #define SURF_DIST .01
  22.  
  23. float saw(float d) {
  24.   return d - floor(d);
  25. }
  26.  
  27. vec4 GetDistAndColor(vec3 p) {
  28.   vec4 sphere = vec4(0, 1, 6, 1);
  29.   float dS = length(p-sphere.xyz)-sphere.w;
  30.   //dS += clamp(sin((p.x + p.y + .3*p.z+fGlobalTime)*9.5)*0.1, 0., 1.);
  31.   //dS += clamp(sin(fGlobalTime) * 0.5 + 0.5, 0., sphere.w*0.5);
  32.   //dS += clamp(sin(saw(fGlobalTime+p.z) * 0.5 + 0.5), 0., 1.)*.5;
  33.   dS += (sin(4.*(p.x + p.y*0.5 + fGlobalTime)) * 0.5 + 0.25 + cos(4.*(p.y + p.z*0.5 + fGlobalTime)) * 0.5 + 0.25);
  34.  
  35.   float dP = p.y;
  36.   // rollinrollin
  37.   dP += sin((p.x+1+fGlobalTime)*2)*0.2;
  38.   // make waves
  39.   dP += ((cos(p.x*0.08+fGlobalTime*0.6)*10) + (sin(p.x*0.13+fGlobalTime*1.33)*3)) * clamp(p.z * 0.005 - 0, 0, 100);
  40.  
  41.   // carve out space for sphere in middle
  42.   dP = max(dP, -(length(p-sphere.xyz)-sphere.w*2.4));
  43.  
  44.   float d = min(dS, dP);
  45.  
  46.   vec3 col = vec3(0.,0.,0.);
  47.   if (dS < dP) {
  48.     col = vec3(.2,.5,.9);
  49.   } else {
  50.     vec3 darkColour = vec3(.2,.2,.2);
  51.     vec3 lightColour = vec3(.8,.8,.8);
  52.     bool xIs = sin(p.x*3.25)<0;
  53.     bool zIs = sin(p.z*2)<0;
  54.    
  55.     if (xIs ^^ zIs) {
  56.       col = darkColour;
  57.     } else {
  58.       col = lightColour;
  59.     }
  60.   }
  61.  
  62.   return vec4(col, d);
  63. }
  64.  
  65. float GetDist(vec3 p) {
  66.   return GetDistAndColor(p).w;
  67. }
  68.  
  69. float RayMarch(vec3 ro, vec3 rd) {
  70.   float dO = 0.;
  71.   for (int i=0; i<MAX_STEPS; i++) {
  72.     vec3 p = ro+dO*rd;
  73.     float dS = GetDist(p);
  74.     dO += dS;
  75.     if (dS<SURF_DIST || MAX_DIST<dO)
  76.       break;
  77.   }
  78.   return dO;
  79. }
  80.  
  81. vec3 GetNormal(vec3 p) {
  82.   vec2 e = vec2(.01, 0);
  83.   float d = GetDist(p);
  84.   vec3 n = d-vec3(
  85.     GetDist(p-e.xyy),
  86.     GetDist(p-e.yxy),
  87.     GetDist(p-e.yyx)
  88.   );
  89.   return normalize(n);
  90. }
  91.  
  92. vec3 GetLight(vec3 p, vec3 eye) {
  93.   vec3 lightPos = vec3(0, 10, 6);
  94.   lightPos.xz += vec2(sin(fGlobalTime), cos(fGlobalTime))*15.;
  95.   vec3 l = normalize(lightPos-p);
  96.   vec3 n = GetNormal(p);
  97.  
  98.   // diffuse
  99.   float dif = clamp(dot(n, l), 0., 1.);
  100.   float d = RayMarch(p+n*SURF_DIST*3., l);
  101.   bool isShadowed = d < length(lightPos-p);
  102.   if (isShadowed) {
  103.     dif *= .1;
  104.   }
  105.  
  106.   // ambient light
  107.   dif = max(dif, 0.02);
  108.  
  109.   // start generating light colours
  110.   vec3 lightColor = normalize(vec3(0.2, 0.3, 0.6));
  111.   vec3 col = lightColor * dif * 0.8;
  112.  
  113.   // specular
  114.   if (!isShadowed) {
  115.     vec3 LightToP = normalize(p-lightPos);
  116.     vec3 PToEye = normalize(eye-p);
  117.  
  118.     vec3 reflectedDir = reflect(LightToP, n);
  119.     float spec = pow(max(dot(PToEye, reflectedDir), 0.), 32);
  120.     vec3 specColor = normalize(vec3(0.2, 0.1, 0.8));
  121.  
  122.     col += spec*specColor*7;
  123.   }
  124.  
  125.   return col;
  126. }
  127.  
  128. void main(void)
  129. {
  130.   vec2 uv = vec2(gl_FragCoord.x / v2Resolution.x, gl_FragCoord.y / v2Resolution.y);
  131.   uv -= 0.5;
  132.   uv /= vec2(v2Resolution.y / v2Resolution.x, 1);
  133.  
  134.   // eye origin and ray direction
  135.   vec3 ro = vec3(0, 1, 0);
  136.   vec3 rd = normalize(vec3(uv.x, uv.y, 1));
  137.  
  138.   // distance marched
  139.   float d = RayMarch(ro, rd);
  140.  
  141.   // calculate light and colour
  142.   vec3 col;
  143.  
  144.   // treat sky specially
  145.   if (MAX_DIST < d) {
  146.     //col = vec3(0.1,0.3,1) * sin(fGlobalTime*5+uv.x*20)*0.5+0.5+cos(fGlobalTime+uv.y*30)*0.5+0.5 * 0.00002;
  147.     col = vec3(.005,.002,.094);
  148.     col += vec3(.6,.2,.8) * clamp(sin(fGlobalTime*0.8 + uv.x*1.7) + cos(uv.y*2.4 + fGlobalTime*1.3) + sin(uv.x*4.6 + fGlobalTime) * abs(cos(uv.y*12 + fGlobalTime)), 0, 1);
  149.   } else {
  150.     // collision point
  151.     vec3 p = ro + rd * d;
  152.  
  153.     // object color
  154.     col = GetDistAndColor(p).rgb;
  155.    
  156.     // light colour
  157.     col *= GetLight(p, ro); // diffused lighting
  158.   }
  159.  
  160.   // correct gamma
  161.   col = pow(col, vec3(1./2.2));
  162.  
  163.   out_color = vec4(col, 1.0);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement