-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLight.lua
485 lines (298 loc) · 9.71 KB
/
Light.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
module("shadows.Light", package.seeall)
Object = require("shadows.Object")
Shadows = require("shadows")
Transform = require("shadows.Transform")
Light = setmetatable( {}, Object )
Light.__index = Light
Light.__type = "Light"
Light.Arc = 360
Light.Radius = 0
Light.SizeRadius = 10
Light.Blur = true
Light.R, Light.G, Light.B, Light.A = 255, 255, 255, 255
halfPi = math.pi * 0.5
function Light:new(World, Radius)
-- Class constructor
if World and Radius then
local self = setmetatable({}, Light)
self.Transform = Transform:new()
self.Transform:SetLocalPosition(0, 0, 1)
self.Transform.Object = self
self.Radius = Radius
World:AddLight(self)
return self
end
end
function Light:InitCanvas()
if not self.Canvas then
self.Canvas = love.graphics.newCanvas( self.Radius * 2, self.Radius * 2 )
end
if not self.ShadowCanvas then
self.ShadowCanvas = love.graphics.newCanvas( self.Radius * 2, self.Radius * 2 )
end
if not self.Shapes then
self.Shapes = {}
end
self.Changed = true
self.Moved = true
end
function Light:DestroyCanvas()
self.Canvas = nil
self.ShadowCanvas = nil
self.Shapes = nil
end
function Light:GetCanvasDestroyed()
return self.Canvas == nil
end
function Light:GenerateShadows(x, y, z, Layer)
local preLayerShapes = self.Shapes[Layer] or {}
local newLayerShapes = {}
local newZ
for Index = self.World.Bodies:GetLength(), 1, -1 do
local Body = self.World.Bodies:Get(Index)
-- If a body has been removed from the local reference, the light has moved, or the body has moved
local preBodyShapes = preLayerShapes[Body] or {}
local newBodyShapes = {}
newLayerShapes[Body] = newBodyShapes
local ShapesList = Body:GetShapes()
for i = ShapesList:GetLength(), 1, -1 do
local Shape = ShapesList:Get(i)
local SqrRadius = ( self.Radius + Shape:GetRadius() ) * ( self.Radius + Shape:GetRadius() ) --self.Radius * self.Radius + Shape:GetSqrRadius()
local ShapeX, ShapeY, ShapeZ = Shape:GetCentroid()
local dx, dy, dz = ShapeX - x, ShapeY - y, Layer
if ShapeZ <= Layer then
-- All the remaining bodies on the iteration will have a ShapeZ lower than 'Layer' so just return the shapes and the new z
return newLayerShapes, newZ
end
-- Is the light in the draw range?
if dx * dx + dy * dy + dz * dz <= SqrRadius then
if Shape:GetChanged() or Body:GetChanged() or self.Changed or not preBodyShapes[Shape] then
local ShadowList = {}
Shape:GenerateShadows(ShadowList, Body, 0, 0, dz, self)
if #ShadowList > 0 then
newBodyShapes[Shape] = ShadowList
end
else
newBodyShapes[Shape] = preBodyShapes[Shape]
end
if not newZ or ShapeZ < newZ then
newZ = ShapeZ
end
end
end
end
return newLayerShapes, newZ
end
function Light:GetCanvasCenter()
local x, y, z = self.Transform:GetPosition()
return self.Radius, self.Radius, z
end
function Light:GenerateDarkness(x, y, z)
local newShapes = {}
local MinAltitude = 0
local MinAltitudeLast
while MinAltitude ~= MinAltitudeLast and MinAltitude and MinAltitude < z do
local Layer = MinAltitude
-- Shadow shapes should subtract white color, so that you see black
love.graphics.setBlendMode("subtract", "alphamultiply")
love.graphics.setColor(1, 1, 1, 1)
-- Produce the shadow shapes
MinAltitudeLast = MinAltitude
Shapes, MinAltitude = self:GenerateShadows(x, y, z, Layer)
-- Draw the shadow shapes
for Body, BodyShapes in pairs(Shapes) do
for Shape, ShadowList in pairs(BodyShapes) do
for _, Shadow in pairs(ShadowList) do
Shadow:Draw(MinAltitude)
end
end
end
-- Draw the shapes over the shadow shapes, so that the shadow of a object doesn't cover another object
love.graphics.setBlendMode("add", "alphamultiply")
love.graphics.setColor(1, 1, 1, 1)
love.graphics.setShader()
for Index = self.World.Bodies:GetLength(), 1, -1 do
local Body = self.World.Bodies:Get(Index)
local Bx, By, Bz = Body:GetPosition()
if Bz <= Layer then
break
end
-- As long as this body is on top of the layer
Body:DrawRadius(x, y, z, self.Radius)
end
newShapes[Layer] = Shapes
end
self.Shapes = newShapes
end
function Light:GetUpdateVisibility()
-- World's position
local wx, wy, wz = self.World:GetPosition()
-- Light's position
local x, y, z = self:GetPosition()
-- Delta
local dx = ( wx + self.World:GetWidth() * 0.5 ) - x
local dy = ( wy + self.World:GetHeight() * 0.5 ) - y
local Distance = math.sqrt( dx * dx + dy * dy )
if self:GetCanvasDestroyed() then
-- If the distance is lower than the light's radius + world's border radius
if Distance <= ( self.World:GetBorderRadius() + self.Radius ) * wz then
-- Must be initialized
self:InitCanvas()
return true
end
else
-- If the distance is higher than the light's radius + world's border radius
if Distance > ( self.World:GetBorderRadius() + self.Radius ) * wz then
self:DestroyCanvas()
return false
end
end
return not self:GetCanvasDestroyed()
end
function Light:Update()
-- If the object shouldn't be visible
if not self:GetUpdateVisibility() then
-- Do nothing
return nil
end
if self.Transform.HasChanged then
self.Transform.HasChanged = false
self.Changed = true
end
if self.Changed or self.World.Changed then
local x, y, z = self.Transform:GetPosition()
-- Generate new content for the shadow canvas
love.graphics.setCanvas(self.ShadowCanvas)
love.graphics.setShader()
love.graphics.clear(255, 255, 255, 255)
-- Move all the objects so that their position local to the light are corrected
love.graphics.origin()
love.graphics.translate(self.Radius - x, self.Radius - y)
self:GenerateDarkness(x, y, z)
-- Draw custom shadows
love.graphics.setBlendMode("subtract", "alphamultiply")
love.graphics.setColor(1, 1, 1, 1)
self.World:DrawShadows(self)
-- Draw the sprites so that shadows don't cover them
love.graphics.setShader(Shadows.ShapeShader)
love.graphics.setBlendMode("add", "alphamultiply")
love.graphics.setColor(1, 1, 1, 1)
self.World:DrawSprites(self)
-- Now stop using the shadow canvas and generate the light
love.graphics.setCanvas(self.Canvas)
love.graphics.setShader()
love.graphics.clear()
love.graphics.origin()
if self.Image then
-- If there's a image to be used as light texture, use it
love.graphics.setBlendMode("lighten", "premultiplied")
love.graphics.setColor(self.R / 255, self.G / 255, self.B / 255, self.A / 255)
love.graphics.draw(self.Image, self.Radius, self.Radius)
else
-- Use a shader to generate the light
Shadows.LightShader:send("Radius", self.Radius)
Shadows.LightShader:send("Center", {self.Radius, self.Radius, z})
-- Calculate the rotation of the light
local Arc = math.rad(self.Arc * 0.5)
local Angle = self.Transform:GetRadians(-halfPi)
-- Set the light shader
love.graphics.setShader(Shadows.LightShader)
love.graphics.setBlendMode("alpha", "premultiplied")
-- Filling it with a arc is more efficient than with a rectangle for this case
love.graphics.setColor(self.R / 255, self.G / 255, self.B / 255, self.A / 255)
love.graphics.arc("fill", self.Radius, self.Radius, self.Radius, Angle - Arc, Angle + Arc)
-- Unset the shader
love.graphics.setShader()
end
if self.Blur then
-- Generate a radial blur (to make the light softer)
love.graphics.setShader(Shadows.RadialBlurShader)
Shadows.RadialBlurShader:send("Size", {self.Canvas:getDimensions()})
Shadows.RadialBlurShader:send("Position", {self.Radius, self.Radius})
Shadows.RadialBlurShader:send("Radius", self.Radius)
end
-- Now apply the blur along with the shadow shapes over the light canvas
love.graphics.setBlendMode("multiply", "premultiplied")
love.graphics.draw(self.ShadowCanvas, 0, 0)
-- Reset the blending mode
love.graphics.setBlendMode("alpha", "alphamultiply")
-- Tell the world it needs to update it's canvas
self.Changed = nil
self.World.UpdateCanvas = true
end
end
function Light:SetWorld(World)
self.World = World
end
function Light:GetWorld()
return self.World
end
function Light:SetAngle(Angle)
self.Transform:SetLocalRotation(Angle)
return self
end
function Light:GetAngle()
return self.Transform:GetRotation()
end
function Light:SetPosition(x, y, z)
self.Transform:SetLocalPosition(x, y, z)
return self
end
function Light:GetPosition()
return self.Transform:GetPosition()
end
function Light:SetColor(R, G, B, A)
if R ~= self.R then
self.R = R
self.Changed = true
end
if G ~= self.G then
self.G = G
self.Changed = true
end
if B ~= self.B then
self.B = B
self.Changed = true
end
if A ~= self.A then
self.A = A
self.Changed = true
end
return self
end
function Light:GetColor()
return self.R, self.G, self.B, self.A
end
function Light:SetImage(Image)
if Image ~= self.Image then
local Width, Height = Image:getDimensions()
self.Image = Image
self.Radius = math.sqrt( Width * Width + Height * Height ) * 0.5
self.Changed = true
end
end
function Light:GetImage()
return self.Image
end
function Light:SetRadius(Radius)
if Radius ~= self.Radius then
self.Radius = Radius
self.Changed = true
end
end
function Light:GetRadius()
return self.Radius
end
function Light:Remove()
if self.World then
self.World.Lights[self.ID] = nil
self.World.Changed = true
self.World = nil
self.ID = nil
self.Transform:SetParent(nil)
end
end
function Light:GetTransform()
return self.Transform
end
return Light