11 static constexpr
const char* vertex = R
"(// floor(127 / 2) == 63.0
12 // the maximum allowed miter limit is 2.0 at the moment. the extrude normal is
13 // stored in a byte (-128..127). we scale regular normals up to length 63, but
14 // there are also "special" normals that have a bigger length (of up to 126 in
17 #define scale 0.015873016
19 layout (location = 0) in vec2 a_pos_normal;
20 layout (location = 1) in vec4 a_data;
22 uniform mat4 u_matrix;
23 uniform mediump float u_ratio;
24 uniform vec2 u_units_to_pixels;
25 uniform lowp float u_device_pixel_ratio;
29 out float v_gamma_scale;
30 out highp float v_linesofar;
32 #ifndef HAS_UNIFORM_u_color
33 uniform lowp float u_color_t;
34 layout (location = 2) in highp vec4 a_color;
37 uniform highp vec4 u_color;
39 #ifndef HAS_UNIFORM_u_blur
40 uniform lowp float u_blur_t;
41 layout (location = 3) in lowp vec2 a_blur;
44 uniform lowp float u_blur;
46 #ifndef HAS_UNIFORM_u_opacity
47 uniform lowp float u_opacity_t;
48 layout (location = 4) in lowp vec2 a_opacity;
49 out lowp float opacity;
51 uniform lowp float u_opacity;
53 #ifndef HAS_UNIFORM_u_gapwidth
54 uniform lowp float u_gapwidth_t;
55 layout (location = 5) in mediump vec2 a_gapwidth;
57 uniform mediump float u_gapwidth;
59 #ifndef HAS_UNIFORM_u_offset
60 uniform lowp float u_offset_t;
61 layout (location = 6) in lowp vec2 a_offset;
63 uniform lowp float u_offset;
65 #ifndef HAS_UNIFORM_u_width
66 uniform lowp float u_width_t;
67 layout (location = 7) in mediump vec2 a_width;
69 uniform mediump float u_width;
73 #ifndef HAS_UNIFORM_u_color
74 color = unpack_mix_color(a_color, u_color_t);
76 highp vec4 color = u_color;
78 #ifndef HAS_UNIFORM_u_blur
79 blur = unpack_mix_vec2(a_blur, u_blur_t);
81 lowp float blur = u_blur;
83 #ifndef HAS_UNIFORM_u_opacity
84 opacity = unpack_mix_vec2(a_opacity, u_opacity_t);
86 lowp float opacity = u_opacity;
88 #ifndef HAS_UNIFORM_u_gapwidth
89 mediump float gapwidth = unpack_mix_vec2(a_gapwidth, u_gapwidth_t);
91 mediump float gapwidth = u_gapwidth;
93 #ifndef HAS_UNIFORM_u_offset
94 lowp float offset = unpack_mix_vec2(a_offset, u_offset_t);
96 lowp float offset = u_offset;
98 #ifndef HAS_UNIFORM_u_width
99 mediump float width = unpack_mix_vec2(a_width, u_width_t);
101 mediump float width = u_width;
104 // the distance over which the line edge fades out.
105 // Retina devices need a smaller distance to avoid aliasing.
106 float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
108 vec2 a_extrude = a_data.xy - 128.0;
109 float a_direction = mod(a_data.z, 4.0) - 1.0;
111 v_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0;
113 vec2 pos = floor(a_pos_normal * 0.5);
115 // x is 1 if it's a round cap, 0 otherwise
116 // y is 1 if the normal points up, and -1 if it points down
117 // We store these in the least significant bit of a_pos_normal
118 mediump vec2 normal = a_pos_normal - 2.0 * pos;
119 normal.y = normal.y * 2.0 - 1.0;
122 // these transformations used to be applied in the JS and native code bases.
123 // moved them into the shader for clarity and simplicity.
124 gapwidth = gapwidth / 2.0;
125 float halfwidth = width / 2.0;
126 offset = -1.0 * offset;
128 float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0);
129 float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING);
131 // Scale the extrusion vector down to a normal and then up by the line width
133 mediump vec2 dist = outset * a_extrude * scale;
135 // Calculate the offset when drawing a line that is to the side of the actual line.
136 // We do this by creating a vector that points towards the extrude, but rotate
137 // it when we're drawing round end points (a_direction = -1 or 1) since their
138 // extrude vector points in another direction.
139 mediump float u = 0.5 * a_direction;
140 mediump float t = 1.0 - abs(u);
141 mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t);
143 vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0);
144 gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude;
146 // calculate how much the perspective view squishes or stretches the extrude
147 float extrude_length_without_perspective = length(dist);
148 float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels);
149 v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective;
151 v_width2 = vec2(outset, inset);
154 static constexpr
const char* fragment = R
"(uniform lowp float u_device_pixel_ratio;
158 in float v_gamma_scale;
160 #ifndef HAS_UNIFORM_u_color
163 uniform highp vec4 u_color;
165 #ifndef HAS_UNIFORM_u_blur
168 uniform lowp float u_blur;
170 #ifndef HAS_UNIFORM_u_opacity
171 in lowp float opacity;
173 uniform lowp float u_opacity;
177 #ifdef HAS_UNIFORM_u_color
178 highp vec4 color = u_color;
180 #ifdef HAS_UNIFORM_u_blur
181 lowp float blur = u_blur;
183 #ifdef HAS_UNIFORM_u_opacity
184 lowp float opacity = u_opacity;
187 // Calculate the distance of the pixel from the line in pixels.
188 float dist = length(v_normal) * v_width2.s;
190 // Calculate the antialiasing fade factor. This is either when fading in
191 // the line in case of an offset line (v_width2.t) or when fading out
193 float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
194 float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
196 fragColor = color * (alpha * opacity);
198 #ifdef OVERDRAW_INSPECTOR
199 fragColor = vec4(1.0);
@ OpenGL
The OpenGL API backend.
BuiltIn
This enum is used with the ShaderSource template to select source code for the desired program and gr...
Select shader source based on a program type and a desired graphics API.