Skip to content

Commit

Permalink
Expose projection matrix parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum committed Jun 5, 2024
1 parent 598760e commit 0bc2dff
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/geo/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export class Transform {
_constraining: boolean;
_posMatrixCache: {[_: string]: mat4};
_alignedPosMatrixCache: {[_: string]: mat4};
_farZ: number;
_nearZ: number;

constructor(minZoom?: number, maxZoom?: number, minPitch?: number, maxPitch?: number, renderWorldCopies?: boolean) {
this.tileSize = 512; // constant
Expand Down Expand Up @@ -870,7 +872,7 @@ export class Transform {
// Calculate z distance of the farthest fragment that should be rendered.
// Add a bit extra to avoid precision problems when a fragment's distance is exactly `furthestDistance`
const topHalfMinDistance = Math.min(topHalfSurfaceDistance, topHalfSurfaceDistanceHorizon);
const farZ = (Math.cos(Math.PI / 2 - this._pitch) * topHalfMinDistance + lowestPlane) * 1.01;
this._farZ = (Math.cos(Math.PI / 2 - this._pitch) * topHalfMinDistance + lowestPlane) * 1.01;

// The larger the value of nearZ is
// - the more depth precision is available for features (good)
Expand All @@ -879,11 +881,11 @@ export class Transform {
// Other values work for mapbox-gl-js but deck.gl was encountering precision issues
// when rendering custom layers. This value was experimentally chosen and
// seems to solve z-fighting issues in deck.gl while not clipping buildings too close to the camera.
const nearZ = this.height / 50;
this._nearZ = this.height / 50;

// matrix for conversion from location to clip space(-1 .. 1)
m = new Float64Array(16) as any;
mat4.perspective(m, this._fov, this.width / this.height, nearZ, farZ);
mat4.perspective(m, this._fov, this.width / this.height, this._nearZ, this._farZ);

// Apply center of perspective offset
m[8] = -offset.x * 2 / this.width;
Expand Down
2 changes: 1 addition & 1 deletion src/render/draw_custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function drawCustom(painter: Painter, sourceCache: SourceCache, layer: Cu

context.setDepthMode(depthMode);

implementation.render(context.gl, painter.transform.customLayerMatrix());
implementation.render(context.gl, painter.transform.customLayerMatrix(), {farZ: painter.transform._farZ, nearZ: painter.transform._nearZ, projMatrix: painter.transform.projMatrix});

Check failure on line 39 in src/render/draw_custom.ts

View workflow job for this annotation

GitHub Actions / Code Hygiene

Property 'projMatrix' does not exist on type 'Transform'.

context.setDirty();
painter.setBaseState();
Expand Down
3 changes: 2 additions & 1 deletion src/style/style_layer/custom_style_layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {LayerSpecification} from '@maplibre/maplibre-gl-style-spec';
* the `renderingMode` is `"3d"`, the z coordinate is conformal. A box with identical x, y, and z
* lengths in mercator units would be rendered as a cube. {@link MercatorCoordinate.fromLngLat}
* can be used to project a `LngLat` to a mercator coordinate.
* @param args - Argument object. Properties are farZ, nearZ, projMatrix.
*/
type CustomRenderMethod = (gl: WebGLRenderingContext|WebGL2RenderingContext, matrix: mat4) => void;
type CustomRenderMethod = (gl: WebGLRenderingContext|WebGL2RenderingContext, matrix: mat4, args: { farZ: number; nearZ: number; projMatrix: mat4 }) => void;

/**
* Interface for custom style layers. This is a specification for
Expand Down

0 comments on commit 0bc2dff

Please sign in to comment.