-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim.lua
254 lines (217 loc) · 8.23 KB
/
sim.lua
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
local sim = {
maxStepsPerSec = 120, -- negate for unlimited
maxFPS = 60 -- (but not for this)
}
local function nc32(w, h)
return love.graphics.newCanvas(w, h, { format='rgba32f' })
end
function sim.load()
canvases.x = nc32(64 * 4, 64)
canvases.h = nc32(64 * 128 / 4, 64)
canvases.y = nc32(64 * 4, 64)
canvases.edit = nc32(64, 64)
shaders.h = love.graphics.newShader('shaders/h.glsl')
shaders.y = love.graphics.newShader('shaders/y.glsl')
shaders.edit = love.graphics.newShader('shaders/edit.glsl')
shaders.move = love.graphics.newShader('shaders/move.glsl')
sim.xQuad = love.graphics.newQuad(0, 0, 64, 64, 64 * 4, 64)
sim.viewport = { w=64 * 8, h=64 * 8 }
sim.viewport.x = math.floor(ssx / 2 - sim.viewport.w / 2)
sim.viewport.y = math.floor(ssy / 2 - sim.viewport.h / 2)
sim.movePos = { x=0, y=0 }
sim.initializeX()
sim.loadWeights()
sim.stepCount = 0
sim.stepCountTime = love.timer.getTime()
sim.stepTimer = 0
sim.stepsPerSec = 0
end
function sim.initializeX()
love.graphics.setCanvas(canvases.x)
love.graphics.clear()
-- rgb 0, alpha & all other channels 1
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', 32, 32, 1, 1)
love.graphics.setColor(1, 1, 1)
for i=1, 3 do
love.graphics.rectangle('fill', 32 + i * 64, 32, 1, 1)
end
love.graphics.setCanvas(canvases.h)
love.graphics.clear()
love.graphics.setCanvas()
end
function sim.loadWeights()
local weights = love.filesystem.load('data/persist_4k.lua')()
local dense1 = love.image.newImageData(16 * 3 + 1, 128 / 4, 'rgba32f')
local dense2 = love.image.newImageData(128 + 1, 16 / 4, 'rgba32f')
for j=1, 128 / 4 do
local j0 = (j - 1) * 4
local w = weights.d1_kernel
for i=1, 16 * 3 do
dense1:setPixel(i - 1, j - 1, w[i][j0 + 1], w[i][j0 + 2], w[i][j0 + 3], w[i][j0 + 4])
end
local b = weights.d1_bias
dense1:setPixel(16 * 3, j - 1, b[j0 + 1], b[j0 + 2], b[j0 + 3], b[j0 + 4])
end
for j=1, 16 / 4 do
local j0 = (j - 1) * 4
local w = weights.d2_kernel
for i=1, 128 do
dense2:setPixel(i - 1, j - 1, w[i][j0 + 1], w[i][j0 + 2], w[i][j0 + 3], w[i][j0 + 4])
end
local b = weights.d2_bias
dense2:setPixel(128, j - 1, b[j0 + 1], b[j0 + 2], b[j0 + 3], b[j0 + 4])
end
shaders.h:send('dense1', love.graphics.newImage(dense1))
shaders.y:send('dense2', love.graphics.newImage(dense2))
end
function sim.edit(editType, cellX, cellY)
local cellX, cellY = sim.mouseToCell(love.mouse.getPosition())
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
love.graphics.setBlendMode('replace', 'premultiplied')
-- get edit
love.graphics.setCanvas(canvases.edit)
love.graphics.setShader()
love.graphics.clear()
if editType == 'seed' then
love.graphics.setColor(1, 1, 1)
love.graphics.rectangle('fill', cellX, cellY, 1, 1)
elseif editType == 'damage' then
love.graphics.setColor(0, 0, 0)
-- loop edit circle
for i=0, 1 do
local circleX = cellX + i * 64 * (cellX < 32 and 1 or -1)
for j=0, 1 do
local circleY = cellY + j * 64 * (cellY < 32 and 1 or -1)
love.graphics.circle('fill', circleX, circleY, 10)
end
end
end
-- get y
love.graphics.setCanvas(canvases.y)
love.graphics.setShader(shaders.edit)
shaders.edit:send('xin', canvases.x)
shaders.edit:send('edit', canvases.edit)
love.graphics.rectangle('fill', 0, 0, canvases.x:getDimensions())
-- set x to y
love.graphics.setShader()
love.graphics.setCanvas(canvases.x)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.y)
love.graphics.setBlendMode('alpha', 'alphamultiply')
love.graphics.setShader(_shader)
love.graphics.setCanvas(_canvas)
sim.stepCount = sim.stepCount + 1
end
function sim.move(dx, dy)
-- get movement in cell space - move when >= 1 cell
sim.movePos.x = sim.movePos.x + dx / 8
sim.movePos.y = sim.movePos.y + dy / 8
if math.abs(sim.movePos.x) >= 1 or math.abs(sim.movePos.y) >= 1 then
local mpxDir = sim.movePos.x >= 0 and 1 or -1
local mpyDir = sim.movePos.y >= 0 and 1 or -1
local cell_dx = math.floor(math.abs(sim.movePos.x)) * mpxDir
local cell_dy = math.floor(math.abs(sim.movePos.y)) * mpyDir
sim.movePos.x = (math.abs(sim.movePos.x) % 1) * mpxDir
sim.movePos.y = (math.abs(sim.movePos.y) % 1) * mpyDir
local _canvas = love.graphics.getCanvas()
local _shader = love.graphics.getShader()
love.graphics.setBlendMode('replace', 'premultiplied')
-- get y
love.graphics.setCanvas(canvases.y)
love.graphics.setShader(shaders.move)
shaders.move:send('delta', { cell_dx, cell_dy })
love.graphics.draw(canvases.x)
-- set x to y
love.graphics.setShader()
love.graphics.setCanvas(canvases.x)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.y)
love.graphics.setBlendMode('alpha', 'alphamultiply')
love.graphics.setShader(_shader)
love.graphics.setCanvas(_canvas)
sim.stepCount = sim.stepCount + 1
end
end
function sim.mouseToCell(x, y)
local v = sim.viewport
local cx = math.floor((x - v.x) / v.w * 64)
local cy = math.floor((y - v.y) / v.h * 64)
return cx, cy
end
function sim.mousepressed(x, y, btn)
if btn == 1 and love.keyboard.isScancodeDown('lshift') then
sim.edit('seed', sim.mouseToCell(x, y))
end
end
function sim.mousemoved(x, y, dx, dy)
if love.mouse.isDown(2) then
sim.move(dx, dy)
end
end
function sim.mouseUpdate(dt)
if love.mouse.isDown(1) and not love.keyboard.isScancodeDown('lshift') then
sim.edit('damage', sim.mouseToCell(love.mouse.getPosition()))
end
end
function sim.update(dt)
local startTime = love.timer.getTime()
sim.stepTimer = sim.stepTimer - dt
-- getTime doesn't seem to work when called with sub-ms delay
-- so limiting by frameSteps is necessary
local frameSteps = 0
while love.timer.getTime() - startTime < 1 / sim.maxFPS
and sim.stepTimer < 0
and frameSteps < sim.stepsPerSec * 2 / sim.maxFPS + 1 do
sim.step()
sim.stepTimer = sim.stepTimer + 1 / sim.maxStepsPerSec
frameSteps = frameSteps + 1
end
local remainingTimeBudget = 1 / sim.maxFPS
- (love.timer.getTime() - startTime)
love.timer.sleep(math.max(remainingTimeBudget, 0))
end
function sim.step()
love.graphics.setColor(1, 1, 1)
love.graphics.setBlendMode('replace', 'premultiplied')
-- get h
love.graphics.setCanvas(canvases.h)
love.graphics.setShader(shaders.h)
shaders.h:send('xin', canvases.x)
love.graphics.rectangle('fill', 0, 0, canvases.h:getDimensions())
-- get y
love.graphics.setCanvas(canvases.y)
love.graphics.setShader(shaders.y)
shaders.y:send('xin', canvases.x)
shaders.y:send('h', canvases.h)
shaders.y:send('randOffset', { love.math.random() * 1000, love.math.random() * 1000 })
love.graphics.rectangle('fill', 0, 0, canvases.y:getDimensions())
-- set x to y
love.graphics.setCanvas(canvases.x)
love.graphics.setShader()
love.graphics.draw(canvases.y)
love.graphics.setCanvas()
love.graphics.setBlendMode('alpha', 'alphamultiply')
sim.stepCount = sim.stepCount + 1
if love.timer.getTime() - sim.stepCountTime > 1 then
sim.stepsPerSec = sim.stepCount
love.window.setTitle('Neural CA ('
.. love.timer.getFPS() .. ' FPS, '
.. sim.stepsPerSec .. ' steps/sec)')
sim.stepCount = 0
sim.stepCountTime = love.timer.getTime()
end
end
function sim.draw()
love.graphics.setColor(0.1, 0.1, 0.1)
local v = sim.viewport
love.graphics.rectangle('fill', v.x - 4, v.y - 4, v.w + 8, v.h + 8)
love.graphics.setColor(0.9, 0.9, 0.9)
love.graphics.rectangle('fill', v.x, v.y, v.w, v.h)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(canvases.x, sim.xQuad, ssx / 2, ssy / 2, 0, 8, 8, 32, 32)
love.graphics.draw(canvases.h, 0, 0, 0, 0.5, 0.5)
love.graphics.draw(canvases.x, 0, 32)
end
return sim