PulseCanvas / app · pulsecanvas.ai / scripts / VisualPerformanceSmoke.swift
| 1 | import AppKit |
| 2 | import MetalKit |
| 3 | import Foundation |
| 4 | @main struct VisualPerformanceSmoke { |
| 5 | @MainActor static func main() throws { |
| 6 | _ = NSApplication.shared |
| 7 | let state = RenderState(), renderer = VisualizerRenderer(state: state) |
| 8 | let spectrum = (0..<32).map { Float($0 % 7) / 10 } |
| 9 | state.scene = 21; state.motionBlur = 0 |
| 10 | for (width,height) in [(1920,1080),(960,540),(540,960)] { |
| 11 | try renderer.prepareOffscreen(width: width, height: height) |
| 12 | for index in 0..<5 { _ = try renderer.renderOffscreen(time: Double(index)/60, audioBands: spectrum) } |
| 13 | let start = Date() |
| 14 | for index in 0..<60 { _ = try renderer.renderOffscreen(time: Double(index+5)/60, audioBands: spectrum) } |
| 15 | print("Resonant Shell \(width)x\(height), 60 completed GPU frames, mean \(Date().timeIntervalSince(start)*1000/60) ms/frame") |
| 16 | } |
| 17 | if CommandLine.arguments.count > 1 { |
| 18 | let destination = URL(fileURLWithPath: CommandLine.arguments[1]) |
| 19 | try FileManager.default.createDirectory(at: destination, withIntermediateDirectories: true) |
| 20 | for (width,height) in [(960,540),(540,960)] { |
| 21 | try renderer.prepareOffscreen(width: width, height: height) |
| 22 | for scene in [15,17,18,21] { |
| 23 | state.scene = scene; state.palette = scene == 18 ? 3 : 0; state.particleSize = 1.6; state.zoom = 1.05 |
| 24 | let texture = try renderer.renderOffscreen(time: 3.4, audioBands: spectrum, audioPulse: 0.5) |
| 25 | let bitmap = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: width, pixelsHigh: height, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: width*4, bitsPerPixel: 32)! |
| 26 | texture.getBytes(bitmap.bitmapData!, bytesPerRow: width*4, from: MTLRegionMake2D(0,0,width,height), mipmapLevel: 0) |
| 27 | let bytes = bitmap.bitmapData! |
| 28 | for i in stride(from: 0, to: width*height*4, by: 4) { let b = bytes[i]; bytes[i] = bytes[i+2]; bytes[i+2] = b } |
| 29 | try bitmap.representation(using: .png, properties: [:])!.write(to: destination.appendingPathComponent("visual-\(scene)-\(width)x\(height).png")) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | } |