shader things

This commit is contained in:
2025-07-15 23:05:45 +03:00
parent 3cba52f4ae
commit 5e431e5a85
5 changed files with 256 additions and 225 deletions

View File

@ -8,10 +8,9 @@ layout(location = 0) uniform float syncs[7];
layout(location = 8) uniform float fft_output[512]; // FFT_SIZE / 4
float u_time = syncs[0];
vec2 getUV(vec2 offset) {
vec2 uv = 2.0 * ((gl_FragCoord.xy + offset *0.5) / vec2(1920,1080) - 0.5);
uv.x *= 1920/1080;
return uv;
vec2 getUV() {
const vec2 scale = vec2(0.00104166667, 0.00185185185);
return gl_FragCoord.xy * scale - 1.0;
}
float noise(in vec2 xy, in float seed) {
@ -31,50 +30,59 @@ float sdHex(vec3 pos, float i, float angle) {
return d1;
}
float getScaledFFT(int index, float scale, float offset) {
// Clamp index to valid range
index = clamp(index, 0, 511);
// Get raw FFT value
float raw = fft_output[index];
// Apply logarithmic scaling: log(1 + value * scale) + offset
return log(1.0 + raw * scale) + offset;
}
// Modify your mapScene function
vec2 mapScene(in vec3 p) {
float mat = 0.;
float d = 1e9;
float a = 0.;
vec3 po = p;
vec3 rippleCenter = vec3(7.5, 0, 7.5);
vec2 rippleCenter = vec2(7.,7.);
float rippleSpeed = 4.0;
float rippleFreq = 1.0;
float rippleDecay = 0.25;
// Hexagonal grid
float counter = 0.;
float hexGap = 0.2;
for(float j = 0.; j < 15.; j++) {
po = p;
po += vec3(1.5 * 7.5, 0., 7.5);
po += vec3(0, 0., (1.88 + hexGap)*j);
for(float j = 0.; j < 16.; j++) {
vec3 po = p;
po += vec3((1.6 + hexGap) * 8, -5., -(1.88 + hexGap) * 10);
po += vec3(0, 0., (1.88 + hexGap) * j);
for(float i = 0.; i < 15.; i++) {
if( mod(i, 2.) == 0.) {
po -= vec3(1.6+hexGap, 0., 1.);
for(float i = 0.; i < 16.; i++) {
if(mod(i, 2.) == 0.) {
po -= vec3(1.6 + hexGap, 0., 1.);
} else {
po += vec3(-(1.6+hexGap), 0., 1.);
po += vec3(-(1.6 + hexGap), 0., 1.);
}
// Add individual hexagon ripples based on distance from center
float hexDist = length(vec2(i , j) - rippleCenter.xz);
float wave = sin(hexDist * rippleFreq - u_time * rippleSpeed) * exp(-hexDist * rippleDecay);
int hexDist = int(length(vec2(i, j) - rippleCenter.xy));
//float wave = sin(hexDist * rippleFreq - u_time * rippleSpeed) * exp(-hexDist * rippleDecay);
// Apply ripple to hexagon size and position
float hexSize = fft_output[int(i+1)*int(j+1)]*5.0; // sin(1.5*u_time)+ wave
// float hexSize = fft_output[int(i+1)*int(j+1)]*5.0; // sin(1.5*u_time)+ wave
//float hexSize = fft_output[hexDist] * 5.0;
float hexSize = getScaledFFT(hexDist, 15.0, 0.0) * 2.0; // Adjusted multiplier
a = sdHex(po, 1. + hexSize, 0.);
d = min(d, a);
if (d == a) {
mat = 4.;
if(d == a) {
mat = 1.;
}
counter += 1.;
}
}
@ -86,13 +94,13 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
float mat = 0.;
float hit = 0.;
// Reduced from 40 to 24 steps
for(int i = 0; i < 24; i++) {
for(int i = 0; i < 30; i++) {
pos = ro + rd * t;
vec2 res = mapScene(pos);
// Increase step size multiplier for faster marching
t += res.x * 1.2;
t += res.x;
mat = res.y;
if(t > 60.) { // Reduced max distance
if(t > 100.) { // Reduced max distance
break;
}
if(res.x < 0.001 * t) { // Less precise hit detection
@ -100,7 +108,8 @@ vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
break;
}
}
if(t > 60.) t = 0.;
if (t > 100.)
t = 0.;
return vec3(t, mat, hit);
}
@ -123,30 +132,61 @@ float softshadow(in vec3 ro, in vec3 rd, float mint, float maxt, float w) {
vec3 calcNormal(vec3 pos) {
vec2 e = vec2(.01, 0.);
vec3 n = vec3(mapScene(pos + e.xyy).x - mapScene(pos - e.xyy).x,
mapScene(pos + e.yxy).x - mapScene(pos - e.yxy).x,
mapScene(pos + e.yyx).x - mapScene(pos - e.yyx).x);
vec3 n = vec3(mapScene(pos + e.xyy).x - mapScene(pos - e.xyy).x, mapScene(pos + e.yxy).x - mapScene(pos - e.yxy).x, mapScene(pos + e.yyx).x - mapScene(pos - e.yyx).x);
return normalize(n);
}
vec3 fresnel(vec3 F0, vec3 h, vec3 l) {
return F0 + (1.0 - F0) * pow(clamp(1.0 - dot(h, l), 0.0, 1.0), 5.0);
vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPos, vec3 viewDir, vec3 normal, float roughness) {
// Light vector from surface to light
vec3 lightDir = lightPos - worldPos;
float lightDistance = length(lightDir);
lightDir = normalize(lightDir);
// Attenuation (quadratic falloff)
float attenuation = intensity / (1.0 + 0.09 * lightDistance + 0.032 * lightDistance * lightDistance);
// Diffuse lighting (Lambert)
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 diffuse = lightColor * NdotL * attenuation;
// 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;
}
vec3 addPointLight(vec3 light_pos, vec3 light_color, float shininess, vec3 v, vec3 dir, vec3 n, float occ) {
vec3 Ks = vec3(.4545);
vec3 Kd = vec3(1.);
vec3 ref = reflect(dir, n);
vec3 vl = normalize(v);
vec3 diffuse = Kd * vec3(max(0.0, dot(vl, n)));
vec3 specular = vec3(max(0.0, dot(vl, ref)));
vec3 F = fresnel(Ks, normalize(vl - dir), vl);
float shadow = softshadow(v + n * 0.054, light_pos, .01, 30., 8.);
/*vec3 addPointLight(vec3 lightPos, vec3 lightColor, float intensity, vec3 worldPos, vec3 viewDir, vec3 normal) {
vec3 lightDir = normalize(lightPos - worldPos);
float lightDistance = length(lightPos - worldPos);
//specular = pow(specular, vec3(shininess)) * occ;
return light_color * mix(diffuse, specular, F) * shadow;
// Attenuation
float attenuation = intensity / (1.0 + 0.1 * lightDistance + 0.01 * lightDistance * lightDistance);
// Diffuse
float NdotL = max(dot(normal, lightDir), 0.0);
// Specular (Blinn-Phong)
vec3 halfDir = normalize(lightDir - viewDir);
float NdotH = max(dot(normal, halfDir), 0.0);
float specular = pow(NdotH, 32.0);
// Shadow
float shadow = softshadow(worldPos + normal * 0.01, lightDir, 0.01, lightDistance, 8.0);
return lightColor * (NdotL + specular * 0.5) * attenuation * shadow;
}
*/
float getAmbientOcc(vec3 p, vec3 n) {
float occ = 0.;
@ -161,17 +201,16 @@ float getAmbientOcc(vec3 p, vec3 n) {
}
vec3 shading(vec3 v, vec3 n, vec3 dir, float material) {
float shininess = 0.1;
//float occ = getAmbientOcc(v, n);
float occ = 0.8;
float shininess = 0.01;
vec3 outMaterial = vec3(0.0, 0.0, 0.0);
if(material == 0.) {
outMaterial = vec3(0.8314, 0.2941, 0.2941);
shininess = 0.5;
shininess = 0.1;
} else if(material == 1.) {
outMaterial = vec3(0.6275, 0.1569, 0.9412);
shininess = 2.;
outMaterial = vec3(0.6196, 0.6118, 0.6118);
shininess = .7;
} else if(material == 2.) {
outMaterial = vec3(0.3255, 0.4784, 0.3255);
shininess = .2;
@ -180,25 +219,24 @@ vec3 shading(vec3 v, vec3 n, vec3 dir, float material) {
shininess = 1.0;
} else if(material == 4.) {
outMaterial = vec3(0.9961, 1.0, 0.9922);
shininess = .3;
}
else if(material == 5.) {
shininess = .1;
} else if(material == 5.) {
outMaterial = vec3(0.9961, 1.0, 0.9922);
shininess = .3;
}
vec3 lights = vec3(0.);
// lights += addPointLight(vec3(-2., 10., 0.), vec3(0.73, 0.73, 0.64), shininess, v, dir, n, occ);
lights += addPointLight(vec3(-2., 10., -5.), vec3(0.77, 0.26, 0.73) * 1., shininess, v, dir, n, occ);
lights += addPointLight(vec3( 20., 10.0, -5.0 ),vec3(0.08, 0.62, 0.75)*1., shininess, v, dir, n, occ);
lights += addPointLight(vec3(-10., 10.0, 0.), vec3(0.77, 0.26, 0.73), 3.0, v, dir, n, shininess);
lights += addPointLight(vec3(0., 10.0, -5.0), vec3(0.08, 0.62, 0.75), 3.0, v, dir, n, shininess);
lights += addPointLight(vec3(0., 25.0, 0.0), vec3(0.5137, 0.1961, 0.7725), 3.0, v, dir, n, shininess);
vec3 lightDir = vec3(0., 1., 6.);
vec3 lightDir = vec3(0., 1., -3);
//float sun_dif = clamp(dot(n, lightDir), 0., 1.);
//float shadow = softshadow(v + n * 0.01, lightDir, .01, 30., 18.);
//lights += vec3(0.6431, 0.7804, 0.8588) * sun_dif * shadow * occ;
float ind = clamp(dot(n, normalize(lightDir * vec3(.0, -1.0, -1.0))), 0.0, 1.0);
lights += vec3(0.1255, 0.1255, 0.1255) * ind;
float ind = clamp(dot(n, normalize(lightDir * vec3(.0, -1.0, -2.0))), 0.0, 1.0);
lights += vec3(0.08, 0.62, 0.75) * ind * 0.8;
return outMaterial * max(vec3(0.), lights);
}
@ -209,7 +247,7 @@ vec3 postProcess(vec3 col) {
//col += 0.075*clamp(vec3(0.5*random, 0.5*random2, 0.5*random), 0.02, 1.); // dither
// Normalized pixel coordinates (from 0 to 1)
vec2 screenCoord = getUV(vec2(0., 0.)); //gl_FragCoord.xy / u_resolution.xy;
vec2 screenCoord = getUV();
// Vignette
float radius = 0.8;
@ -234,73 +272,67 @@ vec3 postProcess(vec3 col) {
return col;
}
vec3 getCameraRayDir(vec2 uv, vec3 camPos, vec3 camTarget) {
// Calculate camera's "orthonormal basis", i.e. its transform matrix components
vec3 camForward = normalize(camTarget - camPos);
vec3 camRight = normalize(cross(vec3(.0, 1.0, 0.0), camForward));
vec3 camUp = normalize(cross(camForward, camRight));
float fov = 0.7;
vec3 vDir = normalize(uv.x * camRight + uv.y * camUp + camForward * fov);
return vDir;
}
vec3 getCameraRayDir2(vec2 uv, vec3 camPos, vec3 lookAt, float zoom) {
vec3 f = normalize(lookAt - camPos);
vec3 r = cross(vec3(0.0, 1.0, 0.0), f);
vec3 u = cross(f, r);
vec3 c = camPos + f * zoom;
vec3 i = c + uv.x * r + uv.y * u;
return normalize(i - camPos);
}
vec3 getCameraFov(vec2 uv, vec3 camPos, vec3 camTarget) {
vec3 getCameraRay(vec2 uv, vec3 camPos, vec3 camTarget, float fov) {
// Calculate camera's orthonormal basis
vec3 camForward = normalize(camTarget - camPos);
vec3 camRight = normalize(cross(vec3(0.0, 1.0, 0.0), camForward));
vec3 camUp = normalize(cross(camForward, camRight));
float fov = 1.7;
// Depth of field
float dof = .3;
vec2 h = vec2(noise(gl_FragCoord.xy, u_time * 0.1));
vec3 voff = sqrt(h.x) * (camRight * sin(h.y * 6.283) + camUp * cos(h.y * 6.283)) * dof;
voff -= camTarget;
float focusdistance = 50.;
return normalize(uv.x * camRight + uv.y * camUp + fov * camForward + voff * fov / focusdistance);
vec3 rayDir = normalize(uv.x * camRight + uv.y * camUp + camForward * fov);
return rayDir;
}
vec3 render(vec2 uv) {
// Camera positioning function
vec3 getCameraPosition(float time, int cameraMode) {
vec3 camPos;
camPos = vec3(0.0, 30.0, -10.0);
bool useDof = !true;
// camera
vec3 camTarget = vec3(15., 20., -20.); // Center point to orbit around
float orbitRadius = 20.0; // Distance from target
float orbitSpeed = 0.2; // Speed of orbit
float orbitHeight = 5.0; // Height above target
// Calculate orbiting position
float angle = 0. * orbitSpeed;
vec3 camPos = camTarget + vec3(
cos(angle) * orbitRadius,
orbitHeight + sin(0. * 0.8) * 2.0, // Optional vertical movement
sin(angle) * orbitRadius
);
vec3 rayDir;
// make orbit cam
if(useDof) {
rayDir = getCameraFov(uv, camPos, camTarget);
} else {
rayDir = getCameraRayDir2(uv, camPos, camTarget, 1.0);
if(cameraMode == 1) {
// Orbiting camera
vec3 camTarget = vec3(0.0, 0.0, -20.0);
float orbitRadius = 20.0;
float orbitSpeed = 0.2;
float orbitHeight = 10.0;
float angle = time * orbitSpeed;
camPos = camTarget + vec3(cos(angle) * orbitRadius, orbitHeight + sin(time * 0.8) * 2.0, sin(angle) * orbitRadius);
} else if(cameraMode == 2) {
// Smooth camera movement
float t = time * 0.06;
camPos = vec3(sin(t) * 15.0, 30.0 + cos(t * 0.5) * 5.0, cos(t) * 15.0);
} else if(cameraMode == 3) {
// First person style movement
float walkSpeed = 2.0;
camPos = vec3(sin(time * walkSpeed) * 0.1, 8.0 + sin(time * walkSpeed * 2.0) * 0.05, time * 0.5);
}
vec3 col = vec3(0.102, 0.2431, 0.3412);
return camPos;
}
vec3 hitPos = vec3(0.);
// Main camera function that combines everything
vec3 setupCamera(vec2 uv, float time, int positionMode) {
vec3 camPos = getCameraPosition(time, positionMode);
vec3 camTarget = vec3(0.0, -1.0, 10.0); // Adjust target as needed
float fov = 1.;
return getCameraRay(uv, camPos, camTarget, fov);
}
// Simplified version of your render function using the new camera system
vec3 render(vec2 uv) {
// Choose camera modes:
// Position: 0=static, 1=orbit, 2=smooth, 3=walk
// Ray: 0=standard, 1=zoom, 2=dof
int positionMode = 2; // Static
vec3 rayDir = setupCamera(uv, u_time, positionMode);
vec3 camPos = getCameraPosition(u_time, positionMode);
vec3 col = vec3(0.102, 0.2431, 0.3412);
vec3 hitPos = vec3(0);
vec3 t = castRay(camPos, rayDir, hitPos);
if (t.z == 1.) {
if(t.x > 0.0) {
vec3 nor = calcNormal(hitPos);
col = shading(hitPos, nor, rayDir, t.y);
}
@ -310,7 +342,7 @@ vec3 render(vec2 uv) {
void main() {
vec3 finalColor = render(getUV(vec2(0., 0.)));
vec3 finalColor = render(getUV());
//finalColor = postProcess(finalColor);