-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutliner.lua2p
235 lines (193 loc) · 6.85 KB
/
outliner.lua2p
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
--[==========================================================================[--
Outliner shader
Based on code by Micha (Germanunkol)
Modified by Brandon Blanker Lim-it @flamendless for GH:R
(C) Copyright 2018 Pedro Gimeno Fortea
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Usage:
local outline_only = false -- draw the outline AND the image in the same pass
--local outline_only = true -- draw the outline only
-- 'require' returns a constructor that requires the outline_only parameter
local outliner = require 'outliner'(outline_only)
-- sets the outline colour (in 0..1 range)
outliner:outline(r, g, b)
-- draws an outline or an image with outline (decided at construction time),
-- possibly using a quad. Same parameters as love.graphics.draw except for the
-- size prefix, which is the size of the outline.
outliner:draw(size, img, ...)
--]==========================================================================]--
local aux = {0, 0, 0, 0}
!if _GLSL_NORMALS then
local lgdraw = love.graphics.drawLayer
!else
local lgdraw = love.graphics.draw
!end
local lgsetShader = love.graphics.setShader
local function outline(self, r, g, b)
aux[1] = r
aux[2] = g
aux[3] = b
aux[4] = nil
self.shader:send("outline", aux)
end
local function draw(self, size, img, ...)
local quad = (...)
if type(quad) == "userdata" then
-- assumed quad
local sx, sy = quad:getTextureDimensions()
aux[1], aux[2], aux[3], aux[4] = quad:getViewport()
aux[1] = aux[1] / sx
aux[2] = aux[2] / sy
aux[3] = aux[3] / sx
aux[4] = aux[4] / sy
self.shader:send("quad", aux)
aux[1] = size / sx
aux[2] = size / sy
else
local sx, sy = img:getDimensions()
aux[1], aux[2], aux[3], aux[4] = 0, 0, 1, 1
self.shader:send("quad", aux)
aux[1] = size / sx
aux[2] = size / sy
end
aux[3], aux[4] = nil, nil
self.shader:send("stepSize", aux)
lgsetShader(self.shader)
!if _GLSL_NORMALS then
lgdraw(img, 1, ...)
!else
lgdraw(img, ...)
!end
lgsetShader()
end
!if _GLSL_NORMALS then
local function new_outliner(outline_only)
local shader = [[
// This parameter affects the roundness. 0.75 is close to the Euclidean
// correct value. If it's 0.0, the shape of the "brush" making the outline
// will be a diamond; if it's 1.0, it will be a square.
const float t = 0.0;
extern vec3 outline; // Outline R,G,B
extern vec2 stepSize; // Distance parameter
extern vec4 quad;
const vec4 zero = vec4(0.,0.,0.,0.);
vec2 q1 = quad.xy;
vec2 q2 = quad.xy + quad.zw;
uniform ArrayImage MainTex;
void effect()
{
// get color of pixels:
vec2 tc = VaryingTexCoord.xy;
float alpha = -20.0 * Texel(MainTex, vec3(clamp(tc, q1, q2), VaryingTexCoord.z)).a;
vec2 aux = vec2(stepSize.x, 0.);
alpha += Texel(MainTex, vec3(clamp(tc + aux, q1, q2), VaryingTexCoord.z)).a;
alpha += Texel(MainTex, vec3(clamp(tc - aux, q1, q2), VaryingTexCoord.z)).a;
aux = vec2(0., stepSize.y);
alpha += Texel(MainTex, vec3(clamp(tc + aux, q1, q2), VaryingTexCoord.z)).a;
alpha += Texel(MainTex, vec3(clamp(tc - aux, q1, q2), VaryingTexCoord.z)).a;
if (t != 0.0)
{
aux = stepSize;
alpha += t * Texel(MainTex, vec3(clamp(tc + aux, q1, q2), VaryingTexCoord.z)).a;
alpha += t * Texel(MainTex, vec3(clamp(tc - aux, q1, q2), VaryingTexCoord.z)).a;
aux = vec2(-stepSize.x, stepSize.y);
alpha += t * Texel(MainTex, vec3(clamp(tc + aux, q1, q2), VaryingTexCoord.z)).a;
alpha += t * Texel(MainTex, vec3(clamp(tc - aux, q1, q2), VaryingTexCoord.z)).a;
}
@calc_result@
love_PixelColor = result;
}
]]
if outline_only then
shader = shader:gsub("@calc_result@", [[
vec4 result = vec4(outline, alpha);
]])
else
shader = shader:gsub("@calc_result@", [[
vec4 result =
max(max(sign(alpha), 0.) * vec4( outline, alpha ), zero)
- min(min(sign(alpha), 0.) * Texel(MainTex, tc), zero);
]])
end
shader = love.graphics.newShader(shader)
local result = {
shader = shader;
draw = draw;
outline = outline;
}
result:outline(0, 1, 0) -- define some starting value
return result
end
!else
local function new_outliner(outline_only)
local shader = [[
// This parameter affects the roundness. 0.75 is close to the Euclidean
// correct value. If it's 0.0, the shape of the "brush" making the outline
// will be a diamond; if it's 1.0, it will be a square.
const float t = 0.0;
extern vec3 outline; // Outline R,G,B
extern vec2 stepSize; // Distance parameter
extern vec4 quad;
const vec4 zero = vec4(0.,0.,0.,0.);
vec2 q1 = quad.xy;
vec2 q2 = quad.xy + quad.zw;
vec4 effect( vec4 col, Image texture, vec2 texturePos, vec2 screenPos )
{
// get color of pixels:
float alpha = -20.0 * texture2D(texture, clamp(texturePos, q1, q2)).a;
vec2 aux = vec2(stepSize.x, 0.);
alpha += texture2D(texture, clamp(texturePos + aux, q1, q2) ).a;
alpha += texture2D(texture, clamp(texturePos - aux, q1, q2) ).a;
aux = vec2(0., stepSize.y);
alpha += texture2D(texture, clamp(texturePos + aux, q1, q2) ).a;
alpha += texture2D(texture, clamp(texturePos - aux, q1, q2) ).a;
if (t != 0.0)
{
aux = stepSize;
alpha += t * texture2D(texture, clamp(texturePos + aux, q1, q2)).a;
alpha += t * texture2D(texture, clamp(texturePos - aux, q1, q2)).a;
aux = vec2(-stepSize.x, stepSize.y);
alpha += t * texture2D(texture, clamp(texturePos + aux, q1, q2)).a;
alpha += t * texture2D(texture, clamp(texturePos - aux, q1, q2)).a;
}
@calc_result@
return result;
}
]]
if outline_only then
shader = shader:gsub("@calc_result@", [[
vec4 result = vec4(outline, alpha);
]])
else
shader = shader:gsub("@calc_result@", [[
vec4 result =
max(max(sign(alpha), 0.) * vec4( outline, alpha ), zero)
- min(min(sign(alpha), 0.) * texture2D(texture, texturePos), zero);
]])
end
shader = love.graphics.newShader(shader)
local result = {
shader = shader;
draw = draw;
outline = outline;
}
result:outline(0, 1, 0) -- define some starting value
return result
end
!end
return new_outliner