Line data Source code
1 2 : export const HDR_VIEWER_VERTEX_SHADER = `
2 : out vec2 vUv;
3 :
4 : void main() {
5 : vUv = uv;
6 : gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
7 : }
8 : `
9 :
10 2 : export const HDR_VIEWER_FRAGMENT_SHADER = `
11 : in vec2 vUv;
12 : out vec4 frag_color;
13 :
14 : uniform sampler2D uImage;
15 : uniform mat3 uGamutToSRGB;
16 : uniform float uGain;
17 : uniform int uChannel;
18 : uniform bool uDither;
19 : uniform bool uClipWarnings;
20 : uniform vec2 uClipRange;
21 : uniform bool uCheckerboard;
22 : uniform float uCheckerSize;
23 :
24 : const float CHECK_DARK_LINEAR = 0.1329;
25 : const float CHECK_LIGHT_LINEAR = 0.3185;
26 :
27 : float linearToS(float a) {
28 : float s = sign(a);
29 : a = abs(a);
30 : return s * (a < 0.0031308 ? 12.92 * a : 1.055 * pow(a, 1.0 / 2.4) - 0.055);
31 : }
32 :
33 : vec3 linearToSRGB(vec3 c) {
34 : return vec3(linearToS(c.r), linearToS(c.g), linearToS(c.b));
35 : }
36 :
37 : float ign(vec2 p) {
38 : return fract(52.9829189 * fract(0.06711056 * p.x + 0.00583715 * p.y));
39 : }
40 :
41 : float tent(float r) {
42 : float rp = sqrt(2.0 * r);
43 : float rn = sqrt(2.0 * r + 1.0) - 1.0;
44 : return (r < 0.0) ? 0.5 * rn : 0.5 * rp;
45 : }
46 :
47 : void main() {
48 : vec4 texel = texture(uImage, vUv);
49 : vec3 mapped = uGamutToSRGB * texel.rgb;
50 :
51 : vec3 selected;
52 : if (uChannel == 1) selected = vec3(mapped.r);
53 : else if (uChannel == 2) selected = vec3(mapped.g);
54 : else if (uChannel == 3) selected = vec3(mapped.b);
55 : else if (uChannel == 4) selected = vec3(texel.a);
56 : else if (uChannel == 5)
57 : selected = vec3(dot(mapped, vec3(0.2126, 0.7152, 0.0722)));
58 : else selected = mapped;
59 :
60 : vec3 exposed = selected * uGain;
61 :
62 : vec3 composited = exposed;
63 : if (uCheckerboard && uChannel == 0) {
64 : vec2 cell = floor(gl_FragCoord.xy / uCheckerSize);
65 : float check = mod(cell.x + cell.y, 2.0) == 0.0
66 : ? CHECK_DARK_LINEAR
67 : : CHECK_LIGHT_LINEAR;
68 : composited += (1.0 - clamp(texel.a, 0.0, 1.0)) * vec3(check);
69 : }
70 :
71 : vec3 display = linearToSRGB(composited);
72 :
73 : if (uDither) {
74 : float r = ign(gl_FragCoord.xy) - 0.5;
75 : display += vec3(tent(r) / 255.0);
76 : }
77 :
78 : display = clamp(display, 0.0, 1.0);
79 :
80 : if (uClipWarnings) {
81 : float zebra1 =
82 : mod(floor((gl_FragCoord.x + gl_FragCoord.y) / 8.0), 2.0) == 0.0 ? 0.0 : 1.0;
83 : float zebra2 =
84 : mod(floor((gl_FragCoord.x - gl_FragCoord.y) / 8.0), 2.0) == 0.0 ? 0.0 : 1.0;
85 : bvec3 over = greaterThan(exposed, vec3(uClipRange.y));
86 : bvec3 under = lessThan(exposed, vec3(uClipRange.x));
87 : display = mix(display, vec3(zebra1), vec3(over));
88 : display = mix(display, vec3(zebra2), vec3(under));
89 : }
90 :
91 : frag_color = vec4(display, 1.0);
92 : }
93 : `
|