UNPKG

three-globe

Version:

Globe data visualization as a ThreeJS reusable 3D object

102 lines (86 loc) 3.16 kB
<head> <script type="importmap">{ "imports": { "three": "https://esm.sh/three", "three/": "https://esm.sh/three/" }}</script> <!-- <script type="module"> import * as THREE from 'three'; window.THREE = THREE;</script>--> <!-- <script src="../../dist/three-globe.js" defer></script>--> <style> body { margin: 0; } #time { position: absolute; bottom: 8px; left: 8px; color: lightblue; font-family: monospace; } </style> </head> <body> <div id="globeViz"></div> <div id="time"></div> <script type="module"> import ThreeGlobe from 'https://esm.sh/three-globe?external=three'; import * as THREE from 'https://esm.sh/three'; import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls.js?external=three'; import * as solar from 'https://esm.sh/solar-calculator'; const VELOCITY = 9; // minutes per frame const sunPosAt = dt => { const day = new Date(+dt).setUTCHours(0, 0, 0, 0); const t = solar.century(dt); const longitude = (day - dt) / 864e5 * 360 - 180; return [longitude - solar.equationOfTime(t) / 4, solar.declination(t)]; }; let dt = +new Date(); const solarTile = { pos: sunPosAt(dt) }; const timeEl = document.getElementById('time'); const Globe = new ThreeGlobe() .globeImageUrl('//cdn.jsdelivr.net/npm/three-globe/example/img/earth-dark.jpg') .tilesData([solarTile]) .tileLng(d => d.pos[0]) .tileLat(d => d.pos[1]) .tileAltitude(0.005) .tileWidth(180) .tileHeight(180) .tileUseGlobeProjection(false) .tileMaterial(() => new THREE.MeshLambertMaterial({ color: '#ffff00', opacity: 0.3, transparent: true })) .tilesTransitionDuration(0); // animate time of day requestAnimationFrame(() => (function animate() { dt += VELOCITY * 60 * 1000; solarTile.pos = sunPosAt(dt); Globe.tilesData([solarTile]); timeEl.textContent = new Date(dt).toLocaleString(); requestAnimationFrame(animate); })() ); // Setup renderer const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setPixelRatio(window.devicePixelRatio); document.getElementById('globeViz').appendChild(renderer.domElement); // Setup scene const scene = new THREE.Scene(); scene.add(Globe); scene.add(new THREE.AmbientLight(0xcccccc, Math.PI)); scene.add(new THREE.DirectionalLight(0xffffff, 0.6 * Math.PI)); // Setup camera const camera = new THREE.PerspectiveCamera(); camera.aspect = window.innerWidth/ window.innerHeight; camera.updateProjectionMatrix(); camera.position.z = 500; // Add camera controls const tbControls = new TrackballControls(camera, renderer.domElement); tbControls.minDistance = 101; tbControls.rotateSpeed = 5; tbControls.zoomSpeed = 0.8; // Kick-off renderer (function animate() { // IIFE // Frame cycle tbControls.update(); renderer.render(scene, camera); requestAnimationFrame(animate); })(); </script> </body>