Skip to content

Commit 0aeac5d

Browse files
Deduplicate perspective ratio math
1 parent 4109f61 commit 0aeac5d

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

src/render/draw_symbol.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ function updateVariableAnchorsForBucket(bucket, rotateWithMap, pitchWithMap, var
156156
} else {
157157
const tileAnchor = new Point(symbol.anchorX, symbol.anchorY);
158158
const projectedAnchor = symbolProjection.project(tileAnchor, pitchWithMap ? posMatrix : labelPlaneMatrix);
159-
const perspectiveRatio = 0.5 + 0.5 * (transform.cameraToCenterDistance / projectedAnchor.signedDistanceFromCamera);
159+
const perspectiveRatio = symbolProjection.getPerspectiveRatio(transform.cameraToCenterDistance, projectedAnchor.signedDistanceFromCamera);
160160
let renderTextSize = symbolSize.evaluateSizeForFeature(bucket.textSizeData, size, symbol) * perspectiveRatio / ONE_EM;
161161
if (pitchWithMap) {
162162
// Go from size in pixels to equivalent size in tile units

src/symbol/collision_index.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ class CollisionIndex {
103103
textPixelPadding: number): { circles: Array<number>, offscreen: boolean, collisionDetected: boolean } {
104104
const placedCollisionCircles = [];
105105

106-
const perspectiveRatio = this.projectAnchor(posMatrix, symbol.anchorX, symbol.anchorY).perspectiveRatio;
106+
const tileUnitAnchorPoint = new Point(symbol.anchorX, symbol.anchorY);
107+
const screenAnchorPoint = projection.project(tileUnitAnchorPoint, posMatrix);
108+
const perspectiveRatio = projection.getPerspectiveRatio(this.transform.cameraToCenterDistance, screenAnchorPoint.signedDistanceFromCamera);
107109
const labelPlaneFontSize = pitchWithMap ? fontSize / perspectiveRatio : fontSize * perspectiveRatio;
108110
const labelPlaneFontScale = labelPlaneFontSize / ONE_EM;
109111

110-
const tileUnitAnchorPoint = new Point(symbol.anchorX, symbol.anchorY);
111112
const labelPlaneAnchorPoint = projection.project(tileUnitAnchorPoint, labelPlaneMatrix).point;
112113

113114
const projectionCache = {};
@@ -330,15 +331,6 @@ class CollisionIndex {
330331
}
331332
}
332333

333-
projectAnchor(posMatrix: mat4, x: number, y: number) {
334-
const p = [x, y, 0, 1];
335-
projection.xyTransformMat4(p, p, posMatrix);
336-
return {
337-
perspectiveRatio: 0.5 + 0.5 * (this.transform.cameraToCenterDistance / p[3]),
338-
cameraDistance: p[3]
339-
};
340-
}
341-
342334
projectPoint(posMatrix: mat4, x: number, y: number) {
343335
const p = [x, y, 0, 1];
344336
projection.xyTransformMat4(p, p, posMatrix);

src/symbol/projection.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
} from '../data/array_types';
1717
import {WritingMode} from '../symbol/shaping';
1818

19-
export {updateLineLabels, hideGlyphs, getLabelPlaneMatrix, getGlCoordMatrix, project, placeFirstAndLastGlyph, placeGlyphAlongLine, xyTransformMat4};
19+
export {updateLineLabels, hideGlyphs, getLabelPlaneMatrix, getGlCoordMatrix, project, getPerspectiveRatio, placeFirstAndLastGlyph, placeGlyphAlongLine, xyTransformMat4};
2020

2121
/*
2222
* # Overview of coordinate spaces
@@ -113,6 +113,10 @@ function project(point: Point, matrix: mat4) {
113113
};
114114
}
115115

116+
function getPerspectiveRatio(cameraToCenterDistance: number, signedDistanceFromCamera: number): number {
117+
return 0.5 + 0.5 * (cameraToCenterDistance / signedDistanceFromCamera);
118+
}
119+
116120
function isVisible(anchorPos: [number, number, number, number],
117121
clippingBuffer: [number, number]) {
118122
const x = anchorPos[0] / anchorPos[3];
@@ -178,7 +182,7 @@ function updateLineLabels(bucket: SymbolBucket,
178182
}
179183

180184
const cameraToAnchorDistance = anchorPos[3];
181-
const perspectiveRatio = 0.5 + 0.5 * (painter.transform.cameraToCenterDistance / cameraToAnchorDistance);
185+
const perspectiveRatio = getPerspectiveRatio(painter.transform.cameraToCenterDistance, cameraToAnchorDistance);
182186

183187
const fontSize = symbolSize.evaluateSizeForFeature(sizeData, partiallyEvaluatedSize, symbol);
184188
const pitchScaledFontSize = pitchWithMap ? fontSize / perspectiveRatio : fontSize * perspectiveRatio;

0 commit comments

Comments
 (0)