diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index 504afeb..9a348d5 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -8,11 +8,12 @@ layout(location = 0) uniform float syncs[11]; layout(location = 20) uniform float fft_output[512]; // FFT_SIZE / 4 // float u_time = syncs[0]; -vec3 palette(float t){ +// paletti muunnos: arg 0.75 --> 0. +vec3 palette(float t, float arg){ vec3 a=vec3(0.39,0.56,0.47); vec3 b=vec3(0.54,0.56,0.51); vec3 c=vec3(0.45,0.79,0.42); - vec3 d=vec3(0.08,0.37,0.75); // tästä viimenen floatti siirrettävä 0, kun tehdään paletti switch + vec3 d=vec3(0.08,0.37,arg); // t�st� viimenen floatti siirrett�v� 0, kun tehd��n paletti switch return a+b*cos(6.28318*(c*t+d)); } @@ -85,10 +86,6 @@ float hexDistance(vec2 axial) { return max(abs(q), max(abs(r), abs(s))); } -float sdSphere(vec3 p, float r) { - return le(p) - r; -} - float hexPylon(vec3 p, vec2 h) { //vec3 p = vec3(p.x, p.z, p2.y); vec3 b = vec3(h.x, h.y, h.x); @@ -110,13 +107,6 @@ vec2 opU(vec2 d1, vec2 d2) { return (d1.x < d2.x) ? d1 : d2; } -float noyce(vec2 axial) { - if(mod(floor(syncs[0]), 2.) == 0.) { - return mix(noise(axial, 0.1), noise(axial, 0.2), sin(syncs[0] * 2.)); - } - return mix(noise(axial, 0.2), noise(axial, 0.1), sin(syncs[0] * 2.)); -} - vec2 mapScene(in vec3 p) { float res = p.y; @@ -129,10 +119,8 @@ vec2 mapScene(in vec3 p) { float distFromCenter = hexDistance(hex.axial); int fftIndex = int(cl(distFromCenter + 1.0, 0.0, 511.0)); float fftVal = fft_output[fftIndex]; - float noise = noyce(hex.axial); - - // float hexHeight = 1.0 + fftVal * 5.0 + noise; - float hexHeight = 1.0 + noise; + float noise = mix(noise(hex.axial+1., 0.1), noise(hex.axial+1., 0.2), sin(syncs[0] * 4.)); + float hexHeight = clamp(1.0 + fftVal * 5.0 + noise, 0., 15.);; // Rotate individual hex tiles if needed vec3 r = hex.local; @@ -158,7 +146,7 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { vec2 res; // Raymarching - for(int i = 0; i < 20; i++) { + for(int i = 0; i < 40; i++) { pos = ro + rd * t; res = mapScene(pos); // Get distance to objects, x = dist, y = material mat = res.y; @@ -180,21 +168,26 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { //////////////// // SHADING // //////////////// +vec3 phongLighting(vec3 p, vec3 normal, vec3 lightPos, vec3 viewPos, vec3 lightColor, vec3 objectColor) { + vec3 lightDir = normalize(lightPos - p); + vec3 viewDir = normalize(viewPos - p); + vec3 reflectDir = reflect(-lightDir, normal); -float softshadow(in vec3 ro, in vec3 rd, float mint, float maxt, float w) { - float res = 1.0; - float t = mint; - for(int i = 0; i < 6; i++) { - if(t > maxt) - break; - float h = mapScene(ro + t * rd).x; - res = min(res, h / (w * t)); - t += cl(h, 0.1, 0.80); - if(res < -1.0) - break; - } - res = max(res, -1.0); - return 0.25 * (1.0 + res) * (1.0 + res) * (2.0 - res); + // Ambient + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * lightColor; + + // Diffuse + float diff = max(dot(normal, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + // Specular + float specularStrength = 11.5; + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // shininess + vec3 specular = specularStrength * spec * lightColor; + + // Combine + return (ambient + diffuse + specular) * objectColor; } vec3 calcNormal(vec3 pos) { @@ -203,6 +196,14 @@ vec3 calcNormal(vec3 pos) { return no(n); } +vec3 pal(float color) { + vec3 c = palette(color, 0.75); + vec3 c2 = palette(color, 0.0); + + float t = clamp((syncs[0] - 12.0) / 2.0, 0.0, 1.0); // Smoothly ramps from 0 to 1 after 12s + return mix(c, c2, t); // Blend between c and c2 over ~2 seconds +} + vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float fogAmount) { float syncsBass = cl((syncs[1] + syncs[2] + syncs[3]), 0., 1.); @@ -215,30 +216,18 @@ vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float fogAmount) { return mix(col, fogColor, fogAmount2); } -vec3 shading(vec3 p, vec3 n, vec3 dir, float material) { - float shininess = 0.0; - vec3 outMaterial = vec3(0.); +vec3 shading(vec3 p, vec3 n, vec3 dir, vec3 camPos) { + vec3 outMaterial = vec3(0.0); - if(material == 0.) { - outMaterial = palette(p.y * 0.1); - if(syncs[0] > 5.) { - outMaterial = vec3(0.5); - } - outMaterial = mix(palette(p.y * 0.1), vec3(0.5), abs(sin(syncs[0] * 0.2))); - shininess = 0.6; - } - - if(material == 1.) { - outMaterial = vec3(0.5); - shininess = 0.6; - } + outMaterial = pal(p.y*p.y*0.01); vec3 lights = vec3(0.); + lights += phongLighting(p, n, camPos, dir, vec3(0.51), outMaterial); vec3 lightDir = vec3(0., 2., 3); float ind = cl(dot(n, no(lightDir * vec3(.0, 1.0, -2.0))), 0.0, 1.0); - lights += vec3(0.08, 0.62, 0.75) * ind * 0.8; + //lights += vec3(0.08, 0.62, 0.75) * ind * 0.8; outMaterial *= max(vec3(0.), lights); // output with lights; @@ -289,11 +278,11 @@ vec3 render(vec2 uv) { if(t.x > 0.0) { vec3 nor = calcNormal(hitPos); - col = shading(hitPos, nor, rayDir, t.y); + col = shading(hitPos, nor, rayDir, camPos); } //glow from the bottom - vec3 bGlowColor = palette(syncs[0] * .075); // color change + vec3 bGlowColor = pal(syncs[0] * .075); // color change float bGlowDistance = 0.3; vec3 p = camPos + t.x * rayDir;