|
| 1 | +import Foundation |
| 2 | +import Metal |
| 3 | +import MetalSplatter |
| 4 | +import simd |
| 5 | +import SplatIO |
| 6 | + |
| 7 | +/// Controller that procedurally generates colored splat cubes and cycles through LOD levels. |
| 8 | +/// Demonstrates adding/removing/enabling/disabling chunks on a live SplatRenderer. |
| 9 | +final class ProceduralSplatController: @unchecked Sendable { |
| 10 | + let splatRenderer: SplatRenderer |
| 11 | + |
| 12 | + // 3 colors × 3 LOD levels |
| 13 | + private let chunks: [[SplatChunk]] // [colorIndex][lodIndex] |
| 14 | + private var activeChunkIDs: [ChunkID] // One per color (3 total) |
| 15 | + |
| 16 | + // Swap cycle state |
| 17 | + private var cycleStep = 0 |
| 18 | + private var cycleTask: Task<Void, Never>? |
| 19 | + |
| 20 | + private static let colors: [SIMD3<UInt8>] = [ |
| 21 | + SIMD3(255, 0, 0), // Red |
| 22 | + SIMD3(0, 255, 0), // Green |
| 23 | + SIMD3(0, 0, 255), // Blue |
| 24 | + ] |
| 25 | + |
| 26 | + init(device: MTLDevice, |
| 27 | + colorFormat: MTLPixelFormat, |
| 28 | + depthFormat: MTLPixelFormat, |
| 29 | + sampleCount: Int, |
| 30 | + maxViewCount: Int, |
| 31 | + maxSimultaneousRenders: Int) async throws { |
| 32 | + splatRenderer = try SplatRenderer(device: device, |
| 33 | + colorFormat: colorFormat, |
| 34 | + depthFormat: depthFormat, |
| 35 | + sampleCount: sampleCount, |
| 36 | + maxViewCount: maxViewCount, |
| 37 | + maxSimultaneousRenders: maxSimultaneousRenders) |
| 38 | + |
| 39 | + // Generate cube centers at 120° intervals in the XZ plane |
| 40 | + let centers: [SIMD3<Float>] = (0..<3).map { i in |
| 41 | + let angle = Float(i) * (2 * .pi / 3) |
| 42 | + return SIMD3( |
| 43 | + cos(angle) * Constants.proceduralCubeDistance, |
| 44 | + 0, |
| 45 | + sin(angle) * Constants.proceduralCubeDistance |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + // Generate 3 colors × 3 LOD grid sizes |
| 50 | + var allChunks: [[SplatChunk]] = [] |
| 51 | + for colorIndex in 0..<3 { |
| 52 | + var lodChunks: [SplatChunk] = [] |
| 53 | + for gridSize in Constants.proceduralCubeGridSizes { |
| 54 | + let chunk = try Self.generateCubeChunk( |
| 55 | + device: device, |
| 56 | + center: centers[colorIndex], |
| 57 | + size: Constants.proceduralCubeSize, |
| 58 | + gridSize: gridSize, |
| 59 | + color: Self.colors[colorIndex] |
| 60 | + ) |
| 61 | + lodChunks.append(chunk) |
| 62 | + } |
| 63 | + allChunks.append(lodChunks) |
| 64 | + } |
| 65 | + chunks = allChunks |
| 66 | + |
| 67 | + // Add initial chunks (LOD 0) as enabled |
| 68 | + var initialIDs: [ChunkID] = [] |
| 69 | + for colorIndex in 0..<3 { |
| 70 | + let id = await splatRenderer.addChunk(chunks[colorIndex][0], enabled: true) |
| 71 | + initialIDs.append(id) |
| 72 | + } |
| 73 | + activeChunkIDs = initialIDs |
| 74 | + } |
| 75 | + |
| 76 | + /// Generate a cube of splats arranged in a uniform grid. |
| 77 | + private static func generateCubeChunk( |
| 78 | + device: MTLDevice, |
| 79 | + center: SIMD3<Float>, |
| 80 | + size: Float, |
| 81 | + gridSize: Int, |
| 82 | + color: SIMD3<UInt8> |
| 83 | + ) throws -> SplatChunk { |
| 84 | + let count = gridSize * gridSize * gridSize |
| 85 | + let spacing = size / Float(gridSize) |
| 86 | + let splatScale = spacing * Constants.proceduralCubeSplatRelativeRadius |
| 87 | + let halfSize = size / 2 |
| 88 | + |
| 89 | + var points: [SplatPoint] = [] |
| 90 | + points.reserveCapacity(count) |
| 91 | + |
| 92 | + for ix in 0..<gridSize { |
| 93 | + for iy in 0..<gridSize { |
| 94 | + for iz in 0..<gridSize { |
| 95 | + let position = center + SIMD3( |
| 96 | + -halfSize + (Float(ix) + 0.5) * spacing, |
| 97 | + -halfSize + (Float(iy) + 0.5) * spacing, |
| 98 | + -halfSize + (Float(iz) + 0.5) * spacing |
| 99 | + ) |
| 100 | + let point = SplatPoint( |
| 101 | + position: position, |
| 102 | + color: .sRGBUInt8(color), |
| 103 | + opacity: .linearFloat(1.0), |
| 104 | + scale: .linearFloat(SIMD3(repeating: splatScale)), |
| 105 | + rotation: simd_quatf(ix: 0, iy: 0, iz: 0, r: 1) |
| 106 | + ) |
| 107 | + points.append(point) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return try SplatChunk(device: device, from: points) |
| 113 | + } |
| 114 | + |
| 115 | + /// Called each frame to advance the LOD swap cycle. |
| 116 | + func update() { |
| 117 | + guard cycleTask == nil else { return } |
| 118 | + |
| 119 | + let colorIndex = cycleStep % 3 |
| 120 | + let targetLOD = (cycleStep / 3 + 1) % 3 |
| 121 | + let oldChunkID = activeChunkIDs[colorIndex] |
| 122 | + let newChunk = chunks[colorIndex][targetLOD] |
| 123 | + let renderer = splatRenderer |
| 124 | + |
| 125 | + cycleTask = Task { |
| 126 | + // Phase 1: Add new chunk (disabled), then wait for a sort that includes it |
| 127 | + let newID = await renderer.addChunk(newChunk, enabled: false) |
| 128 | + await withCheckedContinuation { continuation in |
| 129 | + renderer.afterNextSort { |
| 130 | + continuation.resume() |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Phase 2: Enable new chunk and remove old chunk atomically |
| 135 | + await renderer.withChunkAccess { |
| 136 | + await renderer.setChunkEnabled(newID, enabled: true) |
| 137 | + await renderer.removeChunk(oldChunkID) |
| 138 | + } |
| 139 | + self.activeChunkIDs[colorIndex] = newID |
| 140 | + |
| 141 | + try? await Task.sleep(for: .seconds(Constants.proceduralCubeSwapDelay)) |
| 142 | + |
| 143 | + // Advance to next step |
| 144 | + self.cycleStep = (self.cycleStep + 1) % 9 |
| 145 | + self.cycleTask = nil |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments