This commit is contained in:
2024-06-01 17:09:23 +03:00
parent 4c7c2e863b
commit 8c1fe50342
13 changed files with 1977 additions and 424 deletions

View File

@ -1,12 +1,17 @@
precision mediump float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
uniform sampler2D texture_sampler;
uniform sampler2D texts;
struct Ray {
vec3 rd;
vec3 dir;
vec3 rayDir;
vec3 rayOrigin;
vec3 hitPos;
float distance;
bool isHit;
};
mat2 scale(vec2 scale){
@ -622,52 +627,47 @@ float sdCog(vec3 pos, float angle) {
float sdHex(vec3 pos, float i, float angle) {
vec3 po = pos;
vec3 po = pos;
po.xz *= Rot(angle);
po.yz *= Rot(angle);
pR(po.yz, PI/2.);
float d1 = fHexagonCircumcircle(po, vec2(0.5+i, .1));
float d2 = fHexagonCircumcircle(po, vec2(0.2+i, .1));
return fOpDifferenceRound(d1,d2,0.1);
}
// Scene
vec2 mapScene(in vec3 p) {
/*
* Scene geometry
* returns vec2(distance, material.id)
*/
vec2 mapScene(in vec3 pos) {
float mat = 0.;
float d = 1e10;
//float dGround = p.y + 2.5;
//d = min(d, dGround);
vec3 po = p;
vec3 po = pos;
//po.y += sin(u_time);
// pMod3(po, vec3(3.));
po.xy *= scale(vec2(1.3, 1.3));
po.xz *= Rot(.745);
const float num = 6.;
const float num = 6.;
for (float i = 1.; i <= num; i++) {
// pos.z += i*.1;
float a = sdHex(po,i*0.35, u_time + abs( 2. + 0.4 * sin(u_time)) * i*3.1415/num);
d = min(d,a);
if (d == a) mat = 1. + mod(i,3.);
}
/*float c2 = sdCog(p, u_time);
}
/* float c2 = sdCog(pos, 0.);
d = min(d, c2);
if ( d == c2) mat = 4.;
/*
float c3 = fBox(pos+vec3(0.0, .0, 0.), vec3(1., 1.,1.));
d = min(d, c3);
if ( d == c3) mat = 4.;
*/
// float c3 = fBox(p+vec3(0.5, .87, 0.), vec3(1., 1.,1.));
// d = min(d, c3);
// if ( d == c1) mat = 1.;
//if ( d == c3) mat = 3.;
return vec2(d, mat);
}
@ -728,19 +728,6 @@ float softshadow( in vec3 ro, in vec3 rd, float mint, float maxt, float w )
return 0.25*(1.0+res)*(1.0+res)*(2.0-res);
}
float castShadow(vec3 ro, vec3 rd) {
float res = 1.0;
float t = 0.001;
for(int i = 0; i < 40; i++) {
float h = mapScene(ro + t* rd).x;
res = min(res, 10.0*h/t);
if (abs(h) < (0.001*t) ) break;
t += h;
if (t > 20.) break;
}
return clamp(res,0., 1.);
}
vec3 calcNormal(vec3 pos) {
vec2 e = vec2(.001, 0.);
vec3 n = vec3( mapScene(pos+e.xyy).x - mapScene(pos-e.xyy).x,
@ -759,8 +746,6 @@ vec3 eRot(vec3 p, vec3 ax, float ro) {
return mix(dot(ax,p)*ax, p, cos(ro)) + sin(ro)*cross(ax,p);
}
vec3 addPointLight(vec3 light_pos, vec3 light_color, float shininess, vec3 v, vec3 dir, vec3 n, float occ) {
vec3 Ks = vec3( .5454 );
vec3 Kd = vec3( 1. );
@ -770,8 +755,7 @@ vec3 addPointLight(vec3 light_pos, vec3 light_color, float shininess, vec3 v, ve
vec3 specular = vec3( max( 0.0, dot( vl, ref ) ) );
vec3 F = fresnel( Ks, normalize( vl - dir ), vl )*occ;
float shadow = softshadow(v+n*0.01,light_pos, .01, 30., 18.);
//float shadow = castShadow(v + n*0.02, light_pos);
specular = pow( specular, vec3( shininess ) )*occ;
specular = pow( specular, vec3( shininess ) )*occ;
return light_color * mix( diffuse, specular, F ) * shadow;
}
@ -848,55 +832,21 @@ vec3 postProcess(vec3 col) {
float constrast = .5;
col = mix(col, smoothstep(0.0, 1.0, col), constrast);
// Colour mapping
col *= vec3(1.0, 1.0, 1.0);
// gamma
col = pow( col, vec3(1.0/2.2) );
col = pow( col, vec3(1.0/2.2) ); // gamma
// fade in at the beginning
//col*=vec3(clamp((u_time-1.8)*0.5,0., 1.));
// fade out at the end
// col*=vec3(clamp((120.-u_time)*.35, 0., 1.));
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 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 = .25;
vec2 h = vec2( noise(gl_FragCoord.xy, .13), noise(gl_FragCoord.xy, .4));
//vec3 h= rnd23(gl_FragCoord.xy);
vec3 voff = sqrt(h.x)*(camRight*sin(h.y*6.283)+camUp*cos(h.y*6.283))*dof;
// camTarget -=voff;
float focusdistance = 150.2;
return normalize(uv.x * camRight + uv.y * camUp + fov * camForward + voff * fov/focusdistance);
mat3 getCamera(vec3 camPosition, vec3 lookAt) {
vec3 camF = normalize(lookAt - camPosition);
vec3 camR = normalize(cross(vec3(0., 1., 0.), camF));
vec3 camU = cross(camF, camR);
return mat3(camR, camU, camF);
}
vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float b ) {
@ -908,31 +858,24 @@ vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float b ) {
return mix( col, fogColor, fogAmount );
}
/**
* renders the scene
* @param uv uv coordinate from gl_fragCoord
*/
vec3 render(vec2 uv) {
bool useDof = !true;
//vec2 uv = (2.0 * gl_FragCoord.xy - u_resolution.xy) / u_resolution.y;
float angle = -2.4 +u_time*0.4;
//angle = 0.;
// camera
vec3 camPos = vec3(0., 3., -4.);
vec3 camTarget = vec3(0., 0., 0.);
vec3 rayDir;
if (useDof) {
rayDir = getCameraFov(uv, camPos, camTarget);
} else {
rayDir = getCameraRayDir2(uv, camPos, camTarget, 1.0);
}
vec3 col = vec3(0.051, 0.0667, 0.1529);
//vec3 col = vec3(0.0314, 0.0118, 0.1255) + rayDir.y * 0.4;
// camera
vec3 camPos = vec3( sin(u_time)*2., 0., 4.);
vec3 lookAt = vec3(0., 0., 0.);
vec3 rayDir = getCamera(camPos, lookAt) * normalize(vec3(uv, 1.2));
vec3 hitPos = vec3(0.);
vec3 t = castRay(camPos, rayDir, hitPos);
vec3 rd = rayDir;
vec3 col = vec3(0.051, 0.0667, 0.1529);
if (t.z > 0.) {
vec3 nor = calcNormal(hitPos);
col = shading(hitPos, nor, rayDir , t.y);
@ -948,15 +891,15 @@ vec3 render(vec2 uv) {
nor = calcNormal(hitPos);
col += 0.1 * shading(hitPos, nor, rayDir , t2.y);
/* rayDir = normalize(reflect(rayDir, nor));
/* rayDir = normalize(reflect(rayDir, nor));
rayOrigin = hitPos + (rayDir * 0.01);
vec3 t3 = castReflectedRay(rayOrigin, rayDir, hitPos);
if (t3.z > 0.) {
hitPos = rayOrigin + rayDir * t3.x;
nor = calcNormal(hitPos);
col += 0.025 * shading(hitPos, nor, rayDir , t3.y);
} */
col += 0.05 * shading(hitPos, nor, rayDir , t3.y);
} */
}
}
// pixelColor*exp(-distance*b) + fogColor*(1.0-exp(-distance*b));
@ -973,9 +916,10 @@ void main()
{
vec3 finalColor = vec3(0.);
/*
const float AA_SIZE = 1.;
float count = 0.0;
/*
for (float aaY = 0.0; aaY < AA_SIZE; aaY++) {
for (float aaX = 0.0; aaX < AA_SIZE; aaX++) {
finalColor += render(getUV(vec2( aaX, aaY)));
@ -983,10 +927,17 @@ void main()
}
}
finalColor /= count; */
finalColor += render(getUV(vec2( 0.,0.)));
finalColor = postProcess(finalColor);
finalColor += render(getUV(vec2( 0.,0.)));
finalColor = postProcess(finalColor);
// fade in at the beginning
// finalColor*=vec3(clamp((u_time-.5)*0.35,0., 1.));
// fade out at the end
// finalColor*=vec3(clamp((120.-u_time)*.35, 0., 1.));
gl_FragColor = vec4(finalColor, 1.);
}