-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridSpaceView.swift
157 lines (132 loc) · 7.38 KB
/
GridSpaceView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// GridSpaceView.swift
// XRMapTest
//
// Created by Michael A Edgcumbe on 12/16/23.
//
import SwiftUI
import RealityKit
import RealityKitContent
import AudioKit
struct GridSpaceView: View {
let rootEntity = AnchorEntity()
@State private var mapEntities:[Entity] = [Entity]()
static let audioEngineModel = AudioEngineModel()
@StateObject private var audioModel = FFTModel(GridSpaceView.audioEngineModel.player)
static let numGrids:Int = 2
static let stripLightCount:Int = 60
static let stripCount:Int = 10
static let stripLightLayerCount = 1
@ObservedObject var animationParameters:HSVAnimation
static let allLightsCount = GridSpaceView.stripLightCount * GridSpaceView.stripLightLayerCount * GridSpaceView.stripCount
@State private var scaleMiddleLayer:Float = 10
var body: some View {
RealityView { content in
content.add(rootEntity)
for gridIndex in 0..<GridSpaceView.numGrids {
do {
let entity = try await Entity(named: "Grid", in:realityKitContentBundle)
entity.name = "Grid"
// entity.transform.translation = SIMD3(Float(gridIndex) * 0.1625,0,0)
entity.transform.scale = SIMD3(2,2,2)
entity.transform.translation = SIMD3(0,0,Float(gridIndex))
mapEntities.append(entity)
} catch {
print(error)
}
}
for entity in mapEntities {
rootEntity.addChild(entity)
print("Adding \(entity.name) to root")
}
rootEntity.transform.translation = SIMD3(0,0.25,-1)
rootEntity.transform.rotation = simd_quatf(angle: Float.pi / 2.0, axis: SIMD3(0,1,0))
print("Finished Building")
}
.onAppear(perform: {
Task { @MainActor in
try GridSpaceView.audioEngineModel.engine.start()
GridSpaceView.audioEngineModel.player.play()
}
audioModel.onAppear(node:GridSpaceView.audioEngineModel.player)
})
.onDisappear(perform: {
GridSpaceView.audioEngineModel.player.pause()
})
.onChange(of: animationParameters.displayLinkTimestamp, { oldValue, newValue in
guard animationParameters.lightOffset.count == GridSpaceView.numGrids else {
return
}
animationParameters.resetOffset(with:audioModel.amplitudes, lightCount: GridSpaceView.stripLightCount, stripCount: GridSpaceView.stripCount, gridCount: GridSpaceView.numGrids)
for gridIndex in 0..<GridSpaceView.numGrids {
for stripIndex in 0..<GridSpaceView.stripCount {
for lightIndex in 0..<GridSpaceView.stripLightCount {
do {
if stripIndex == 0 {
try animationParameters.setBrightness(to:Float( animationParameters.offsetRandomness[gridIndex][stripIndex][lightIndex]), for: stripIndex, lightIndex: lightIndex, gridIndex:gridIndex)
try animationParameters.setSaturation(to:0, for: stripIndex, lightIndex: lightIndex, gridIndex:gridIndex)
} else {
try animationParameters.setHue(to:1.0 - 0.025 * Float((GridSpaceView.stripCount - stripIndex)), for: stripIndex, lightIndex: lightIndex, gridIndex:gridIndex)
try animationParameters.setSaturation(to:0.1 * Float((GridSpaceView.stripCount - stripIndex)), for: stripIndex, lightIndex: lightIndex, gridIndex:gridIndex)
try animationParameters.setBrightness(to:0.1 * Float((GridSpaceView.stripCount - stripIndex)), for: stripIndex, lightIndex:lightIndex, gridIndex:gridIndex)
}
} catch {
print(error)
}
}
}
}
})
.onChange(of: animationParameters.lightOffset, { oldValue, newValue in
for gridIndex in 0..<mapEntities.count {
for stripIndex in 0..<GridSpaceView.stripCount {
for layerIndex in 0..<GridSpaceView.stripLightLayerCount {
for lightIndex in 0..<GridSpaceView.stripLightCount {
let lightAddress = 180 * stripIndex + 60 * layerIndex + lightIndex
guard let lightEntity = mapEntities[gridIndex].findEntity(named: "Light_\(lightIndexString(index:lightAddress))")
else {
print("Did not find light \(lightIndexString(index: lightIndex))")
return
}
guard var modelComponent = lightEntity.components[ModelComponent.self] else {
print("Did not find model component")
return
}
guard var shaderGraphMaterial = modelComponent.materials.first as? ShaderGraphMaterial else {
print("Did not find shader graph material")
return
}
do {
var displacement = newValue[gridIndex][stripIndex][lightIndex][layerIndex]
// displacement.y = displacement.y Float(GridSpaceView.stripLightLayerCount)
// if stripIndex == 0 {
lightEntity.transform.scale = SIMD3(1,5,1)
displacement.y /= 5
// }
try shaderGraphMaterial.setParameter(name: "HSVAdjustment", value: .simd3Float(animationParameters.hsv[gridIndex][stripIndex][lightIndex][layerIndex]))
try shaderGraphMaterial.setParameter(name: "ModelOffset", value: .simd3Float(displacement))
modelComponent.materials = [shaderGraphMaterial]
lightEntity.components.set(modelComponent)
} catch {
print(error)
}
}
}
}
}
})
}
func lightIndexString(index:Int)->String{
var lightIndexString = "\(index + 1)"
if lightIndexString.count == 1 {
lightIndexString = "00\(lightIndexString)"
} else if lightIndexString.count == 2 {
lightIndexString = "0\(lightIndexString)"
}
return lightIndexString
}
}
#Preview {
let animationParameters = HSVAnimation(lightCount:GridSpaceView.stripLightCount, stripCount:GridSpaceView.stripCount, layerCount: GridSpaceView.stripLightLayerCount, numGrids: 1)
return GridSpaceView(animationParameters: animationParameters)
}