-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
196 lines (150 loc) · 4.77 KB
/
main.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
local band = assert(bit.band)
local cos = assert(math.cos)
local floor = assert(math.floor)
local huge = assert(math.huge)
local max = assert(math.max)
local min = assert(math.min)
local pi = assert(math.pi)
local sin = assert(math.sin)
local sqrt = assert(math.sqrt)
-- See: https://www.rorydriscoll.com/2009/01/07/better-sampling/
function cosineSampleHemisphere(u1, u2)
local r = sqrt(u1)
local theta = 2 * pi * u2
local x = r * cos(theta)
local y = r * sin(theta)
-- local z = sqrt(max(0, 1 - u1))
return x, y
end
-- See: https://tavianator.com/2011/ray_box.html
function rayCastBox(x, y, invDx, invDy, minX, minY, maxX, maxY)
local tx1 = (minX - x) * invDx
local tx2 = (maxX - x) * invDx
local minTx = min(tx1, tx2)
local maxTx = max(tx1, tx2)
local ty1 = (minY - y) * invDy
local ty2 = (maxY - y) * invDy
local minTy = min(ty1, ty2)
local maxTy = max(ty1, ty2)
local minT = max(minTx, minTy)
local maxT = min(maxTx, maxTy)
return maxT >= 0 and minT <= maxT and max(0, minT) or huge
end
function rayCast(x, y, dx, dy, neighborMask)
local invDx = 1 / dx
local invDy = 1 / dy
local distance = huge
if dx < 0 and dy < 0 and band(neighborMask, 1) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, -1, -1, 0, 0))
end
if dy < 0 and band(neighborMask, 2) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, 0, -1, 1, 0))
end
if dx > 0 and dy < 0 and band(neighborMask, 4) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, 1, -1, 2, 0))
end
if dx < 0 and band(neighborMask, 8) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, -1, 0, 0, 1))
end
if dx > 0 and band(neighborMask, 16) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, 1, 0, 2, 1))
end
if dx < 0 and dy > 0 and band(neighborMask, 32) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, -1, 1, 0, 2))
end
if dy > 0 and band(neighborMask, 64) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, 0, 1, 1, 2))
end
if dx > 0 and dy > 0 and band(neighborMask, 128) ~= 0 then
distance = min(distance, rayCastBox(x, y, invDx, invDy, 1, 1, 2, 2))
end
return distance
end
function saveScreenshot()
local gammaImageData = love.image.newImageData(imageSize, imageSize)
gammaImageData:mapPixel(function(x, y, r, g, b, a)
return love.math.linearToGamma(imageData:getPixel(x, y))
end)
local filename = "screenshot-" .. os.time() .. ".png"
gammaImageData:encode("png", filename)
print(
"Saved screenshot: "
.. love.filesystem.getSaveDirectory()
.. "/"
.. filename
.. ""
)
end
function love.load()
mapSize = 16
imageSize = mapSize * 16
globalPixel = 0
meanLightings = {}
sampleCounts = {}
imageData = love.image.newImageData(imageSize, imageSize, "rgba16f")
image = love.graphics.newImage(imageData)
image:setFilter("linear", "nearest")
neighborMask = 255
end
function love.update(dt)
for i = 1, 4096 do
local globalPixelX = globalPixel % imageSize
local globalPixelY = floor(globalPixel / imageSize)
local mapX = floor(globalPixelX / mapSize)
local mapY = floor(globalPixelY / mapSize)
local neighborMask = 16 * mapY + mapX
local pixelX = globalPixelX % mapSize
local pixelY = globalPixelY % mapSize
local x = (pixelX + 0.5) / mapSize
local y = (pixelY + 0.5) / mapSize
local meanLighting = meanLightings[globalPixel] or 0
local sampleCount = sampleCounts[globalPixel] or 0
for j = 1, 16 do
local u1 = love.math.random()
local u2 = love.math.random()
local dx, dy = cosineSampleHemisphere(u1, u2)
local distance = rayCast(x, y, dx, dy, neighborMask)
-- local lighting = min(distance, 1)
local lighting = distance <= 1 and 0 or 1
meanLighting = (meanLighting * sampleCount + lighting) / (sampleCount + 1)
sampleCount = sampleCount + 1
end
meanLightings[globalPixel] = meanLighting
sampleCounts[globalPixel] = sampleCount
imageData:setPixel(
globalPixelX,
globalPixelY,
meanLighting,
meanLighting,
meanLighting,
1
)
globalPixel = (globalPixel + 1) % (imageSize * imageSize)
end
image:replacePixels(imageData)
end
function love.draw()
local graphicsWidth, graphicsHeight = love.graphics.getDimensions()
local scale = graphicsHeight / imageSize
love.graphics.draw(
image,
0.5 * graphicsWidth,
0.5 * graphicsHeight,
0,
scale,
scale,
0.5 * imageSize,
0.5 * imageSize
)
love.graphics.print(love.timer.getFPS())
end
function love.keypressed(key, scancode, isrepeat)
if key == "escape" then
love.event.quit()
elseif key == "return" then
saveScreenshot()
end
end
function love.quit()
saveScreenshot()
end