PulseCanvas / app · pulsecanvas.ai / scripts / ZoomSmoke.swift
| 1 | import AppKit |
| 2 | import MetalKit |
| 3 | import Foundation |
| 4 | @main struct ZoomSmoke { |
| 5 | @MainActor static func main() throws { |
| 6 | _ = NSApplication.shared |
| 7 | let state = RenderState(), renderer = VisualizerRenderer(state: RenderState()) |
| 8 | renderer.state = state |
| 9 | state.chaos = 0 |
| 10 | let width = 320, height = 180 |
| 11 | try renderer.prepareOffscreen(width: width, height: height) |
| 12 | func pixels(_ time: Double) throws -> [UInt8] { |
| 13 | let texture = try renderer.renderOffscreen(time: time, audioBands: Array(repeating: 0, count: 32)) |
| 14 | var bytes = [UInt8](repeating: 0, count: width * height * 4) |
| 15 | bytes.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: width * 4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0) } |
| 16 | return bytes |
| 17 | } |
| 18 | func difference(_ a: [UInt8], _ b: [UInt8]) -> Double { |
| 19 | zip(a,b).reduce(0.0) { $0 + Double(abs(Int($1.0)-Int($1.1))) } / Double(a.count) |
| 20 | } |
| 21 | for scene in NativeEffects.activeIDs { |
| 22 | state.scene = scene; state.zoomSpeed = 0 |
| 23 | let flat = try pixels(2.3), movingFlat = try pixels(2.7) |
| 24 | precondition(difference(flat, movingFlat) > 0.05, "Zero zoom must retain animation: \(RenderState.sceneNames[scene])") |
| 25 | state.zoomSpeed = 1 |
| 26 | let travel = try pixels(2.3) |
| 27 | state.zoomSpeed = 3 |
| 28 | let fast = try pixels(2.3) |
| 29 | precondition(difference(flat,travel) > 0.5 && difference(travel,fast) > 0.5, "Zoom speed must visibly affect \(RenderState.sceneNames[scene])") |
| 30 | state.zoomSpeed = 0 |
| 31 | let restored = try pixels(2.3) |
| 32 | precondition(flat == restored, "Disabling zoom must restore deterministic framing") |
| 33 | print("PASS: \(RenderState.sceneNames[scene]) zero retains animation, low/high speed changes GPU pixels, zero restores exact frame") |
| 34 | } |
| 35 | // Every expanding layer resets with zero contribution, including across |
| 36 | // both its own boundary and the offset layer's boundary. |
| 37 | state.scene = 18; state.zoomSpeed = 1 |
| 38 | for boundary in [3.125, 6.25, 9.375] { |
| 39 | let before = try pixels(boundary - 0.0001), after = try pixels(boundary + 0.0001) |
| 40 | precondition(difference(before,after) < 1, "Forward zoom loop cannot visibly jump at cycle boundaries") |
| 41 | } |
| 42 | for scene in [22,25,29] { |
| 43 | state.scene = scene |
| 44 | for speed: Float in [0,1.5,3] { |
| 45 | state.zoomSpeed = speed |
| 46 | var maximumChange = 0.0 |
| 47 | for timestamp in [0.3,2.3,3.125,6.25,9.375,17,33,60,90] { |
| 48 | let before = try pixels(timestamp - 0.0001), after = try pixels(timestamp + 0.0001) |
| 49 | let change = difference(before,after) |
| 50 | maximumChange = max(maximumChange,change) |
| 51 | precondition(change < 1, "Continuous tunnel motion cannot jump: \(RenderState.sceneNames[scene]), zoom \(speed), time \(timestamp), change \(change)") |
| 52 | } |
| 53 | print("PASS: \(RenderState.sceneNames[scene]) zero-Chaos tunnel continuity, zoom \(speed), max adjacent-time pixel change \(maximumChange)") |
| 54 | } |
| 55 | } |
| 56 | // Real playback cadence also catches an isolated reset that a sparse |
| 57 | // infinitesimal-time check could miss. Inspect early and late motion. |
| 58 | var cadenceChecks = 0 |
| 59 | for scene in [22,25,29] { |
| 60 | state.scene = scene |
| 61 | for speed: Float in [0,1.5,3] { |
| 62 | state.zoomSpeed = speed |
| 63 | for fps in [30,60] { |
| 64 | for start in [2.0,85.0] { |
| 65 | var previous = try pixels(start), changes: [Double] = [] |
| 66 | for frame in 1...fps * 2 { |
| 67 | let current = try pixels(start + Double(frame) / Double(fps)) |
| 68 | changes.append(difference(previous,current)); previous = current |
| 69 | cadenceChecks += 1 |
| 70 | } |
| 71 | let median = changes.sorted()[changes.count / 2] |
| 72 | let peak = changes.max()! |
| 73 | precondition(peak < median * 3 + 5, "Isolated tunnel jump at playback cadence: scene \(scene), speed \(speed), fps \(fps), start \(start), peak \(peak), median \(median)") |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | print("PASS: \(cadenceChecks) actual 30/60fps tunnel frame transitions without isolated jumps at early/late timestamps and three zoom speeds") |
| 79 | print("PASS: seamless forward zoom cycle boundaries, smooth tunnels at zero Chaos, and all \(NativeEffects.activeIDs.count) styles") |
| 80 | } |
| 81 | } |