Outlines became a necessary feature when I started to include NPC’s with similar skin tones into the game. The effect helped a lot with contrasting the player and other NPC’s.
This also afforded me the option of changing the color of the outline without having to edit every sprite as well as at runtime.
After completing the tutorial I then modified the shader to be an outline shader rather than an inline shader. (The tutorial version places the outline inside the existing sprite)
Logic
I don’t know a lot about the specifics of how this works, but essentially the shader is looking at the alpha value of adjacent pixels.
The shader then adds those alpha values up clamped between 0 and 1 and rounded up. This means that if ANY adjacent pixel has an alpha greater than zero, the value is 1.
After that, it subtracts the rounded up alpha value of the current pixel. If the new value is still 1, That pixel is part of the outline. This is because a solid pixel with solid neighbors will negate to 0 and not be highlighted while a transparent pixel with solid neighbors will remain at 1 and be highlighted. Any pixel with all transparent neighbors is also not part of the outline
Sprite Padding
The only problem I had left was that the player filled the 16x16 sprite and there were no pixels to sample below his feet and above his head. This was solved by adding a pixel of transparent padding to make the sprite 18x18.
Unedited Shader (Update Soon?)
Apologies for the highly unoptimized and uncommented code.