Skip to content

Commit

Permalink
this rotates labels as I intended it to when pitchWithMap = true, rot…
Browse files Browse the repository at this point in the history
…ateWithMap= false, but I don't think that's what we want
  • Loading branch information
NathanMOlson committed Oct 1, 2024
1 parent fce2547 commit 01845a9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/render/draw_symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import type {ColorMode} from '../gl/color_mode';
import type {Program} from './program';
import type {TextAnchor} from '../style/style_layer/variable_text_anchor';
import {getGlCoordMatrix, getPerspectiveRatio, getPitchedLabelPlaneMatrix, hideGlyphs, projectWithMatrix, projectTileCoordinatesToClipSpace, projectTileCoordinatesToLabelPlane, SymbolProjectionContext, updateLineLabels} from '../symbol/projection';
import {translatePosition} from '../util/util';
import {degreesToRadians, translatePosition} from '../util/util';
import type {ProjectionData} from '../geo/projection/projection_data';

type SymbolTileRenderState = {
Expand Down Expand Up @@ -401,22 +401,23 @@ function drawLayerSymbols(
const uLabelPlaneMatrix = noLabelPlane ? identityMat4 : combinedLabelPlaneMatrix;

const hasHalo = isSDF && layer.paint.get(isText ? 'text-halo-width' : 'icon-halo-width').constantOr(1) !== 0;
const symbolRotation = degreesToRadians(transform.roll);

let uniformValues: UniformValues<SymbolSDFUniformsType | SymbolIconUniformsType>;
if (isSDF) {
if (!bucket.iconsInText) {
uniformValues = symbolSDFUniformValues(sizeData.kind,
size, rotateInShader, pitchWithMap, alongLine, shaderVariableAnchor, painter,
uLabelPlaneMatrix, glCoordMatrixForShader, translation, isText, texSize, true, pitchedTextRescaling);
uLabelPlaneMatrix, glCoordMatrixForShader, translation, isText, texSize, true, pitchedTextRescaling, symbolRotation);
} else {
uniformValues = symbolTextAndIconUniformValues(sizeData.kind,
size, rotateInShader, pitchWithMap, alongLine, shaderVariableAnchor, painter,
uLabelPlaneMatrix, glCoordMatrixForShader, translation, texSize, texSizeIcon, pitchedTextRescaling);
uLabelPlaneMatrix, glCoordMatrixForShader, translation, texSize, texSizeIcon, pitchedTextRescaling, symbolRotation);
}
} else {
uniformValues = symbolIconUniformValues(sizeData.kind,
size, rotateInShader, pitchWithMap, alongLine, shaderVariableAnchor, painter,
uLabelPlaneMatrix, glCoordMatrixForShader, translation, isText, texSize, pitchedTextRescaling);
uLabelPlaneMatrix, glCoordMatrixForShader, translation, isText, texSize, pitchedTextRescaling, symbolRotation);
}

const state = {
Expand Down
22 changes: 16 additions & 6 deletions src/render/program/symbol_program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type SymbolIconUniformsType = {
'u_size_t': Uniform1f;
'u_size': Uniform1f;
'u_camera_to_center_distance': Uniform1f;
'u_symbol_rotation': Uniform1f;
'u_pitch': Uniform1f;
'u_rotate_symbol': Uniform1i;
'u_aspect_ratio': Uniform1f;
Expand All @@ -34,6 +35,7 @@ export type SymbolSDFUniformsType = {
'u_size_t': Uniform1f;
'u_size': Uniform1f;
'u_camera_to_center_distance': Uniform1f;
'u_symbol_rotation': Uniform1f;
'u_pitch': Uniform1f;
'u_rotate_symbol': Uniform1i;
'u_aspect_ratio': Uniform1f;
Expand All @@ -59,6 +61,7 @@ export type symbolTextAndIconUniformsType = {
'u_size_t': Uniform1f;
'u_size': Uniform1f;
'u_camera_to_center_distance': Uniform1f;
'u_symbol_rotation': Uniform1f;
'u_pitch': Uniform1f;
'u_rotate_symbol': Uniform1i;
'u_aspect_ratio': Uniform1f;
Expand Down Expand Up @@ -86,6 +89,7 @@ const symbolIconUniforms = (context: Context, locations: UniformLocations): Symb
'u_size_t': new Uniform1f(context, locations.u_size_t),
'u_size': new Uniform1f(context, locations.u_size),
'u_camera_to_center_distance': new Uniform1f(context, locations.u_camera_to_center_distance),
'u_symbol_rotation': new Uniform1f(context, locations.u_symbol_rotation),
'u_pitch': new Uniform1f(context, locations.u_pitch),
'u_rotate_symbol': new Uniform1i(context, locations.u_rotate_symbol),
'u_aspect_ratio': new Uniform1f(context, locations.u_aspect_ratio),
Expand All @@ -108,6 +112,7 @@ const symbolSDFUniforms = (context: Context, locations: UniformLocations): Symbo
'u_size_t': new Uniform1f(context, locations.u_size_t),
'u_size': new Uniform1f(context, locations.u_size),
'u_camera_to_center_distance': new Uniform1f(context, locations.u_camera_to_center_distance),
'u_symbol_rotation': new Uniform1f(context, locations.u_symbol_rotation),
'u_pitch': new Uniform1f(context, locations.u_pitch),
'u_rotate_symbol': new Uniform1i(context, locations.u_rotate_symbol),
'u_aspect_ratio': new Uniform1f(context, locations.u_aspect_ratio),
Expand All @@ -133,6 +138,7 @@ const symbolTextAndIconUniforms = (context: Context, locations: UniformLocations
'u_size_t': new Uniform1f(context, locations.u_size_t),
'u_size': new Uniform1f(context, locations.u_size),
'u_camera_to_center_distance': new Uniform1f(context, locations.u_camera_to_center_distance),
'u_symbol_rotation': new Uniform1f(context, locations.u_symbol_rotation),
'u_pitch': new Uniform1f(context, locations.u_pitch),
'u_rotate_symbol': new Uniform1i(context, locations.u_rotate_symbol),
'u_aspect_ratio': new Uniform1f(context, locations.u_aspect_ratio),
Expand Down Expand Up @@ -170,7 +176,8 @@ const symbolIconUniformValues = (
translation: [number, number],
isText: boolean,
texSize: [number, number],
pitchedScale: number
pitchedScale: number,
symbolRotation: number
): UniformValues<SymbolIconUniformsType> => {
const transform = painter.transform;

Expand All @@ -193,7 +200,8 @@ const symbolIconUniformValues = (
'u_texsize': texSize,
'u_texture': 0,
'u_translation': translation,
'u_pitched_scale': pitchedScale
'u_pitched_scale': pitchedScale,
'u_symbol_rotation': symbolRotation
};
};

Expand All @@ -214,13 +222,14 @@ const symbolSDFUniformValues = (
isText: boolean,
texSize: [number, number],
isHalo: boolean,
pitchedScale: number
pitchedScale: number,
symbolRotation: number
): UniformValues<SymbolSDFUniformsType> => {
const transform = painter.transform;

return extend(symbolIconUniformValues(functionType, size,
rotateInShader, pitchWithMap, isAlongLine, isVariableAnchor, painter, labelPlaneMatrix,
glCoordMatrix, translation, isText, texSize, pitchedScale), {
glCoordMatrix, translation, isText, texSize, pitchedScale, symbolRotation), {
'u_gamma_scale': (pitchWithMap ? Math.cos(transform.pitch * Math.PI / 180.0) * transform.cameraToCenterDistance : 1),
'u_device_pixel_ratio': painter.pixelRatio,
'u_is_halo': +isHalo
Expand All @@ -243,11 +252,12 @@ const symbolTextAndIconUniformValues = (
translation: [number, number],
texSizeSDF: [number, number],
texSizeIcon: [number, number],
pitchedScale: number
pitchedScale: number,
symbolRotation: number
): UniformValues<SymbolIconUniformsType> => {
return extend(symbolSDFUniformValues(functionType, size,
rotateInShader, pitchWithMap, isAlongLine, isVariableAnchor, painter, labelPlaneMatrix,
glCoordMatrix, translation, true, texSizeSDF, true, pitchedScale), {
glCoordMatrix, translation, true, texSizeSDF, true, pitchedScale, symbolRotation), {
'u_texsize_icon': texSizeIcon,
'u_texture_icon': 1
});
Expand Down
17 changes: 13 additions & 4 deletions src/shaders/symbol_icon.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ uniform bool u_is_size_feature_constant;
uniform highp float u_size_t; // used to interpolate between zoom stops when size is a composite function
uniform highp float u_size; // used when size is both zoom and feature constant
uniform highp float u_camera_to_center_distance;
uniform highp float u_symbol_rotation;
uniform highp float u_pitch;
uniform bool u_rotate_symbol;
uniform highp float u_aspect_ratio;
Expand Down Expand Up @@ -78,7 +79,7 @@ void main() {
vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
symbol_rotation += atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
}

highp float angle_sin = sin(segment_angle + symbol_rotation);
Expand All @@ -104,11 +105,19 @@ void main() {
}
#endif

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);
vec4 anchorScreenPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0);
vec4 screenPos = 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);
anchorScreenPos = projectTileWithElevation(anchorScreenPos.xy, anchorScreenPos.z);
screenPos = projectTileWithElevation(screenPos.xy, screenPos.z);
}
gl_Position = finalPos;

highp float angle_sin1 = sin(u_symbol_rotation);
highp float angle_cos1 = cos(u_symbol_rotation);
mat2 rotation_matrix1 = mat2(angle_cos1, -1.0 * angle_sin1 * u_aspect_ratio, angle_sin1 / u_aspect_ratio, angle_cos1);
screenPos.xy = anchorScreenPos.xy + rotation_matrix1 * (screenPos.xy - anchorScreenPos.xy);

gl_Position = screenPos;

v_tex = a_tex / u_texsize;
vec2 fade_opacity = unpack_opacity(a_fade_opacity);
Expand Down
19 changes: 14 additions & 5 deletions src/shaders/symbol_sdf.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 highp float u_symbol_rotation;
uniform float u_fade_change;
uniform vec2 u_texsize;
uniform vec2 u_translation;
Expand Down Expand Up @@ -99,7 +100,7 @@ void main() {
vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
symbol_rotation += atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
}

highp float angle_sin = sin(segment_angle + symbol_rotation);
Expand Down Expand Up @@ -127,12 +128,20 @@ void main() {
}
#endif

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);
vec4 anchorScreenPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0);
vec4 screenPos = 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);
anchorScreenPos = projectTileWithElevation(anchorScreenPos.xy, anchorScreenPos.z);
screenPos = projectTileWithElevation(screenPos.xy, screenPos.z);
}
float gamma_scale = finalPos.w;
gl_Position = finalPos;

highp float angle_sin1 = sin(u_symbol_rotation);
highp float angle_cos1 = cos(u_symbol_rotation);
mat2 rotation_matrix1 = mat2(angle_cos1, -1.0 * angle_sin1 * u_aspect_ratio, angle_sin1 / u_aspect_ratio, angle_cos1);
screenPos.xy = anchorScreenPos.xy + rotation_matrix1 * (screenPos.xy - anchorScreenPos.xy);

float gamma_scale = screenPos.w;
gl_Position = screenPos;

vec2 fade_opacity = unpack_opacity(a_fade_opacity);
float visibility = calculate_visibility(projectedPoint);
Expand Down
19 changes: 14 additions & 5 deletions src/shaders/symbol_text_and_icon.vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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 highp float u_symbol_rotation;
uniform float u_fade_change;
uniform vec2 u_texsize;
uniform vec2 u_texsize_icon;
Expand Down Expand Up @@ -97,7 +98,7 @@ void main() {
vec2 a = projectedPoint.xy / projectedPoint.w;
vec2 b = offsetProjectedPoint.xy / offsetProjectedPoint.w;

symbol_rotation = atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
symbol_rotation += atan((b.y - a.y) / u_aspect_ratio, b.x - a.x);
}

highp float angle_sin = sin(segment_angle + symbol_rotation);
Expand All @@ -123,12 +124,20 @@ void main() {
}
#endif

vec4 finalPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w + rotation_matrix * (a_offset / 32.0 * fontScale) * projectionScaling, z, 1.0);
vec4 anchorScreenPos = u_coord_matrix * vec4(projected_pos.xy / projected_pos.w, z, 1.0);
vec4 screenPos = 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);
anchorScreenPos = projectTileWithElevation(anchorScreenPos.xy, anchorScreenPos.z);
screenPos = projectTileWithElevation(screenPos.xy, screenPos.z);
}
float gamma_scale = finalPos.w;
gl_Position = finalPos;

highp float angle_sin1 = sin(u_symbol_rotation);
highp float angle_cos1 = cos(u_symbol_rotation);
mat2 rotation_matrix1 = mat2(angle_cos1, -1.0 * angle_sin1 * u_aspect_ratio, angle_sin1 / u_aspect_ratio, angle_cos1);
screenPos.xy = anchorScreenPos.xy + rotation_matrix1 * (screenPos.xy - anchorScreenPos.xy);

float gamma_scale = screenPos.w;
gl_Position = screenPos;

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

0 comments on commit 01845a9

Please sign in to comment.