4

I know this a very well documented subject, but I still have some problems. I made the outline for a silhouette but with doubling the mesh. I have a good fps but the outline is too harsh, I want it to progressive fade out. Here is the result:

enter image description here

It is a two passes shader, one for rendering the man in that pink color and one for rendering the man with texture.

I am thinking to apply a Gaussian blur on the outline but I don't have any result... Any ideas? Thanks in advance.

ahsoka
  • 133
  • 1
  • 3
  • 12
  • you mention you're a beginner, why not jump straight to an advanced topic! heh! http://www.aclockworkberry.com/shader-derivative-functions/ – Fattie Mar 14 '19 at 20:14

2 Answers2

1

The problem with stuff like gaussian blur is that it requires knowledge of the color of neighboring pixels which were rendered before... and in shaders, you don't have that (at best, you can sample neighboring pixels in a texture). Most games who make this effect will instead render all outlined objects to a separate buffer, perform some post-processing on it, and overlay it onto the screen.

One slightly hackish option you can go for is to fade out the outline based on a fresnel factor. Basically, you take the view vector inside the vertex shader like this:

float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
float3 viewDir = _WorldSpaceCameraPos.xyz - worldPos;

Then you get the dot product between them inside the fragment shader like this:

half NdotV = saturate(dot(IN.normal, IN.viewDir));

Then you have a value which goes from 0 to 1 depending on how much the normal faces the camera. You can use this to mask the alpha of your outline:

col.a *= sqrt(NdotV); // Square root for more dramatic effect

I haven't tried this, but it could be worth a try. Just remember to set the proper blending and render queue.

EDIT: You can also check out this project:

https://github.com/cakeslice/Outline-Effect

Kalle Halvarsson
  • 1,240
  • 1
  • 7
  • 15
  • hey Kalle, what about using the differential stuff? **ddx.** I don't totally understand OPs needs but ........ maybe that's it – Fattie Mar 14 '19 at 20:11
  • The problem is that ddx/ddy only has the derivatives for any interpolated values which we receive from the vertex shader. They can't be used to obtain any information regarding the relationship between the rendered object and the background. They also only give the derivative with regards to the neighboring 2 pixels... which might be a bit too granular. – Kalle Halvarsson Mar 14 '19 at 20:36
  • ah - I see, you reckon OP wants something eased off the background. Well - great explanation thanks! – Fattie Mar 14 '19 at 20:40
  • Yeah, derivatives are cool though! I recommend looking into "Geometric Specular Anti-Aliasing" if you want to find an interesting application for them. – Kalle Halvarsson Mar 14 '19 at 20:50
0

How about checking whether anti-aliasing is active?

It can be found in project Quality Settings.

Corne
  • 496
  • 2
  • 8
  • 22