Branchwell

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

Raw 6.8 KB on 0c0f13aedcaf984646237f478d4915d73e9d0aeb
1import AppKit
2import MetalKit
3import Foundation
4@main struct SpatialVisualSmoke {
5 @MainActor static func main() throws {
6 _ = NSApplication.shared
7 let root = URL(fileURLWithPath: ".build/spatial-validation")
8 try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
9 let state = RenderState(), renderer = VisualizerRenderer(state: state)
10 let scenes = [10,12,22,23,24,25,26,27,28,29]
11 let spectrum = (0..<32).map { Float($0 % 7) / 10 }
12 let width=960, height=540
13 try renderer.prepareOffscreen(width: width, height: height)
14 var results: [[UInt8]] = []
15 for scene in scenes {
16 state.scene=scene; state.palette=scene % 5; state.particleSize=1.5; state.zoom=1; state.motionBlur=0
17 let texture = try renderer.renderOffscreen(time: 2.3, audioBands: spectrum, audioPulse: 0.35, audioMomentum: 0.5, audioBeatMotionTime: 0.3)
18 let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: width*4, bitsPerPixel: 32)!
19 texture.getBytes(bitmap.bitmapData!, bytesPerRow: width*4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0)
20 for i in stride(from:0,to:width*height*4,by:4) { let b=bitmap.bitmapData![i]; bitmap.bitmapData![i]=bitmap.bitmapData![i+2]; bitmap.bitmapData![i+2]=b }
21 try bitmap.representation(using: .png, properties: [:])!.write(to: root.appendingPathComponent("\(scene)-\(RenderState.sceneNames[scene].replacingOccurrences(of:" ",with:"-")).png"))
22 state.palette=0
23 let image = try renderer.renderOffscreen(time:2.3,audioBands:spectrum)
24 var bytes=[UInt8](repeating:0,count:width*height*4)
25 bytes.withUnsafeMutableBytes { image.getBytes($0.baseAddress!,bytesPerRow:width*4,from:MTLRegionMake2D(0,0,width,height),mipmapLevel:0) }
26 for other in results {
27 let difference=zip(bytes,other).reduce(0.0) { $0+Double(abs(Int($1.0)-Int($1.1))) }/Double(bytes.count)
28 precondition(difference>4,"New spatial styles must differ with the same palette and settings")
29 }
30 if scene == 22 {
31 var occupied = Set<Int>()
32 for pixel in 0..<(width * height) where (0..<3).contains(where: { bytes[pixel*4+$0] > 80 }) {
33 occupied.insert(((pixel / width) * 4 / height) * 4 + (pixel % width) * 4 / width)
34 }
35 precondition(occupied.count >= 14, "Octagon spiral must contain bright geometry throughout the canvas")
36 }
37 if scene == 23 {
38 var edges = 0
39 for pixel in 1..<(width * height) {
40 if (0..<3).contains(where: { abs(Int(bytes[pixel*4+$0])-Int(bytes[(pixel-1)*4+$0])) > 4 }) { edges += 1 }
41 }
42 precondition(edges > 300, "Liquid surface must retain local geometric detail instead of a flat color wash")
43 }
44 if scene == 23 {
45 var detailFrames: [[UInt8]] = []
46 for detail: Float in [0, 1] {
47 state.liquidDetail = detail
48 let image = try renderer.renderOffscreen(time: 2.3, audioBands: spectrum)
49 var detailPixels = [UInt8](repeating: 0, count: bytes.count)
50 detailPixels.withUnsafeMutableBytes { image.getBytes($0.baseAddress!, bytesPerRow: width*4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0) }
51 detailFrames.append(detailPixels)
52 }
53 let change = zip(detailFrames[0],detailFrames[1]).reduce(0.0) { $0+Double(abs(Int($1.0)-Int($1.1))) } / Double(bytes.count)
54 precondition(change > 5, "Liquid detail must visibly change actual rendered geometry")
55 let levels = stride(from: 0, to: bytes.count, by: 4).map { max(bytes[$0], max(bytes[$0+1], bytes[$0+2])) }
56 precondition(levels.filter { $0 < 30 }.count > width*height/100 && levels.filter { $0 > 140 }.count > width*height/100, "Liquid frame needs both dark cavities and bright reflections")
57 state.liquidDetail = 0.6
58 print("PASS: Liquid detail changes GPU pixels (mean \(change)); dark cavities and bright reflections both cover visible areas")
59 }
60 if scene == 28 {
61 var countFrames: [[UInt8]] = []
62 for count in [1,12] {
63 state.haloCount = count
64 let texture = try renderer.renderOffscreen(time: 2.3, audioBands: spectrum)
65 var pixels = [UInt8](repeating: 0, count: bytes.count)
66 pixels.withUnsafeMutableBytes { texture.getBytes($0.baseAddress!, bytesPerRow: width*4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0) }
67 countFrames.append(pixels)
68 let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: width*4, bitsPerPixel: 32)!
69 for pixel in 0..<(width*height) {
70 bitmap.bitmapData![pixel*4] = pixels[pixel*4+2]; bitmap.bitmapData![pixel*4+1] = pixels[pixel*4+1]
71 bitmap.bitmapData![pixel*4+2] = pixels[pixel*4]; bitmap.bitmapData![pixel*4+3] = 255
72 }
73 try bitmap.representation(using: .png, properties: [:])!.write(to: root.appendingPathComponent("Halo-Count-\(count).png"))
74 }
75 let changed = zip(countFrames[0],countFrames[1]).reduce(0.0) { $0 + Double(abs(Int($1.0)-Int($1.1))) } / Double(bytes.count)
76 func visible(_ frame: [UInt8]) -> Int { stride(from: 0, to: frame.count, by: 4).filter { max(frame[$0],max(frame[$0+1],frame[$0+2])) > 60 }.count }
77 let sparse = visible(countFrames[0]), dense = visible(countFrames[1])
78 precondition(changed > 3 && dense > sparse * 2, "Halo count must add visible rings rather than only changing their thickness")
79 state.haloCount = 6
80 print("PASS: Halo count changes GPU geometry, mean \(changed), visible pixels one/twelve \(sparse)/\(dense)")
81 }
82 results.append(bytes)
83 let start=Date()
84 for frame in 0..<20 { _ = try renderer.renderOffscreen(time:2.3+Double(frame)/60,audioBands:spectrum,audioPulse:0.35,audioMomentum:0.5) }
85 print("\(RenderState.sceneNames[scene]): 960x540, 20 completed GPU frames, mean \(Date().timeIntervalSince(start)*1000/20) ms/frame")
86 }
87 print("PASS: all \(scenes.count) spatial designs are visually distinct using identical palette/settings; GPU captures saved for inspection")
88 }
89}