From 3f71dd30e0127a1f3ef293817e80744ae7ede969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20J=C3=A4rvisalo?= Date: Fri, 1 Aug 2025 11:11:30 +0300 Subject: [PATCH 1/2] =?UTF-8?q?paletti=20+=20post=20process=20s=C3=A4?= =?UTF-8?q?=C3=A4t=C3=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/shaders/fragment.frag | 46 +++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index 9a348d5..dc708ab 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -8,15 +8,24 @@ layout(location = 0) uniform float syncs[11]; layout(location = 20) uniform float fft_output[512]; // FFT_SIZE / 4 // float u_time = syncs[0]; -// 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,arg); // t�st� viimenen floatti siirrett�v� 0, kun tehd��n paletti switch +// paletti muunnos: arg 0.8 --> 0.5 +vec3 palette(float t, float arg, float arg2){ + vec3 a=vec3(0.52,0.56,0.47); + vec3 b=vec3(0.62,0.56,0.51); + vec3 c=vec3(0.43,0.79,0.42); + vec3 d=vec3(arg,0.42,arg2); // t�st� viimenen floatti siirrett�v� 0, kun tehd��n paletti switch return a+b*cos(6.28318*(c*t+d)); } +/* +vec3 palette(float t){ + vec3 a=vec3(0.52,0.56,0.47); + vec3 b=vec3(0.62,0.56,0.51); + vec3 c=vec3(0.43,0.79,0.42); + vec3 d=vec3(0,0.42,0.63); + return a+b*cos(6.28318*(c*t+d)); +} +*/ vec2 getUV() { const vec2 scale = vec2(0.00104166667, 0.00185185185); return gl_FragCoord.xy * scale - 1.0; @@ -128,7 +137,7 @@ vec2 mapScene(in vec3 p) { r.xz *= rot2D(0.5); //float d1 = hexPylon(vec3(r.x, (r.y + hexHeight / 2), r.z), vec2(hexRadius, hexHeight / 2)); - float d1 = hexPylon(vec3(r.x, r.y, r.z), vec2(hexRadius, hexHeight)); + float d1 = hexPylon(vec3(r.x, r.y, r.z), vec2(hexRadius, hexHeight + 5)); res = (d1 < res) ? d1 : res; @@ -182,7 +191,7 @@ vec3 phongLighting(vec3 p, vec3 normal, vec3 lightPos, vec3 viewPos, vec3 lightC vec3 diffuse = diff * lightColor; // Specular - float specularStrength = 11.5; + float specularStrength = 13.5; float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // shininess vec3 specular = specularStrength * spec * lightColor; @@ -197,11 +206,15 @@ vec3 calcNormal(vec3 pos) { } 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 + color *= 0.2; + + vec3 c = palette(color, 0.25, 0.63); + vec3 c2 = palette(color, 0.5 ,0.2); + + float t = clamp((syncs[0] - 129.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 + //return c2; } vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float fogAmount) { @@ -222,7 +235,8 @@ vec3 shading(vec3 p, vec3 n, vec3 dir, vec3 camPos) { outMaterial = pal(p.y*p.y*0.01); vec3 lights = vec3(0.); - lights += phongLighting(p, n, camPos, dir, vec3(0.51), outMaterial); + //lights += phongLighting(p, n, camPos, dir, vec3(0.51), outMaterial); + lights += phongLighting(p, n, vec3(4., 4., -4.), dir, vec3(2.51), outMaterial); vec3 lightDir = vec3(0., 2., 3); @@ -236,15 +250,15 @@ vec3 shading(vec3 p, vec3 n, vec3 dir, vec3 camPos) { vec3 postProcess(vec3 col) { // Contrast - float contrast = 0.75; + float contrast = 0.85; col = mix(col, smoothstep(0.0, 1.0, col), contrast); // Colour mapping - // col *= vec3(1.0, 1.0, 1.0); + //col *= vec3(1.0, 1.0, 1.0); // Gamma - col = pow(col, vec3(0.4545)); // gamma 2.2 + col = pow(col, vec3(.55)); // gamma 2.2 // fade in at the beginning //col*=vec3(clamp((u_time-1.8)*0.5,0., 1.)); @@ -283,7 +297,7 @@ vec3 render(vec2 uv) { //glow from the bottom vec3 bGlowColor = pal(syncs[0] * .075); // color change - float bGlowDistance = 0.3; + float bGlowDistance = 0.8; vec3 p = camPos + t.x * rayDir; vec3 bGlowLevel = bGlowColor * exp(-(p.y + 0.0) / bGlowDistance) * 9900.; From 3a9737ce18e3d28b7b0ec29165fe06411d8aa494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Teemu=20J=C3=A4rvisalo?= Date: Fri, 1 Aug 2025 13:26:19 +0300 Subject: [PATCH 2/2] uudet valot --- src/shaders/fragment.frag | 111 +++++++++++++++++++++++++++++++------- 1 file changed, 93 insertions(+), 18 deletions(-) diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index dc708ab..463b8e8 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -116,6 +116,10 @@ vec2 opU(vec2 d1, vec2 d2) { return (d1.x < d2.x) ? d1 : d2; } +float sdSphere(vec3 p, float r){ + return length(p) -r; +} + vec2 mapScene(in vec3 p) { float res = p.y; @@ -138,9 +142,33 @@ vec2 mapScene(in vec3 p) { //float d1 = hexPylon(vec3(r.x, (r.y + hexHeight / 2), r.z), vec2(hexRadius, hexHeight / 2)); float d1 = hexPylon(vec3(r.x, r.y, r.z), vec2(hexRadius, hexHeight + 5)); - res = (d1 < res) ? d1 : res; + + // DEBUGGING --- LIGHT CHANGING WITH CIRCLE RADIUS + float maxRadius = 15.; // Circle radius + float particleHeight = 5. + (syncs[5] * 40.); + float particlePos = (syncs[0] * 0.5) + syncs[5]; + + // Map param to angle + float particleStartPos = particlePos * 2.0 * PI; + + // Direction from center to initial circle position (in XY plane) + vec3 particleDir = normalize(vec3(cos(particleStartPos), 0.0, sin(particleStartPos))); // XZ direction + + float rad = max(maxRadius - syncs[5], maxRadius); + + vec3 particleOffset = vec3(0. , particleHeight, 0.); // particle offset + + // Final object position = center (offset) + radial movement + vec3 center = particleOffset; // Circle center + vec3 objPos = center + particleDir * rad; // Object slides inward + //res = opU(res, vec2(sdSphere(p - objPos, sphereRadius), 1.)); + d1 = sdSphere(p - objPos, 1.); + //res = opU(res, vec2(sdSphere(p - objPos, 0.4), 1.)); + //res = (d1 < res) ? d1 : res; + + return vec2(res, mat); } @@ -177,26 +205,51 @@ 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 += clamp(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; +vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPos, vec3 viewDir, vec3 normal) { + // Light vector from surface to light + float roughness = 1.0; + vec3 lightDir = lightPos - worldPos; + float lightDistance = length(lightDir); + lightDir = normalize(lightDir); - // Diffuse - float diff = max(dot(normal, lightDir), 0.0); - vec3 diffuse = diff * lightColor; + // Attenuation (quadratic falloff) + float attenuation = intensity / (1.0 + 0.09 * lightDistance + 0.032 * lightDistance * lightDistance); - // Specular - float specularStrength = 13.5; - float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0); // shininess - vec3 specular = specularStrength * spec * lightColor; + // Diffuse lighting (Lambert) + float NdotL = max(dot(normal, lightDir), 0.0); + vec3 diffuse = lightColor * NdotL * attenuation; - // Combine - return (ambient + diffuse + specular) * objectColor; + // Specular lighting (Blinn-Phong) + vec3 halfDir = normalize(lightDir + (-viewDir)); + float NdotH = max(dot(normal, halfDir), 0.0); + float shininess = mix(128.0, 8.0, roughness); // Convert roughness to shininess + vec3 specular = lightColor * pow(NdotH, shininess) * attenuation; + + // Fresnel effect + vec3 F0 = vec3(0.04); // Base reflectance for dielectrics + vec3 fresnel = F0 + (1.0 - F0) * pow(clamp(1.0 - max(dot(halfDir, lightDir), 0.0), 0.0, 1.0), 5.0); + + // Soft shadows + float shadow = softshadow(worldPos + normal * 0.01, lightDir, 0.02, lightDistance, 4.0); + + // Combine diffuse and specular with shadow + return (diffuse + specular * fresnel) * shadow * shadow; } vec3 calcNormal(vec3 pos) { @@ -236,7 +289,29 @@ vec3 shading(vec3 p, vec3 n, vec3 dir, vec3 camPos) { vec3 lights = vec3(0.); //lights += phongLighting(p, n, camPos, dir, vec3(0.51), outMaterial); - lights += phongLighting(p, n, vec3(4., 4., -4.), dir, vec3(2.51), outMaterial); + lights += addPointLight(vec3(0., 20.0, 0.), vec3(0.77, 0.26, 0.73), 30.0, p, dir, n); + + // LIGHT CHANGING WITH CIRCLE RADIUS + float maxRadius = 25.; // Circle radius + float particleHeight = 15.; //(syncs[5] * 40.); + float particlePos = (syncs[0] * 0.5) + syncs[5]; + + // Map param to angle + float particleStartPos = particlePos * 2.0 * PI; + + // Direction from center to initial circle position (in XY plane) + vec3 particleDir = normalize(vec3(cos(particleStartPos), 0.0, sin(particleStartPos))); // XZ direction + + float r = max(maxRadius - 0.0, maxRadius); + + vec3 particleOffset = vec3(0. , particleHeight, 0.); // particle offset + + // Final object position = center (offset) + radial movement + vec3 center = particleOffset; // Circle center + vec3 objPos = center + particleDir * r; // Object slides inward + //res = opU(res, vec2(sdSphere(p - objPos, sphereRadius), 1.)); + + lights += addPointLight(objPos, vec3(0.33, 0.91, 0.93), 30.0, p, dir, n); vec3 lightDir = vec3(0., 2., 3);