forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScene.swift
More file actions
77 lines (67 loc) · 1.97 KB
/
Scene.swift
File metadata and controls
77 lines (67 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// Scene.swift
// ShapeScript
//
// Created by Nick Lockwood on 27/09/2018.
// Copyright © 2018 Nick Lockwood. All rights reserved.
//
import Euclid
import Foundation
public final class Scene {
public let background: MaterialProperty
public let children: [Geometry]
public let cameras: [Geometry]
public let lights: [Geometry]
public let cache: GeometryCache?
public init(
background: MaterialProperty,
children: [Geometry],
cache: GeometryCache?
) {
self.background = background
self.children = children
self.cameras = children.flatMap(\._cameras)
self.lights = children.flatMap(\._lights)
self.cache = cache
children.forEach { $0.cache = cache }
}
}
extension Scene: Equatable {
public static func == (lhs: Scene, rhs: Scene) -> Bool {
lhs.background == rhs.background &&
lhs.children == rhs.children &&
lhs.cameras == rhs.cameras
}
}
public extension Scene {
static let empty = Scene(background: .color(.clear), children: [], cache: nil)
/// Returns the approximate (overestimated) bounds of the scene geometry.
var overestimatedBounds: Bounds {
Bounds(children.map(\.overestimatedBounds))
}
/// Returns the approximate (overestimated) bounds of the scene geometry.
@available(*, deprecated, renamed: "overestimatedBounds")
var bounds: Bounds {
overestimatedBounds
}
func build(_ callback: @escaping LegacyCallback) -> Bool {
for geometry in children where !geometry.build(callback) {
return false
}
return true
}
}
private extension Geometry {
var _cameras: [Geometry] {
guard case .camera = type else {
return children.flatMap(\._cameras)
}
return [self]
}
var _lights: [Geometry] {
guard case .light = type else {
return children.flatMap(\._lights)
}
return [self]
}
}