optimoitu

This commit is contained in:
2024-07-28 22:41:20 +03:00
parent 1d606da897
commit 02fc36ddd8
6 changed files with 441 additions and 621 deletions

View File

@ -627,6 +627,14 @@ float sdCog(vec3 pos, float angle) {
return 0.8 * fOpDifferenceRound(d1,d2,0.05)-0.003;
}
float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
{
vec3 pa = p - a, ba = b - a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
}
float sdText(vec3 pos, float angle) {
//pos.xy *= Rot(angle);
vec3 color = texture2D(texts, getUV(vec2( 0.,0.))).rgb;
@ -651,21 +659,45 @@ float sdHex(vec3 pos, float i, float angle) {
return fOpDifferenceRound(d1,d2,0.1);
}
float sdCylinder(vec3 p, vec3 a, vec3 b, float r)
{
vec3 ba = b - a;
vec3 pa = p - a;
float baba = dot(ba,ba);
float paba = dot(pa,ba);
float x = length(pa*baba-ba*paba) - r*baba;
float y = abs(paba-baba*0.5)-baba*0.5;
float x2 = x*x;
float y2 = y*y*baba;
float d = (max(x,y)<0.0)?-min(x2,y2):(((x>0.0)?x2:0.0)+((y>0.0)?y2:0.0));
return sign(d)*sqrt(abs(d))/baba;
}
float sdPlane( vec3 p, vec4 n )
{
// n must be normalized
return dot(p,n.xyz) + n.w;
}
// Scene
vec2 mapScene(in vec3 p) {
float mat = 0.;
float d = 1e10;
float d = 1e9;
//float dGround = p.y + 2.5;
//d = min(d, dGround);
float dGround = sdPlane(p, vec4(0., 1.0, 0., 5.));
// d = min(d, dGround);
vec3 po = p;
//po.y += sin(u_time);
// pMod3(po, vec3(3.));
po.xy *= scale(vec2(1.3, 1.3));
po.xy *= scale(vec2(1.1));
const float num = 6.;
const float num = 2.;
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);
@ -673,60 +705,43 @@ vec2 mapScene(in vec3 p) {
if (d == a) mat = 1. + mod(i,3.);
}
float b = fSphere(p, 0.5);
d = min(d, b);
if (d == b) {
mat = 1.;
}
//float c2 = sdText(p, u_time);
//d = min(d, c2);
//if ( d == c2) mat = 4.;
/*
float c3 = fBox(p+vec3(0.5, .0, 0.), vec3(1., 1.,1.));
d = min(d, c3);
if ( d == c3) mat = 1.;
// 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.;
float c1= fBox(p+vec3(-2., 0, -2.), vec3(1., 1.,1.));
d = min(d, c1);
if (d == c1) mat = 3.;
*/
return vec2(d, mat);
}
vec3 castRay(vec3 ro, vec3 rd, inout vec3 pos) {
float t = 0.0;
float mat = 0.;
float hit = 0.;
float t = 0.;
float mat = 0.;
float hit = 0.;
for(int i=0; i < 150; i++) {
pos = ro + rd * t;
vec2 res = mapScene(pos);
t += res.x;
mat = res.y;
if (t > 80.) break;
if (t > 100.) break;
if (res.x < abs(0.001*t) ) {
hit = 1.;
break;
}
}
if (t > 80.) t = -1.0;
return vec3(t, mat, hit);
}
vec3 castReflectedRay(vec3 ro, vec3 rd, vec3 pos) {
float t = 0.0;
float mat = 0.;
float hit = 0.;
for(int i=0; i < 50; i++) {
pos = ro + rd * t;
vec2 res = mapScene(pos);
t += res.x;
mat = res.y;
if (t > 40.) break;
if (res.x < abs(0.001*t) ) {
hit = 1.;
break;
}
}
if (t > 40.) t = -1.0;
if (t > 100.) t = 0.;
return vec3(t, mat, hit);
}
@ -739,12 +754,11 @@ float softshadow( in vec3 ro, in vec3 rd, float mint, float maxt, float w )
if (t > maxt) break;
float h = mapScene(ro + t*rd).x;
res = min( res, h/(w*t) );
t += clamp(h, 0.005, 0.50);
if( res < -1.0 || t>maxt ) break;
t += clamp(h, 0.05, 0.50);
if(res < -1.0) break;
}
res = max(res,-1.0);
return 0.25*(1.0+res)*(1.0+res)*(2.0-res);
return 0.25 * (1.0+res)*(1.0+res)*(2.0-res);
}
float castShadow(vec3 ro, vec3 rd) {
@ -773,25 +787,19 @@ 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 );
}
// https://suricrasia.online/blog/shader-functions/
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. );
vec3 ref = reflect( dir, n );
vec3 vl = normalize( v );
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 )*occ;
float shadow = softshadow(v+n*0.01,light_pos, .01, 30., 18.);
vec3 F = fresnel( Ks, normalize( vl - dir ), vl );
float shadow = softshadow(v+n*0.02,light_pos, .01, 30., 8.);
//float shadow = castShadow(v + n*0.02, light_pos);
specular = pow( specular, vec3( shininess ) )*occ;
return light_color * mix( diffuse, specular, F ) * shadow;
//float shadow = 1.;
specular = pow( specular, vec3( shininess )) * occ;
return light_color * mix( diffuse, specular, F ) * shadow * occ;
}
@ -808,44 +816,40 @@ float getAmbientOcc(vec3 p, vec3 n) {
}
vec3 shading(vec3 v, vec3 n, vec3 dir, float material) {
float shininess = 1.;
float shininess = 0.;
float occ = getAmbientOcc(v,n);
vec3 outMaterial = vec3(0.1529, 0.1529, 0.1529);
vec3 outMaterial = vec3(0.0, 0.0, 0.0);
if (material == 0.) {
outMaterial = vec3(0.2863, 0.1059, 0.2431);
shininess = 1.5;
outMaterial = vec3(0.4667, 0.4667, 0.4667);
shininess = 10.;
} else if (material == 1.) {
outMaterial = vec3(0.3294, 0.0941, 0.6);
shininess = 0.6;
} else if (material == 2.) {
outMaterial = vec3(0.5804, 0.9647, 1.0);
outMaterial = vec3(0.6275, 0.1569, 0.9412);
shininess = 1.;
} else if (material == 3.) {
outMaterial = vec3(0.0, 0.0, 0.0);
} else if (material == 2.) {
outMaterial = vec3(0.0314, 0.0314, 0.0314);
shininess = 100.;
} else if (material == 3.) {
outMaterial = vec3(0.5451, 0.5451, 0.5451);
shininess = 1.0;
} else if (material == 4.) {
outMaterial = vec3(0.9961, 1.0, 0.9922);
shininess = .3;
}
vec3 lights = vec3(0.);
lights += addPointLight(vec3( 0., -40., -1. ),vec3(0.56, 0.44, 0.18)*2., shininess, v, dir, n, occ);
lights += addPointLight(vec3( -20.,4., 10. ),vec3(0.04, 0.2, 0.71)*2., shininess, v, dir, n, occ);
// lights += addPointLight(vec3( 2., -1.0, -1.0 ),vec3(0.12, 0.51, 0.63)*2., shininess, v,dir,n,occ );
lights += addPointLight(vec3( 2., 0., 2. ),vec3(0.12, 0.43, 0.77)*4., shininess, v, dir, n, occ);
lights += addPointLight(vec3( -2., -2., -5. ),vec3(0.27, 0.65, 0.46)*3., shininess, v, dir, n, occ);
//lights += addPointLight(vec3( 2., 1.0, 1.0 ),vec3(0.01, 1.0, 0.09)*2., shininess, v,dir,n,occ );
vec3 lightDir = vec3(0. , 4., 2.);
vec3 lightDir = vec3( 0. , 1., 6.);
float sun_dif = clamp(dot(n, lightDir), 0., 1.);
float shadow = softshadow(v+n*0.01,lightDir, .01, 30., 18.);
lights += vec3(0.6627, 0.7098, 0.8863) * sun_dif*shadow*occ; //* shadow; //* mix( vec3(sun_dif), specular, F )
// final += texture( iChannel0, ref ).rgb * fresnel( Ks, n, -dir );
// vec3 col = vec3(0.4)* ref.x;
//lights += vec3(0.6431, 0.7804, 0.8588) *sun_dif*shadow*occ;
float ind = clamp( dot( n, normalize(lightDir*vec3(-1.0,.0,-1.0)) ), 0.0, 1.0 );
lights += vec3(0.1333, 0.1333, 0.1216) * ind *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; // * occ;
return outMaterial * max(vec3(0.), lights);
}
@ -909,34 +913,31 @@ vec3 getCameraFov(vec2 uv, vec3 camPos, vec3 camTarget) {
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);
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;
// camTarget -=voff;
float focusdistance = 150.2;
voff -=camTarget;
float focusdistance = 50.;
return normalize(uv.x * camRight + uv.y * camUp + fov * camForward + voff * fov/focusdistance);
}
vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float b ) {
float fogAmount = 1.0 - exp(-t*b);
float sunAmount = max( dot(rd, lightDir), 0.0 );
vec3 fogColor = mix( vec3(0.3686, 0.2431, 0.4392), // blue
vec3(0.4, 0.7294, 0.9216), // yellow
pow(sunAmount,8.0) );
float sunAmount = max( dot(rd, lightDir), 0. );
vec3 fogColor = mix( vec3(0.1529, 0.1137, 0.2), // blue
vec3(0.2588, 0.1765, 0.3529), // yellow
pow(sunAmount,1.0) );
return mix( col, fogColor, fogAmount );
}
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., 5., -3.);
vec3 camPos = vec3( 0., 0., -2.);
vec3 camTarget = vec3(0., 0., 0.);
vec3 rayDir;
@ -945,41 +946,31 @@ vec3 render(vec2 uv) {
} 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;
vec3 hitPos = vec3(0.);
vec3 t = castRay(camPos, rayDir, hitPos);
vec3 rd = rayDir;
if (t.z > 0.) {
vec3 nor = calcNormal(hitPos);
col = shading(hitPos, nor, rayDir , t.y);
float fogAmount = 0.04;
col = col*exp(-t.x*fogAmount) + applyFog(col, t.x, rd, vec3(0., .3, -1.), fogAmount) * (1.0-exp(-t.x*fogAmount));
rayDir = normalize(reflect(rayDir, nor));
vec3 rayOrigin = hitPos + (rayDir * 0.01);
vec3 t2 = castReflectedRay(rayOrigin, rayDir, hitPos);
if (t2.z > 0.) {
hitPos = rayOrigin + rayDir * t2.x;
nor = calcNormal(hitPos);
col += 0.1 * shading(hitPos, nor, rayDir , t2.y);
/* 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);
} */
}
}
// pixelColor*exp(-distance*b) + fogColor*(1.0-exp(-distance*b));
vec3 col = vec3(0.0471, 0.0471, 0.0471);
//vec3 col = vec3(0.0314, 0.0118, 0.1255) + rayDir.y * 0.4;
vec3 hitPos = vec3(0.);
vec3 t = castRay(camPos, rayDir, hitPos);
if (t.z > 0.) {
vec3 nor = calcNormal(hitPos);
col = shading(hitPos, nor, rayDir, t.y);
float fogAmount = 0.03;
//col = col*exp(-t.x*fogAmount) + applyFog(col, t.x, rayDir, vec3(-1., .0, 1.), fogAmount) * (1.0-exp(-t.x*fogAmount));
col = applyFog(col, t.x, rayDir, vec3(0., .3, 1.), fogAmount);
nor = calcNormal(hitPos);
vec3 rayDir2 = normalize(reflect(rayDir, nor));
vec3 rayOrigin = hitPos + (rayDir2 * 0.1);
vec3 t2 = castRay(rayOrigin, rayDir2,hitPos);
if (t2.z > 0.) {
hitPos = rayOrigin + rayDir*0.01;
nor = calcNormal(hitPos);
col = .2 * shading(hitPos, nor, rayDir2 , t2.y);
}
}
return col;
}