-
Notifications
You must be signed in to change notification settings - Fork 8
/
raster-layer-webgl2.fs.glsl
88 lines (72 loc) · 2.04 KB
/
raster-layer-webgl2.fs.glsl
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#version 300 es
#define SHADER_NAME raster-layer-fragment-shader
// Ref https://www.khronos.org/registry/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf#page=60
precision mediump float;
precision mediump int;
precision mediump usampler2D;
in vec2 vTexCoord;
in vec2 vTexPos;
out vec4 color;
uniform float desaturate;
uniform vec4 transparentColor;
uniform vec3 tintColor;
uniform float opacity;
uniform mediump float coordinateConversion;
uniform vec4 bounds;
/* projection utils */
const float TILE_SIZE = 512.0;
const float PI = 3.1415926536;
const float WORLD_SCALE = TILE_SIZE / PI / 2.0;
// from degrees to Web Mercator
vec2 lnglat_to_mercator(vec2 lnglat) {
float x = lnglat.x;
float y = clamp(lnglat.y, -89.9, 89.9);
return vec2(
radians(x) + PI,
PI + log(tan(PI * 0.25 + radians(y) * 0.5))
) * WORLD_SCALE;
}
// from Web Mercator to degrees
vec2 mercator_to_lnglat(vec2 xy) {
xy /= WORLD_SCALE;
return degrees(vec2(
xy.x - PI,
atan(exp(xy.y - PI)) * 2.0 - PI * 0.5
));
}
/* End projection utils */
// apply desaturation
vec3 color_desaturate(vec3 color) {
float luminance = (color.r + color.g + color.b) * 0.333333333;
return mix(color, vec3(luminance), desaturate);
}
// apply tint
vec3 color_tint(vec3 color) {
return color * tintColor;
}
// blend with background color
vec4 apply_opacity(vec3 color, float alpha) {
return mix(transparentColor, vec4(color, 1.0), alpha);
}
vec2 getUV(vec2 pos) {
return vec2(
(pos.x - bounds[0]) / (bounds[2] - bounds[0]),
(pos.y - bounds[3]) / (bounds[1] - bounds[3])
);
}
void main(void) {
vec2 uv = vTexCoord;
if (coordinateConversion < -0.5) {
vec2 lnglat = mercator_to_lnglat(vTexPos);
uv = getUV(lnglat);
} else if (coordinateConversion > 0.5) {
vec2 commonPos = lnglat_to_mercator(vTexPos);
uv = getUV(commonPos);
}
vec4 image;
DECKGL_CREATE_COLOR(image, uv);
DECKGL_MUTATE_COLOR(image, uv);
color = apply_opacity(color_tint(color_desaturate(image.rgb)), image.a * opacity);
geometry.uv = uv;
DECKGL_FILTER_COLOR(color, geometry);
}