- Home /
Creating an outline around the opaque pixels on a plane. Outline should extend outside of plane.
Short question:
Start with a 2 triangle rectangular mesh with an alpha tested texture on it. The mesh's corner UV coordinates will be somewhere inside the texture, not at the extremes. I'd like to draw an outline around only the pixels which are opaque. The outline would extend outside the mesh boundaries when it needs to, so that all of the pixels are bordered. If there is a shader-only solution, that would be brilliant, but I'm willing to create more planes behind the existing ones if necessary.
The view will always be top down, no moving camera, the plane will always be directly facing the camera. This is for mobile processors, so I'd like to keep the draw calls down, but I don't mind if the actual algorithm / setup takes a while as I can crunch in the background.
More information:
The plane mesh is part of a jigsaw puzzle piece. Each piece is made up of many rectangular meshes which were "merged" (placed in the same game object, not merged at the mesh level) to make a randomly shaped piece. The jigsaw puzzle texture will have a transparent background, and I drop any meshes which do not contain an opaque pixel. I need an outline around each piece (which is made up of many meshes), but I'm happy to draw it around each mesh, as this seems more viable. If the texture extends to the edge of one of the meshes, I still need to draw the outline around it.
I hope I've explained that well, please let me know if it's not understandable.
Things I've tried which failed:
Simple edge detect on the alpha of the texture. This does not have outlines around the mesh edges which are not at the edge of the image.
Scaling the image in many forms. Any time you scale, the pixels toward the top of the image all move upwards, so the leaves on the top of a tree don't have shadows on the underside. Also, obviously, outline width varies with x and y pos.
The way I think it might be possible but I'm not smart enough to do:
In the shader, on the first pass, draw the texture pixel with ZWrite On
On the second pass, with ZTest Greater, instead of painting one pixel, paint an n x n square, where n is the width of the outline. That's the part I'd like to know about!
I know there are a lot of other questions out there about outlines, but I think this one is a little different, given the transparency and the UVs inside the texture. I've also considered generating a separate texture atlas and applying each piece's texture to a single plane, which would be possible but annoying.
Thanks for reading!
Just a thought. If a solution can only draw an outline in world units, not texture pixels, that's also brilliant.
Your answer
![](https://koobas.hobune.stream/wayback/20220613113712im_/https://answers.unity.com/themes/thub/images/avi.jpg)