-
Notifications
You must be signed in to change notification settings - Fork 0
/
raycast.lua
488 lines (403 loc) · 18.8 KB
/
raycast.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
local Object = require("lib.classic")
local vector = require("lib.hump.vector")
local wallShader = love.graphics.newShader("shaders/walls.glsl")
local floorShader = love.graphics.newShader("shaders/floor.glsl")
local ceillingShader = love.graphics.newShader("shaders/ceilling.glsl")
local spriteShader = love.graphics.newShader("shaders/sprite.glsl")
WallSide = {
North = 1,
East = 2,
South = 3,
West = 4
}
RayCastResult = Object:extend()
function RayCastResult:new()
self.collisionOccurred = false
self.collisionPoint = vector()
self.collisionSide = 0
self.rayLength = 0
self.totalChecks = 0
self.v = 0
end
function RayCastResult:reset()
self.collisionOccurred = false
self.collisionPoint.x = 0
self.collisionPoint.y = 0
self.collisionSide = 0
self.rayLength = 0
self.totalChecks = 0
self.v = 0
end
sin = math.sin
cos = math.cos
local raycast = {}
raycast.init = function(width, height, maxDepth, shadeDepth, drawCanvas, textureAtlas)
raycast.width = width
raycast.height = height
raycast.drawCanvas = drawCanvas
raycast.sprDepthBuffer = love.graphics.newCanvas(width, height, {type = "2d", format = "depth16", readable = true})
raycast.spriteMode = {raycast.drawCanvas, depthstencil = raycast.sprDepthBuffer}
raycast.justDepthBuffer = {depthstencil = raycast.sprDepthBuffer}
raycast.rayDir = vector()
--[[
data buffer is an image the width of the render canvas, with a height of 2. Wall render data is pushed here before being rendered
by the wall shader. A height of 2 allows up to 8 values per column (2*rgba) to be sent to the shader. More rows can be added to allow more data transfer
but the point of using a shader is to minimise the number of 'pixels' written to by the CPU, so it's best that this be kept to just what's
necessary.
pixel format rgba16f allows values to have a range of [-65504, +65504], which should be more than ample. See https://love2d.org/wiki/PixelFormat
data layout:
________________________________________________________________________
| | r | g | b | a |
|=====|===========|=============================|===========|===========|
| 0 | textureId | wallHeight | texture U | shade |
| 1 | rayLength | normalised ray length | not used | not used |
|_____|___________|_____________________________|___________|___________|
]]
if raycast.dataBuffer then raycast.dataBuffer:release() end
raycast.dataBuffer = love.image.newImageData(width, 2, "rgba16f")
raycast.dataBufferTexture = love.graphics.newImage(raycast.dataBuffer)
raycast.result = RayCastResult()
raycast.maxDepth = maxDepth
raycast.shadeDepth = shadeDepth
wallShader:send("dataBuffer", raycast.dataBufferTexture)
raycast.spriteProjectionMatrix = ortho(0, width, height, 0, 0, maxDepth)
spriteShader:send("spriteProjectionMatrix", raycast.spriteProjectionMatrix)
spriteShader:send("shadeDepth", raycast.shadeDepth)
ceillingShader:send("width", raycast.width)
ceillingShader:send("height", raycast.height/2)
ceillingShader:send("shadeDepth", raycast.shadeDepth)
floorShader:send("width", raycast.width)
floorShader:send("height", raycast.height/2)
floorShader:send("shadeDepth", raycast.shadeDepth)
raycast.spriteBatch = love.graphics.newSpriteBatch(textureAtlas, 10000)
local format = {
{"VertexDepth", "float", 1}
}
raycast.spriteDepths = love.graphics.newMesh(format, raycast.spriteBatch:getBufferSize()*4)
raycast.spriteBatch:attachAttribute("VertexDepth", raycast.spriteDepths)
raycast.stats = {
floorRenderTime = 0,
ceillingRenderTime = 0,
wallsRenderTime = 0,
spritesRenderTime = 0,
numChecks = 0
}
end
local rayUnitStepSize = vector()
local mapCheck = vector()
local rayLength = vector(0, 0)
local step = vector(0, 0)
local function rayCastDDA(rayStart, rayDir, map, result)
result:reset()
rayUnitStepSize.x = math.sqrt(1+(rayDir.y/rayDir.x)*(rayDir.y/rayDir.x))
rayUnitStepSize.y = math.sqrt(1+(rayDir.x/rayDir.y)*(rayDir.x/rayDir.y))
mapCheck.x = math.floor(rayStart.x)
mapCheck.y = math.floor(rayStart.y)
rayLength.x, rayLength.y = 0, 0
step.x, step.y = 0, 0
if rayDir.x < 0 then
step.x = -1
rayLength.x = (rayStart.x - mapCheck.x) * rayUnitStepSize.x
else
step.x = 1
rayLength.x = (mapCheck.x + 1 - rayStart.x) * rayUnitStepSize.x
end
if rayDir.y < 0 then
step.y = -1
rayLength.y = (rayStart.y - mapCheck.y) * rayUnitStepSize.y
else
step.y = 1
rayLength.y = (mapCheck.y + 1 - rayStart.y) * rayUnitStepSize.y
end
local tileFound = false
local maxDistance = raycast.maxDepth
local distance = 0
local side = -1
local numChecks = 0
result.dx = rayDir.x
result.dy = rayDir.y
-- raycast.visibleTiles[(mapCheck.y * 256) + mapCheck.x +1] = true
while distance <= maxDistance do
numChecks = numChecks + 1
if rayLength.x < rayLength.y then
mapCheck.x = mapCheck.x + step.x
distance = rayLength.x
rayLength.x = rayLength.x + rayUnitStepSize.x
side = 1
else
mapCheck.y = mapCheck.y + step.y
distance = rayLength.y
rayLength.y = rayLength.y + rayUnitStepSize.y
side = 0
end
local wall = map:getWallTileAt(mapCheck.x, mapCheck.y)
if wall then
local i = (mapCheck.y * map.width) + mapCheck.x
local id = wall.type
if wall.type > 0 then
result.tileId = wall.tileId
result.x = mapCheck.x
result.y = mapCheck.y
result.rayLength = distance
result.totalChecks = numChecks
result.side = side
result.collisionOccurred = true
result.collisionPoint.x = rayStart.x + (rayDir.x * distance)
result.collisionPoint.y = rayStart.y + (rayDir.y * distance)
if side == 1 then
if rayDir.x >= 0 then
result.collisionSide = WallSide.West
result.u = math.remainder(result.collisionPoint.y)
elseif rayDir.x <= 0 then
result.collisionSide = WallSide.East
result.u = 1- math.remainder(result.collisionPoint.y)
end
end
if side == 0 then
if rayDir.y >= 0 then
result.collisionSide = WallSide.North
result.u = 1- math.remainder(result.collisionPoint.x)
elseif rayDir.y <= 0 then
result.collisionSide = WallSide.South
result.u = math.remainder(result.collisionPoint.x)
end
end
if wall.type == 2 then -- horizontal thin, animatable wall
--[[
- determine x-axis gradient of ray direction
- extend the ray by the amount required to reach the centre of the tile on the y-axis
- recalculate collision point
- if the extended ray is still within the horizontal bounds of this tile then collision occurred
-- horizontal bounds can be truncated, this allows for animatable objects like doors
--]]
local m = rayDir.x / rayDir.y
local dy = 0.5
local dx = 0
local doCheck = true
if side == 1 then
--[[
if the ray entered the tile on the y-axis we need to readjust the y-delta to reach the mid-point of the tile
We just remove the collision point's fractional y value from the mid-point
--]]
if rayDir.y < 0 then
doCheck = math.remainder(result.collisionPoint.y) > 0.5
dy = (1-dy) - math.remainder(result.collisionPoint.y)
elseif rayDir.y > 0 then
doCheck = math.remainder(result.collisionPoint.y) < 0.5
dy = (dy) - math.remainder(result.collisionPoint.y)
end
end
if doCheck then
dx = m * dy
local tmpDistance = distance + math.len(dx,dy)
local colX = rayStart.x + (rayDir.x * tmpDistance)
local colY = rayStart.y + (rayDir.y * tmpDistance)
local offset=wall.offset or 0
if colX >= mapCheck.x+offset and colX < mapCheck.x+1 then
distance = tmpDistance
result.rayLength = tmpDistance
result.collisionPoint.x = colX
result.collisionPoint.y = colY
result.side = 0
result.u = math.remainder(colX) - offset
return true
end
end
elseif wall.type == 3 then -- vertical thin, animatable wall
--[[
- determine y-axis gradient of ray direction
- extend the ray by the amount required to reach the centre of the tile on the x-axis
- recalculate collision point
- if the extended ray is still within the vertical bounds of this tile then collision occurred
-- vertical bounds can be truncated, this allows for animatable objects like doors
--]]
local m = rayDir.y / rayDir.x
local dx =0.5
local dy = 0
local doCheck = true
if side == 0 then
--[[
if the ray entered the tile on the x-axis we need to readjust the x-delta to reach the mid-point of the tile
We just remove the collision point's fractional x value from the mid-point
--]]
if rayDir.x <= 0 then
doCheck = math.remainder(result.collisionPoint.x) > 0.5
dx = (1-dx) - math.remainder(result.collisionPoint.x)
elseif rayDir.x >= 0 then
doCheck = math.remainder(result.collisionPoint.x) < 0.5
dx = (dx) - math.remainder(result.collisionPoint.x)
end
end
if doCheck then
dy = m * dx
local tmpDistance = distance + math.len(dx,dy)
local colX = rayStart.x + (rayDir.x * tmpDistance)
local colY = rayStart.y + (rayDir.y * tmpDistance)
local offset=wall.offset or 0
if colY >= mapCheck.y+offset and colY < mapCheck.y+1 then
result.rayLength = tmpDistance
result.collisionPoint.x = colX
result.collisionPoint.y = colY
result.side = 1
result.u = math.remainder(colY) - offset
return true
end
end
else -- just a normal wall
return true
end
else
-- raycast.visibleTiles[i+1] = true
end
end
end
result.totalChecks = numChecks
return false
end
local function renderWalls(camera, map)
local position = camera.position
local headOffset = camera.height
local angle = camera.angle
local fov = camera.fov
-- raycast.visibleTiles = {}
local totalChecks = 0
local angleStep = fov / raycast.width
local startAngle = angle - (fov/2)
local rayDir = raycast.rayDir
local start = love.timer.getTime()
local result = raycast.result
-- collect wall data
for x = 0,raycast.width-1 do
local rayAngle = startAngle + (x * angleStep)
rayDir.x = cos(rayAngle)
rayDir.y = sin(rayAngle)
-- cast ray into the scene for this screen column
if rayCastDDA(position, rayDir, map, result) then
-- collect data pertinent to render step and push into a texture to be read by the wall shader
local correctedRayLength = result.rayLength * cos(rayAngle - angle)
local wallHeight = (1*raycast.width)/correctedRayLength
local shade = (1 - (0.5 * result.side)) * (1 - (correctedRayLength/raycast.shadeDepth))
raycast.dataBuffer:setPixel(x, 0, result.tileId or 0, wallHeight, result.u, shade)
raycast.dataBuffer:setPixel(x, 1, correctedRayLength,correctedRayLength * (1/raycast.maxDepth),result.dx,result.dy)
else
-- ray reached max length without hitting anything
raycast.dataBuffer:setPixel(x, 0, 0, 0, 0, 0)
raycast.dataBuffer:setPixel(x, 1, raycast.maxDepth, 1,result.dx,result.dy)
end
totalChecks = totalChecks + result.totalChecks
end
local raycastTime = (love.timer.getTime() - start)
start = love.timer.getTime()
raycast.dataBufferTexture:replacePixels(raycast.dataBuffer)
-- do render
wallShader:send("cameraOffset", headOffset)
wallShader:send("cameraTilt", camera.tilt)
wallShader:send("textures", map.texturePack)
love.graphics.setCanvas({raycast.drawCanvas, depthstencil = raycast.sprDepthBuffer})
love.graphics.setDepthMode("always", true)
love.graphics.setShader(wallShader)
love.graphics.rectangle("fill", 0, 0, raycast.width, raycast.height)
love.graphics.setShader()
local renderTime = (love.timer.getTime() - start)
local stats = raycast.stats
stats.raycastTime = raycastTime
stats.wallsRenderTime = renderTime
stats.numChecks = totalChecks
end
local function drawTexturedPlane(shader, top, h, camera, textures, map, dimensions)
local position = camera.position
local headOffset = camera.height
local angle = camera.angle
local fov = camera.fov
love.graphics.setShader(shader)
shader:send("position", camera.positionArray)
shader:send("textures", textures)
shader:send("fov", fov)
shader:send("angle", angle)
shader:send("cameraTilt", camera.tilt)
shader:send("cameraOffset", headOffset)
shader:send("map", map)
shader:send("mapDimensions", dimensions)
love.graphics.rectangle("fill",0,top,raycast.width,h)
end
local function renderCeillingAndFloor(camera, map)
local stats = raycast.stats
love.graphics.setCanvas(raycast.drawCanvas)
local start = love.timer.getTime()
drawTexturedPlane(ceillingShader, 0, (raycast.height/2)+camera.tilt, camera, map.texturePack, map.ceillingsTexture, map.dimensions)
stats.ceillingRenderTime = love.timer.getTime() - start
start = love.timer.getTime()
local top = (raycast.height/2)+camera.tilt
local h = raycast.height - top
drawTexturedPlane(floorShader, top, h, camera, map.texturePack, map.floorsTexture, map.dimensions)
stats.floorRenderTime = love.timer.getTime() - start
end
local toSpr = vector(0,0)
local eyeDir = vector(0,0)
local function renderSprites(camera, sprites)
local position = camera.position
local headOffset = camera.height
local angle = camera.angle
local fov = camera.fov
love.graphics.setCanvas(raycast.spriteMode)
love.graphics.setDepthMode("lequal", true)
raycast.spriteBatch:clear()
love.graphics.setShader(spriteShader)
local eyeX = math.cos(angle)
local eyeY = math.sin(angle)
local start = love.timer.getTime()
for i=1,#sprites do
local spr = sprs[i]
local sprX = spr.position.x - position.x
local sprY = spr.position.y - position.y
local dst = math.sqrt(sprX*sprX + sprY*sprY)
local objAngle = math.atan2(sprY, sprX)- math.atan2(eyeY, eyeX)
if objAngle < -math.pi then
objAngle = objAngle + 2* math.pi
end
if objAngle > math.pi then
objAngle = objAngle - 2* math.pi
end
toSpr.x, toSpr.y = sprX, sprY
eyeDir.x, eyeDir.y = eyeX, eyeY
local visible = (toSpr * eyeDir) >= 0.5
if visible then
local texture = spr.texture
local perpDistance = dst * math.cos( objAngle )
local fullHeight = raycast.width / perpDistance
local objHeight = (math.min(1,texture:getHeight()/32)* raycast.width) / perpDistance
local floor = (raycast.height/2) + (fullHeight*0.5) + (headOffset/perpDistance) + camera.tilt
local ceilling = floor - objHeight
local aspectRatio = texture:getHeight() / texture:getWidth()
local objWidth = objHeight / aspectRatio
local objMiddle = ((0.5 * (objAngle/(fov/2)))+0.5) * raycast.width
local startY = ceilling
local startX = math.floor(objMiddle - (objWidth/2))
local id = raycast.spriteBatch:add(startX, startY, 0,objWidth / texture:getWidth(), objHeight/ texture:getHeight())
local dep = 1+ 4 * ( id - 1 )
raycast.spriteDepths:setVertex(dep, perpDistance)
raycast.spriteDepths:setVertex(dep+1, perpDistance)
raycast.spriteDepths:setVertex(dep+2, perpDistance)
raycast.spriteDepths:setVertex(dep+3, perpDistance)
if raycast.spriteBatch:getCount() == raycast.spriteBatch:getBufferSize() then
love.graphics.draw(raycast.spriteBatch)
raycast.spriteBatch:clear()
end
end
end
love.graphics.draw(raycast.spriteBatch)
raycast.stats.spritesRenderTime = love.timer.getTime() - start
end
raycast.rayCastDDA = rayCastDDA
raycast.renderScene = function(camera, map, sprites)
-- clear all raycast specific buffers, but not the draw buffer as it doesn't belong to us
love.graphics.setCanvas(raycast.justDepthBuffer)
love.graphics.clear()
love.graphics.reset()
renderCeillingAndFloor(camera, map)
renderWalls(camera, map)
renderSprites(camera, sprites)
local stats = raycast.stats
raycast.stats.renderTime = stats.ceillingRenderTime + stats.floorRenderTime + stats.wallsRenderTime + stats.spritesRenderTime
end
return raycast