In a 3D scene in Godot, I am attempting to create a pixel-perfect outline for a Spatial shader (applied after a pixelation effect to ensure the same resolution). To achieve this, I would like to modify pixels directly adjacent to the target mesh.
That said, I have a hunch that I simply cannot modify pixels outside of a mesh's area in screespace, and that I would have to use a separate donor mesh to achieve this effect. The issue with this is that I'm even more unsure of how to access an external mesh (I am fine applying the same pixel perfect effect to all meshes on-screen, but it would have to be pixel-perfect).
A secondary solution that I may have to settle with: do an inwards bleed for the outline, sacrificing the outermost pixels for the outline, which would be acceptable compromise.
You can render just the elements you want to a
Viewport
usingcull_mask
as I just described in another answer here.Now, you can take the texture from that
Viewport
use aViewportTexture
(make sure it is local to the scene and you are using it aNode
placed after theViewport
in the scene tree) and process it using a shader.I suggest you make the background of the
Viewport
transparent, so you can use the alpha channel to check if a pixel is rendered or not. The outline pixels will be those which are not rendered but are adjacent to a pixel that was rendered.This is the idea behind convolution edge detection. See Kernel (image processing).