|
| 1 | +import { Scene, Engine, GeospatialCamera, Vector3, Color4 } from '@babylonjs/core'; |
| 2 | +import { GeospatialClippingBehavior } from '@babylonjs/core/Behaviors/Cameras'; |
| 3 | +import { TilesRenderer } from '3d-tiles-renderer/babylonjs'; |
| 4 | +import { CesiumIonAuthPlugin } from '3d-tiles-renderer/core/plugins'; |
| 5 | +import GUI from 'lil-gui'; |
| 6 | + |
| 7 | +const GOOGLE_TILES_ASSET_ID = 2275207; |
| 8 | + |
| 9 | +const PLANET_RADIUS = 6378137; |
| 10 | + |
| 11 | +// gui |
| 12 | +const params = { |
| 13 | + enabled: true, |
| 14 | + visibleTiles: 0, |
| 15 | + errorTarget: 20, |
| 16 | + minZ: 0, |
| 17 | + maxZ: 0, |
| 18 | +}; |
| 19 | + |
| 20 | +const gui = new GUI(); |
| 21 | +gui.add( params, 'enabled' ); |
| 22 | +gui.add( params, 'visibleTiles' ).name( 'Visible Tiles' ).listen().disable(); |
| 23 | +gui.add( params, 'errorTarget', 1, 100 ); |
| 24 | +gui.add( params, 'minZ' ).name( 'Camera MinZ' ).listen().disable(); |
| 25 | +gui.add( params, 'maxZ' ).name( 'Camera MaxZ' ).listen().disable(); |
| 26 | + |
| 27 | +// engine |
| 28 | +const canvas = document.getElementById( 'renderCanvas' ); |
| 29 | +const engine = new Engine( canvas, true, { useLargeWorldRendering: true } ); |
| 30 | +engine.setHardwareScalingLevel( 1 / window.devicePixelRatio ); |
| 31 | + |
| 32 | +// scene |
| 33 | +const scene = new Scene( engine ); |
| 34 | +scene.clearColor = new Color4( 0.05, 0.05, 0.05, 1 ); |
| 35 | + |
| 36 | +// 3D Tiles data uses right-handed coordinate system |
| 37 | +scene.useRightHandedSystem = true; |
| 38 | + |
| 39 | +// camera |
| 40 | +const camera = new GeospatialCamera( 'geo', scene, { planetRadius: PLANET_RADIUS } ); |
| 41 | + |
| 42 | +camera.attachControl( true ); |
| 43 | +const clippingBehavior = new GeospatialClippingBehavior(); |
| 44 | +camera.addBehavior( clippingBehavior ); |
| 45 | + |
| 46 | +// Start farther out, then fly in once tiles are loaded |
| 47 | +camera.radius = 50000; |
| 48 | + |
| 49 | +// Set center to Tokyo Tower location (ECEF coordinates) |
| 50 | +// Tokyo Tower: 35.6586° N, 139.7454° E |
| 51 | +camera.center = new Vector3( - 3959611.825621192, 3352599.0363458656, 3697549.0362687325 ); |
| 52 | +camera.pitch = 1.167625429373872; |
| 53 | +camera.yaw = - 0.2513281792775774; |
| 54 | + |
| 55 | +camera.checkCollisions = true; |
| 56 | +scene.collisionsEnabled = true; |
| 57 | + |
| 58 | +// Fly to close view once tiles load |
| 59 | +let hasZoomedIn = false; |
| 60 | + |
| 61 | +// tiles |
| 62 | +const tiles = new TilesRenderer( null, scene ); |
| 63 | +tiles.registerPlugin( new CesiumIonAuthPlugin( { |
| 64 | + apiToken: import.meta.env.VITE_ION_KEY, |
| 65 | + assetId: GOOGLE_TILES_ASSET_ID, |
| 66 | + autoRefreshToken: true, |
| 67 | +} ) ); |
| 68 | +tiles.errorTarget = params.errorTarget; |
| 69 | + |
| 70 | +// Babylon render loop |
| 71 | + |
| 72 | +scene.onBeforeRenderObservable.add( () => { |
| 73 | + |
| 74 | + if ( params.enabled ) { |
| 75 | + |
| 76 | + tiles.errorTarget = params.errorTarget; |
| 77 | + tiles.update(); |
| 78 | + params.visibleTiles = tiles.visibleTiles.size; |
| 79 | + params.minZ = camera.minZ; |
| 80 | + params.maxZ = camera.maxZ; |
| 81 | + |
| 82 | + |
| 83 | + // Once we have some tiles visible, fly in to target |
| 84 | + if ( ! hasZoomedIn && tiles.visibleTiles.size > 5 ) { |
| 85 | + |
| 86 | + hasZoomedIn = true; |
| 87 | + camera.flyToAsync( undefined, undefined, 300, undefined, 2000 ); |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + // update attributions |
| 94 | + const attributions = tiles.getAttributions(); |
| 95 | + const creditsEl = document.getElementById( 'credits' ); |
| 96 | + creditsEl.innerText = attributions[ 0 ]?.value || ''; |
| 97 | + |
| 98 | +} ); |
| 99 | + |
| 100 | +engine.runRenderLoop( () => { |
| 101 | + |
| 102 | + scene.render(); |
| 103 | + |
| 104 | +} ); |
| 105 | + |
| 106 | +// Handle window resize |
| 107 | +window.addEventListener( 'resize', () => { |
| 108 | + |
| 109 | + engine.resize(); |
| 110 | + |
| 111 | +} ); |
0 commit comments