Skip to content

Instantly share code, notes, and snippets.

@elkraneo
Created August 14, 2024 10:18
Show Gist options
  • Save elkraneo/c795f130b7ab91b534efa0e324805994 to your computer and use it in GitHub Desktop.
Save elkraneo/c795f130b7ab91b534efa0e324805994 to your computer and use it in GitHub Desktop.
import CoreLocation
import RealityKit
import RealityKitContent
import SwiftUI
struct ImmersiveView: View {
var body: some View {
RealityView { content in
if let earthEntity = try? await Entity(named: "Earth", in: realityKitContentBundle) {
content.add(earthEntity)
earthEntity.position.y = 1
earthEntity.position.z = -1
let berlinPosition = coordinatesToCartesian(coordinate: .init(latitude: 52.5200, longitude: 13.4050), radius: 0.55)
let pinEntity = ModelEntity(mesh: .generateSphere(radius: 0.01))
pinEntity.position = berlinPosition
earthEntity.addChild(pinEntity)
let laHabanaPosition = coordinatesToCartesian(coordinate: .init(latitude: 23.1136, longitude: -82.3666), radius: 0.55)
let pinEntity2 = ModelEntity(mesh: .generateSphere(radius: 0.01))
pinEntity2.position = laHabanaPosition
earthEntity.addChild(pinEntity2)
}
}
}
func coordinatesToCartesian(coordinate: CLLocationCoordinate2D, radius: Float) -> SIMD3<Float> {
/// φ (phi) is the latitude in radians.
let phi = Float(coordinate.latitude) * .pi / 180
/// λ (lambda) is the longitude in radians.
let lambda = Float(coordinate.longitude) * .pi / 180
let x = radius * cos(phi) * cos(lambda)
let y = radius * cos(phi) * sin(lambda)
let z = radius * sin(phi)
return SIMD3<Float>.init(x: x, y: y, z: z)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment