-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrenderer.lua
More file actions
323 lines (284 loc) · 9.1 KB
/
renderer.lua
File metadata and controls
323 lines (284 loc) · 9.1 KB
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
313
314
315
316
317
318
319
320
321
322
323
local module = {}
local traceDistance = not true
local progressiveVision = not true
local maxStroke = 150 / love.graphics.getDPIScale()
local strokeDecay = traceDistance and 500000 or 1000000
local lume = require('lume')
local noise = require('noise')
local pi = math.pi
local huge = math.huge
local min = math.min
local max = math.max
local abs = math.abs
local sqrt = math.sqrt
local sin = math.sin
local cos = math.cos
local atan2 = math.atan2
local random = math.random
function module.new(width, height, stroke, opacity)
local instance = setmetatable({}, {__index=module})
instance.canvas = love.graphics.newCanvas(width, height)
instance.stroke = stroke or maxStroke
instance.currentStroke = progressiveVision and maxStroke or instance.stroke
instance.opacity = progressiveVision and .1 or (opacity or .9)
return instance
end
function module:draw(scene, duration, canvas, stroke, opacity)
canvas = canvas or self.canvas
local width = canvas:getWidth()
local height = canvas:getHeight()
stroke = stroke or self.currentStroke / height
opacity = opacity or self.opacity
local t = love.timer.getTime()
local raysShot = 0
love.graphics.push('all')
love.graphics.reset()
love.graphics.setCanvas(canvas)
love.graphics.translate(width/2, height/2)
love.graphics.scale(height/2, -height/2)
while love.timer.getTime() - t < duration do
local x = -width/height + 2 * width/height * random()
local y = -1 + 2 * random()
local r,g,b,a
local h, s, l, d = module.trace(scene, x, y, {}, 1)
local ss = stroke
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.rotate(.1 + random())
if traceDistance then -- trace distance from edge for stroke size
ss = max(stroke, min(maxStroke, .0 + .39 * abs(d)))
end
-- l = l * d / abs(d) -- backify if d < 0 by setting negative lightness
if d < 0 then --noise background
l = .03 + .8 * l + .1 * math.random()
s = s * .3
end
r,g,b,a = lume.hsl(h, s, l, opacity)
--a = d > 0 and d * 100 or 0
love.graphics.setColor(r,g,b,a)
love.graphics.ellipse('fill', 0, 0, ss, ss/2, 17)
love.graphics.pop()
raysShot = raysShot + 1
end
if progressiveVision then
self.opacity = min(.9, self.opacity + 10 * raysShot / strokeDecay)
self.currentStroke = max(self.stroke, self.currentStroke - self.currentStroke^2 * raysShot / strokeDecay)
else
self.currentStroke = self.stroke
end
love.graphics.pop()
return raysShot
end
function module:resetStroke()
if progressiveVision then
self.currentStroke = maxStroke
self.opacity = 0.3
end
end
-- local memos = {}
--
-- local ffi = require('ffi')
-- ffi.cdef[[
-- typedef struct { double h,s,l,d; } memoryT;
-- ]]
--
-- function memoLookup(node, precision, x, y, env, depth, tracefun)
-- tracefun = tracefun or module.trace
-- local hsld, h, s, l, d
-- ---[[
-- -- introduce dithering
-- local xd = x + precision * (random() - .5)
-- local yd = y + precision * (random() - .5)
-- -- snap x,y to grid
-- local xg = xd - (xd % precision) + precision / 2
-- local yg = yd - (yd % precision) + precision / 2
-- --]]
--
-- memos[node] = memos[node] or {count = 0}
-- local memo = memos[node] -- this is memo table for specific node
-- --memo[xg] = memo[xg] or {} -- search by x
-- local mem = memo[xg] and memo[xg][yg] -- search by y
-- if not mem or random() > .95 then
-- h, s, l, d = tracefun(node[3], x, y, env, depth + 1)
-- if true or d > 0 then
-- mem = ffi.new("memoryT")
-- --mem = {}
-- mem.h, mem.s, mem.l, mem.d = h,s,l,d
-- memo[xg] = memo[xg] or {}
-- memo[xg][yg] = mem
-- end
-- -- print(mem)
-- -- memo[xg][yg] = {h,s,l,d}
-- memo.count = memo.count + 1
-- else
-- -- h,s,l,d = unpack(hsld)
-- h,s,l,d = mem.h, mem.s, mem.l, mem.d
-- end
-- --print(h, s, l, d)
-- return h, s, l, d
-- end
---[[
local memos = {}
local ffi = require('ffi')
--local hash = ffi.new('int32_t[1]', 0)
function memoLookup(node, precision, x, y, env, depth, tracefun)
local h, s, l, d
local memo = memos[node]
if not memo then
memo = {}
memo.count = 0
memos[node] = memo
end
x = x - x % precision + precision / 2
y = y - y % precision + precision / 2
hash = x .. y
local stored = memo[hash]
--print(x, y, hash + 0.00, stored)
if not stored or random() > .95 then
h,s,l,d = tracefun(node[3], x, y, env, depth + 1)
stored = ffi.new('double[4]')
stored[0], stored[1], stored[2], stored[3] = h, s, l, d
memo[hash] = stored
memo.count = memo.count + 1
end
return stored[0], stored[1], stored[2], stored[3]
end
--]]
function memoReset(node)
memos[node] = nil
end
function R(exp, env, default) -- resolve
return exp or default
--[[
if type(exp) == 'number' then
return exp
elseif type(exp) == 'string' then
return env[exp] or default
elseif type(exp) == 'nil' then
return default
end
--]]
end
function module.trace(node, x, y, env, depth, tracefun) -- returns ray color
tracefun = tracefun or module.trace
if depth > 28 then return 0, 1, 1, 0 end
if type(node[1]) ~= 'string' then
error('node has no type?!', node)
return .9, 1, .5, 1 -- color errors with magenta color
elseif node[1] == 'disable' then
return 0, 0, 1, -1
elseif node[1] == 'edge' then
-- H S L distance from edge
return 0, 0, 1, -(R(node[2], env, 0) + y) * R(node[3], env, 1)
elseif node[1] == 'simplex' then
-- H S L distance from edge
return 0, 0, 1, .2 * R(node[3], env, 1) * (R(node[2], env, 0) + noise.Simplex2D(x, y))
elseif node[1] == 'position' then
local t = getTransform(node)
x,y = t:transformPoint(x, y)
local h,s,l,d = tracefun(node[3], x, y, env, depth + 1)
if traceDistance then
d = d * math.min(R(node[2][4], env, 1), R(node[2][5], env, 1))
end
return h,s,l,d
elseif node[1] == 'wrap' then
local ph = -atan2(x, y)
local r = sqrt(x^2 + y^2)
return tracefun(node[3], ph / pi, r^node[2] -1, env, depth + 1)
elseif node[1] == 'unwrap' then
local ph, r = x, y
x = (r + 1) * cos(-ph)
y = (r + 1) * sin(-ph)
return tracefun(node[2], x, y, env, depth + 1)
elseif node[1] == 'negate' then
local h,s,l,d = tracefun(node[2], x, y, env, depth + 1)
d = -d
return h,s,l,d
elseif node[1] == 'mirror' then
return tracefun(node[2], abs(x), y, env, depth + 1)
elseif node[1] == 'mod' then
local modX = node[3] or 1
local modY = node[4] or 1
x = ((x/2 + .5) % modX) * 2 - 1
y = ((y/2 + .5) % modY) * 2 - 1
--x = ((x + 1)/2 % 1) * abs(x)/x
return tracefun(node[2], x, y, env, depth + 1)
elseif node[1] == 'replicate' then
local h,s,l,d
local t = getTransform(node[3])
for i = 1, node[2] do
h,s,l,d = tracefun(node[4], x, y, env, depth + 1)
if d > 0 then break end
x,y = t:transformPoint(x, y)
end
return h,s,l,d
elseif node[1] == 'smooth' then
local h, s, l, a, b, e, d
local r = node[2]
h,s,l, a = tracefun(node[3], x, y, env, depth + 1)
h,s,l, b = tracefun(node[4], x, y, env, depth + 1)
if r > 0 then
e = max(r - abs(a - b), 0)
d = max(a, b) + e^2 * 0.25 / r
return h, s, l, d
else
e = max(-r - abs(-a - b), 0)
d = -max(-a, b) + e^2 * 0.25 / r
return h, s, l, d
end
elseif node[1] == 'combine' then
local h,s,l,d
local minA = huge -- huGEE!
for i=2, #node do
branch = node[i]
h,s,l,d = tracefun(branch, x, y, env, depth + 1)
minA = min(minA, abs(d))
if d > 0 then break end
end
d = minA * d / abs(d) -- minimal a, but with correct sign
return h,s,l,d
elseif node[1] == 'sum' then
local h, s, l, d, sumD
sumD = 0
for i=2, #node do
h,s,l,d = tracefun(node[i], x, y, env, depth + 1)
sumD = sumD + d
end
d = sumD
return h,s,l,d
elseif node[1] == 'clip' then
local h,s,l,d
local minD = huge -- huge! o_O
for i=2, #node do
branch = node[i]
h,s,l,d = tracefun(branch, x, y, env, depth + 1)
minD = min(minD, d)
end
d = minD
return h,s,l,d
elseif node[1] == 'noise' then
local h,s,l,d = tracefun(node[3], x, y, env, depth + 1)
h = h + R(node[2][1], env, h) * (random() - .5)
s = s + R(node[2][2], env, s) * (random() - .5)
l = l + R(node[2][3], env, l) * (random() - .5)
return h,s,l,d
elseif node[1] == 'tint' then
local h,s,l,d = tracefun(node[3], x, y, env, depth + 1)
local intensity = R(node[2][4], env, 1)
h = h * (1 - intensity) + R(node[2][1], env, h) * intensity
s = s * (1 - intensity) + R(node[2][2], env, s) * intensity
l = l * (1 - intensity) + R(node[2][3], env, l) * intensity
if node.react then
l = l + .2 * math.random() -- pixie dust
end
return h,s,l,d
elseif node[1] == 'memo' then
return memoLookup(node, R(node[2], env)/100, x, y, env, depth, tracefun)
elseif node[1] == 'interact' then
return tracefun(node[3], x, y, setmetatable(node[2], {__index=env}), depth + 1)
else
error('unrecognized type', node)
return .9, 1, .5, 1 -- color errors with magenta color
end
end
return module