Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / OrganicPulseSmoke.swift

Raw 4.8 KB on 32796b3b2cff314d20e7c78bdc17d74e2eace8c1
1import AppKit
2import MetalKit
3
4@main struct OrganicPulseSmoke {
5 @MainActor static func main() throws {
6 _ = NSApplication.shared
7 let state = RenderState(), renderer = VisualizerRenderer(state: state)
8 let width = 320, height = 180
9 try renderer.prepareOffscreen(width: width, height: height)
10 state.speed = 1; state.zoomSpeed = 0; state.palette = VisualPalette.blueHour.rawValue
11 let root = URL(fileURLWithPath: ".build/organic-pulse-validation")
12 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
13 func image(chaos: Float, hit: Bool) throws -> [UInt8] {
14 state.chaos = chaos
15 // Hold time/camera fixed to isolate shape deformation from acceleration.
16 let texture = try renderer.renderOffscreen(time: 2.3,
17 audioBands: Array(repeating: hit ? 0.65 : 0, count: 32),
18 audioPulse: hit ? 0.9 : 0, audioMomentum: hit ? 0.85 : 0)
19 var bytes = [UInt8](repeating: 0, count: width*height*4)
20 bytes.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: width*4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0) }
21 return bytes
22 }
23 func difference(_ a: [UInt8], _ b: [UInt8]) -> Double {
24 var total = 0.0
25 for i in stride(from: 0, to: a.count, by: 4) { for c in 0..<3 { total += Double(abs(Int(a[i+c])-Int(b[i+c]))) } }
26 return total / Double(width*height*3)
27 }
28 // Fit global zoom, translation and brightness. Local ripples must retain
29 // substantial differences after the best whole-screen transform.
30 func globalResidual(_ quiet: [UInt8], _ active: [UInt8]) -> Double {
31 var best = Double.infinity
32 for scaleIndex in -10...10 {
33 let scale = 1 + Double(scaleIndex)*0.02
34 for dx in [-5,0,5] { for dy in [-5,0,5] {
35 var samples: [(Double,Double)] = [], aa = 0.0, ab = 0.0
36 for y in stride(from: 16, to: height-16, by: 4) { for x in stride(from: 24, to: width-24, by: 4) {
37 let sx = Int((Double(x-width/2-dx)/scale)+Double(width/2))
38 let sy = Int((Double(y-height/2-dy)/scale)+Double(height/2))
39 guard (0..<width).contains(sx), (0..<height).contains(sy) else { continue }
40 for c in 0..<3 {
41 let a = Double(quiet[(sy*width+sx)*4+c]), b = Double(active[(y*width+x)*4+c])
42 samples.append((a,b)); aa += a*a; ab += a*b
43 }
44 } }
45 let gain = min(2,max(0.5,ab/max(1,aa)))
46 let error = samples.reduce(0.0) { $0 + abs(min(255,$1.0*gain)-$1.1) } / Double(samples.count)
47 best = min(best,error)
48 } }
49 }
50 return best
51 }
52 func save(_ pixels: [UInt8], _ name: String) throws {
53 let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: width*4, bitsPerPixel: 32)!
54 for i in stride(from: 0, to: pixels.count, by: 4) {
55 bitmap.bitmapData![i]=pixels[i+2]; bitmap.bitmapData![i+1]=pixels[i+1]; bitmap.bitmapData![i+2]=pixels[i]; bitmap.bitmapData![i+3]=255
56 }
57 try bitmap.representation(using: .png, properties: [:])!.write(to: root.appendingPathComponent(name+".png"))
58 }
59 for scene in [18,22,23,25,29] {
60 state.scene = scene
61 let quiet = try image(chaos: 1, hit: false), low = try image(chaos: 0.2, hit: true), high = try image(chaos: 1, hit: true)
62 let lowChange = difference(quiet,low), highChange = difference(quiet,high)
63 let residual = globalResidual(quiet,high)
64 print("\(RenderState.sceneNames[scene]): low/high shape change \(lowChange)/\(highChange), residual after best global transform \(residual)")
65 precondition(highChange > lowChange*1.25 && residual > 1 && residual > highChange*0.35, "Beat response must deform local elements and grow more liquid with Chaos")
66 let repeatHigh = try image(chaos: 1, hit: true), released = try image(chaos: 1, hit: false), zero = try image(chaos: 0, hit: true)
67 precondition(high == repeatHigh && quiet == released && quiet == zero, "Waves must release, stay deterministic, and disappear at zero Chaos")
68 try save(quiet,"\(scene)-quiet"); try save(high,"\(scene)-waves")
69 }
70 print("PASS: local geometry waves cannot be explained by global zoom/translation/brightness; higher Chaos strengthens deformation, release and zero restore exact procedural frames")
71 }
72}