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);