@@ -2,18 +2,28 @@ import {
22 Scene ,
33 WebGLRenderer ,
44 PerspectiveCamera ,
5+ Raycaster ,
6+ Vector2 ,
7+ Matrix4 ,
8+ MathUtils ,
59} from 'three' ;
610import { TilesRenderer , GlobeControls , EnvironmentControls } from '3d-tiles-renderer' ;
7- import { TilesFadePlugin , UpdateOnChangePlugin , XYZTilesPlugin , } from '3d-tiles-renderer/plugins' ;
11+ import { TilesFadePlugin , UpdateOnChangePlugin , GeneratedSurfacePlugin , XYZTilesOverlay , CesiumIonOverlay } from '3d-tiles-renderer/plugins' ;
812import { GUI } from 'three/addons/libs/lil-gui.module.min.js' ;
913
1014let controls , scene , renderer ;
11- let tiles , camera ;
15+ let tiles , camera , surfacePlugin ;
16+
17+ const toLocalMat = new Matrix4 ( ) ;
18+ const raycaster = new Raycaster ( ) ;
19+ const mouse = new Vector2 ( ) ;
20+ const coordsEl = document . getElementById ( 'coords' ) ;
1221
1322const params = {
1423
1524 errorTarget : 1 ,
1625 planar : false ,
26+ overlay : 'OpenStreetMap' ,
1727
1828} ;
1929
@@ -44,10 +54,12 @@ function init() {
4454 // events
4555 onWindowResize ( ) ;
4656 window . addEventListener ( 'resize' , onWindowResize , false ) ;
57+ renderer . domElement . addEventListener ( 'mousemove' , onMouseMove , false ) ;
4758
4859 // gui initialization
4960 const gui = new GUI ( ) ;
5061 gui . add ( params , 'planar' ) . onChange ( initTiles ) ;
62+ gui . add ( params , 'overlay' , [ 'OpenStreetMap' , 'Sentinel-2' ] ) . onChange ( initTiles ) ;
5163 gui . add ( params , 'errorTarget' , 1 , 40 ) . onChange ( ( ) => {
5264
5365 tiles . getPluginByName ( 'UPDATE_ON_CHANGE_PLUGIN' ) . needsUpdate = true ;
@@ -73,21 +85,26 @@ function initTiles() {
7385
7486 }
7587
88+ const overlay = params . overlay === 'Sentinel-2'
89+ ? new CesiumIonOverlay ( { assetId : 3954 , apiToken : import . meta. env . VITE_ION_KEY } )
90+ : new XYZTilesOverlay ( { url : 'https://tile.openstreetmap.org/{z}/{x}/{y}.png' } ) ;
91+
7692 // tiles
7793 tiles = new TilesRenderer ( ) ;
7894 tiles . registerPlugin ( new TilesFadePlugin ( { maximumFadeOutTiles : 200 } ) ) ;
7995 tiles . registerPlugin ( new UpdateOnChangePlugin ( ) ) ;
80- tiles . registerPlugin ( new XYZTilesPlugin ( {
81- center : true ,
96+ surfacePlugin = new GeneratedSurfacePlugin ( {
97+ overlay ,
8298 shape : params . planar ? 'planar' : 'ellipsoid' ,
83- url : 'https://tile.openstreetmap.org/{z}/{x}/{y}.png'
84- } ) ) ;
99+ } ) ;
100+ tiles . registerPlugin ( surfacePlugin ) ;
85101
86102 tiles . lruCache . minSize = 900 ;
87103 tiles . lruCache . maxSize = 1300 ;
88104 tiles . parseQueue . maxJobs = 3 ;
89105 tiles . setCamera ( camera ) ;
90106 scene . add ( tiles . group ) ;
107+ window . TILES = tiles ;
91108
92109 if ( params . planar ) {
93110
@@ -158,6 +175,31 @@ function render() {
158175
159176}
160177
178+ function onMouseMove ( e ) {
179+
180+ mouse . x = ( e . clientX / window . innerWidth ) * 2 - 1 ;
181+ mouse . y = - ( e . clientY / window . innerHeight ) * 2 + 1 ;
182+
183+ raycaster . setFromCamera ( mouse , camera ) ;
184+ const hits = raycaster . intersectObject ( tiles . group , true ) ;
185+ if ( hits . length > 0 ) {
186+
187+ toLocalMat . copy ( tiles . group . matrixWorld ) . invert ( ) ;
188+ hits [ 0 ] . point . applyMatrix4 ( toLocalMat ) ;
189+
190+ const cart = surfacePlugin . getCartographicFromPosition ( hits [ 0 ] . point ) ;
191+ const lat = MathUtils . radToDeg ( cart . lat ) . toFixed ( 2 ) ;
192+ const lon = MathUtils . radToDeg ( cart . lon ) . toFixed ( 2 ) ;
193+ coordsEl . textContent = `${ lat } ° ${ lon } °` ;
194+
195+ } else {
196+
197+ coordsEl . textContent = '' ;
198+
199+ }
200+
201+ }
202+
161203function throttle ( callback ) {
162204
163205 let scheduled = false ;
0 commit comments