Selection Outline Shaders

Hi!

Im trying to do a (in theory) very simple thing.
I want to highlight/outline object in the scene (edit mode) in a similar way as Unity does when objects are selected.
I have found many attempts online trying to mimic this outlining but I cannot find get the result I am looking for.

I found this (very old) post where a Unity dev posts the code they seemingly used for this outlining, but I am too inexperienced to be able to get this to work.
Selection outline ?_ga=2.201353496.886022561.1617006626-1137731682.1592809934#post-2776318

So, short story, how am I able to get a similar outling effect on gameobjects in the sceneview as the Selection Outlining?

Thanks in advance!

There are several free options available on GitHub, including ones that are based off of that post.

3 Likes

I think I have spent 2/3 hours searching for projects like these yesterday and I have not found these. Maybe I was looking wrong!
Thank you, I will take a look at these! :slight_smile:

Here is another one:

That’s a completely different style of effect. Very cheap, but highly dependent on the geometry it’s used on. Basically it requires 3D meshes with smoothed normals to work properly, and even then will result in inconsistent line widths depending on the “pointy-ness” of the mesh’s corners. Great for certain looks or when you need something very cheap.

1 Like

Yeah, good call out. It is a different style since it will render the lines based on geometry. However, the tutorial goes into detail about the pointed geometry issues and he provides a pretty decent solution to that problem. he has a script that re-calculates the normals of the model during “onStart” and puts that data in the Texcoord channel of the outline shader and has the shader use that to generate the outline.

The video & accompanying script handles hard edges, yes, that’s the mesh smoothing I mentioned. But that’s not what I was talking about.

See this image grabbed from the video showing off the “fixed” normals.
6991208--825602--13357875-69A3-432E-96A2-773C7FA2179A.jpeg
Notice the sphere and capsule outlines are significantly thicker than the cube’s. And the cylinder has different thickness lines on the sides compared to the top. You can do some things to compensate for some extreme cases, but ultimately it’s an unsolvable problem for hull based outlines. At least without having to store a list of every edge linked to each vertex position. You don’t know the screen space edges, only the averaged normal.

1 Like

I didn’t notice the differences in line width there. Very good points!

I just found this one recently and it has become my favorite technique for selection outlines thus far: Object Outlines | Ronja's tutorials

Ronja has some great, straight forward tutorials. And that technique, or ones very similar, are super common.

It also has serious problems if you have any features that are thinner than the outline width. If you look at the example gif, when the Dragon model is highlighted the outline around the tail, horns, and the tip of the tongue look weird and spiky. This is because it’s only sampling 8 positions around the edge of a circle, so smaller details can “slip through” and go unseen. It can be solved by sampling more points both around the edge and inside the interior of the circle, but that gets expensive fast.

Unity’s selection outline, and the github projects I linked to above either blur the silhouette, and/or use a similar single pass expansion like in Ronja’s tutorial. Blurring is nice in that you can kind of “sample all points” within a radius for a lot less cost, but because it is a blur and not a true search, you end up rounding off edges in the resulting outline.

I wrote a long article exploring a few options, including mesh outlines, using an exhaustive search (rather than the sparse ring samples used in Ronja’s tutorial), and using blurs and looking at the cost and quality of each. Then showing an alternative that beats them on both.

3 Likes

This is great research and exploration on the subject. Thanks for sharing! I have been studying outlines (specifically sobel edge detection over depth and normal textures) for the past year on and off. First time hearing about JFA, so will need to spend some time wrapping my head around it. Any good resources you could recommend to learn it? And do you think JFA could be applied with sobel over something like the depth texture?

They’re linked to in the article.

Yes and no. Could JFA be applied to a depth texture to do outlines? Yes. It’ll be a bit more expensive to do than the example in the article, but certainly plausible and should still be faster than exhaustive searches. I’ve seen some people respond to my article with pictures where they’ve done just that, though I don’t know of any code examples.

And if you read that article, you’ll see I am using a Sobel operator to generate the initialization buffer. But it’s not exactly the use case you’re thinking.

The key limitation of JFA is it only works on “hard edges”. When you say Sobel, you’re thinking about these kind of post processing outlines:

That’s not something you can reproduce with JFA, not alone at least, because after the jump floods you’re left with only the position of the closest “edge” from each pixel, but not any other information. You could use a combination of a classic Sobel or similar narrow search edge detection, and JFA, and use the intensity calculated by the sobel to modulate the thickness of the line drawn using the JFA distance field, or as a modifier for the JFA’s “jump” pass selection.

1 Like

Not to necromance the thread, but I’m also hunting particular outlining. I need one that will work well in VR. I found Quick Outline on the Asset Store, and it’s quick enough, but when outlined objects overlap visually, the outlines combine (due to the way the stencil is filled). I need the fore-most objects’ outlines to always show over ones underneath. I could also settle for drawing all outlines regardless of depth. And, I need solutions for both built-in and URP, if possible.
I’m busy reading over the linked material above, but it looks like they use post-processing, which I need to avoid for VR.
Any further tips/tricks greatly appreciated.