Skip to content
Source
Sign inSign up
samwillis/jerry
Open History to see the last commit

PaintedPart.swift

185 lines10.5 KB

1import AppKit
2import SpriteKit
3​
4@MainActor
5enum ExplorerPalette {
6 static let jacket = NSColor(calibratedRed: 0.83, green: 0.53, blue: 0.18, alpha: 1)
7 static let trousers = NSColor(calibratedRed: 0.27, green: 0.30, blue: 0.32, alpha: 1)
8 static let skin = NSColor(calibratedRed: 0.93, green: 0.74, blue: 0.55, alpha: 1)
9 static let bag = NSColor(calibratedRed: 0.52, green: 0.38, blue: 0.25, alpha: 1)
10 static let hat = NSColor(calibratedRed: 0.16, green: 0.51, blue: 0.49, alpha: 1)
11 static let boots = NSColor(calibratedRed: 0.4, green: 0.26, blue: 0.16, alpha: 1)
13​
14enum ExplorerMaterial: String {
15 case cloth, limbCloth, canvas, skin, leather, felt, canopy
17​
18/// Small, cached, high-resolution material paintings. No textures are regenerated per frame.
19@MainActor
20final class PaintedPart: SKSpriteNode {
21 private let silhouette: CGPath
22 var silhouettePath: CGPath {
23 // The painted path is authored in its parent's coordinates, while the
24 // sprite itself is positioned at the bounds' centre.
25 var transform = CGAffineTransform(translationX: -bounds.midX, y: -bounds.midY)
26 return silhouette.copy(using: &transform) ?? silhouette
27 }
28 private let material: ExplorerMaterial
29 private let base: NSColor
30 private let bounds: CGRect
31 private let cacheTag: String
32 private static var textures: [String: SKTexture] = [:]
33 /// Higher resolution for exported artwork; desktop rendering keeps its usual cache size.
34 static var rasterScale: CGFloat = 4
35​
36 init(rect: CGRect, radius: CGFloat, color: NSColor, material: ExplorerMaterial) {
37 silhouette = CGPath(roundedRect: rect, cornerWidth: radius, cornerHeight: radius, transform: nil)
38 bounds = rect
39 base = color.usingColorSpace(.deviceRGB) ?? color
40 self.material = material
41 cacheTag = "rect-\(radius)"
42 super.init(texture: nil, color: .clear, size: rect.insetBy(dx: -1, dy: -1).size)
43 position = CGPoint(x: rect.midX, y: rect.midY)
44 setPainted(true)
45 }
46​
47 init(path: CGPath, color: NSColor, material: ExplorerMaterial, tag: String) {
48 silhouette = path
49 bounds = path.boundingBoxOfPath
50 base = color.usingColorSpace(.deviceRGB) ?? color
51 self.material = material
52 cacheTag = tag
53 super.init(texture: nil, color: .clear, size: bounds.insetBy(dx: -1, dy: -1).size)
54 position = CGPoint(x: bounds.midX, y: bounds.midY)
55 setPainted(true)
56 }
57​
58 required init?(coder: NSCoder) { fatalError("Use the material constructors") }
59​
60 func setPainted(_ painted: Bool) {
61 let key = "\(cacheTag)-\(bounds)-\(base.redComponent)-\(base.greenComponent)-\(base.blueComponent)-\(material.rawValue)-\(painted)-\(Self.rasterScale)"
62 if let cached = Self.textures[key] { texture = cached; return }
63 let image = paint(painted: painted)
64 let result = SKTexture(cgImage: image)
65 result.filteringMode = .linear
66 Self.textures[key] = result
67 texture = result
68 }
69​
70 private func paint(painted: Bool) -> CGImage {
71 let scale = max(1, min(32, Self.rasterScale))
72 let width = Int(ceil(size.width * scale)), height = Int(ceil(size.height * scale))
73 let context = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
74 context.scaleBy(x: scale, y: scale)
75 var translation = CGAffineTransform(translationX: 1 - bounds.minX, y: 1 - bounds.minY)
76 let path = silhouette.copy(using: &translation)!
77 let area = CGRect(x: 1, y: 1, width: bounds.width, height: bounds.height)
78 context.saveGState()
79 context.addPath(path)
80 context.clip()
81 if painted {
82 let light = mixed(base, with: .white, amount: material == .limbCloth ? 0.065 : (material == .skin ? 0.13 : 0.24))
83 let shadow = mixed(base, with: NSColor(calibratedRed: 0.16, green: 0.12, blue: 0.09, alpha: 1), amount: material == .limbCloth ? 0.1 : 0.27)
84 let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: [shadow.cgColor, base.cgColor, light.cgColor] as CFArray, locations: [0, 0.48, 1])!
85 context.drawLinearGradient(gradient, start: CGPoint(x: area.maxX, y: area.minY), end: CGPoint(x: area.minX, y: area.maxY), options: [.drawsBeforeStartLocation, .drawsAfterEndLocation])
86 glow(context, at: CGPoint(x: area.minX + area.width * 0.27, y: area.minY + area.height * 0.78), radius: max(area.width, area.height) * 0.65, color: NSColor.white.withAlphaComponent(material == .limbCloth ? 0.025 : 0.13))
87 addGrain(context, in: area)
88 details(context, in: area)
89 } else {
90 context.setFillColor(base.cgColor)
91 context.fill(area)
92 }
93 context.restoreGState()
94 context.addPath(path)
95 context.setStrokeColor(painted ? mixed(base, with: .black, amount: 0.6).withAlphaComponent(0.9).cgColor : NSColor(calibratedWhite: 0.15, alpha: 1).cgColor)
96 context.setLineWidth(painted ? 0.7 : 1)
97 context.strokePath()
98 return context.makeImage()!
99 }
100​
101 private func mixed(_ a: NSColor, with b: NSColor, amount: CGFloat) -> NSColor {
102 (a.blended(withFraction: amount, of: b) ?? a).usingColorSpace(.deviceRGB) ?? a
103 }
104​
105 private func glow(_ context: CGContext, at point: CGPoint, radius: CGFloat, color: NSColor) {
106 let gradient = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: [color.cgColor, color.withAlphaComponent(0).cgColor] as CFArray, locations: [0, 1])!
107 context.drawRadialGradient(gradient, startCenter: point, startRadius: 0, endCenter: point, endRadius: max(radius, 0.1), options: [])
108 }
109​
110 private func addGrain(_ context: CGContext, in area: CGRect) {
111 // A fixed seed gives stable fabric grain instead of frame-to-frame noise.
112 var seed: UInt64 = 14731
113 func next() -> CGFloat {
114 seed = seed &* 6364136223846793005 &+ 1442695040888963407
115 return CGFloat(seed >> 32) / CGFloat(UInt32.max)
116 }
117 let count = Int(area.width * area.height * (material == .skin ? 0.5 : 2.5))
118 for _ in 0..<count {
119 let x = area.minX + next() * area.width
120 let y = area.minY + next() * area.height
121 let pale = next() > 0.5
122 context.setFillColor((pale ? NSColor.white : NSColor.black).withAlphaComponent(material == .skin ? 0.015 : 0.035).cgColor)
123 context.fillEllipse(in: CGRect(x: x, y: y, width: 0.13 + next() * 0.13, height: 0.13 + next() * 0.18))
124 }
125 }
126​
127 private func details(_ context: CGContext, in area: CGRect) {
128 let w = area.width, h = area.height
129 context.saveGState()
130 context.translateBy(x: area.minX, y: area.minY)
131 context.setLineCap(.round)
132 switch material {
133 case .skin:
134 if w > 15, h > 12 {
135 for x in [w * 0.2, w * 0.78] {
136 glow(context, at: CGPoint(x: x, y: h * 0.3), radius: w * 0.24, color: NSColor(calibratedRed: 0.78, green: 0.3, blue: 0.22, alpha: 0.17))
137 }
138 let shade = CGGradient(colorsSpace: CGColorSpace(name: CGColorSpace.sRGB), colors: [NSColor.brown.withAlphaComponent(0.2).cgColor, NSColor.brown.withAlphaComponent(0).cgColor] as CFArray, locations: [0, 1])!
139 context.drawLinearGradient(shade, start: CGPoint(x: 0, y: h), end: CGPoint(x: 0, y: h - 5), options: [])
140 }
141 case .canvas:
142 context.setStrokeColor(mixed(base, with: .black, amount: 0.4).withAlphaComponent(0.08).cgColor)
143 context.setLineWidth(0.15)
144 for x in stride(from: CGFloat(0), through: w, by: 1.4) {
145 context.move(to: CGPoint(x: x, y: 0)); context.addLine(to: CGPoint(x: x, y: h))
146 }
147 for y in stride(from: CGFloat(0), through: h, by: 1.4) {
148 context.move(to: CGPoint(x: 0, y: y)); context.addLine(to: CGPoint(x: w, y: y))
149 }
150 context.strokePath()
151 let flap = CGPath(roundedRect: CGRect(x: 1.2, y: h * 0.63, width: max(1, w - 2.4), height: h * 0.23), cornerWidth: 2, cornerHeight: 2, transform: nil)
152 context.addPath(flap)
153 context.setFillColor(mixed(base, with: .white, amount: 0.08).cgColor)
154 context.fillPath()
155 line(context, from: CGPoint(x: 1.5, y: h * 0.63), to: CGPoint(x: w - 1.5, y: h * 0.63), color: .black.withAlphaComponent(0.16), width: 0.45)
156 context.setFillColor(NSColor(calibratedRed: 0.64, green: 0.47, blue: 0.25, alpha: 1).cgColor)
157 context.fill(CGRect(x: w * 0.5 - 1, y: h * 0.57, width: 2, height: 2.5))
158 case .leather:
159 glow(context, at: CGPoint(x: w * 0.72, y: h * 0.6), radius: w * 0.6, color: NSColor(calibratedRed: 1, green: 0.86, blue: 0.64, alpha: 0.2))
160 line(context, from: CGPoint(x: 0, y: 0.45), to: CGPoint(x: w, y: 0.45), color: .black.withAlphaComponent(0.3), width: 0.75)
161 case .felt:
162 line(context, from: CGPoint(x: 1, y: h * 0.25), to: CGPoint(x: w - 1, y: h * 0.25), color: mixed(base, with: .black, amount: 0.35).withAlphaComponent(0.5), width: h > 5 ? 1.7 : 0.45)
163 case .limbCloth:
164 break
165 case .cloth:
166 line(context, from: CGPoint(x: w * 0.12, y: h * 0.16), to: CGPoint(x: w * 0.6, y: h * 0.23), color: .black.withAlphaComponent(material == .limbCloth ? 0.08 : 0.12), width: 0.4)
167 line(context, from: CGPoint(x: w * 0.35, y: h * 0.32), to: CGPoint(x: w * 0.85, y: h * 0.26), color: .white.withAlphaComponent(material == .limbCloth ? 0.045 : 0.11), width: 0.35)
168 if w > 15 {
169 line(context, from: CGPoint(x: w * 0.53, y: 2), to: CGPoint(x: w * 0.53, y: h - 2), color: .black.withAlphaComponent(0.1), width: 0.3)
170 line(context, from: CGPoint(x: w * 0.68, y: h * 0.39), to: CGPoint(x: w * 0.9, y: h * 0.39), color: .black.withAlphaComponent(0.16), width: 0.45)
171 }
172 case .canopy:
173 for x in [w * 0.33, w * 0.67] {
174 line(context, from: CGPoint(x: x, y: 0), to: CGPoint(x: w * 0.5, y: h), color: .white.withAlphaComponent(0.12), width: 0.6)
175 }
176 }
177 context.restoreGState()
178 }
179​
180 private func line(_ context: CGContext, from start: CGPoint, to end: CGPoint, color: NSColor, width: CGFloat) {
181 context.setStrokeColor(color.cgColor)
182 context.setLineWidth(width)
183 context.move(to: start); context.addLine(to: end); context.strokePath()
184 }