-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.js
312 lines (239 loc) · 9.98 KB
/
compile.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
const fs = require("fs")
const chalk = require("chalk")
let show = "halloween"
let frameData = {}
function insertData(frame, data) {
if (!frameData[frame]) {
frameData[frame] = []
}
frameData[frame].push(data)
}
function toByte(int) {
return Math.round(int * 255)
}
function set(instruction) {
let range = instruction.range
let color = instruction.color
let frame = instruction.frame
if (Array.isArray(range)) {
let compiledInstruction = [
"range",
[range.start, range.end],
[
toByte(color.hue / 360),
toByte(color.saturation / 100),
toByte(color.value / 100)
]
]
insertData(frame, compiledInstruction)
}
console.log(`${chalk.gray("[~]")} Set pixels ${range.start} to ${range.end} to (${color.hue}, ${color.saturation}, ${color.value}) at frame ${frame}!`)
}
function transform(instruction) {
let range = instruction.range
let color = instruction.color
let frame = instruction.frame
let hChange = color.end.hue - color.start.hue
let sChange = color.end.saturation - color.start.saturation
let vChange = color.end.value - color.start.value
let duration = frame.end - frame.start
for (let index = 0; index <= duration; index++) {
let hOffset = (index / duration) * hChange
let sOffset = (index / duration) * sChange
let vOffset = (index / duration) * vChange
let compiledInstruction = [
"range",
[range.start, range.end],
[
toByte((color.start.hue + hOffset) / 360),
toByte((color.start.saturation + sOffset) / 100),
toByte((color.start.value + vOffset) / 100),
]
]
insertData(frame.start + index, compiledInstruction)
}
console.log(`${chalk.gray("[~]")} Transformed pixels ${range.start} to ${range.end} to from (${color.start.hue}, ${color.start.saturation}, ${color.start.value}) to (${color.end.hue}, ${color.end.saturation}, ${color.end.value}) at between frame ${frame.start} and ${frame.end}!`)
}
function segment(instruction) {
let range = instruction.range
let color = instruction.color
let frame = instruction.frame
let numTotal = (range.end - range.start) + 1
let numPer = instruction.length || numTotal / color.length
let numSegments = numTotal / numPer
for (let index = 0; index < numSegments; index++) {
let start = range.start + (numPer * index)
let finish = range.start + (numPer * (index + 1)) - 1
if (finish > range.end) finish = range.end
let setColor = color[index % color.length]
let compiledInstruction = [
"range",
[start, finish],
[
toByte(setColor.hue / 360),
toByte(setColor.saturation / 100),
toByte(setColor.value / 100)
]
]
insertData(frame, compiledInstruction)
}
console.log(`${chalk.gray("[~]")} Segmented pixels ${range.start} to ${range.end} into ${color.length} colors at frame ${frame}!`)
}
function trail(instruction) {
let range = instruction.range
let color = instruction.color
let life = instruction.life
let decay = 1 - (instruction.decay / 100)
let reverse = instruction.reverse
let frame = instruction.frame
let totalLength = (range.end - range.start) + 1
let duration = (frame.end - frame.start) + 1
let changePerFrame = totalLength / duration
for (let index = 0; index <= (duration - 1); index++) {
let start = Math.round(range.start + (changePerFrame * index))
let end = Math.round((start + changePerFrame) - 1)
if (reverse) {
end = Math.round(range.end - (changePerFrame * index))
start = Math.round((end - changePerFrame) + 1)
}
let compiledInstruction = [
"range",
[start, end],
[
toByte(color.hue / 360),
toByte(color.saturation / 100),
toByte(color.value / 100)
]
]
insertData(frame.start + index, compiledInstruction)
if (life != 0) {
let compiledInstruction = [
"range",
[start, end],
[0, 0, 0]
]
insertData(frame.start + index + life, compiledInstruction)
if (decay != 1) {
let startDecay = Math.round(frame.start + index + ((life * decay)))
let endDecay = frame.start + index + life
let decayDuration = endDecay - startDecay
let decayFactor = color.value / decayDuration
for (let index = 1; index < decayDuration; index++) {
let compiledInstruction = [
"range",
[start, end],
[
toByte(color.hue / 360),
toByte(color.saturation / 100),
toByte((color.value - (decayFactor * index)) / 100)
]
]
insertData(startDecay + index, compiledInstruction)
}
}
}
}
console.log(`${chalk.gray("[~]")} Trailed pixels ${range.start} to ${range.end} to (${color.hue}, ${color.saturation}, ${color.value}) with a life of ${life} between frame ${frame.start} and ${frame.end}!`)
}
function twinkle(instruction) {
let range = instruction.range
let color = instruction.color
let life = instruction.life
let decay = 1 - (instruction.decay / 100)
let rate = instruction.rate
let frame = instruction.frame
let length = (range.end - range.start) + 1
let duration = (frame.end - frame.start) + 1
for (let index = 0; index <= (duration - 1); index++) {
let toTwinkle = []
for (let twinkled = 1; twinkled <= rate; twinkled++) {
toTwinkle.push(Math.round(Math.random() * length) + range.start)
}
let compiledInstruction = [
"list",
toTwinkle,
[
toByte(color.hue / 360),
toByte(color.saturation / 100),
toByte(color.value / 100)
]
]
insertData(frame.start + index, compiledInstruction)
if (life && life != 0) {
let compiledInstruction = [
"list",
toTwinkle,
[0, 0, 0]
]
insertData(frame.start + index + life, compiledInstruction)
if (decay != 1) {
let startDecay = Math.round(frame.start + index + ((life * decay)))
let endDecay = frame.start + index + life
let decayDuration = endDecay - startDecay
let decayFactor = color.value / decayDuration
for (let index = 1; index < decayDuration; index++) {
let compiledInstruction = [
"list",
toTwinkle,
[
toByte(color.hue / 360),
toByte(color.saturation / 100),
toByte((color.value - (decayFactor * index)) / 100)
]
]
insertData(startDecay + index, compiledInstruction)
}
}
}
}
console.log(`${chalk.gray("[~]")} Trailed pixels ${range.start} to ${range.end} to (${color.hue}, ${color.saturation}, ${color.value}) with a life of ${life} between frame ${frame.start} and ${frame.end}!`)
}
let filePath = __dirname + `/public/shows/${show}`
let zones = fs.readdirSync(filePath + "/raw")
zones.forEach((zone) => {
let startTime = Date.now()
console.log(chalk.yellow(`[=] Compiling ${chalk.underline(zone)}`))
frameData = {}
let raws = fs.readdirSync(filePath + `/raw/${zone}`)
raws.forEach((raw) => {
let data = fs.readFileSync(filePath + `/raw/${zone}/${raw}`, "utf-8")
data = JSON.parse(data)
data.forEach((instruction) => {
if (instruction.type == "set") set(instruction)
if (instruction.type == "transform") transform(instruction)
if (instruction.type == "segment") segment(instruction)
if (instruction.type == "trail") trail(instruction)
if (instruction.type == "twinkle") twinkle(instruction)
})
})
let info = fs.readFileSync(filePath + "/info.json", "utf-8")
info = JSON.parse(info)
if (info.zones[zone] & false) {
console.log(chalk.yellow(`[=] Creating Master Frame Data for ${chalk.underline(zone)}...`))
let length = info.length
let pixelCount = info.zones[zone]
let masterFrameData = {}
for (let pixel = 1; pixel <= pixelCount; pixel++) {
masterFrameData[pixel] = {}
}
Object.keys(frameData).forEach(frameNum => {
frame = frameData[frameNum]
frame.forEach((step) => {
if (step[0] == "range") {
for (let pixel = step[1][0]; pixel <= step[1][1]; pixel++) {
for (let changeFrame = frameNum; changeFrame <= length; changeFrame++) {
masterFrameData[pixel][changeFrame] = step[2]
}
}
}
})
})
masterFrameData = JSON.stringify(masterFrameData)
fs.mkdirSync(filePath + `/compiled/${zone}`, { recursive: true })
fs.writeFileSync(filePath + `/compiled/${zone}/master.json`, masterFrameData)
console.log(chalk.green(`[+] Done Creating Master Frame Data for ${chalk.underline(zone)}! (${(Date.now() - startTime) / 1000}s)`))
}
frameData = JSON.stringify(frameData)
fs.writeFileSync(filePath + `/compiled/${zone}/compact.json`, frameData)
console.log(`${chalk.green("[+]")} Done Compiling ${chalk.underline(zone)}!`)
})