outline shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By swipis
:warning: Old Version Published before Godot 3 was released.

Hello there!
Maybe someone can help to make outline shaders for 3D models? Or maybe can give some tips how to make one?

Thank you

A cheap way to add an outline would be to extrude backfaces a little along normals, and color them in black, all within the vertex shader. It has the effect of showing a black “clone” of the model’s backfaces that is slightly larger, and then creates an outline behind it.
This works well if your model doesn’t have hard edges (sphere, character…) but it can be problematic with cubes or buildings: Cutout toon shader - Questions & Answers - Unity Discussions.
This technique requires two passes: render the model black with inverted backface culling, then render the model normally over it. But I seriously doubt Godot shaders can do this yet. So you can cheat and instantiate twice the MeshInstance and have one play the role of the outline with a different material.
Edit: you might be able to do that in one pass if your shader knows the transform of the camera, which is usually located in the VIEW matrix. This way, you can recognize backfaces, color them in black and extrude them. You would need to check “double-sided” option thought.

Another, very expensive approach would be to run a fragment shader the same way as the 2D outline example does: render all objects needing an outline in a buffer (RenderTexture), run the 2D outline shader on it, then draw this over the scene. This would get the best results, but it’s a lot slower.

I’ve never implemented these two myself, I just know the theory :slight_smile:

Zylann | 2016-05-28 10:38

thank you, I guess I will leave this for now.

swipis | 2016-05-28 12:29