diff --git a/src/shaders/fragment.frag b/src/shaders/fragment.frag index cc1f7e8..4999d37 100644 --- a/src/shaders/fragment.frag +++ b/src/shaders/fragment.frag @@ -13,19 +13,10 @@ 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 + vec3 d=vec3(arg,0.42,arg2); 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; @@ -116,41 +107,12 @@ vec2 opU(vec2 d1, vec2 d2) { return (d1.x < d2.x) ? d1 : d2; } -#define PI 3.14159265 -#define TAU 6.2831853 -#define HEX_SIZE 1.0 -#define PYLON_HEIGHT 4.0 - -// Rotate 2D vector -vec2 rotate(vec2 p, float a) { - float s = sin(a), c = cos(a); - return vec2(c*p.x - s*p.y, s*p.x + c*p.y); -} - -// Hexagonal tiling -vec2 hex(vec2 p) { - vec2 q = vec2( - p.x * 2.0 / 3.0, - (-p.x + sqrt(3.0) * p.y) / 3.0 - ); - return q; -} - -// Signed distance function for pylon -float pylon(vec3 p) { - float radius = 0.5; - float d = length(p.xz) - radius; - d = max(d, -p.y); - d = max(d, p.y - PYLON_HEIGHT); - return d; +float sdSphere(vec3 p, float r){ + return length(p) -r; } // Scene mapping with occlusion-aware SDF blending vec2 mapScene(vec3 p) { - - //vec2 h = hex(p.xz / HEX_SIZE); - //vec2 id = floor(h); - //vec2 f = fract(h); float dist = 20.; int repeat = 0; if((length(p.xz)) < 2*dist){ @@ -163,7 +125,6 @@ vec2 mapScene(vec3 p) { repeat = 5; } - //int repeat = 2; // mitigate neighbor occlusion with anti-bleed blending float minDist = 1e9; for (int dx = -repeat; dx <= repeat; ++dx) { @@ -187,35 +148,6 @@ vec2 mapScene(vec3 p) { return vec2(minDist,0.); } - -//vec2 mapScene(in vec3 p) { -// -// float res = p.y; -// float mat = 0.; -// -// float hexRadius = 0.83; -// vec3 hexpos = vec3(p.x, p.y - 2.5, p.z); -// HexData hex = hexTile(hexpos, 1.1); -// -// float distFromCenter = hexDistance(hex.axial); -// int fftIndex = int(cl(distFromCenter + 1.0, 0.0, 511.0)); -// float fftVal = fft_output[fftIndex]; -// 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; -// // r.yz *= rot2D(PI * 0.5); -// 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)); -// -// res = (d1 < res) ? d1 : res; -// -// return vec2(res, mat); -//} - //////////////// // RAYCAST // //////////////// @@ -263,55 +195,54 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { return vec3(t, mat, hit); } -//vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) { -// float mat = 0.; -// float hit = 0.; -// float t = 0.; -// vec2 res; -// -// // Raymarching -// for(int i = 0; i < 50; i++) { -// pos = ro + rd * t; -// res = mapScene(pos); // Get distance to objects, x = dist, y = material -// mat = res.y; -// t += res.x; // "march" the ray -// -// // if(abs(t) < tolerance * (t * 0.0125 + 1.0)) { -// if(abs(res.x) < 0.001) { -// hit = 1.; -// break; -// } -// if(t > 300) -// break; -// -// } -// -// return vec3(t, mat, hit); -//} - //////////////// // 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) { @@ -351,7 +282,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);