Skip to content

Commit a18d165

Browse files
alexey-romanovgithub-actions[bot]
authored andcommitted
Add ability to set near clip offset. Relevant for orthographic
projection only. GitOrigin-RevId: 3e8f61ac029282b2723fbcec7451e7014eda4c46
1 parent 4c399d8 commit a18d165

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

debug/3d-playground.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115

116116
this.mapProjection = 'globe';
117117
this.orthographicProjectionAtLowPitch = false;
118+
this.nearClipOffset = 0;
118119
this.buildingExtrusion = "extrusion";
119120
this.enableBuildings = true;
120121

@@ -264,6 +265,11 @@
264265
camera.addInput(demo3d, 'orthographicProjectionAtLowPitch').on('change', (ev) => {
265266
map.setCamera({'camera-projection': ev.value ? 'orthographic' : 'perspective'});
266267
});
268+
camera.addInput(demo3d, 'nearClipOffset', {min:-1000.0, max:1000.0}).on('change', (ev) => {
269+
map.setNearClipOffset(ev.value);
270+
});
271+
272+
267273

268274
// Sky
269275
const sky = gui.addFolder({title:'Sky', expanded:false});

src/geo/transform.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ class Transform {
214214
_pixelsPerMercatorPixel: number;
215215
_nearZ: number;
216216
_farZ: number;
217+
_nearClipOffset: number;
217218
_mercatorScaleRatio: number;
218219
_isCameraConstrained: boolean;
219220

@@ -243,6 +244,7 @@ class Transform {
243244
this._pitch = 0;
244245
this._nearZ = 0;
245246
this._farZ = 0;
247+
this._nearClipOffset = 0;
246248
this._unmodified = true;
247249
this._edgeInsets = new EdgeInsets();
248250
this._projMatrixCache = {};
@@ -286,6 +288,7 @@ class Transform {
286288
clone._pitch = this._pitch;
287289
clone._nearZ = this._nearZ;
288290
clone._farZ = this._farZ;
291+
clone._nearClipOffset = this._nearClipOffset;
289292
clone._averageElevation = this._averageElevation;
290293
clone._orthographicProjectionAtLowPitch = this._orthographicProjectionAtLowPitch;
291294
clone._unmodified = this._unmodified;
@@ -308,6 +311,16 @@ class Transform {
308311
this._updateCameraOnTerrain();
309312
this._calcMatrices();
310313
}
314+
315+
get nearClipOffset(): number {
316+
return this._nearClipOffset;
317+
}
318+
319+
set nearClipOffset(offset: number) {
320+
this._nearClipOffset = offset;
321+
this._calcMatrices();
322+
}
323+
311324
get depthOcclusionForSymbolsAndCircles(): boolean {
312325
return this.projection.name !== 'globe' && !this.isOrthographic;
313326
}
@@ -2407,7 +2420,7 @@ class Transform {
24072420
top += offset.y;
24082421
bottom += offset.y;
24092422

2410-
cameraToClip = this._camera.getCameraToClipOrthographic(left, right, bottom, top, this._nearZ, this._farZ);
2423+
cameraToClip = this._camera.getCameraToClipOrthographic(left, right, bottom, top, this._nearZ + this._nearClipOffset, this._farZ);
24112424

24122425
const mixValue =
24132426
this.pitch >= OrthographicPitchTranstionValue ? 1.0 : this.pitch / OrthographicPitchTranstionValue;

src/ui/map.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,37 @@ export class Map extends Camera {
39753975
return this._triggerCameraUpdate(camera);
39763976
}
39773977

3978+
/**
3979+
* Returns the map's near clip offset. Relevant for orthographic projection only.
3980+
*
3981+
* @memberof Map#
3982+
* @returns {number} The map's current near clip offset.
3983+
* @experimental
3984+
*
3985+
* @example
3986+
* map.getNearClipOffset();
3987+
*/
3988+
getNearClipOffset(): number { return this.transform.nearClipOffset; }
3989+
3990+
/**
3991+
* Sets the map's camera near clip offset value. Relevant for orthographic projection only.
3992+
*
3993+
* @memberof Map#
3994+
* @param {number} offset The near clip offset to set.
3995+
* @returns {Map} Returns itself to allow for method chaining.
3996+
* @experimental
3997+
*
3998+
* @example
3999+
* map.setNearClipOffset(30)
4000+
*/
4001+
setNearClipOffset(offset: number): this {
4002+
const needsUpdate = this.transform.nearClipOffset !== offset;
4003+
4004+
this.transform.nearClipOffset = offset;
4005+
4006+
return this._update(needsUpdate);
4007+
}
4008+
39784009
_triggerCameraUpdate(camera: CameraSpecification): this {
39794010
return this._update(this.transform.setOrthographicProjectionAtLowPitch(camera['camera-projection'] === 'orthographic'));
39804011
}

0 commit comments

Comments
 (0)