|
| 1 | +/** @import { Camera } from 'three' */ |
| 2 | +import { Vector3, Matrix4, MathUtils } from 'three'; |
| 3 | +import { OBB } from '3d-tiles-renderer/three'; |
| 4 | +import { BaseRegion } from '3d-tiles-renderer/three/plugins'; |
| 5 | + |
| 6 | +const _cameraWorld = /* @__PURE__ */ new Vector3(); |
| 7 | +const _cameraLocal = /* @__PURE__ */ new Vector3(); |
| 8 | +const _cartographic = { lat: 0, lon: 0, height: 0 }; |
| 9 | +const _east = /* @__PURE__ */ new Vector3(); |
| 10 | +const _north = /* @__PURE__ */ new Vector3(); |
| 11 | +const _up = /* @__PURE__ */ new Vector3(); |
| 12 | +const _center = /* @__PURE__ */ new Vector3(); |
| 13 | + |
| 14 | +const PILLAR_HEIGHT = 3000; |
| 15 | + |
| 16 | +/** |
| 17 | + * A load region shaped as a vertical pillar below the camera, aligned to the globe gravity |
| 18 | + * direction derived from the tile renderer's ellipsoid. Intended for use with LoadRegionPlugin |
| 19 | + * to force higher-quality tile loading directly below the camera, preventing coarse LoD tiles |
| 20 | + * from producing large height discontinuities under the camera (e.g. with Google Photorealistic |
| 21 | + * Tiles) |
| 22 | + * |
| 23 | + * @param {Object} [options] |
| 24 | + * @param {Camera} options.camera The camera whose position drives the pillar placement. |
| 25 | + * @param {number} [options.radius=1000] Half-width of the pillar in world units. |
| 26 | + * @param {number} [options.errorTarget=10] Geometric error target for tiles inside the region. |
| 27 | + */ |
| 28 | +export class CameraCartographicRegion extends BaseRegion { |
| 29 | + |
| 30 | + constructor( options = {} ) { |
| 31 | + |
| 32 | + const { |
| 33 | + camera, |
| 34 | + radius = 1000, |
| 35 | + ...rest |
| 36 | + } = options; |
| 37 | + |
| 38 | + super( rest ); |
| 39 | + |
| 40 | + this.camera = camera; |
| 41 | + this.radius = radius; |
| 42 | + |
| 43 | + this.obb = new OBB(); |
| 44 | + this._cameraMatrix = new Matrix4(); |
| 45 | + this._localCameraPos = new Vector3(); |
| 46 | + this._localUp = new Vector3(); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + calculateDistance( boundingVolume ) { |
| 51 | + |
| 52 | + return boundingVolume.distanceToPoint( this._localCameraPos ); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + calculateError( tile, tilesRenderer ) { |
| 57 | + |
| 58 | + // calculate the error so it drops off with distance to enable progressive loading |
| 59 | + this._updateOBB( tilesRenderer ); |
| 60 | + |
| 61 | + const boundingVolume = tile.engineData.boundingVolume; |
| 62 | + const dist = boundingVolume.distanceToPoint( this._localCameraPos ); |
| 63 | + const t = Math.min( dist / this.radius, 1 ); |
| 64 | + |
| 65 | + const minTarget = tile.geometricError - this.errorTarget + tilesRenderer.errorTarget; |
| 66 | + const maxTarget = tile.geometricError - this.errorTarget * 5 + tilesRenderer.errorTarget; |
| 67 | + return MathUtils.lerp( minTarget, maxTarget, t ); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + intersectsTile( boundingVolume, tile, tilesRenderer ) { |
| 72 | + |
| 73 | + // update the obb and check the intersection with the tile bounding volume |
| 74 | + this._updateOBB( tilesRenderer ); |
| 75 | + |
| 76 | + const { obb } = this; |
| 77 | + return boundingVolume.intersectsOBB( obb ); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + _updateOBB( tilesRenderer ) { |
| 82 | + |
| 83 | + // update the OBB to live below the camera position toward the globe surface |
| 84 | + const { camera, radius, obb } = this; |
| 85 | + if ( ! camera.matrixWorld.equals( this._cameraMatrix ) ) { |
| 86 | + |
| 87 | + const { ellipsoid, group } = tilesRenderer; |
| 88 | + |
| 89 | + // Transform camera world position into the tile renderer's local (ellipsoid) space |
| 90 | + camera.getWorldPosition( _cameraWorld ); |
| 91 | + _cameraLocal.copy( _cameraWorld ).applyMatrix4( group.matrixWorldInverse ); |
| 92 | + this._localCameraPos.copy( _cameraLocal ); |
| 93 | + |
| 94 | + // Get the geodetic ENU axes at the camera's position on the ellipsoid |
| 95 | + ellipsoid.getPositionToCartographic( _cameraLocal, _cartographic ); |
| 96 | + ellipsoid.getEastNorthUpAxes( _cartographic.lat, _cartographic.lon, _east, _north, _up ); |
| 97 | + this._localUp.copy( _up ); |
| 98 | + |
| 99 | + // Pillar center: camera local position shifted half-depth downward (nadir = -up) |
| 100 | + _center.copy( _cameraLocal ).addScaledVector( _up, - PILLAR_HEIGHT / 2 ); |
| 101 | + |
| 102 | + // OBB transform: east/north/nadir as X/Y/Z column axes |
| 103 | + obb.transform.set( |
| 104 | + _east.x, _north.x, - _up.x, _center.x, |
| 105 | + _east.y, _north.y, - _up.y, _center.y, |
| 106 | + _east.z, _north.z, - _up.z, _center.z, |
| 107 | + 0, 0, 0, 1, |
| 108 | + ); |
| 109 | + |
| 110 | + obb.box.min.set( - radius, - radius, - PILLAR_HEIGHT / 2 ); |
| 111 | + obb.box.max.set( radius, radius, PILLAR_HEIGHT / 2 ); |
| 112 | + |
| 113 | + obb.update(); |
| 114 | + this._cameraMatrix.copy( camera.matrixWorld ); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments