Orrery
A small system of worlds drawn with lines in space: each orbit a dashed ring, a comet with a tail that tapers to nothing, and a belt of rubble laid by a brush. The dashes can be read in world units, where they stay put on their rings as the camera circles, or in canvas points, where they slide (strokeDash(_:in:), strokeProfile, and strokeBrush on drawPolyline over Vector3).
- Run
swift run --package-path Examples Example-3D-Geometry-Orrery- Source
- Examples/3D/Geometry/Orrery/Sketch.swift
import Ollin
/// **Orrery**: a small system of worlds drawn with lines in space. Each orbit is
/// a dashed ring, a comet trails a tail that tapers to nothing, and a belt of
/// rubble is laid down by a brush.
///
/// Every line here is a `drawPolyline` through points in space, and each takes
/// one of the tools a 2D stroke has. The orbits are dashed with
/// `strokeDash(_:in:)`. In world units a dash is a length along the ring in
/// space, so the dashes stay where they are on the ring while the camera circles
/// the system, and they shorten on the far side. Switch **dashes** to screen and
/// the same pattern is read in canvas points: every dash is then the same length
/// on the picture, and the dashes slide round the rings as the camera moves,
/// since the picture of each ring keeps changing. The comet's tail is one line
/// under a `strokeProfile` that runs from its whole weight at the head down to
/// nothing. The belt is a ring under a spray brush and a weight in world units,
/// so its stamps keep their places on the ring and the far ones are smaller.
///
/// The worlds are solid, so each hides the line behind it, and the lines hide
/// one another by depth: the near side of a ring passes over the far side of the
/// next one in.
///
/// Try it: flip **dashes** between world and screen and watch the rings; raise
/// **dash** for longer dashes; take **belt** to zero for the rings alone.
@main
final class Orrery_Example: Sketch {
@Param var dashes: StrokeUnits = .world
@Param(0.1 ... 1, icon: "line.horizontal.3") var dash = 0.42
@Param(0.5 ... 4, icon: "lineweight") var weight = 1.6
@Param(0 ... 1, icon: "sparkles") var belt = 0.7
@Param(2 ... 14, icon: "wind") var tail = 9.0
private let lapSeconds = 24.0
override var loopDuration: Double? { lapSeconds }
/// The worlds: how far out each circles, how big it is, its color, and how
/// many times it goes round in one lap of the loop.
private let worlds: [(radius: Double, size: Double, color: Color, laps: Double)] = [
(1.6, 0.13, Color(hex: 0xC9B79C), 4),
(2.5, 0.2, Color(hex: 0xE0915A), 3),
(3.6, 0.24, Color(hex: 0x5B9BD5), 2),
(6.0, 0.42, Color(hex: 0xD9B77A), 1),
]
private func ring(_ radius: Double, steps: Int = 240) -> [Vector3] {
(0 ..< steps).map { k in
let a = Double(k) / Double(steps) * .tau
return Vector3(radius * cos(a), 0, radius * sin(a))
}
}
/// The comet's place a fraction `t` of the way round its long, tilted loop.
private func comet(_ t: Double) -> Vector3 {
let a = t * .tau
// An ellipse with the sun at one focus, tipped out of the worlds' plane.
let x = 3.9 * cos(a) - 2.6, z = 2.6 * sin(a)
return Vector3(x * cos(0.45), x * sin(0.45) + 0.2, z)
}
override func draw() {
background(Color(hex: 0x05060B))
let lap = loopProgress(over: lapSeconds)
let turn = lap * .tau + 0.5
perspective(eye: Vector3(cos(turn) * 12.5, 8.2, sin(turn) * 12.5),
target: Vector3(0, -0.7, 0), fieldOfView: .pi / 3.8)
// The sun lights the worlds from the middle, and is itself drawn unlit.
pointLight(.white, at: .zero, intensity: 1.5)
ambientLight(Color(white: 0.32))
fill(Color(hex: 0xFFE3A1))
withoutLights { drawSphere(radius: 0.55, segments: 48, rings: 24) }
// The orbits. The unit of the pattern is the one thing the menu changes:
// a dash in world units is `dash` long on the ring, and in canvas points
// it is the length that dash has on the picture at the system's middle.
strokeCap(.butt)
strokeJoin(.round)
strokeWeight(weight)
for world in worlds {
stroke(world.color.withAlpha(0.7))
if dashes == .world {
strokeDash([dash, dash * 0.6], in: .world)
} else {
strokeDash([dash * 60, dash * 36])
}
drawPolyline(ring(world.radius), closed: true)
}
noStrokeDash()
// The belt: one ring, stamped. The brush's stamps are a fraction of their
// own size apart, and the size is the weight, in world units here.
if belt > 0 {
stroke(Color(hex: 0x9AA3B5).withAlpha(0.5))
strokeWeight(0.085, in: .world)
strokeBrush(.spray(spacing: 1.6 - belt, scatter: 3.2, count: 3, seed: 7))
withState {
rotateY(lap * .tau) // the rubble goes round once a lap
drawPolyline(ring(4.7, steps: 360), closed: true)
}
noStrokeBrush()
}
// The worlds themselves.
for world in worlds {
let a = lap * world.laps * .tau
fill(world.color)
withState {
translate(world.radius * cos(a), 0, world.radius * sin(a))
drawSphere(radius: world.size, segments: 32, rings: 16)
}
}
// The comet and its tail: the stretch of its loop behind it, widest at
// the head. A profile reads how far along the line a point is, in space.
let head = lap * 2
var trail: [Vector3] = [], glow: [Color] = []
let steps = 90
for k in 0 ... steps {
let back = Double(k) / Double(steps)
trail.append(comet(head - back * tail / 60))
glow.append(Color(hue: 0.52, saturation: 0.35 + 0.4 * back, brightness: 1, alpha: 1 - 0.75 * back))
}
strokeCap(.round)
strokeWeight(7)
strokeProfile { 1 - $0 }
drawPolyline(trail, colors: glow)
noStrokeProfile()
fill(.white)
withoutLights {
let p = comet(head)
translate(p.x, p.y, p.z)
drawSphere(radius: 0.09, segments: 24, rings: 12)
}
}
}