From d2a36e472e39a70d7db5d8ebe9d3758eb618de08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petri=20J=C3=A4rvisalo?= Date: Wed, 29 May 2024 22:17:28 +0300 Subject: [PATCH] depth of field --- shader.glsl | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/shader.glsl b/shader.glsl index ed1746b..3aabec1 100644 --- a/shader.glsl +++ b/shader.glsl @@ -754,11 +754,25 @@ vec3 shading(vec3 v, vec3 n, vec3 dir, float material) { vec3 postProcess(vec3 pos, vec3 col) { float random = noise(gl_FragCoord.xy, 0.01+u_time); float random2 = noise(gl_FragCoord.xy, .2+u_time); - col += clamp(vec3(0.5*random, 0.5*random2, 0.*random), 0.02, 1.)*0.05; // dither / noise + // col += 0.1*clamp(vec3(0.5*random, 0.5*random2, 0.5*random), 0.02, 1.); // dither col = pow( col, vec3(1.0/1.3) ); // 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 rnd23(vec2 p) +{ + vec3 p3 = fract(p.xyx * vec3(.1031, .1030, .0973)); + p3 += dot(p3, p3.yxz+33.33); + return fract((p3.xxy+p3.yzz)*p3.zyx); +} + void main() { vec2 p = (2.0 * gl_FragCoord.xy - u_resolution.xy) / u_resolution.y; @@ -767,11 +781,28 @@ void main() vec3 ta = vec3(0.0, 0., 1.0); vec3 ro = ta + vec3(4.*sin(angle), cos(angle), 4.*cos(angle)); // *cos(angle)) + /* vec3 ww = normalize(ta-ro); vec3 uu = normalize(cross(ww, vec3(0.,1.0, 0.))); // vec3 = pitch, yaw, pan vec3 vv = normalize(cross(uu,ww)); vec3 rd = normalize(p.x * uu + p.y*vv + ww * 1.8); // camera - + */ + vec3 cz = normalize(ta-ro); + vec3 cx = normalize(cross(cz, vec3(0.,1.0, 0.))); // vec3 = pitch, yaw, pan + vec3 cy = normalize(cross(cx,cz)); + + + float fov = .3+ta.z*.7; + // Depth of field + // we take a random position in a disk in view space + float dof = .02; + vec2 h = vec2( noise(gl_FragCoord.xy, u_time), noise(gl_FragCoord.xy, .1+u_time))*.9; + vec3 voff = sqrt(h.x)*(cx*sin(h.y*6.283)+cy*cos(h.y*6.283))*dof; + ro-=voff; + float focusdistance = 1.5*abs(sin(u_time)+1.); // change what needs to be + vec3 rd=normalize(p.x*cx+p.y*cy+fov*cz + voff*fov/focusdistance); + + // global light vec3 col = vec3(0.1451, 0.1098, 0.1608) - vec3(0.9725, 0.5176, 0.0) *rd.y; vec2 t = castRay(ro, rd);