roamwright 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +12 -0
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/examples/index.html +19 -0
- package/examples/island.js +62 -0
- package/geometry/builder.js +546 -0
- package/geometry/instances.js +76 -0
- package/geometry/placement.js +65 -0
- package/geometry/primitives.js +172 -0
- package/index.js +8 -0
- package/licenses/baking-lab.txt +21 -0
- package/licenses/fidelityfx-fsr.txt +19 -0
- package/licenses/three.txt +21 -0
- package/math/curve.js +94 -0
- package/math/noise.js +80 -0
- package/math/random.js +27 -0
- package/math/scalar.js +12 -0
- package/motion/player.js +248 -0
- package/motion/spaces.js +206 -0
- package/package.json +50 -4
- package/rendering/context.js +20 -0
- package/rendering/dof.js +59 -0
- package/rendering/eye-adapt.js +119 -0
- package/rendering/piece-batch.js +411 -0
- package/rendering/post-shaders.js +236 -0
- package/rendering/post.js +520 -0
- package/rendering/still-casters.js +37 -0
- package/rendering/taa.js +171 -0
- package/rendering/textures.js +63 -0
- package/rendering/world-matrices.js +77 -0
- package/runtime/first-use.js +60 -0
- package/runtime/gpu-timing.js +75 -0
- package/runtime/hand-over.js +102 -0
- package/runtime/pool.js +153 -0
- package/runtime/quality-fit.js +194 -0
- package/runtime/quality-store.js +58 -0
- package/runtime/seen.js +36 -0
- package/runtime/worker.js +53 -0
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { SQUASH, TAA_FS } from './taa.js';
|
|
2
|
+
|
|
3
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
4
|
+
// The shaders of post-processing (post.js): light shafts, ambient occlusion, bloom's blur, the final pass that puts them
|
|
5
|
+
// together with tone mapping and the grade, and the two halves of the upscale. Bloom's settings are here, since its blur
|
|
6
|
+
// bakes its threshold in.
|
|
7
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
8
|
+
const QUAD_VS = 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }';
|
|
9
|
+
|
|
10
|
+
// Light shafts: open sky round the sun is smeared away from it. The supplied sky mask is 0 for open sky, the cover for cloud, 1 for solid things.
|
|
11
|
+
const RAY_FS = `/* glsl */ uniform sampler2D tMask; uniform vec2 uSun; uniform float uAspect; varying vec2 vUv;
|
|
12
|
+
void main(){ vec2 dv = (uSun - vUv) / 30.0, uv = vUv + dv * fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715)))); float acc = 0.0, w = 1.0;
|
|
13
|
+
for (int i = 0; i < 30; i++) { uv += dv; vec2 q = (uv - uSun) * vec2(uAspect, 1.0); acc += clamp(1.0 - texture2D(tMask, clamp(uv, 0.001, 0.999)).x, 0.0, 1.0) * exp(-dot(q, q) * 9.0) * w; w *= 0.96; }
|
|
14
|
+
gl_FragColor = vec4(acc / 30.0, 0.0, 0.0, 1.0); }`;
|
|
15
|
+
|
|
16
|
+
// Ambient occlusion from the depth buffer alone: corners, eaves, doorways and the ground under things darken. Stored with the depth it was taken at.
|
|
17
|
+
const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx; uniform float uAspect, uAO; varying vec2 vUv;
|
|
18
|
+
vec3 vp(vec2 uv){ uv = (floor(uv / uPx) + 0.5) * uPx; // every lookup is moved onto the centre of a depth texel, so the position and the depth it is built from always belong together (otherwise thin dark lines cross flat ground, worst on phones)
|
|
19
|
+
float z = uNF.x * uNF.y / ((uNF.y - uNF.x) * texture2D(tDepth, uv).x - uNF.y); return vec3((uv * 2.0 - 1.0) * uTan * -z, z); }
|
|
20
|
+
void main(){ vec2 uv = (floor(vUv / uPx) + 0.5) * uPx; // a half-size pixel's centre is the corner of four depth texels: step onto one, or rounding picks a different one from row to row and draws lines
|
|
21
|
+
vec3 P = vp(uv); float dist = -P.z, ao = 1.0;
|
|
22
|
+
if (dist < 240.0) {
|
|
23
|
+
vec3 px = vp(uv + vec2(uPx.x, 0.0)) - P, mx = P - vp(uv - vec2(uPx.x, 0.0)), py = vp(uv + vec2(0.0, uPx.y)) - P, my = P - vp(uv - vec2(0.0, uPx.y));
|
|
24
|
+
vec3 N = normalize(cross(abs(px.z) < abs(mx.z) ? px : mx, abs(py.z) < abs(my.z) ? py : my));
|
|
25
|
+
float rad = 0.4 + 0.012 * dist, sr = clamp(rad / dist / (2.0 * uTan.y), 3.0 * uPx.y, 0.1), bias = 0.015 + 0.003 * dist, occ = 0.0;
|
|
26
|
+
float a0 = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715)))) * 6.2832;
|
|
27
|
+
for (int i = 0; i < 14; i++) {
|
|
28
|
+
float an = a0 + float(i) * 2.39996; vec2 o = vec2(cos(an) / uAspect, sin(an)) * (sqrt((float(i) + 0.5) / 14.0) * sr);
|
|
29
|
+
vec3 d = vp(uv + o) - P; float dd = dot(d, d);
|
|
30
|
+
occ += max(dot(N, d) - bias, 0.0) / (dd + 0.0004 + 0.02 * rad * rad) * (1.0 - smoothstep(rad * rad, rad * rad * 4.0, dd)); // what lies within a seventh of the radius counts less: a face's nose and brow made few samples weigh so much that the cheeks were speckled
|
|
31
|
+
}
|
|
32
|
+
float reach = sr * dist * 2.0 * uTan.y; // how far the samples really reach in the world once the screen's cap has bitten (metres)
|
|
33
|
+
ao = mix(clamp(1.0 - uAO * occ * rad / 14.0 * 2.1, 0.0, 1.0), 1.0, max(smoothstep(120.0, 240.0, dist), 1.0 - smoothstep(0.04, 0.12, reach))); // under a hand's breadth it would shade a nose's own curve: a face seen close was smeared dark beside the nose
|
|
34
|
+
}
|
|
35
|
+
gl_FragColor = vec4(ao, dist, 0.0, 1.0); }`;
|
|
36
|
+
|
|
37
|
+
// Additive blending can overflow the half-float buffer; one infinite or invalid pixel would be smeared by bloom into a black square,
|
|
38
|
+
// so both the bloom's input and the final pass make every pixel finite and cap it.
|
|
39
|
+
const SANE =
|
|
40
|
+
'vec3 sane(vec3 c){ if (!(c.r >= 0.0)) c.r = 0.0; if (!(c.g >= 0.0)) c.g = 0.0; if (!(c.b >= 0.0)) c.b = 0.0; return min(c, vec3(48.0)); }';
|
|
41
|
+
|
|
42
|
+
// Bloom: what is brighter than BLOOM.threshold is blurred at five sizes, from half the frame down to a thirty-second, each level blurred
|
|
43
|
+
// across and then down with a wider Gaussian (the same blur and weights as Three's UnrealBloomPass), and the final pass adds the five.
|
|
44
|
+
// The first blur reads the scene itself and keeps only its bright part, so no half-size copy of the bright part is written first.
|
|
45
|
+
const BLOOM = { strength: 0.46, radius: 0.5, threshold: 0.95, knee: 0.01, kernels: [3, 5, 7, 9, 11], factors: [1.0, 0.8, 0.6, 0.4, 0.2] };
|
|
46
|
+
// The fragment shader of one blur direction for bloom, shared by every level (one program): uTaps of the weights uK are used, and
|
|
47
|
+
// the first pass (uBright) also keeps only the bright parts. The loop runs to the widest kernel and stops at uTaps, which every
|
|
48
|
+
// pixel of a pass shares, so each level sums the same samples in the same order as a loop of its own length did.
|
|
49
|
+
const BLUR_TAPS = Math.max(...BLOOM.kernels);
|
|
50
|
+
const BLUR_FS = `/* glsl */ uniform sampler2D tSrc; uniform vec2 uStep; uniform float uK[${BLUR_TAPS}]; uniform int uTaps; uniform bool uBright;
|
|
51
|
+
varying vec2 vUv; ${SANE}
|
|
52
|
+
vec3 src(vec2 uv){
|
|
53
|
+
if (uBright) { vec3 c = sane(texture2D(tSrc, uv).rgb); return c * smoothstep(${BLOOM.threshold.toFixed(3)}, ${(BLOOM.threshold + BLOOM.knee).toFixed(3)}, dot(c, vec3(0.2126, 0.7152, 0.0722))); }
|
|
54
|
+
return texture2D(tSrc, uv).rgb; }
|
|
55
|
+
void main(){ float ws = uK[0]; vec3 s = src(vUv) * ws;
|
|
56
|
+
for (int i = 1; i < ${BLUR_TAPS}; i++) { if (i >= uTaps) break; vec2 o = uStep * float(i); s += (src(vUv + o) + src(vUv - o)) * uK[i]; ws += 2.0 * uK[i]; }
|
|
57
|
+
gl_FragColor = vec4(s / ws, 1.0); }`;
|
|
58
|
+
|
|
59
|
+
// The grade after tone mapping: saturation, a touch of contrast, cool shadows and warm lights, the vignette. Shared by the final
|
|
60
|
+
// pass and the names drawn after it, so both are graded alike.
|
|
61
|
+
const GRADE = `/* glsl */ uniform float uVig, uSat;
|
|
62
|
+
vec3 gradeTail(vec3 c, vec2 uv){
|
|
63
|
+
float l = dot(c, vec3(0.2126, 0.7152, 0.0722));
|
|
64
|
+
c = mix(vec3(l), c, uSat);
|
|
65
|
+
c = mix(c, c * c * (3.0 - 2.0 * c), 0.16);
|
|
66
|
+
c *= mix(vec3(0.95, 0.985, 1.06), vec3(1.045, 1.0, 0.95), smoothstep(0.0, 0.75, l));
|
|
67
|
+
vec2 q = uv - 0.5; return c * (1.0 - uVig * smoothstep(0.22, 1.0, dot(q, q) * 2.3)); }`;
|
|
68
|
+
// A pixel's own noise of half a step of the 8-bit screen, against banding.
|
|
69
|
+
const DITHER = '(fract(sin(dot(gl_FragCoord.xy + uTime, vec2(12.9898, 78.233))) * 43758.5453) - 0.5) / 255.0';
|
|
70
|
+
|
|
71
|
+
// Depth of field (dof.js): the circle of confusion at view depth z, in pixels of the full picture, from uDof (focus distance,
|
|
72
|
+
// the circle far behind it and the widest circle, both in pixels, and how far the effect is in); and what the final pass mixes in by it.
|
|
73
|
+
const DOF_COC = `/* glsl */ float cocAt(float z){ return min(uDof.y * abs(z - uDof.x) / max(z, 0.05), uDof.z); }`;
|
|
74
|
+
const DOF_MIX = `/* glsl */
|
|
75
|
+
if (uDof.w > 0.0) { soft = texture2D(tDof, vUv).rgb; softShare = smoothstep(0.8, 2.5, cocAt(viewDepth(vUv))) * uDof.w; }`;
|
|
76
|
+
// ACES fitted curve by Stephen Hill, from Baking Lab (licenses/baking-lab.txt), as used in Three.js (licenses/three.txt).
|
|
77
|
+
const ACES = `/* glsl */ vec3 fit(vec3 v){ vec3 a = v * (v + 0.0245786) - 0.000090537; vec3 b = v * (0.983729 * v + 0.4329510) + 0.238081; return a / b; }
|
|
78
|
+
vec3 acesCurve(vec3 c){
|
|
79
|
+
const mat3 mi = mat3(vec3(0.59719, 0.07600, 0.02840), vec3(0.35458, 0.90834, 0.13383), vec3(0.04823, 0.01566, 0.83777));
|
|
80
|
+
const mat3 mo = mat3(vec3(1.60475, -0.10208, -0.00327), vec3(-0.53108, 1.10813, -0.07276), vec3(-0.07367, -0.00605, 1.07602));
|
|
81
|
+
return clamp(mo * fit(mi * c), 0.0, 1.0); }`;
|
|
82
|
+
// Light as the screen shows it, encoded for it (sRGB).
|
|
83
|
+
const ENCODE = 'c = mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));';
|
|
84
|
+
// With temporal anti-aliasing (taa.js) the final pass writes the running picture (exposed light, squashed) and the present
|
|
85
|
+
// pass tone-maps and grades it onto the screen. The running picture's neighbourhood is this frame's scene at the pixel and the eight
|
|
86
|
+
// round it, in YCoCg, lit as the pixel itself is: its depth of field, occlusion, shafts and bloom are soft enough to be shared by
|
|
87
|
+
// its neighbours, so they make one gain and one lift for all five, which carry the neighbourhood's mean and spread over whole.
|
|
88
|
+
// Four neighbours made a box that changed with each step of the lens, so every edge flickered in a still picture.
|
|
89
|
+
const STEADY = `/* glsl */
|
|
90
|
+
float ex = uExposure * eyeEx / 0.6, gain = shade * (1.0 - softShare) * ex;
|
|
91
|
+
vec3 lift = toYCoCg((soft * softShare * shade + rays + bloom) * ex), y = toYCoCg(min(raw, vec3(48.0))), m1 = y, m2 = y * y;
|
|
92
|
+
y = toYCoCg(sane(texture2D(tScene, vUv - vec2(uPx.x, 0.0)).rgb)); m1 += y; m2 += y * y;
|
|
93
|
+
y = toYCoCg(sane(texture2D(tScene, vUv + vec2(uPx.x, 0.0)).rgb)); m1 += y; m2 += y * y;
|
|
94
|
+
y = toYCoCg(sane(texture2D(tScene, vUv - vec2(0.0, uPx.y)).rgb)); m1 += y; m2 += y * y;
|
|
95
|
+
y = toYCoCg(sane(texture2D(tScene, vUv + vec2(0.0, uPx.y)).rgb)); m1 += y; m2 += y * y;
|
|
96
|
+
y = toYCoCg(sane(texture2D(tScene, vUv - uPx).rgb)); m1 += y; m2 += y * y;
|
|
97
|
+
y = toYCoCg(sane(texture2D(tScene, vUv + uPx).rgb)); m1 += y; m2 += y * y;
|
|
98
|
+
y = toYCoCg(sane(texture2D(tScene, vUv + vec2(uPx.x, -uPx.y)).rgb)); m1 += y; m2 += y * y;
|
|
99
|
+
y = toYCoCg(sane(texture2D(tScene, vUv + vec2(-uPx.x, uPx.y)).rgb)); m1 += y; m2 += y * y;
|
|
100
|
+
vec3 mean = m1 / 9.0, spread = sqrt(max(m2 / 9.0 - mean * mean, 0.0));
|
|
101
|
+
gl_FragColor = vec4(steady(c * ex, mean * gain + lift, spread * gain), 1.0);`;
|
|
102
|
+
const FINAL_FS = `/* glsl */ uniform sampler2D tScene, tDepth, tAO, tRay, tBloom0, tBloom1, tBloom2, tBloom3, tBloom4; uniform vec3 uBloomW[5]; uniform vec2 uSun, uNF, uTan, uPx, uHp; uniform float uRay, uAspect, uAO, uExposure, uTime; uniform vec3 uRayCol; uniform sampler2D tEye, tDof; uniform vec3 uEye; uniform vec4 uDof; varying vec2 vUv;
|
|
103
|
+
float eyeEx = 1.0;
|
|
104
|
+
${SANE}
|
|
105
|
+
${GRADE}
|
|
106
|
+
#ifdef TAA
|
|
107
|
+
${TAA_FS}
|
|
108
|
+
#endif
|
|
109
|
+
float viewDepth(vec2 uv){ return -uNF.x * uNF.y / ((uNF.y - uNF.x) * texture2D(tDepth, uv).x - uNF.y); }
|
|
110
|
+
${DOF_COC}
|
|
111
|
+
${ACES}
|
|
112
|
+
vec3 exposed(vec3 c){ return c * uExposure * eyeEx / 0.6; }
|
|
113
|
+
vec3 aces(vec3 c){ return acesCurve(exposed(c)); }
|
|
114
|
+
void main(){
|
|
115
|
+
// the eye: a part of how far the metered brightness lies from what the hour leads one to expect (uEye: expected log2 brightness,
|
|
116
|
+
// the part, and how far either way at most, in stops), so a dark room opens it and a bright sky closes it
|
|
117
|
+
eyeEx = exp2(clamp(uEye.y * (uEye.x - texture2D(tEye, vec2(0.5)).r), -uEye.z, uEye.z));
|
|
118
|
+
vec3 c = texture2D(tScene, vUv).rgb; if (!(c.r >= 0.0)) c.r = 0.0; if (!(c.g >= 0.0)) c.g = 0.0; if (!(c.b >= 0.0)) c.b = 0.0;
|
|
119
|
+
vec3 raw = c, soft = vec3(0.0), rays = vec3(0.0); float softShare = 0.0, shade = 1.0; // the scene's light; depth of field, occlusion, the shafts
|
|
120
|
+
${DOF_MIX}
|
|
121
|
+
c = mix(c, soft, softShare);
|
|
122
|
+
if (uAO > 0.0) { // occlusion was worked out at half size, each pixel's samples turned its own way: a tent three
|
|
123
|
+
float z = viewDepth(vUv); // of its pixels wide over the nine nearest smooths that turning into an even shade (alone it
|
|
124
|
+
if (z < 240.0) { // speckles a curved surface, a cheek beside the nose), trusting the values at this pixel's own depth
|
|
125
|
+
vec2 g = floor(vUv / uHp), o = vUv / uHp - g - 0.5; float aoS = 0.0, wS = 0.0;
|
|
126
|
+
for (int j = -1; j < 2; j++) for (int i = -1; i < 2; i++) {
|
|
127
|
+
vec2 sm = texture2D(tAO, (g + vec2(float(i), float(j)) + 0.5) * uHp).rg;
|
|
128
|
+
float w = max(0.0, 1.5 - abs(float(i) - o.x)) * max(0.0, 1.5 - abs(float(j) - o.y)) / (0.004 + abs(sm.g - z) / z) + 1e-5;
|
|
129
|
+
aoS += sm.r * w; wS += w;
|
|
130
|
+
}
|
|
131
|
+
shade = 0.3 + 0.7 * mix(aoS / wS, 1.0, smoothstep(1.5, 5.0, max(c.r, max(c.g, c.b))));
|
|
132
|
+
c *= shade;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (uRay > 0.002) { // the shafts were smeared at half size (they are soft); here they are only added
|
|
136
|
+
vec2 q0 = (vUv - uSun) * vec2(uAspect, 1.0);
|
|
137
|
+
float sh = texture2D(tRay, vUv).r * uRay * 2.2 * exp(-dot(q0, q0) * 1.2);
|
|
138
|
+
rays = uRayCol * (0.6 * (1.0 - exp(-sh * 1.2))); // soft-capped, so facing a low sun gives a glow with shafts in it, not a white-out
|
|
139
|
+
}
|
|
140
|
+
c = min(c + rays, vec3(48.0));
|
|
141
|
+
vec3 bloom = uBloomW[0] * texture2D(tBloom0, vUv).rgb + uBloomW[1] * texture2D(tBloom1, vUv).rgb + uBloomW[2] * texture2D(tBloom2, vUv).rgb
|
|
142
|
+
+ uBloomW[3] * texture2D(tBloom3, vUv).rgb + uBloomW[4] * texture2D(tBloom4, vUv).rgb;
|
|
143
|
+
c += bloom;
|
|
144
|
+
#ifdef TAA
|
|
145
|
+
${STEADY}
|
|
146
|
+
#else
|
|
147
|
+
c = gradeTail(aces(c), vUv);
|
|
148
|
+
${ENCODE}
|
|
149
|
+
gl_FragColor = vec4(c + ${DITHER}, 1.0);
|
|
150
|
+
gl_FragDepth = texture2D(tDepth, vUv).x; // the screen keeps the scene's depth, for the names drawn after (drawnAtFullSize)
|
|
151
|
+
#endif
|
|
152
|
+
}`;
|
|
153
|
+
// Edge adaptation and sharpening follow AMD FidelityFX Super Resolution 1 (licenses/fidelityfx-fsr.txt).
|
|
154
|
+
// A sharpening held within its neighbours (as FSR 1's RCAS): e is pushed away from the four round it (b above, d left, f right, h below)
|
|
155
|
+
// by up to sharp (0 to 1), only as far as keeps it inside their range, so edges come back crisp without halos.
|
|
156
|
+
const SHARPEN = `/* glsl */ vec3 sharpen(vec3 b, vec3 d, vec3 e, vec3 f, vec3 h, float sharp){
|
|
157
|
+
vec3 mn = min(min(b, d), min(f, h)), mx = max(max(b, d), max(f, h));
|
|
158
|
+
vec3 lobes = max(-min(mn, e) / (4.0 * mx + 1e-5), (1.0 - max(mx, e)) / (4.0 * mn - 4.0 - 1e-5));
|
|
159
|
+
float lobe = max(-0.1875, min(max(lobes.r, max(lobes.g, lobes.b)), 0.0)) * sharp;
|
|
160
|
+
return (lobe * (b + d + f + h) + e) / (4.0 * lobe + 1.0); }`;
|
|
161
|
+
// The present pass, with temporal anti-aliasing: the running picture sharpened a little (a running picture is a little softer than one
|
|
162
|
+
// frame; where the upscale follows, its own sharpening does this), tone-mapped, graded and encoded for the screen, with its noise and
|
|
163
|
+
// the depth.
|
|
164
|
+
const PRESENT_FS = `/* glsl */ uniform sampler2D tSrc, tDepth; uniform vec2 uPx; uniform float uSharp, uTime; varying vec2 vUv;
|
|
165
|
+
${GRADE}
|
|
166
|
+
${SHARPEN}
|
|
167
|
+
${ACES}
|
|
168
|
+
${SQUASH}
|
|
169
|
+
vec3 at(float x, float y){ return texture2D(tSrc, vUv + vec2(x, y) * uPx).rgb; }
|
|
170
|
+
void main(){
|
|
171
|
+
vec3 c = at(0.0, 0.0);
|
|
172
|
+
if (uSharp > 0.0) c = sharpen(at(0.0, 1.0), at(-1.0, 0.0), c, at(1.0, 0.0), at(0.0, -1.0), uSharp);
|
|
173
|
+
c = gradeTail(acesCurve(unsquash(c)), vUv);
|
|
174
|
+
${ENCODE}
|
|
175
|
+
gl_FragColor = vec4(c + ${DITHER}, 1.0);
|
|
176
|
+
gl_FragDepth = texture2D(tDepth, vUv).x; }`;
|
|
177
|
+
|
|
178
|
+
// The upscale's first half, edge-adaptive (as FSR 1's EASU): twelve texels round the sample, weighted by a windowed Lanczos-2 that is
|
|
179
|
+
// stretched along the edge the four nearest texels show and narrowed across it, then held within those four, so it neither blurs nor rings.
|
|
180
|
+
// Works on the graded picture, where steps of light are as the eye sees them.
|
|
181
|
+
const EASU_FS = `/* glsl */ uniform sampler2D tSrc; uniform vec2 uSrcSize; varying vec2 vUv;
|
|
182
|
+
vec3 tap(vec2 p){ return texture2D(tSrc, (p + 0.5) / uSrcSize).rgb; }
|
|
183
|
+
float luma(vec3 c){ return c.g + 0.5 * (c.r + c.b); }
|
|
184
|
+
// the edge at one of the four central texels, from its four neighbours, weighted by the sample's nearness to it: its direction, and how
|
|
185
|
+
// much it is an edge (a step on one side only) rather than a thin line or noise (steps both ways)
|
|
186
|
+
void edge(inout vec2 dir, inout float len, float w, float up, float left, float mid, float right, float down){
|
|
187
|
+
float dx = right - left, dy = down - up;
|
|
188
|
+
float lx = clamp(abs(dx) / max(max(abs(right - mid), abs(mid - left)), 1e-5), 0.0, 1.0);
|
|
189
|
+
float ly = clamp(abs(dy) / max(max(abs(down - mid), abs(mid - up)), 1e-5), 0.0, 1.0);
|
|
190
|
+
dir += vec2(dx, dy) * w; len += (lx * lx + ly * ly) * w; }
|
|
191
|
+
// one texel's share: off is from the sample to the texel, turned onto the edge and scaled by len2; lob is the negative lobe's depth
|
|
192
|
+
void take(inout vec3 sum, inout float wsum, vec3 c, vec2 off, vec2 dir, vec2 len2, float lob, float clp){
|
|
193
|
+
vec2 v = vec2(dot(off, dir), dot(off, vec2(-dir.y, dir.x))) * len2;
|
|
194
|
+
float d2 = min(dot(v, v), clp), wb = 0.4 * d2 - 1.0, wa = lob * d2 - 1.0;
|
|
195
|
+
wb = 1.5625 * wb * wb - 0.5625; float w = wb * wa * wa;
|
|
196
|
+
sum += c * w; wsum += w; }
|
|
197
|
+
void main(){
|
|
198
|
+
vec2 pp = vUv * uSrcSize - 0.5, p = floor(pp), f = pp - p;
|
|
199
|
+
// b c
|
|
200
|
+
// e F g h F is the texel at or before the sample, k the one after it both ways
|
|
201
|
+
// i j k l
|
|
202
|
+
// n o
|
|
203
|
+
vec3 b = tap(p + vec2(0.0, -1.0)), c = tap(p + vec2(1.0, -1.0)), e = tap(p + vec2(-1.0, 0.0)), F = tap(p), g = tap(p + vec2(1.0, 0.0)),
|
|
204
|
+
h = tap(p + vec2(2.0, 0.0)), i = tap(p + vec2(-1.0, 1.0)), j = tap(p + vec2(0.0, 1.0)), k = tap(p + vec2(1.0, 1.0)),
|
|
205
|
+
l = tap(p + vec2(2.0, 1.0)), n = tap(p + vec2(0.0, 2.0)), o = tap(p + vec2(1.0, 2.0));
|
|
206
|
+
float lb = luma(b), lc = luma(c), le = luma(e), lF = luma(F), lg = luma(g), lh = luma(h), li = luma(i), lj = luma(j), lk = luma(k),
|
|
207
|
+
ll = luma(l), ln = luma(n), lo = luma(o);
|
|
208
|
+
vec2 dir = vec2(0.0); float len = 0.0;
|
|
209
|
+
edge(dir, len, (1.0 - f.x) * (1.0 - f.y), lb, le, lF, lg, lj);
|
|
210
|
+
edge(dir, len, f.x * (1.0 - f.y), lc, lF, lg, lh, lk);
|
|
211
|
+
edge(dir, len, (1.0 - f.x) * f.y, lF, li, lj, lk, ln);
|
|
212
|
+
edge(dir, len, f.x * f.y, lg, lj, lk, ll, lo);
|
|
213
|
+
float d2 = dot(dir, dir);
|
|
214
|
+
dir = d2 < 1.0 / 32768.0 ? vec2(1.0, 0.0) : dir * inversesqrt(d2);
|
|
215
|
+
len *= 0.5; len *= len;
|
|
216
|
+
float stretch = 1.0 / max(abs(dir.x), abs(dir.y)); // 1 along an axis, the square root of 2 on a diagonal
|
|
217
|
+
vec2 len2 = vec2(1.0 + (stretch - 1.0) * len, 1.0 - 0.5 * len);
|
|
218
|
+
float lob = 0.5 - 0.29 * len, clp = 1.0 / lob;
|
|
219
|
+
vec3 sum = vec3(0.0); float ws = 0.0;
|
|
220
|
+
take(sum, ws, b, vec2(0.0, -1.0) - f, dir, len2, lob, clp); take(sum, ws, c, vec2(1.0, -1.0) - f, dir, len2, lob, clp);
|
|
221
|
+
take(sum, ws, e, vec2(-1.0, 0.0) - f, dir, len2, lob, clp); take(sum, ws, F, -f, dir, len2, lob, clp);
|
|
222
|
+
take(sum, ws, g, vec2(1.0, 0.0) - f, dir, len2, lob, clp); take(sum, ws, h, vec2(2.0, 0.0) - f, dir, len2, lob, clp);
|
|
223
|
+
take(sum, ws, i, vec2(-1.0, 1.0) - f, dir, len2, lob, clp); take(sum, ws, j, vec2(0.0, 1.0) - f, dir, len2, lob, clp);
|
|
224
|
+
take(sum, ws, k, vec2(1.0, 1.0) - f, dir, len2, lob, clp); take(sum, ws, l, vec2(2.0, 1.0) - f, dir, len2, lob, clp);
|
|
225
|
+
take(sum, ws, n, vec2(0.0, 2.0) - f, dir, len2, lob, clp); take(sum, ws, o, vec2(1.0, 2.0) - f, dir, len2, lob, clp);
|
|
226
|
+
gl_FragColor = vec4(clamp(sum / ws, min(min(F, g), min(j, k)), max(max(F, g), max(j, k))), 1.0); }`;
|
|
227
|
+
|
|
228
|
+
// The upscale's second half: the screen's picture sharpened (SHARPEN). Writes the screen, with its noise and the depth.
|
|
229
|
+
const RCAS_FS = `/* glsl */ uniform sampler2D tSrc, tDepth; uniform vec2 uPx; uniform float uSharp, uTime; varying vec2 vUv; ${SHARPEN}
|
|
230
|
+
vec3 at(float x, float y){ return texture2D(tSrc, (gl_FragCoord.xy + vec2(x, y)) * uPx).rgb; }
|
|
231
|
+
void main(){
|
|
232
|
+
vec3 c = sharpen(at(0.0, 1.0), at(-1.0, 0.0), at(0.0, 0.0), at(1.0, 0.0), at(0.0, -1.0), uSharp);
|
|
233
|
+
gl_FragColor = vec4(c + ${DITHER}, 1.0);
|
|
234
|
+
gl_FragDepth = texture2D(tDepth, vUv).x; }`;
|
|
235
|
+
|
|
236
|
+
export { AO_FS, BLOOM, DOF_COC, BLUR_FS, EASU_FS, FINAL_FS, GRADE, PRESENT_FS, QUAD_VS, RAY_FS, RCAS_FS };
|