Skip to content

Commit

Permalink
Update blossom template
Browse files Browse the repository at this point in the history
  • Loading branch information
slerpyyy committed Nov 24, 2021
1 parent f0fe873 commit 7adf4ba
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 22 additions & 2 deletions example/blossom/draw.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
uniform int frame_count;
#define iResolution resolution
#define iFrame frame_count
#define gl_FragColor fragColor

#else

// Blossom header
layout(location = 0) out vec4 fragColor;
layout(location = 0) uniform vec4 iResolution;
layout(location = 1) uniform int iFrame;

Expand Down Expand Up @@ -45,11 +45,13 @@ vec3 noise(vec3 p, float f) {
return acc.xyz / acc.w;
}

// rotation matrix
mat2 rot(float a) {
float s=sin(a), c=cos(a);
return mat2(c,-s,s,c);
}

// Sh4derJockey logo sdf
float logo(vec2 p) {
const float t = 0.015;
const float h = 0.5 * (7.0 / 18.0 + t);
Expand All @@ -65,16 +67,22 @@ float logo(vec2 p) {
float trace(vec3 ro, vec3 rd) {
float r = 1e32;

// intersect with one slice at a time
for(int i = -8; i <= 2; i++) {

// ray-plane intersection
float t = (i - ro.y) / rd.y;
if (t < 0) continue;

// compute point on plane
vec3 p = ro + rd * t;
float s = dot(p, rd);
p.xz *= rot(0.06 * p.y - 0.5);

// evaluate logo sdf
float d = logo(p.xz);

// add noise pattern
if (i < 1) {
d = min(d, abs(noise(p, 5).x) - 0.002);

Expand All @@ -87,35 +95,47 @@ float trace(vec3 ro, vec3 rd) {
d = min(d, min(a.x, a.y));
}

// update nearest intersection
if (d < 0.01 * (rv.x - 0.5)) r = min(r, t);

// cycle random state
rv = rv.wxyz;
}

return r;
}

void main() {
// initialise random state
rv = 7 * cos(iFrame + gl_FragCoord);
for (int i=0; i<8; i++) shuffle();

// compute uv coordinates with anti-aliasing
vec2 aa = rv.xy * 2 - 1;
vec2 uv = (2 * gl_FragCoord.xy - iResolution.xy + aa) / iResolution.y;

// compute depth of field parameters
vec2 a = vec2(1, 2 * pi) * rv.zw;
vec2 dof = 0.6 * pow(a.x, 0.45) * vec2(cos(a.y),sin(a.y));

// setup camera
const float focal = 8.0;
vec3 ro = vec3(dof, -3.5 * focal);
vec3 rd = normalize(vec3(uv - focal * dof / length(ro), focal));

// orbit rotate camera
ro.yz *= rot(0.7);
rd.yz *= rot(0.7);

// update random state
shuffle();
shuffle();
shuffle();

// trace and compute color
float t = trace(ro, rd);
vec3 col = (1 + 2 * vec3(aa, aa.x)) * exp(-0.1*pow(length(ro) - t - 1, 2));
gl_FragColor = vec4(col, 1);

// set output color
fragColor = vec4(col, 1);
}
8 changes: 5 additions & 3 deletions example/blossom/present.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

// compatibility header
out vec4 fragColor;
uniform sampler2D accumulatorTex;
uniform vec4 resolution;
uniform sampler2D accumulatorTex;
#define iResolution resolution
#define gl_FragColor fragColor

#else

// Blossom header
layout(location = 0) out vec4 fragColor;
layout(location = 0) uniform vec4 iResolution;
layout(binding = 0) uniform sampler2D accumulatorTex;

Expand All @@ -28,12 +28,14 @@
void main() {
ivec2 coord = ivec2(gl_FragCoord.xy);

// apply unsharp filter
const float f = 0.2;
vec4 acc = (1 + 4 * f) * texelFetch(accumulatorTex, coord, 0);
acc -= f * texelFetch(accumulatorTex, coord + ivec2(0, 1), 0);
acc -= f * texelFetch(accumulatorTex, coord + ivec2(1, 0), 0);
acc -= f * texelFetch(accumulatorTex, coord - ivec2(0, 1), 0);
acc -= f * texelFetch(accumulatorTex, coord - ivec2(1, 0), 0);

gl_FragColor = vec4(sqrt(acc.rgb / acc.a), 1);
// set output color
fragColor = vec4(sqrt(acc.rgb / acc.a), 1);
}

0 comments on commit 7adf4ba

Please sign in to comment.