Skip to content

Commit b64f15a

Browse files
committed
Release 1.1: Works on HTML5 target now!
1 parent dd92e9a commit b64f15a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

scripts/outline_drawer/outline_drawer.gml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
See the DemoTank object in the Demo Project of the original repository at
1010
https://github.com/Grisgram/gml-outline-shader-drawer
1111
12-
************************************************************
13-
*** NOTE: THIS SHADER DOES NOT WORK ON THE HTML5 TARGET! ***
14-
************************************************************
15-
1612
(c)2022 Grisgram aka Haerion@GameMakerKitchen Discord
1713
Please respect the MIT License for this Library.
1814
*/
@@ -28,6 +24,10 @@ function outline_drawer(_viewport = 0, _outline_color = c_white, _outline_alpha
2824
__outline_surface_1 = -1;
2925
__outline_surface_2 = -1;
3026

27+
// html flips the final surface vertically... hell knows, why.
28+
// so, on html we need to draw it upside down.
29+
__draw_yscale = (os_browser != browser_not_a_browser) ? -1 : 1;
30+
3131
viewport = _viewport;
3232
camera = view_get_camera(_viewport);
3333

@@ -141,7 +141,10 @@ function outline_drawer(_viewport = 0, _outline_color = c_white, _outline_alpha
141141
surface_reset_target();
142142

143143
//Draw surface 2
144-
draw_surface(__outline_surface_2, _obj.bbox_left - outline_strength - 1, _obj.bbox_top - outline_strength - 1);
144+
draw_surface_ext(__outline_surface_2,
145+
_obj.bbox_left - outline_strength - 1,
146+
(__draw_yscale == 1 ? _obj.bbox_top - outline_strength - 1 : _obj.bbox_bottom + outline_strength + 2),
147+
1, __draw_yscale, 0, c_white, 1);
145148

146149
return true;
147150

shaders/shd_outline/shd_outline.fsh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
///
22
/// variable-pixel-width outline shader
33
/// Based on juju adams' selective outline and improved with alpha fading and variable thickness
4+
/// even on HTML5 target
45
///
56
/// (c)2022 Grisgram aka Haerion@GameMakerKitchen Discord
67
/// Please respect the MIT License for this Library.
@@ -9,6 +10,7 @@
910

1011
const float ALPHA_THRESHOLD = 1.0/255.0;
1112
const float BRIGHTNESS_THRESHOLD = 1.0;
13+
const float MAX_OUTLINE_STRENGTH = 10.0;
1214

1315
varying vec2 v_vTexcoord;
1416
varying vec4 v_vColour;
@@ -39,27 +41,46 @@ void main()
3941
float alphaVal = 0.0; // set by both for loops below (decides alpha fading)
4042
float edgeAlphaMax = 0.0;
4143

44+
// The "continue" and "breaks" in the loops below are for HTML/WEBGL compatibility
45+
// A WebGL shader must be compiled with a constant range in "for" loops.
46+
// So I defined a max-width of 10 for the outline but skip all values
47+
// below and above the desired range. A minor performance loss and a great comfort gain
48+
4249
if (u_vThickness.y == 1.0) { // Alpha fading = true
4350
float pxCount = u_vThickness.x * u_vThickness.x * 2.0;
44-
float pxHit = 0.0;
51+
float pxHit = 0.0;
4552
float curA = 0.0;
46-
for(float dX = -u_vThickness.x; dX <= u_vThickness.x; dX += 1.0)
53+
for(float dX = -MAX_OUTLINE_STRENGTH; dX <= MAX_OUTLINE_STRENGTH; dX += 1.0)
4754
{
48-
for(float dY = -u_vThickness.x; dY <= u_vThickness.x; dY += 1.0)
55+
if (dX < -u_vThickness.x) continue;
56+
57+
for(float dY = -MAX_OUTLINE_STRENGTH; dY <= MAX_OUTLINE_STRENGTH; dY += 1.0)
4958
{
59+
if (dY < -u_vThickness.x) continue;
60+
5061
curA = texture2D(u_sSpriteSurface, v_vSurfaceUV + vec2(dX, dY)*u_vTexel).a;
5162
if (curA >= ALPHA_THRESHOLD) pxHit++;
5263
edgeAlphaMax = max(edgeAlphaMax, curA);
64+
65+
if (dY > u_vThickness.x) break;
5366
}
67+
68+
if (dX > u_vThickness.x) break;
5469
}
5570
alphaVal = 0.25 + pxHit/pxCount;
5671
} else { // no alpha fading
57-
for(float dX = -u_vThickness.x; dX <= u_vThickness.x; dX += 1.0)
72+
for(float dX = -MAX_OUTLINE_STRENGTH; dX <= MAX_OUTLINE_STRENGTH; dX += 1.0)
5873
{
59-
for(float dY = -u_vThickness.x; dY <= u_vThickness.x; dY += 1.0)
74+
if (dX < -u_vThickness.x) continue;
75+
76+
for(float dY = -MAX_OUTLINE_STRENGTH; dY <= MAX_OUTLINE_STRENGTH; dY += 1.0)
6077
{
78+
if (dY < -u_vThickness.x) continue;
6179
edgeAlphaMax = max(edgeAlphaMax, texture2D(u_sSpriteSurface, v_vSurfaceUV + vec2(dX, dY)*u_vTexel).a);
80+
if (dY > u_vThickness.x) break;
6281
}
82+
83+
if (dX > u_vThickness.x) break;
6384
}
6485
alphaVal = edgeAlphaMax;
6586
}

0 commit comments

Comments
 (0)