-
Notifications
You must be signed in to change notification settings - Fork 2
/
cl_melons_masks.lua
368 lines (325 loc) · 10.7 KB
/
cl_melons_masks.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
---
--- Melon's Masks
--- https://github.com/melonstuff/melonsmasks/
--- Licensed under MIT
---
----
---@module
---@name masks
---@realm CLIENT
----
---- An alternative to stencils that samples a texture
---- For reference:
---- The destination is what is being masked, so a multi stage gradient or some other complex stuff
---- The source is the text, or the thing with alpha
----
local masks = {}
--- youraddon.masks = masks
masks.source = {}
masks.dest = {}
masks.source.rt = GetRenderTargetEx("MelonMasks_Source", ScrW(), ScrH(), RT_SIZE_NO_CHANGE, MATERIAL_RT_DEPTH_SEPARATE, bit.bor(1, 256), 0, IMAGE_FORMAT_BGRA8888)
masks.dest.rt = GetRenderTargetEx("MelonMasks_Destination", ScrW(), ScrH(), RT_SIZE_NO_CHANGE, MATERIAL_RT_DEPTH_SEPARATE, bit.bor(1, 256), 0, IMAGE_FORMAT_BGRA8888)
masks.source.mat = CreateMaterial("MelonMasks_Source", "UnlitGeneric", {
["$basetexture"] = masks.source.rt:GetName(),
["$translucent"] = "1",
["$vertexalpha"] = "1",
["$vertexcolor"] = "1",
})
masks.dest.mat = CreateMaterial("MelonMasks_Destination", "UnlitGeneric", {
["$basetexture"] = masks.dest.rt:GetName(),
["$translucent"] = "1",
["$vertexalpha"] = "1",
["$vertexcolor"] = "1",
})
----
---@enumeration
---@name masks.KIND
----
---@enum (CUT) Cuts the source out of the destination
---@enum (STAMP) Cuts the destination out of the source
----
---- Determines the type of mask were rendering
----
masks.KIND_CUT = {BLEND_ZERO, BLEND_SRC_ALPHA, BLENDFUNC_ADD}
masks.KIND_STAMP = {BLEND_ZERO, BLEND_ONE_MINUS_SRC_ALPHA, BLENDFUNC_ADD}
----
---@name masks.Start
----
----
---- Starts the mask destination render
---- Whats between this and the `masks.Source` call is the destination
---- See the module declaration for an explaination
----
function masks.Start()
render.PushRenderTarget(masks.dest.rt)
render.Clear(0, 0, 0, 0, true, true)
cam.Start2D()
end
----
---@name masks.Source
----
---- Stops the destination render
---- Whats between this and the `masks.End` call is the source
---- See the module declaration for an explaination
----
function masks.Source()
cam.End2D()
render.PopRenderTarget()
render.PushRenderTarget(masks.source.rt)
render.Clear(0, 0, 0, 0, true, true)
cam.Start2D()
end
----
---@name masks.And
----
---@arg (kind: masks.KIND_) The kind of mask this is, remember this is not a number enum
----
---- Renders the given kind of mask and continues the mask render
---- This can be used to layer masks
---- This must be called post [masks.Source]
---- You still need to call End
----
function masks.And(kind)
cam.End2D()
render.PopRenderTarget()
render.PushRenderTarget(masks.dest.rt)
cam.Start2D()
render.OverrideBlend(true,
kind[1], kind[2], kind[3]
)
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(masks.source.mat)
surface.DrawTexturedRect(0, 0, ScrW(), ScrH())
render.OverrideBlend(false)
masks.Source()
end
----
---@name masks.End
----
---@arg (kind: masks.KIND_) The kind of mask this is, remember this is not a number enum
---@arg (x: number) The x coordinate to render the rectangle at, defaults to 0
---@arg (y: number) The y coordinate to render the rectangle at, defaults to 0
---@arg (w: number) The width of the rectangle to render
---@arg (h: number) The height of the rectangle to render
----
---- Stops the source render and renders everything finally
---- See the module declaration for an explaination
----
function masks.End(kind, x, y, w, h)
kind = kind or masks.KIND_CUT
cam.End2D()
render.PopRenderTarget()
render.PushRenderTarget(masks.dest.rt)
cam.Start2D()
render.OverrideBlend(true,
kind[1], kind[2], kind[3]
)
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(masks.source.mat)
surface.DrawTexturedRect(0, 0, ScrW(), ScrH())
render.OverrideBlend(false)
cam.End2D()
render.PopRenderTarget()
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(masks.dest.mat)
surface.DrawTexturedRect(x or 0, y or 0, w or ScrW(), h or ScrH())
end
----
---@name masks.EndToTexture
----
---@arg (tex: ITexture)
---@arg (kind: masks.KIND_) The kind of mask this is, remember this is not a number enum
----
---- Stops the source render and renders everything to the given ITexture
----
function masks.EndToTexture(texture, kind)
kind = kind or masks.KIND_CUT
cam.End2D()
render.PopRenderTarget()
render.PushRenderTarget(masks.dest.rt)
cam.Start2D()
render.OverrideBlend(true,
kind[1], kind[2], kind[3]
)
surface.SetDrawColor(255, 255, 255)
surface.SetMaterial(masks.source.mat)
surface.DrawTexturedRect(0, 0, ScrW(), ScrH())
render.OverrideBlend(false)
cam.End2D()
render.PopRenderTarget()
if IsValid(texture) then
render.CopyTexture(masks.dest.rt, texture)
end
end
---
--- Examples
--- These depend on melonlib being installed
--- Although you can copy from them just fine
---
if not melon then return masks end
---
--- Basic example
--- This shows how to render a gradient rounded box
---
melon.DebugHook(false, "HUDPaint", function()
---
--- First, we start the mask
---
masks.Start()
---
--- Now, we draw whatever we want to be cutting
--- This can be a gradient, an image, anything you want
--- For this example we'll use a blue rectangle with a red gradient coming from the top right
---
surface.SetDrawColor(255, 0, 0)
surface.DrawRect(50, 50, 150, 150)
surface.SetDrawColor(0, 0, 255)
surface.SetMaterial(melon.Material("vgui/gradient-r"))
surface.DrawTexturedRectRotated(50 + (150 / 2), 50 + (150 / 2), 300, 300, 45)
---
--- Next, we start what we call the "Source"
--- This is what is doing the cutting
--- For example, it could be text, a rounded box, anything you want
--- For this example it will be a rounded box
---
masks.Source()
draw.RoundedBox(20, 50, 50, 150, 150, color_white)
---
--- Finally we end the mask render, and you should see your rounded box!
---
masks.End()
if __render_melons_masks_ then
__render_melons_masks_("gradround")
end
end )
----
---- Gradient Text Example
---- This shows how to render text that is a gradient
----
melon.DebugHook(false, "HUDPaint", function()
---
--- First, we start the mask and do what we did before
--- which is draw a red/blue gradient
---
masks.Start()
surface.SetDrawColor(255, 0, 0)
surface.DrawRect(50, 50, 150, 150)
surface.SetDrawColor(0, 0, 255)
surface.SetMaterial(melon.Material("vgui/gradient-r"))
surface.DrawTexturedRectRotated(50 + (150 / 2), 50 + (150 / 2), 300, 300, 45)
masks.Source()
---
--- Now we draw the text and end
--- Notice how this is exactly the same as the example above
--- These two examples operate on exactly the same principles
---
draw.Text({
text = "Some Text",
pos = {50 + (150 / 2), 50 + (150 / 2)},
xalign = 1,
yalign = 1,
font = melon.Font(50, "Poppins"),
})
masks.End()
if __render_melons_masks_ then
__render_melons_masks_("gradtext")
end
end )
----
---- Gradient Rounded Box Border
---- This shows how to render the border of a rounded box being a gradient
----
melon.DebugHook(false, "HUDPaint", function()
---
--- First, we start the mask and do what we did before
---
masks.Start()
surface.SetDrawColor(255, 0, 0)
surface.DrawRect(50, 50, 150, 150)
surface.SetDrawColor(0, 0, 255)
surface.SetMaterial(melon.Material("vgui/gradient-r"))
surface.DrawTexturedRectRotated(50 + (150 / 2), 50 + (150 / 2), 300, 300, 45)
masks.Source()
---
--- Now we draw the rounded box like we did before
--- This cuts a rounded box out of the gradient
---
draw.RoundedBox(20, 50, 50, 150, 150, color_white)
---
--- Next, we do something new!
--- We use the `And` function
--- This allows you to overlay masks
---
--- Youll also notice the argument given
--- This is an enum that determines which kind of mask is being used
---
--- The default is `masks.KIND_CUT` which cuts the source out of the mask
--- That is what were doing here, were cutting the rounded box drawn above
--- out of the gradient mask
---
masks.And(masks.KIND_CUT)
---
--- Next we draw the inner box to cut
---
draw.RoundedBox(20 - 5, 50 + 10, 50 + 10, 150 - 20, 150 - 20, color_white)
---
--- Now, we end the mask render
--- But we give the argument `masks.KIND_STAMP` to the End function
---
--- This is the opposite of `masks.KIND_CUT`
--- This "stamps" a shape out of the mask
---
masks.End(masks.KIND_STAMP)
---
--- As a rundown, this is the order of operations
---
--- 1. Start the mask
--- 2. Render the gradient
--- 3. Start the "Source"
--- 4. Render the larger, border rounded box
--- 5. Cut it out of the gradient mask with `And`, and continue the render
--- 6. Render the smaller, inner rounded box
--- 7. "Stamp" the smaller box out of the box drawn in step 5
--- 8. Finish the render
---
--- If youre confused, just play around with it
---
if __render_melons_masks_ then
__render_melons_masks_("gradborder")
end
end )
----
---- Transparent masks
---- This shows that transparency is translated into the mask render
----
melon.DebugHook(false, "HUDPaint", function()
---
--- First, we start the mask and render the red/blue gradient, like all the other examples
---
masks.Start()
surface.SetDrawColor(255, 0, 0)
surface.DrawRect(50, 50, 150, 150)
surface.SetDrawColor(0, 0, 255)
surface.SetMaterial(melon.Material("vgui/gradient-r"))
surface.DrawTexturedRectRotated(50 + (150 / 2), 50 + (150 / 2), 300, 300, 45)
masks.Source()
---
--- Now we draw the rounded box like we did before
--- But we use a color with an Alpha on it
--- Note that colors need to be cached
--- Best practice is using `surface.SetAlphaMultiplier`, but this is an example
---
draw.RoundedBox(20, 50, 50, 150, 150, Color(255, 255, 255, 100))
draw.RoundedBox(20, 50 + 20, 50 + 20, 150 - 40, 150 - 40, Color(255, 255, 255, 100))
---
--- And finish the mask
---
masks.End()
if __render_melons_masks_ then
__render_melons_masks_("transparent")
end
end )
---
--- End of examples
---
return masks