-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStar.lua
176 lines (121 loc) · 4.49 KB
/
Star.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
module("shadows.Star", package.seeall)
Shadows = require("shadows")
Light = require("shadows.Light")
Transform = require("shadows.Transform")
Star = setmetatable( {}, Light )
Star.__index = Star
Star.__type = "Star"
Star.Star = true
Star.Blur = true
halfPi = math.pi * 0.5
function Star:new(World, Radius)
-- Class constructor
if World and Radius then
local self = setmetatable({}, Star)
local Width, Height = World.Canvas:getDimensions()
self.Transform = Transform:new()
self.Transform:SetLocalPosition(0, 0, 1)
self.Transform.Object = self
self.Radius = Radius
self.Width = Width
self.Height = Height
self.Canvas = love.graphics.newCanvas( Width, Height )
self.ShadowCanvas = love.graphics.newCanvas( Width, Height )
self.Shapes = {}
World:AddStar(self)
return self
end
end
function Star:GetCanvasCenter()
local x, y, z = self.Transform:GetPosition()
local wx, wy, wz = self.World:GetPosition()
return ( x - wx ) * wz, ( y - wy ) * wz, z * wz
end
function Star:Update()
if self.Transform.HasChanged then
self.Transform.HasChanged = false
self.Changed = true
end
if self.Changed or self.World.Changed or self.World.UpdateStars then
local x, y, z = self.Transform:GetPosition()
local wx, wy, wz = self.World: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 are corrected
love.graphics.origin()
love.graphics.translate(-wx * wz, -wy * wz)
love.graphics.scale(wz, wz)
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()
--love.graphics.translate((x - wx - self.Radius) * wz, (y - wy - self.Radius) * wz)
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 * wz, self.Radius * wz)
else
-- Use a shader to generate the light
Shadows.LightShader:send("Radius", self.Radius * wz)
Shadows.LightShader:send("Center", { self:GetCanvasCenter() })
-- 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.rectangle("fill", 0, 0, self.Width, self.Height)
-- Unset the shader
love.graphics.setShader()
end
if self.Blur then
-- Generate a radial blur (to make the light softer)
love.graphics.origin()
love.graphics.setShader(Shadows.RadialBlurShader)
Shadows.RadialBlurShader:send("Size", { self.Canvas:getDimensions() })
Shadows.RadialBlurShader:send("Position", { self:GetCanvasCenter() })
Shadows.RadialBlurShader:send("Radius", self.Radius * wz)
end
-- Draw the shadow shapes over the canvas
love.graphics.setBlendMode("multiply", "premultiplied")
love.graphics.draw(self.ShadowCanvas, 0, 0)
-- Reset the blending mode
love.graphics.setBlendMode("alpha", "alphamultiply")
love.graphics.setShader()
-- Tell the world it needs to update it's canvas
self.Changed = nil
self.World.UpdateCanvas = true
end
end
function Star:Resize(Width, Height)
local w, h = self.Canvas:getDimensions()
if Width ~= w or Height ~= h then
self.Width = Width
self.Height = Height
self.Canvas = love.graphics.newCanvas(Width, Height)
self.ShadowCanvas = love.graphics.newCanvas(Width, Height)
self.Changed = true
end
end
function Star:Remove()
self.World.Stars[self.ID] = nil
self.World.Changed = true
self.Transform:SetParent(nil)
end
return Star