Globe: Position camera to current continent

Quite complex to figure out but needed to use spherical coordinates to apply to the camera, inspired from Three.js
Thanks @Tape on Discord/Three.js!
This commit is contained in:
2022-09-26 20:00:17 +02:00
parent 6ca570c745
commit 7cc778e1cc
3 changed files with 28 additions and 11 deletions

View File

@@ -52,7 +52,10 @@
autoRotate, autoRotate,
speed, speed,
sunAngle: 2, sunAngle: 2,
rotationStart: randomContinent.rotation, rotationStart: {
x: randomContinent.rotation_x,
y: randomContinent.rotation_y,
},
enableMarkers, enableMarkers,
enableMarkersLinks: enableMarkersLinks && type !== 'cropped', enableMarkersLinks: enableMarkersLinks && type !== 'cropped',
markers, markers,

View File

@@ -16,11 +16,7 @@ export class Globe {
this.width = this.el.offsetWidth this.width = this.el.offsetWidth
this.height = this.el.offsetHeight this.height = this.el.offsetHeight
this.markers = options.markers || [] this.markers = options.markers || []
this.globeZoom = 1.3075 this.zoom = 1.3075
this.globeRotation = {
lat: degToRad(-this.options.rotationStart) || 0,
// lng: degToRad(-this.options.rotationStart.lng) || 0,
}
// Calculate the current sun position from a given location // Calculate the current sun position from a given location
const locations = [ const locations = [
@@ -93,7 +89,12 @@ export class Globe {
// Create camera // Create camera
this.camera = new Camera(this.gl) this.camera = new Camera(this.gl)
this.camera.position.set(0, 0, this.globeZoom) this.camera.position.set(setFromSphericalCoords(
this.zoom,
degToRad(this.options.rotationStart.y || 40), // phi: y
degToRad(this.options.rotationStart.x || 0), // theta: x
))
this.camera.lookAt(0,0,0)
// Create controls // Create controls
this.controls = new Orbit(this.camera, { this.controls = new Orbit(this.camera, {
@@ -285,8 +286,7 @@ export class Globe {
// Rotate globe if not dragging neither hovering marker // Rotate globe if not dragging neither hovering marker
if (this.params.autoRotate && !this.hoveringMarker) { if (this.params.autoRotate && !this.hoveringMarker) {
this.globeRotation.lat += this.params.speed * delta this.globe.rotation.y += this.params.speed * delta
this.globe.rotation.y = this.globeRotation.lat
} }
// Update controls and renderer // Update controls and renderer
@@ -340,7 +340,7 @@ type Options = {
autoRotate: boolean autoRotate: boolean
speed: number speed: number
sunAngle: number sunAngle: number
rotationStart?: number rotationStart?: { x: number, y: number }
enableMarkers?: boolean enableMarkers?: boolean
enableMarkersLinks?: boolean enableMarkersLinks?: boolean
markers?: any[] markers?: any[]
@@ -390,6 +390,19 @@ const latLonToVec3 = (lat: number, lng: number) => {
return new Vec3(x,y,z) return new Vec3(x,y,z)
} }
/**
* Get position from spherical coordinates
*/
const setFromSphericalCoords = (radius: number, phi: number, theta: number) => {
const sinPhiRadius = Math.sin(phi) * radius
const x = sinPhiRadius * Math.sin(theta)
const y = Math.cos(phi) * radius
const z = sinPhiRadius * Math.cos(theta)
return new Vec3(x,y,z)
}
/** /**
* Convert Degrees to Radians * Convert Degrees to Radians
*/ */

View File

@@ -44,7 +44,8 @@ export const load: PageServerLoad = async () => {
continents: continent (filter: { countries: { slug: { _neq: "_empty" }}}) { continents: continent (filter: { countries: { slug: { _neq: "_empty" }}}) {
name name
slug slug
rotation rotation_x
rotation_y
} }
settings { settings {