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 Apr 27, 2024
1 parent 3f481c8 commit 9dadb77
Show file tree
Hide file tree
Showing 3 changed files with 10 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 deckgl was encountering precision issues
// when rendering custom layers. This value was experimentally chosen and
// seems to solve z-fighting issues in deckgl 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(), painter.transform);

context.setDirty();
painter.setBaseState();
Expand Down
5 changes: 4 additions & 1 deletion src/style/style_layer/custom_style_layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {StyleLayer} from '../style_layer';
import type {Map} from '../../ui/map';
import {mat4} from 'gl-matrix';
import {LayerSpecification} from '@maplibre/maplibre-gl-style-spec';
import {Transform} from '../../geo/transform';

/**
* @param gl - The map's gl context.
Expand All @@ -11,8 +12,10 @@ 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 transform - The map's transform object. It contains properties and methods used to calculate
* the map's matrix transformations.
*/
type CustomRenderMethod = (gl: WebGLRenderingContext|WebGL2RenderingContext, matrix: mat4) => void;
type CustomRenderMethod = (gl: WebGLRenderingContext|WebGL2RenderingContext, matrix: mat4, transform: Transform) => void;

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

0 comments on commit 9dadb77

Please sign in to comment.