Skip to content

Commit 7adf4ba

Browse files
committed
Update blossom template
1 parent f0fe873 commit 7adf4ba

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

example/blossom/draw.frag

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
uniform int frame_count;
1313
#define iResolution resolution
1414
#define iFrame frame_count
15-
#define gl_FragColor fragColor
1615

1716
#else
1817

1918
// Blossom header
19+
layout(location = 0) out vec4 fragColor;
2020
layout(location = 0) uniform vec4 iResolution;
2121
layout(location = 1) uniform int iFrame;
2222

@@ -45,11 +45,13 @@ vec3 noise(vec3 p, float f) {
4545
return acc.xyz / acc.w;
4646
}
4747

48+
// rotation matrix
4849
mat2 rot(float a) {
4950
float s=sin(a), c=cos(a);
5051
return mat2(c,-s,s,c);
5152
}
5253

54+
// Sh4derJockey logo sdf
5355
float logo(vec2 p) {
5456
const float t = 0.015;
5557
const float h = 0.5 * (7.0 / 18.0 + t);
@@ -65,16 +67,22 @@ float logo(vec2 p) {
6567
float trace(vec3 ro, vec3 rd) {
6668
float r = 1e32;
6769

70+
// intersect with one slice at a time
6871
for(int i = -8; i <= 2; i++) {
72+
73+
// ray-plane intersection
6974
float t = (i - ro.y) / rd.y;
7075
if (t < 0) continue;
7176

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

82+
// evaluate logo sdf
7683
float d = logo(p.xz);
7784

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

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

98+
// update nearest intersection
9099
if (d < 0.01 * (rv.x - 0.5)) r = min(r, t);
100+
101+
// cycle random state
91102
rv = rv.wxyz;
92103
}
93104

94105
return r;
95106
}
96107

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

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

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

121+
// setup camera
107122
const float focal = 8.0;
108123
vec3 ro = vec3(dof, -3.5 * focal);
109124
vec3 rd = normalize(vec3(uv - focal * dof / length(ro), focal));
110125

126+
// orbit rotate camera
111127
ro.yz *= rot(0.7);
112128
rd.yz *= rot(0.7);
113129

130+
// update random state
114131
shuffle();
115132
shuffle();
116133
shuffle();
117134

135+
// trace and compute color
118136
float t = trace(ro, rd);
119137
vec3 col = (1 + 2 * vec3(aa, aa.x)) * exp(-0.1*pow(length(ro) - t - 1, 2));
120-
gl_FragColor = vec4(col, 1);
138+
139+
// set output color
140+
fragColor = vec4(col, 1);
121141
}

example/blossom/present.frag

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
// compatibility header
1010
out vec4 fragColor;
11-
uniform sampler2D accumulatorTex;
1211
uniform vec4 resolution;
12+
uniform sampler2D accumulatorTex;
1313
#define iResolution resolution
14-
#define gl_FragColor fragColor
1514

1615
#else
1716

1817
// Blossom header
18+
layout(location = 0) out vec4 fragColor;
1919
layout(location = 0) uniform vec4 iResolution;
2020
layout(binding = 0) uniform sampler2D accumulatorTex;
2121

@@ -28,12 +28,14 @@
2828
void main() {
2929
ivec2 coord = ivec2(gl_FragCoord.xy);
3030

31+
// apply unsharp filter
3132
const float f = 0.2;
3233
vec4 acc = (1 + 4 * f) * texelFetch(accumulatorTex, coord, 0);
3334
acc -= f * texelFetch(accumulatorTex, coord + ivec2(0, 1), 0);
3435
acc -= f * texelFetch(accumulatorTex, coord + ivec2(1, 0), 0);
3536
acc -= f * texelFetch(accumulatorTex, coord - ivec2(0, 1), 0);
3637
acc -= f * texelFetch(accumulatorTex, coord - ivec2(1, 0), 0);
3738

38-
gl_FragColor = vec4(sqrt(acc.rgb / acc.a), 1);
39+
// set output color
40+
fragColor = vec4(sqrt(acc.rgb / acc.a), 1);
3941
}

0 commit comments

Comments
 (0)