Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // first proper ray-marching shader
- // mostly cobbled together from youtube and blogs~
- //
- // proper phong shading - ambient, diffuse, and specular lighting
- // but no reflections or see-through objects *yet*
- #version 410 core
- uniform float fGlobalTime; // in seconds
- uniform vec2 v2Resolution; // viewport resolution (in pixels)
- uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
- uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
- uniform sampler1D texFFTIntegrated; // this is continually increasing
- layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything
- #define MAX_STEPS 100
- #define MAX_DIST 100.
- #define SURF_DIST .01
- float saw(float d) {
- return d - floor(d);
- }
- vec4 GetDistAndColor(vec3 p) {
- vec4 sphere = vec4(0, 1, 6, 1);
- float dS = length(p-sphere.xyz)-sphere.w;
- //dS += clamp(sin((p.x + p.y + .3*p.z+fGlobalTime)*9.5)*0.1, 0., 1.);
- //dS += clamp(sin(fGlobalTime) * 0.5 + 0.5, 0., sphere.w*0.5);
- //dS += clamp(sin(saw(fGlobalTime+p.z) * 0.5 + 0.5), 0., 1.)*.5;
- 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);
- float dP = p.y;
- // rollinrollin
- dP += sin((p.x+1+fGlobalTime)*2)*0.2;
- // make waves
- 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);
- // carve out space for sphere in middle
- dP = max(dP, -(length(p-sphere.xyz)-sphere.w*2.4));
- float d = min(dS, dP);
- vec3 col = vec3(0.,0.,0.);
- if (dS < dP) {
- col = vec3(.2,.5,.9);
- } else {
- vec3 darkColour = vec3(.2,.2,.2);
- vec3 lightColour = vec3(.8,.8,.8);
- bool xIs = sin(p.x*3.25)<0;
- bool zIs = sin(p.z*2)<0;
- if (xIs ^^ zIs) {
- col = darkColour;
- } else {
- col = lightColour;
- }
- }
- return vec4(col, d);
- }
- float GetDist(vec3 p) {
- return GetDistAndColor(p).w;
- }
- float RayMarch(vec3 ro, vec3 rd) {
- float dO = 0.;
- for (int i=0; i<MAX_STEPS; i++) {
- vec3 p = ro+dO*rd;
- float dS = GetDist(p);
- dO += dS;
- if (dS<SURF_DIST || MAX_DIST<dO)
- break;
- }
- return dO;
- }
- vec3 GetNormal(vec3 p) {
- vec2 e = vec2(.01, 0);
- float d = GetDist(p);
- vec3 n = d-vec3(
- GetDist(p-e.xyy),
- GetDist(p-e.yxy),
- GetDist(p-e.yyx)
- );
- return normalize(n);
- }
- vec3 GetLight(vec3 p, vec3 eye) {
- vec3 lightPos = vec3(0, 10, 6);
- lightPos.xz += vec2(sin(fGlobalTime), cos(fGlobalTime))*15.;
- vec3 l = normalize(lightPos-p);
- vec3 n = GetNormal(p);
- // diffuse
- float dif = clamp(dot(n, l), 0., 1.);
- float d = RayMarch(p+n*SURF_DIST*3., l);
- bool isShadowed = d < length(lightPos-p);
- if (isShadowed) {
- dif *= .1;
- }
- // ambient light
- dif = max(dif, 0.02);
- // start generating light colours
- vec3 lightColor = normalize(vec3(0.2, 0.3, 0.6));
- vec3 col = lightColor * dif * 0.8;
- // specular
- if (!isShadowed) {
- vec3 LightToP = normalize(p-lightPos);
- vec3 PToEye = normalize(eye-p);
- vec3 reflectedDir = reflect(LightToP, n);
- float spec = pow(max(dot(PToEye, reflectedDir), 0.), 32);
- vec3 specColor = normalize(vec3(0.2, 0.1, 0.8));
- col += spec*specColor*7;
- }
- return col;
- }
- void main(void)
- {
- vec2 uv = vec2(gl_FragCoord.x / v2Resolution.x, gl_FragCoord.y / v2Resolution.y);
- uv -= 0.5;
- uv /= vec2(v2Resolution.y / v2Resolution.x, 1);
- // eye origin and ray direction
- vec3 ro = vec3(0, 1, 0);
- vec3 rd = normalize(vec3(uv.x, uv.y, 1));
- // distance marched
- float d = RayMarch(ro, rd);
- // calculate light and colour
- vec3 col;
- // treat sky specially
- if (MAX_DIST < d) {
- //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;
- col = vec3(.005,.002,.094);
- 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);
- } else {
- // collision point
- vec3 p = ro + rd * d;
- // object color
- col = GetDistAndColor(p).rgb;
- // light colour
- col *= GetLight(p, ro); // diffused lighting
- }
- // correct gamma
- col = pow(col, vec3(1./2.2));
- out_color = vec4(col, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement