Packages/FoldKit/Sources/Folding/Camera.swift

00ea029d0652da12d0f4237769e7020b72cfa7f3
fold/Packages/FoldKit/Sources/Folding/Camera.swift history · blame · raw

72 lines · 3163 bytes

4 symbols in this file
 1import simd
 2
 3/// A perspective camera at `eye` looking at `target`. `fieldOfView` is vertical, in radians.
 4public struct Camera: Sendable {
 5    public var eye: SIMD3<Double>
 6    public var target: SIMD3<Double>
 7    public var up: SIMD3<Double>
 8    public var fieldOfView: Double
 9
10    public init(eye: SIMD3<Double>, target: SIMD3<Double>, up: SIMD3<Double>, fieldOfView: Double) {
11        self.eye = eye
12        self.target = target
13        self.up = up
14        self.fieldOfView = fieldOfView
15    }
16
17    /// The unit vector from `eye` toward `target`.
18    public var viewDirection: SIMD3<Double> {
19        let d = target - eye
20        return d / length(d)
21    }
22
23    /// World to clip space, Metal's: depth 0 at the near plane and 1 at the far plane, which are
24    /// 1/100 and 100 times the distance from `eye` to `target`.
25    public func viewProjection(aspect: Double) -> simd_double4x4 {
26        let f = viewDirection
27        let s = normalize(cross(f, up))
28        let u = cross(s, f)
29        let view = simd_double4x4(rows: [
30            SIMD4(s.x, s.y, s.z, -dot(s, eye)),
31            SIMD4(u.x, u.y, u.z, -dot(u, eye)),
32            SIMD4(-f.x, -f.y, -f.z, dot(f, eye)),
33            SIMD4(0, 0, 0, 1),
34        ])
35        let distance = length(target - eye)
36        let near = distance / 100, far = distance * 100
37        let y = 1 / tan(fieldOfView / 2), z = far / (near - far)
38        let projection = simd_double4x4(columns: (
39            SIMD4(y / aspect, 0, 0, 0),
40            SIMD4(0, y, 0, 0),
41            SIMD4(0, 0, z, -1),
42            SIMD4(0, 0, z * near, 0)
43        ))
44        return projection * view
45    }
46}
47
48extension Camera {
49    /// The camera moved around `target` at its distance so that the scene appears to turn by
50    /// `length(angles)` radians: `angles.x` about the camera's up axis, carrying the near side
51    /// rightward on screen, and `angles.y` about its right axis, carrying the near side downward.
52    /// The returned `up` is a unit vector perpendicular to the view direction, at any angle.
53    public func orbited(by angles: SIMD2<Double>) -> Camera {
54        let f = viewDirection
55        let s = normalize(cross(f, up))
56        let u = cross(s, f)
57        let angle = length(angles)
58        guard angle > 0 else { return Camera(eye: eye, target: target, up: u, fieldOfView: fieldOfView) }
59        // The camera turns the opposite way to the scene.
60        let turn = simd_quatd(angle: -angle, axis: (angles.x * u + angles.y * s) / angle)
61        let turnedF = turn.act(f), turnedU = turn.act(u)
62        return Camera(eye: target + turn.act(eye - target), target: target,
63                      up: normalize(turnedU - dot(turnedU, turnedF) * turnedF), fieldOfView: fieldOfView)
64    }
65
66    /// The camera with its field of view narrowed by `scale`, as a pinch of that scale magnifies
67    /// the image, and clamped to `range`.
68    public func zoomed(by scale: Double, within range: ClosedRange<Double>) -> Camera {
69        let fieldOfView = min(max(2 * atan(tan(self.fieldOfView / 2) / scale), range.lowerBound), range.upperBound)
70        return Camera(eye: eye, target: target, up: up, fieldOfView: fieldOfView)
71    }
72}