forked from lettier/3d-game-shaders-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup-table.frag
58 lines (40 loc) · 1.49 KB
/
lookup-table.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
(C) 2020 David Lettier
lettier.com
*/
#version 150
uniform vec2 pi;
uniform vec2 gamma;
uniform sampler2D colorTexture;
uniform sampler2D lookupTableTexture0;
uniform sampler2D lookupTableTexture1;
uniform vec2 sunPosition;
uniform vec2 enabled;
out vec4 fragColor;
void main() {
vec2 texSize = textureSize(colorTexture, 0).xy;
vec4 color = texture(colorTexture, gl_FragCoord.xy / texSize);
if (enabled.x != 1) { fragColor = color; return; }
color.rgb = pow(color.rgb, vec3(gamma.y));
float u = floor(color.b * 15.0) / 15.0 * 240.0;
u = (floor(color.r * 15.0) / 15.0 * 15.0) + u;
u /= 255.0;
float v = 1.0 - (floor(color.g * 15.0) / 15.0);
vec3 left0 = texture(lookupTableTexture0, vec2(u, v)).rgb;
vec3 left1 = texture(lookupTableTexture1, vec2(u, v)).rgb;
u = ceil(color.b * 15.0) / 15.0 * 240.0;
u = (ceil(color.r * 15.0) / 15.0 * 15.0) + u;
u /= 255.0;
v = 1.0 - (ceil(color.g * 15.0) / 15.0);
vec3 right0 = texture(lookupTableTexture0, vec2(u, v)).rgb;
vec3 right1 = texture(lookupTableTexture1, vec2(u, v)).rgb;
float sunPosition = sin(sunPosition.x * pi.y);
sunPosition = 0.5 * (sunPosition + 1);
vec3 left = mix(left0, left1, sunPosition);
vec3 right = mix(right0, right1, sunPosition);
color.r = mix(left.r, right.r, fract(color.r * 15.0));
color.g = mix(left.g, right.g, fract(color.g * 15.0));
color.b = mix(left.b, right.b, fract(color.b * 15.0));
color.rgb = pow(color.rgb, vec3(gamma.x));
fragColor = color;
}