-
-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial version of the basic line shader
- Loading branch information
1 parent
2ce98b3
commit 7c9385f
Showing
13 changed files
with
463 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
// Generated code, do not modify this file! | ||
#pragma once | ||
#include <mbgl/shaders/shader_source.hpp> | ||
|
||
namespace mbgl { | ||
namespace shaders { | ||
|
||
template <> | ||
struct ShaderSource<BuiltIn::LineBasicShader, gfx::Backend::Type::OpenGL> { | ||
static constexpr const char* name = "LineBasicShader"; | ||
static constexpr const char* vertex = R"(// floor(127 / 2) == 63.0 | ||
// the maximum allowed miter limit is 2.0 at the moment. the extrude normal is | ||
// stored in a byte (-128..127). we scale regular normals up to length 63, but | ||
// there are also "special" normals that have a bigger length (of up to 126 in | ||
// this case). | ||
// #define scale 63.0 | ||
#define scale 0.015873016 | ||
layout (location = 0) in vec2 a_pos_normal; | ||
layout (location = 1) in vec4 a_data; | ||
layout (std140) uniform LineBasicUBO { | ||
highp mat4 u_matrix; | ||
highp vec2 u_units_to_pixels; | ||
mediump float u_ratio; | ||
lowp float u_device_pixel_ratio; | ||
}; | ||
layout (std140) uniform LineBasicPropertiesUBO { | ||
highp vec4 u_color; | ||
lowp float u_blur; | ||
lowp float u_opacity; | ||
mediump float u_gapwidth; | ||
lowp float u_offset; | ||
mediump float u_width; | ||
highp float pad1; | ||
highp vec2 pad2; | ||
}; | ||
out vec2 v_normal; | ||
out vec2 v_width2; | ||
out float v_gamma_scale; | ||
out highp float v_linesofar; | ||
void main() { | ||
highp vec4 color = u_color; | ||
lowp float blur = u_blur; | ||
lowp float opacity = u_opacity; | ||
mediump float gapwidth = u_gapwidth; | ||
lowp float offset = u_offset; | ||
mediump float width = u_width; | ||
// the distance over which the line edge fades out. | ||
// Retina devices need a smaller distance to avoid aliasing. | ||
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0; | ||
vec2 a_extrude = a_data.xy - 128.0; | ||
float a_direction = mod(a_data.z, 4.0) - 1.0; | ||
v_linesofar = (floor(a_data.z / 4.0) + a_data.w * 64.0) * 2.0; | ||
vec2 pos = floor(a_pos_normal * 0.5); | ||
// x is 1 if it's a round cap, 0 otherwise | ||
// y is 1 if the normal points up, and -1 if it points down | ||
// We store these in the least significant bit of a_pos_normal | ||
mediump vec2 normal = a_pos_normal - 2.0 * pos; | ||
normal.y = normal.y * 2.0 - 1.0; | ||
v_normal = normal; | ||
// these transformations used to be applied in the JS and native code bases. | ||
// moved them into the shader for clarity and simplicity. | ||
gapwidth = gapwidth / 2.0; | ||
float halfwidth = width / 2.0; | ||
offset = -1.0 * offset; | ||
float inset = gapwidth + (gapwidth > 0.0 ? ANTIALIASING : 0.0); | ||
float outset = gapwidth + halfwidth * (gapwidth > 0.0 ? 2.0 : 1.0) + (halfwidth == 0.0 ? 0.0 : ANTIALIASING); | ||
// Scale the extrusion vector down to a normal and then up by the line width | ||
// of this vertex. | ||
mediump vec2 dist = outset * a_extrude * scale; | ||
// Calculate the offset when drawing a line that is to the side of the actual line. | ||
// We do this by creating a vector that points towards the extrude, but rotate | ||
// it when we're drawing round end points (a_direction = -1 or 1) since their | ||
// extrude vector points in another direction. | ||
mediump float u = 0.5 * a_direction; | ||
mediump float t = 1.0 - abs(u); | ||
mediump vec2 offset2 = offset * a_extrude * scale * normal.y * mat2(t, -u, u, t); | ||
vec4 projected_extrude = u_matrix * vec4(dist / u_ratio, 0.0, 0.0); | ||
gl_Position = u_matrix * vec4(pos + offset2 / u_ratio, 0.0, 1.0) + projected_extrude; | ||
// calculate how much the perspective view squishes or stretches the extrude | ||
float extrude_length_without_perspective = length(dist); | ||
float extrude_length_with_perspective = length(projected_extrude.xy / gl_Position.w * u_units_to_pixels); | ||
v_gamma_scale = extrude_length_without_perspective / extrude_length_with_perspective; | ||
v_width2 = vec2(outset, inset); | ||
} | ||
)"; | ||
static constexpr const char* fragment = R"(layout (std140) uniform LineBasicUBO { | ||
highp mat4 u_matrix; | ||
highp vec2 u_units_to_pixels; | ||
mediump float u_ratio; | ||
lowp float u_device_pixel_ratio; | ||
}; | ||
layout (std140) uniform LineBasicPropertiesUBO { | ||
highp vec4 u_color; | ||
lowp float u_blur; | ||
lowp float u_opacity; | ||
mediump float u_gapwidth; | ||
lowp float u_offset; | ||
mediump float u_width; | ||
highp float pad1; | ||
highp vec2 pad2; | ||
}; | ||
in vec2 v_width2; | ||
in vec2 v_normal; | ||
in float v_gamma_scale; | ||
void main() { | ||
highp vec4 color = u_color; | ||
lowp float blur = u_blur; | ||
lowp float opacity = u_opacity; | ||
// Calculate the distance of the pixel from the line in pixels. | ||
float dist = length(v_normal) * v_width2.s; | ||
// Calculate the antialiasing fade factor. This is either when fading in | ||
// the line in case of an offset line (v_width2.t) or when fading out | ||
// (v_width2.s) | ||
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale; | ||
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); | ||
fragColor = color * (alpha * opacity); | ||
#ifdef OVERDRAW_INSPECTOR | ||
fragColor = vec4(1.0); | ||
#endif | ||
} | ||
)"; | ||
}; | ||
|
||
} // namespace shaders | ||
} // namespace mbgl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
layout (std140) uniform LineBasicUBO { | ||
highp mat4 u_matrix; | ||
highp vec2 u_units_to_pixels; | ||
mediump float u_ratio; | ||
lowp float u_device_pixel_ratio; | ||
}; | ||
|
||
layout (std140) uniform LineBasicPropertiesUBO { | ||
highp vec4 u_color; | ||
lowp float u_blur; | ||
lowp float u_opacity; | ||
mediump float u_gapwidth; | ||
lowp float u_offset; | ||
mediump float u_width; | ||
|
||
highp float pad1; | ||
highp vec2 pad2; | ||
}; | ||
|
||
in vec2 v_width2; | ||
in vec2 v_normal; | ||
in float v_gamma_scale; | ||
|
||
void main() { | ||
highp vec4 color = u_color; | ||
lowp float blur = u_blur; | ||
lowp float opacity = u_opacity; | ||
|
||
// Calculate the distance of the pixel from the line in pixels. | ||
float dist = length(v_normal) * v_width2.s; | ||
|
||
// Calculate the antialiasing fade factor. This is either when fading in | ||
// the line in case of an offset line (v_width2.t) or when fading out | ||
// (v_width2.s) | ||
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale; | ||
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0); | ||
|
||
fragColor = color * (alpha * opacity); | ||
|
||
#ifdef OVERDRAW_INSPECTOR | ||
fragColor = vec4(1.0); | ||
#endif | ||
} |
Oops, something went wrong.