Commit 4c72d5eff7
Unsigned
Layout: unified · split
App/Sources/SolarSystem3D.swift +68 −34
| @@ -186,6 +186,10 @@ final class SolarSceneEntities { | ||
| 186 | 186 | var debrisBuilt: (SolarScale, Float, String)? |
| 187 | 187 | let flare = ModelEntity() |
| 188 | 188 | let aurora = Entity() |
| 189 | ||
| 190 | /// RealityView's ARView can outlive the 3D view; detach the scene so its meshes, materials and | |
| 191 | /// textures are freed when 3D is left. | |
| 192 | isolated deinit { root.removeFromParent() } | |
| 189 | 193 | } |
| 190 | 194 | |
| 191 | 195 | struct SolarSystem3DView: View { |
| @@ -205,6 +209,7 @@ struct SolarSystem3DView: View { | ||
| 205 | 209 | @State private var flight: Task<Void, Never>? |
| 206 | 210 | @State private var pulse = false |
| 207 | 211 | @State private var viewSize = CGSize(width: 400, height: 600) |
| 212 | @State private var ready = false | |
| 208 | 213 | @AppStorage("solar.moons") private var showMoons = true |
| 209 | 214 | @AppStorage("solar.dwarfs") private var showDwarfs = true |
| 210 | 215 | @AppStorage("solar.craft") private var showCraft = true |
| @@ -224,16 +229,20 @@ struct SolarSystem3DView: View { | ||
| 224 | 229 | var body: some View { |
| 225 | 230 | GeometryReader { geo in |
| 226 | 231 | ZStack { |
| 227 | RealityView { content in | |
| 228 | content.camera = .virtual | |
| 229 | build(content) | |
| 230 | } update: { _ in | |
| 231 | place() | |
| 232 | } | |
| 233 | .gesture(dragGesture.simultaneously(with: pinchGesture)) | |
| 234 | .onTapGesture(coordinateSpace: .local) { tap($0, size: geo.size) } | |
| 232 | if ready { | |
| 233 | RealityView { content in | |
| 234 | content.camera = .virtual | |
| 235 | build(content) | |
| 236 | } update: { _ in | |
| 237 | place() | |
| 238 | } | |
| 239 | .gesture(dragGesture.simultaneously(with: pinchGesture)) | |
| 240 | .onTapGesture(coordinateSpace: .local) { tap($0, size: geo.size) } | |
| 235 | 241 | |
| 236 | labels(size: geo.size).allowsHitTesting(false) | |
| 242 | labels(size: geo.size).allowsHitTesting(false) | |
| 243 | } else { | |
| 244 | Color.black.overlay(ProgressView().tint(ink)) | |
| 245 | } | |
| 237 | 246 | |
| 238 | 247 | VStack(spacing: 8) { |
| 239 | 248 | if let s = selected { card(s) } |
| @@ -256,6 +265,11 @@ struct SolarSystem3DView: View { | ||
| 256 | 265 | .accessibilityElement(children: .contain) |
| 257 | 266 | .accessibilityLabel("3D solar system. Drag to turn, pinch to zoom, tap a body to learn about it.") |
| 258 | 267 | .onChange(of: scale) { resetView() } |
| 268 | .task { | |
| 269 | // RealityKit's start-up blocks the main thread; let the placeholder reach the screen first. | |
| 270 | try? await Task.sleep(for: .milliseconds(1)) | |
| 271 | ready = true | |
| 272 | } | |
| 259 | 273 | .task { await loadWeather() } |
| 260 | 274 | .task { |
| 261 | 275 | while !Task.isCancelled { |
| @@ -369,8 +383,10 @@ struct SolarSystem3DView: View { | ||
| 369 | 383 | e.camera.camera.fieldOfViewInDegrees = SolarCamera.fovDegrees |
| 370 | 384 | e.root.addChild(e.camera) |
| 371 | 385 | |
| 386 | // One unit sphere for every round body; entities are scaled to size. | |
| 387 | let sphere = MeshResource.generateSphere(radius: 1) | |
| 372 | 388 | let sunMaterial = UnlitMaterial(color: UIColor(red: 1, green: 0.86, blue: 0.5, alpha: 1)) |
| 373 | e.sun.model = ModelComponent(mesh: .generateSphere(radius: 1), materials: [sunMaterial]) | |
| 389 | e.sun.model = ModelComponent(mesh: sphere, materials: [sunMaterial]) | |
| 374 | 390 | e.root.addChild(e.sun) |
| 375 | 391 | // Glow: nested translucent shells, fainter outward. |
| 376 | 392 | for i in 1...10 { |
| @@ -378,7 +394,9 @@ struct SolarSystem3DView: View { | ||
| 378 | 394 | let a = CGFloat(0.09 * pow(0.78, Double(i - 1))) |
| 379 | 395 | var g = UnlitMaterial(color: UIColor(red: 1, green: 0.72, blue: 0.32, alpha: a)) |
| 380 | 396 | g.blending = .transparent(opacity: .init(floatLiteral: Float(a))) |
| 381 | e.sun.addChild(ModelEntity(mesh: .generateSphere(radius: r), materials: [g])) | |
| 397 | let shell = ModelEntity(mesh: sphere, materials: [g]) | |
| 398 | shell.scale = .init(repeating: r) | |
| 399 | e.sun.addChild(shell) | |
| 382 | 400 | } |
| 383 | 401 | |
| 384 | 402 | // Planets, dwarf planets and moons: textured where there is a map, with a night side. |
| @@ -386,36 +404,45 @@ struct SolarSystem3DView: View { | ||
| 386 | 404 | if let pluto = SolarPlanet.pluto { spheres.append(("pluto", pluto.color, pluto.tiltDeg)) } |
| 387 | 405 | spheres += KeplerOrbit.dwarfPlanets.map { ($0.id, Library.page(id: $0.id)?.drawing?.color ?? "#999999", 0) } |
| 388 | 406 | spheres += SolarMoon.all.map { ($0.id, $0.color, 0) } |
| 407 | let nightMesh = Self.nightShell() | |
| 408 | var nightMaterial = UnlitMaterial(color: .black) | |
| 409 | nightMaterial.blending = .transparent(opacity: .init(floatLiteral: 0.88)) | |
| 410 | nightMaterial.faceCulling = .none | |
| 389 | 411 | for s in spheres { |
| 390 | 412 | let body = ModelEntity() |
| 391 | 413 | // Plain colour first; the map, if there is one, is swapped in once it has loaded. |
| 392 | 414 | let m = UnlitMaterial(color: UIColor(Color(hex: s.color) ?? .gray)) |
| 393 | body.model = ModelComponent(mesh: .generateSphere(radius: 1), materials: [m]) | |
| 415 | body.model = ModelComponent(mesh: sphere, materials: [m]) | |
| 394 | 416 | body.orientation = simd_quatf(angle: Float(s.tilt * .pi / 180), axis: [1, 0, 0]) |
| 395 | 417 | if s.id == "saturn" { body.addChild(Self.annulus(inner: 1.24, outer: 2.27, color: UIColor(red: 0.85, green: 0.78, blue: 0.62, alpha: 0.7))) } |
| 396 | 418 | e.bodies[s.id] = body |
| 397 | 419 | e.root.addChild(body) |
| 398 | let night = Self.nightShell() | |
| 420 | let night = ModelEntity(mesh: nightMesh, materials: [nightMaterial]) | |
| 399 | 421 | e.nights[s.id] = night |
| 400 | 422 | e.root.addChild(night) |
| 401 | 423 | } |
| 402 | 424 | |
| 403 | 425 | // Spacecraft and comet nuclei: dots sized each frame to stay visible. |
| 426 | let craftMaterial = UnlitMaterial(color: Self.craftColor) | |
| 404 | 427 | for craft in Spacecraft.all { |
| 405 | let dot = ModelEntity(mesh: .generateSphere(radius: 1), materials: [UnlitMaterial(color: Self.craftColor)]) | |
| 428 | let dot = ModelEntity(mesh: sphere, materials: [craftMaterial]) | |
| 406 | 429 | e.bodies["craft:\(craft.name)"] = dot |
| 407 | 430 | e.root.addChild(dot) |
| 408 | 431 | } |
| 432 | let nucleusMaterial = UnlitMaterial(color: UIColor(white: 0.92, alpha: 1)) | |
| 433 | // A soft tail: nested cones, wider and fainter outward. | |
| 434 | let tailParts = ([(0.025, 0.3), (0.06, 0.14), (0.11, 0.06)] as [(Float, Float)]).map { radius, alpha in | |
| 435 | var t = UnlitMaterial(color: UIColor(red: 0.7, green: 0.88, blue: 1, alpha: 1)) | |
| 436 | t.blending = .transparent(opacity: .init(floatLiteral: alpha)) | |
| 437 | return (mesh: MeshResource.generateCone(height: 1, radius: radius), material: t) | |
| 438 | } | |
| 409 | 439 | for c in KeplerOrbit.comets { |
| 410 | let nucleus = ModelEntity(mesh: .generateSphere(radius: 1), materials: [UnlitMaterial(color: UIColor(white: 0.92, alpha: 1))]) | |
| 440 | let nucleus = ModelEntity(mesh: sphere, materials: [nucleusMaterial]) | |
| 411 | 441 | e.bodies["comet:\(c.id)"] = nucleus |
| 412 | 442 | e.root.addChild(nucleus) |
| 413 | // A soft tail: nested cones, wider and fainter outward. | |
| 414 | 443 | let tail = ModelEntity() |
| 415 | for (radius, alpha) in [(0.025, 0.3), (0.06, 0.14), (0.11, 0.06)] as [(Float, Float)] { | |
| 416 | var t = UnlitMaterial(color: UIColor(red: 0.7, green: 0.88, blue: 1, alpha: 1)) | |
| 417 | t.blending = .transparent(opacity: .init(floatLiteral: alpha)) | |
| 418 | tail.addChild(ModelEntity(mesh: .generateCone(height: 1, radius: radius), materials: [t])) | |
| 444 | for part in tailParts { | |
| 445 | tail.addChild(ModelEntity(mesh: part.mesh, materials: [part.material])) | |
| 419 | 446 | } |
| 420 | 447 | e.tails[c.id] = tail |
| 421 | 448 | e.root.addChild(tail) |
| @@ -429,7 +456,7 @@ struct SolarSystem3DView: View { | ||
| 429 | 456 | |
| 430 | 457 | var flareMaterial = UnlitMaterial(color: UIColor(red: 1, green: 1, blue: 0.85, alpha: 0.9)) |
| 431 | 458 | flareMaterial.blending = .transparent(opacity: .init(floatLiteral: 0.9)) |
| 432 | e.flare.model = ModelComponent(mesh: .generateSphere(radius: 1), materials: [flareMaterial]) | |
| 459 | e.flare.model = ModelComponent(mesh: sphere, materials: [flareMaterial]) | |
| 433 | 460 | e.root.addChild(e.flare) |
| 434 | 461 | for y: Float in [0.86, -0.86] { |
| 435 | 462 | for (inner, outer, alpha) in [(0.36, 0.66, 0.22), (0.45, 0.56, 0.5)] as [(Float, Float, CGFloat)] { |
| @@ -535,13 +562,19 @@ struct SolarSystem3DView: View { | ||
| 535 | 562 | if let (s, w) = e.orbitsBuilt, s == scale, w / width < 1.6, width / w < 1.6 { return } |
| 536 | 563 | e.orbitsBuilt = (scale, width) |
| 537 | 564 | let map = mapping |
| 565 | // Orbits of the same colour and opacity share a material, made when the first of them is created. | |
| 566 | var materials: [String: UnlitMaterial] = [:] | |
| 538 | 567 | func set(_ id: String, _ points: [SIMD3<Float>], color: UIColor, opacity: Float) { |
| 539 | 568 | let mesh = Self.ribbon(points, width: width, closed: true) |
| 540 | 569 | if let existing = e.orbits[id] { existing.model?.mesh = mesh; return } |
| 541 | var m = UnlitMaterial(color: color) | |
| 542 | m.blending = .transparent(opacity: .init(floatLiteral: opacity)) | |
| 543 | m.faceCulling = .none | |
| 544 | let orbit = ModelEntity(mesh: mesh, materials: [m]) | |
| 570 | let key = "\(color) \(opacity)" | |
| 571 | if materials[key] == nil { | |
| 572 | var m = UnlitMaterial(color: color) | |
| 573 | m.blending = .transparent(opacity: .init(floatLiteral: opacity)) | |
| 574 | m.faceCulling = .none | |
| 575 | materials[key] = m | |
| 576 | } | |
| 577 | let orbit = ModelEntity(mesh: mesh, materials: [materials[key]!]) | |
| 545 | 578 | e.orbits[id] = orbit |
| 546 | 579 | e.root.addChild(orbit) |
| 547 | 580 | } |
| @@ -567,6 +600,7 @@ struct SolarSystem3DView: View { | ||
| 567 | 600 | e.pathsBuilt = (scale, width, bucket) |
| 568 | 601 | let map = mapping |
| 569 | 602 | let day = date.timeIntervalSince1970 / 86_400 |
| 603 | var material: UnlitMaterial? | |
| 570 | 604 | for craft in Spacecraft.all { |
| 571 | 605 | var pts = craft.points.filter { $0[0] <= day }.map { map.point(SIMD3($0[1], $0[2], $0[3])) } |
| 572 | 606 | if let now = craft.position3(at: date) { pts.append(map.point(now)) } |
| @@ -574,10 +608,13 @@ struct SolarSystem3DView: View { | ||
| 574 | 608 | if let existing = e.paths[craft.name] { |
| 575 | 609 | existing.model?.mesh = mesh |
| 576 | 610 | } else { |
| 577 | var m = UnlitMaterial(color: Self.craftColor) | |
| 578 | m.blending = .transparent(opacity: .init(floatLiteral: 0.4)) | |
| 579 | m.faceCulling = .none | |
| 580 | let path = ModelEntity(mesh: mesh, materials: [m]) | |
| 611 | if material == nil { | |
| 612 | var m = UnlitMaterial(color: Self.craftColor) | |
| 613 | m.blending = .transparent(opacity: .init(floatLiteral: 0.4)) | |
| 614 | m.faceCulling = .none | |
| 615 | material = m | |
| 616 | } | |
| 617 | let path = ModelEntity(mesh: mesh, materials: [material!]) | |
| 581 | 618 | e.paths[craft.name] = path |
| 582 | 619 | e.root.addChild(path) |
| 583 | 620 | } |
| @@ -666,7 +703,7 @@ struct SolarSystem3DView: View { | ||
| 666 | 703 | } |
| 667 | 704 | |
| 668 | 705 | /// The half of a unit sphere below y = 0, drawn dark and mostly opaque over a body's night side. |
| 669 | private static func nightShell() -> ModelEntity { | |
| 706 | private static func nightShell() -> MeshResource { | |
| 670 | 707 | let rings = 24, segments = 64 |
| 671 | 708 | var positions: [SIMD3<Float>] = [] |
| 672 | 709 | var indices: [UInt32] = [] |
| @@ -686,10 +723,7 @@ struct SolarSystem3DView: View { | ||
| 686 | 723 | var d = MeshDescriptor(name: "night") |
| 687 | 724 | d.positions = MeshBuffer(positions) |
| 688 | 725 | d.primitives = .triangles(indices) |
| 689 | var m = UnlitMaterial(color: .black) | |
| 690 | m.blending = .transparent(opacity: .init(floatLiteral: 0.88)) | |
| 691 | m.faceCulling = .none | |
| 692 | return ModelEntity(mesh: (try? MeshResource.generate(from: [d])) ?? .generateBox(size: 0.001), materials: [m]) | |
| 726 | return (try? MeshResource.generate(from: [d])) ?? .generateBox(size: 0.001) | |
| 693 | 727 | } |
| 694 | 728 | |
| 695 | 729 | // MARK: Labels, taps and the card |