Skip to content

Commit

Permalink
rev shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyiatmicrosoft committed Jul 17, 2024
1 parent dd1f3bb commit 348119a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 88 deletions.
7 changes: 0 additions & 7 deletions src/shaders/collision_box.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ vec4 projectTileWithElevation(vec2 posInTile, float elevation) {
void main() {
gl_Position = projectTileWithElevation(a_anchor_pos, get_elevation(a_anchor_pos));
gl_Position.xy = ((a_box_real + 0.5) * u_pixel_extrude_scale * 2.0 - 1.0) * vec2(1.0, -1.0) * gl_Position.w;
if (gl_Position.z / gl_Position.w < 1.1) {
// Globe projection would set Z beyond visible range if the anchor point gets hidden behind the planet's horizon.
// We force Z to a visible value, even for anchors that are slightly behind the horizon.
// Anchors that are too far beyond the horizon are still hidden.
gl_Position.z = 0.5;
}

v_placed = a_placed.x;
v_notUsed = a_placed.y;
}
31 changes: 6 additions & 25 deletions src/shaders/symbol_icon.vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const float PI = 3.141592653589793;

in vec4 a_pos_offset;
in vec4 a_data;
in vec4 a_pixeloffset;
Expand Down Expand Up @@ -27,10 +29,6 @@ uniform float u_pitched_scale;
out vec2 v_tex;
out float v_fade_opacity;

vec4 projectTileWithElevation(vec2 posInTile, float elevation) {
return u_matrix * vec4(posInTile, elevation, 1.0);
}

#pragma mapbox: define lowp float opacity

void main() {
Expand Down Expand Up @@ -58,9 +56,7 @@ void main() {
size = u_size;
}

vec2 translated_a_pos = a_pos + u_translation;
vec4 projectedPoint = projectTileWithElevation(translated_a_pos, ele);

vec4 projectedPoint = u_matrix * vec4(a_pos, ele, 1);
highp float camera_to_anchor_distance = projectedPoint.w;
// See comments in symbol_sdf.vertex
highp float distance_ratio = u_pitch_with_map ?
Expand All @@ -78,7 +74,7 @@ void main() {
highp float symbol_rotation = 0.0;
if (u_rotate_symbol) {
// See comments in symbol_sdf.vertex
vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), ele);
vec4 offsetProjectedPoint = u_matrix * vec4(a_pos + vec2(1, 0), ele, 1);

vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
Expand All @@ -90,24 +86,9 @@ void main() {
highp float angle_cos = cos(segment_angle + symbol_rotation);
mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

vec4 projected_pos;
if (u_is_along_line || u_is_variable_anchor) {
projected_pos = vec4(a_projected_pos.xy, ele, 1.0);
} else if (u_pitch_with_map) {
projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, ele, 1.0);
} else {
projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, ele);
}

vec4 projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy, ele, 1.0);
float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

float projectionScaling = 1.0;

vec4 finalPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * max(a_minFontScale, fontScale) + a_pxoffset / 16.0) * projectionScaling, z, 1.0);
if(u_pitch_with_map) {
finalPos = projectTileWithElevation(finalPos.xy, finalPos.z);
}
gl_Position = finalPos;
gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * max(a_minFontScale, fontScale) + a_pxoffset / 16.0), z, 1.0);

v_tex = a_tex / u_texsize;
vec2 fade_opacity = unpack_opacity(a_fade_opacity);
Expand Down
39 changes: 10 additions & 29 deletions src/shaders/symbol_sdf.vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const float PI = 3.141592653589793;

in vec4 a_pos_offset;
in vec4 a_data;
in vec4 a_pixeloffset;
Expand All @@ -20,24 +22,21 @@ uniform mat4 u_label_plane_matrix;
uniform mat4 u_coord_matrix;
uniform bool u_is_text;
uniform bool u_pitch_with_map;
uniform bool u_is_along_line;
uniform bool u_is_variable_anchor;
uniform highp float u_pitch;
uniform bool u_rotate_symbol;
uniform highp float u_aspect_ratio;
uniform highp float u_camera_to_center_distance;
uniform float u_fade_change;
uniform vec2 u_texsize;

uniform bool u_is_along_line;
uniform bool u_is_variable_anchor;
uniform vec2 u_translation;
uniform float u_pitched_scale;

out vec2 v_data0;
out vec3 v_data1;

vec4 projectTileWithElevation(vec2 posInTile, float elevation) {
return u_matrix * vec4(posInTile, elevation, 1.0);
}

#pragma mapbox: define highp vec4 fill_color
#pragma mapbox: define highp vec4 halo_color
#pragma mapbox: define lowp float opacity
Expand Down Expand Up @@ -72,9 +71,7 @@ void main() {
size = u_size;
}

vec2 translated_a_pos = a_pos + u_translation;
vec4 projectedPoint = projectTileWithElevation(translated_a_pos, ele);

vec4 projectedPoint = u_matrix * vec4(a_pos, ele, 1);
highp float camera_to_anchor_distance = projectedPoint.w;
// If the label is pitched with the map, layout is done in pitched space,
// which makes labels in the distance smaller relative to viewport space.
Expand All @@ -99,7 +96,7 @@ void main() {
// Point labels with 'rotation-alignment: map' are horizontal with respect to tile units
// To figure out that angle in projected space, we draw a short horizontal line in tile
// space, project it, and measure its angle in projected space.
vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), ele);
vec4 offsetProjectedPoint = u_matrix * vec4(a_pos + vec2(1, 0), ele, 1);

vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
Expand All @@ -111,26 +108,10 @@ void main() {
highp float angle_cos = cos(segment_angle + symbol_rotation);
mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

vec4 projected_pos;
if (u_is_along_line || u_is_variable_anchor) {
// Label plane matrix is identity in this case
projected_pos = vec4(a_projected_pos.xy, ele, 1.0);
} else if (u_pitch_with_map) {
projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, ele, 1.0);
} else {
projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, ele);
}

vec4 projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy, ele, 1.0);
float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

float projectionScaling = 1.0;

vec4 finalPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale + a_pxoffset) * projectionScaling, z, 1.0);
if(u_pitch_with_map) {
finalPos = projectTileWithElevation(finalPos.xy, finalPos.z);
}
float gamma_scale = finalPos.w;
gl_Position = finalPos;
gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale + a_pxoffset), z, 1.0);
float gamma_scale = gl_Position.w;

vec2 fade_opacity = unpack_opacity(a_fade_opacity);
float visibility = calculate_visibility(projectedPoint);
Expand Down
38 changes: 11 additions & 27 deletions src/shaders/symbol_text_and_icon.vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const float PI = 3.141592653589793;

in vec4 a_pos_offset;
in vec4 a_data;
in vec3 a_projected_pos;
Expand Down Expand Up @@ -26,6 +28,7 @@ uniform highp float u_camera_to_center_distance;
uniform float u_fade_change;
uniform vec2 u_texsize;
uniform vec2 u_texsize_icon;

uniform bool u_is_along_line;
uniform bool u_is_variable_anchor;
uniform vec2 u_translation;
Expand All @@ -34,10 +37,6 @@ uniform float u_pitched_scale;
out vec4 v_data0;
out vec4 v_data1;

vec4 projectTileWithElevation(vec2 posInTile, float elevation) {
return u_matrix * vec4(posInTile, elevation, 1.0);
}

#pragma mapbox: define highp vec4 fill_color
#pragma mapbox: define highp vec4 halo_color
#pragma mapbox: define lowp float opacity
Expand Down Expand Up @@ -72,9 +71,7 @@ void main() {
size = u_size;
}

vec2 translated_a_pos = a_pos + u_translation;
vec4 projectedPoint = projectTileWithElevation(translated_a_pos, ele);

vec4 projectedPoint = u_matrix * vec4(a_pos, ele, 1);
highp float camera_to_anchor_distance = projectedPoint.w;
// If the label is pitched with the map, layout is done in pitched space,
// which makes labels in the distance smaller relative to viewport space.
Expand All @@ -96,8 +93,10 @@ void main() {

highp float symbol_rotation = 0.0;
if (u_rotate_symbol) {
// See comments in symbol_sdf.vertex
vec4 offsetProjectedPoint = projectTileWithElevation(translated_a_pos + vec2(1, 0), ele);
// Point labels with 'rotation-alignment: map' are horizontal with respect to tile units
// To figure out that angle in projected space, we draw a short horizontal line in tile
// space, project it, and measure its angle in projected space.
vec4 offsetProjectedPoint = u_matrix * vec4(a_pos + vec2(1, 0), ele, 1);

vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;
Expand All @@ -109,25 +108,10 @@ void main() {
highp float angle_cos = cos(segment_angle + symbol_rotation);
mat2 rotation_matrix = mat2(angle_cos, -1.0 * angle_sin, angle_sin, angle_cos);

vec4 projected_pos;
if (u_is_along_line || u_is_variable_anchor) {
projected_pos = vec4(a_projected_pos.xy, ele, 1.0);
} else if (u_pitch_with_map) {
projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy + u_translation, ele, 1.0);
} else {
projected_pos = u_label_plane_matrix * projectTileWithElevation(a_projected_pos.xy + u_translation, ele);
}

vec4 projected_pos = u_label_plane_matrix * vec4(a_projected_pos.xy, ele, 1.0);
float z = float(u_pitch_with_map) * projected_pos.z / projected_pos.w;

float projectionScaling = 1.0;

vec4 finalPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale) * projectionScaling, z, 1.0);
if(u_pitch_with_map) {
finalPos = projectTileWithElevation(finalPos.xy, finalPos.z);
}
float gamma_scale = finalPos.w;
gl_Position = finalPos;
gl_Position = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale), z, 1.0);
float gamma_scale = gl_Position.w;

vec2 fade_opacity = unpack_opacity(a_fade_opacity);
float visibility = calculate_visibility(projectedPoint);
Expand Down

0 comments on commit 348119a

Please sign in to comment.