Branchwell

PulseCanvas / app · pulsecanvas.ai / Sources / PulseCanvas / OriginalGeometry.swift

Raw 17.7 KB on 15681b389943bdb82ce0ee35ea3df6538f046361
1import Foundation
2import simd
3
4struct OriginalVertex: Sendable {
5 var position: SIMD3<Float>
6 var color: SIMD4<Float>
7 var uv: SIMD2<Float>
8}
9
10/// Original PulseCanvas designs. Pure functions let preview, seeking and export
11/// produce the same geometry for the same soundtrack spectrum and timestamp.
12enum OriginalGeometry {
13 typealias Vertex = OriginalVertex
14 enum Primitive: Sendable { case points, triangles }
15 struct Mesh: Sendable {
16 var vertices: [Vertex]
17 var indices: [UInt32]
18 var primitive: Primitive
19 var pointSize: Float = 0.055
20 }
21 enum Scene: Int, CaseIterable, Sendable {
22 case audioMandala = 15
23 case orbitalRings = 16
24 case ribbonWave = 17
25 case spectralHelix = 18
26 case particleFountain = 19
27 case pulseGrid = 20
28 case harmonicBloom = 21
29
30 var title: String {
31 switch self {
32 case .audioMandala: return "Lotus Resonance"
33 case .orbitalRings: return "Orbit Loom"
34 case .ribbonWave: return "Silk Current"
35 case .spectralHelix: return "Double Twist"
36 case .particleFountain: return "Arc Spray"
37 case .pulseGrid: return "Signal Tiles"
38 case .harmonicBloom: return "Resonant Shell"
39 }
40 }
41 }
42 static var sceneNames: [String] { Scene.allCases.map(\.title) }
43
44 static func make(scene: Int, time: Float, spectrum: [Float], intensity: Float = 1, palette: Int = 0) -> Mesh {
45 let time = time.isFinite ? max(-100_000, min(100_000, time)) : 0
46 let intensity = intensity.isFinite ? max(0, min(3, intensity)) : 1
47 let audio = Audio(spectrum: spectrum, intensity: intensity)
48 switch Scene(rawValue: scene) {
49 case .audioMandala: return mandala(time: time, audio: audio, palette: palette)
50 case .orbitalRings: return orbits(time: time, audio: audio, palette: palette)
51 case .ribbonWave: return ribbons(time: time, audio: audio, palette: palette)
52 case .spectralHelix: return helix(time: time, audio: audio, palette: palette)
53 case .particleFountain: return fountain(time: time, audio: audio, palette: palette)
54 case .pulseGrid: return grid(time: time, audio: audio, palette: palette)
55 case .harmonicBloom: return bloom(time: time, audio: audio, palette: palette)
56 case nil: return Mesh(vertices: [], indices: [], primitive: .triangles)
57 }
58 }
59
60 private struct Audio {
61 let bands: [Float]
62 let intensity: Float
63 init(spectrum: [Float], intensity: Float) {
64 bands = spectrum.isEmpty ? [0] : spectrum.map { $0.isFinite ? min(1, max(0, $0)) : 0 }
65 self.intensity = intensity
66 }
67 func at(_ position: Float) -> Float {
68 let index = max(0, min(1, position)) * Float(bands.count - 1)
69 let lower = Int(index), upper = min(lower + 1, bands.count - 1)
70 return (bands[lower] + (bands[upper] - bands[lower]) * (index - Float(lower))) * intensity
71 }
72 var bass: Float {
73 let count = max(1, bands.count / 5)
74 return bands.prefix(count).reduce(0, +) / Float(count) * intensity
75 }
76 }
77
78 private static let turn = Float.pi * 2
79 private static func color(_ phase: Float, energy: Float, palette: Int, alpha: Float = 1) -> SIMD4<Float> {
80 var result = VisualPalette.color(phase, palette: palette)
81 let light = min(1, 0.4 + energy * 0.6)
82 result.x *= light; result.y *= light; result.z *= light; result.w = alpha
83 return result
84 }
85 private static func vertex(_ position: SIMD3<Float>, phase: Float, energy: Float, palette: Int, uv: SIMD2<Float> = .zero) -> Vertex {
86 Vertex(position: position, color: color(phase, energy: energy, palette: palette), uv: uv)
87 }
88 private static func stripIndices(rows: Int, columns: Int) -> [UInt32] {
89 var result: [UInt32] = []
90 result.reserveCapacity(rows * columns * 6)
91 for row in 0..<rows {
92 for column in 0..<columns {
93 let first = UInt32(row * (columns + 1) + column)
94 let next = first + UInt32(columns + 1)
95 result.append(contentsOf: [first, next, first + 1, first + 1, next, next + 1])
96 }
97 }
98 return result
99 }
100
101 /// Concentric rosettes with twelve petal lobes, radial bass breathing and
102 /// angle-local frequency displacement. The open center preserves its silhouette.
103 private static func mandala(time: Float, audio: Audio, palette: Int) -> Mesh {
104 let rings = 11, segments = 192
105 var vertices: [Vertex] = [], indices: [UInt32] = []
106 vertices.reserveCapacity(rings * (segments + 1) * 2)
107 for ring in 0..<rings {
108 let ringPosition = Float(ring) / Float(rings - 1)
109 let baseRadius = 1.1 + ringPosition * 9.2
110 for side in 0...1 {
111 for segment in 0...segments {
112 let u = Float(segment) / Float(segments)
113 let angle = u * turn + time * (ring.isMultiple(of: 2) ? 0.07 : -0.05)
114 let band = audio.at((1 - cos(u * turn)) * 0.5)
115 let petal = cos(angle * 12 + time * 0.4 + ringPosition * .pi)
116 let radius = baseRadius + petal * (0.22 + band * 0.45) + audio.bass * 0.25 + Float(side) * (0.09 + band * 0.2)
117 let position = SIMD3(radius * cos(angle), radius * sin(angle), sin(angle * 6 + time * 0.6) * (0.15 + band * 0.4))
118 vertices.append(vertex(position, phase: ringPosition * 0.5 + u * 0.25 + time * 0.015, energy: band, palette: palette, uv: SIMD2(u * 8, Float(side))))
119 }
120 }
121 let start = UInt32(ring * (segments + 1) * 2)
122 for segment in 0..<segments {
123 let a = start + UInt32(segment), b = a + UInt32(segments + 1)
124 indices.append(contentsOf: [a, b, a + 1, a + 1, b, b + 1])
125 }
126 }
127 return Mesh(vertices: vertices, indices: indices, primitive: .triangles)
128 }
129
130 /// Tilted tubular rings orbit along different axes, with each arc swelling
131 /// from its own spectral band instead of a single global beat scalar.
132 private static func orbits(time: Float, audio: Audio, palette: Int) -> Mesh {
133 let rings = 7, segments = 144, tubeSegments = 8
134 var vertices: [Vertex] = [], indices: [UInt32] = []
135 for ring in 0..<rings {
136 let f = Float(ring) / Float(rings - 1)
137 let tilt = -1.1 + f * 2.2 + sin(time * 0.22 + f * turn) * 0.18
138 let spin = time * (0.08 + f * 0.08) + f * .pi
139 let baseRadius = 3.2 + f * 7
140 for segment in 0...segments {
141 let u = Float(segment) / Float(segments), angle = u * turn
142 let energy = audio.at((1 - cos(u * turn)) * 0.325 + f * 0.35)
143 let radius = baseRadius + energy * 0.65 + audio.bass * 0.25
144 for edge in 0...tubeSegments {
145 let v = Float(edge) / Float(tubeSegments), tubeAngle = v * turn
146 let thickness = 0.07 + energy * 0.17
147 let x = cos(angle) * (radius + cos(tubeAngle) * thickness)
148 let y = sin(angle) * (radius + cos(tubeAngle) * thickness)
149 let z = sin(tubeAngle) * thickness
150 let tiltedY = y * cos(tilt) - z * sin(tilt)
151 let tiltedZ = y * sin(tilt) + z * cos(tilt)
152 let position = SIMD3(x * cos(spin) - tiltedY * sin(spin), x * sin(spin) + tiltedY * cos(spin), tiltedZ)
153 vertices.append(vertex(position, phase: f * 0.7 + u * 0.35 + time * 0.012, energy: energy, palette: palette, uv: SIMD2(u * 6, v)))
154 }
155 }
156 let offset = UInt32(ring * (segments + 1) * (tubeSegments + 1))
157 indices.append(contentsOf: stripIndices(rows: segments, columns: tubeSegments).map { $0 + offset })
158 }
159 return Mesh(vertices: vertices, indices: indices, primitive: .triangles)
160 }
161
162 /// Twisting silk sails flow in depth, with broad folds and frequency-driven crests.
163 private static func ribbons(time: Float, audio: Audio, palette: Int) -> Mesh {
164 let strips = 8, segments = 180, edges = 4
165 var vertices: [Vertex] = [], indices: [UInt32] = []
166 for strip in 0..<strips {
167 let f = Float(strip) / Float(strips)
168 for segment in 0...segments {
169 let u = Float(segment) / Float(segments), energy = audio.at(u)
170 let angle = f * turn + u * turn * 0.8 - time * 0.6
171 let wave = sin(u * turn * 2.3 - time * 1.4 + f * 3) * (1.4 + energy * 2.2)
172 let radius = 6.5 + wave + audio.bass * 0.7
173 for edge in 0...edges {
174 let across = Float(edge) / Float(edges) - 0.5
175 let width = 1.2 + energy * 1.8
176 let fold = sin(across * .pi * 2 + u * 8 - time) * width * 0.65
177 let position = SIMD3(cos(angle) * radius - sin(angle) * across * width,
178 sin(angle) * radius + cos(angle) * across * width,
179 -18 + u * 36 + fold)
180 vertices.append(vertex(position, phase: u * 0.45 + f * 0.45 + time * 0.025,
181 energy: energy, palette: palette, uv: SIMD2(u * 6, Float(edge) / Float(edges))))
182 }
183 }
184 let offset = UInt32(strip * (segments + 1) * (edges + 1))
185 indices.append(contentsOf: stripIndices(rows: segments, columns: edges).map { $0 + offset })
186 }
187 return Mesh(vertices: vertices, indices: indices, primitive: .triangles)
188 }
189
190 /// Four substantial coils and discrete cross-rungs make a recognizable
191 /// double-helix ball sculpture, even in a narrow portrait preview.
192 private static func helix(time: Float, audio: Audio, palette: Int) -> Mesh {
193 let strands = 4, segments = 256, lanes = 1
194 var vertices: [Vertex] = []
195 vertices.reserveCapacity(strands * segments * lanes + 1408)
196 for strand in 0..<strands {
197 for segment in 0..<segments {
198 let u = Float(segment) / Float(segments - 1)
199 let energy = audio.at(u)
200 let angle = u * turn * 2.6 + Float(strand) * .pi / 2 + time * 0.95
201 let radius = 3.15 + audio.bass * 0.75 + energy * 0.42
202 let y = -10.3 + u * 20.6
203 for lane in 0..<lanes {
204 let crossAngle = Float(lane) / Float(lanes) * turn
205 let thickness = 0.13 + energy * 0.045
206 let laneRadius = radius + cos(crossAngle) * thickness
207 let ripple = sin(u * turn * 18 + time * 2.8) * energy * 0.08
208 let position = SIMD3(cos(angle) * laneRadius + ripple, y + sin(crossAngle) * thickness, sin(angle) * laneRadius)
209 vertices.append(vertex(position, phase: u * 0.5 + Float(strand) * 0.17 + time * 0.018, energy: energy, palette: palette))
210 }
211 }
212 }
213 // Open rungs bridge opposite coils; spacing keeps clear negative space
214 // between levels instead of turning the center into a solid cylinder.
215 for level in 0..<32 {
216 let u = (Float(level) + 0.5) / 32
217 let energy = audio.at(u)
218 let y = -10.3 + u * 20.6
219 let radius = 3.15 + audio.bass * 0.75 + energy * 0.42
220 for pair in 0..<2 {
221 let angle = u * turn * 2.6 + Float(pair) * .pi / 2 + time * 0.95
222 for sample in 0..<14 {
223 let across = -1 + Float(sample) / 13 * 2
224 let position = SIMD3(cos(angle) * radius * across, y + sin(across * .pi) * energy * 0.12, sin(angle) * radius * across)
225 vertices.append(vertex(position, phase: u * 0.5 + Float(pair) * 0.17 + time * 0.018, energy: energy * 0.7, palette: palette))
226 }
227 }
228 }
229 vertices.sort { $0.position.z < $1.position.z }
230 return Mesh(vertices: vertices, indices: [], primitive: .points, pointSize: 0.8)
231 }
232
233 /// Eight connected swirling plumes rise through the full centered frame.
234 /// Ordered flow lanes retain a strong vase-shaped silhouette, while depth
235 /// separates foreground balls from the smaller background arms.
236 private static func fountain(time: Float, audio: Audio, palette: Int) -> Mesh {
237 let arms = 8, samples = 192, lanes = 1
238 var vertices: [Vertex] = []
239 vertices.reserveCapacity(arms * samples * lanes + 1024)
240 for arm in 0..<arms {
241 let armPhase = Float(arm) / Float(arms)
242 let phaseOffset = random(UInt32(arm) &+ 43) * 0.12
243 for sample in 0..<samples {
244 let u = fraction(Float(sample) / Float(samples) + time * 0.055)
245 let energy = audio.at(armPhase * 0.55 + u * 0.45)
246 let belly = pow(max(0, sin(u * .pi)), 0.85)
247 let radius = 1.15 + belly * (5.3 + energy * 0.6) + u * 0.8 + audio.bass * 0.25
248 let angle = armPhase * turn + u * turn * 0.8 + time * 0.2 + phaseOffset
249 let y = -10 + u * 20
250 for lane in 0..<lanes {
251 let laneOffset = Float(lane) - Float(lanes - 1) * 0.5
252 let laneRadius = radius + laneOffset * (0.12 + energy * 0.025)
253 let laneAngle = angle + laneOffset * 0.016
254 let position = SIMD3(cos(laneAngle) * laneRadius, y + laneOffset * 0.025, sin(laneAngle) * laneRadius)
255 vertices.append(vertex(position, phase: armPhase * 0.8 + u * 0.25 + time * 0.018, energy: energy, palette: palette))
256 }
257 }
258 }
259 // The central rising braid connects the outer plumes into one object,
260 // rather than leaving unrelated droplets scattered across the canvas.
261 for strand in 0..<4 {
262 for sample in 0..<192 {
263 let u = fraction(Float(sample) / 192 + time * 0.075)
264 let energy = audio.at(u)
265 let angle = Float(strand) * .pi / 2 - u * turn * 1.6 - time * 0.28
266 let radius = 0.6 + sin(u * .pi) * (0.8 + audio.bass * 0.2) + energy * 0.12
267 let position = SIMD3(cos(angle) * radius, -9.8 + u * 19.6, sin(angle) * radius)
268 vertices.append(vertex(position, phase: u * 0.45 + Float(strand) * 0.15 + time * 0.018, energy: energy, palette: palette))
269 }
270 }
271 // Alpha-over sprites paint far layers before the near sphere surfaces.
272 vertices.sort { $0.position.z < $1.position.z }
273 return Mesh(vertices: vertices, indices: [], primitive: .points, pointSize: 1.0)
274 }
275
276 /// A square field of isolated luminous tiles. Each frequency raises a row,
277 /// with travelling diagonal pulses driven by bass and local band energy.
278 private static func grid(time: Float, audio: Audio, palette: Int) -> Mesh {
279 let side = 32
280 var vertices: [Vertex] = [], indices: [UInt32] = []
281 vertices.reserveCapacity(side * side * 4)
282 for row in 0..<side {
283 let v = Float(row) / Float(side - 1)
284 for column in 0..<side {
285 let u = Float(column) / Float(side - 1)
286 let energy = audio.at(u)
287 let pulse = (sin((u + v) * turn * 2 - time * 1.5) + 1) * 0.5
288 let z = energy * 2.8 + pulse * audio.bass * 1.2 - 1.4
289 let x = -10.8 + u * 21.6, y = -10.8 + v * 21.6
290 let size = 0.18 + 0.065 * energy + 0.05 * pulse
291 let offset = UInt32(vertices.count)
292 for corner in [SIMD2<Float>(-1, -1), SIMD2<Float>(1, -1), SIMD2<Float>(-1, 1), SIMD2<Float>(1, 1)] {
293 vertices.append(vertex(SIMD3(x + corner.x * size, y + corner.y * size, z), phase: u * 0.55 + v * 0.25 + time * 0.02, energy: energy + pulse * audio.bass * 0.2, palette: palette, uv: (corner + SIMD2(repeating: 1)) * 0.5))
294 }
295 indices.append(contentsOf: [offset, offset + 1, offset + 2, offset + 2, offset + 1, offset + 3])
296 }
297 }
298 return Mesh(vertices: vertices, indices: indices, primitive: .triangles)
299 }
300
301 /// A closed three-dimensional flower with five major lobes, a twisting pole
302 /// and frequency-resolved petal ridges across its longitude.
303 private static func bloom(time: Float, audio: Audio, palette: Int) -> Mesh {
304 let rings = 40, segments = 96
305 var vertices: [Vertex] = []
306 vertices.reserveCapacity((rings + 1) * (segments + 1))
307 for ring in 0...rings {
308 let v = Float(ring) / Float(rings), theta = v * .pi
309 for segment in 0...segments {
310 let u = Float(segment) / Float(segments), phi = u * turn
311 let energy = audio.at((1 - cos(u * turn)) * 0.5)
312 let petals = cos(phi * 5 + time * 0.3) * sin(theta) * sin(theta)
313 let radius = 6.6 + petals * (1.25 + energy * 0.6) + sin(theta * 8 - time * 0.75) * energy * 0.45 + audio.bass * 0.4
314 let twist = phi + cos(theta) * (0.22 + sin(time * 0.2) * 0.15)
315 let position = SIMD3(sin(theta) * cos(twist) * radius, cos(theta) * radius * 1.2, sin(theta) * sin(twist) * radius)
316 vertices.append(vertex(position, phase: u * 0.7 + v * 0.3 + time * 0.015, energy: energy, palette: palette, uv: SIMD2(u * 4, v * 3)))
317 }
318 }
319 return Mesh(vertices: vertices, indices: stripIndices(rows: rings, columns: segments), primitive: .triangles)
320 }
321
322 private static func fraction(_ value: Float) -> Float { value - floor(value) }
323 private static func random(_ seed: UInt32) -> Float {
324 var value = seed &+ 0x9e3779b9
325 value = (value ^ (value >> 16)) &* 0x85ebca6b
326 value = (value ^ (value >> 13)) &* 0xc2b2ae35
327 value ^= value >> 16
328 return Float(value >> 8) / Float(1 << 24)
329 }
330}