Lit Outline Shader too thick, and using VR

Hello there!
Firs of all, i’m Sergio, nice to meet you all, first time asking a question around here, so please be gentle hehehe

About my problem, right now my group and i are working on a VR game, with a cartoonish style, and we are using the Toon Outline Lit from the standard assets.

My biggest problem with this shader is the “re-sizing” of the outline, the further away you get, the thicker the outlines become. Let me show you a couple pics.

This is what i’m talking about, because of this the models are completely blocked by the outline if you move the camera away, making them impossible to see unless you are very close.

So, that’s my question, is there any way to make the outline to re-size depending on the distance, to keep the ratio so the model doesn’t become a black dot in the screen?

I started playing with some of the parameters, for example i discovered that what i want to do is actually possible if we change this line. (source code: Shader “Toon/Basic Outline” )

Code (CSharp):

  • #ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE //to handle recent standard asset package on older version of unity (before 5.5)
  • o.pos.xy += offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(o.pos.z) * _Outline;
  • #else
  • o.pos.xy += offset * o.poz.z * _Outline;
  • #endif

If i delete the UNITY_Z_0_FAR_FROM_CLIPSPACE part, leaving it as .pos.xy += offset * o.poz.z * _Outline; my objective is achieved, BUT, it won’t work in VR (i’m using HTC VIVE), only with normal cameras, there is some step inside the VR camera that a normal camera won’t do (the cameras configuration, view, field etc are the same, the problem only appear once the VR is working)

I’m new in shaders so my knowledge is very limited, but any help or workaround is most than welcome.

Thank you for reading!

Unity’s Toon Outline is kind of explicitly setup to scale based on distance. It’s supposed to scale so that it is a constant width in view space, ie: the default 0.005 is approximately 0.5% of the screen height. The technique in general has a problem that at a certain point it’ll start drawing over parts of the mesh you might not want to hide. If you want a constant width outline, you can just add the vertex normal to the vertex position prior to the UnityObjectToClipPos() function. It’s a little nicer if you can get the normal and vertex position in view space and only expand on the xy coordinates, and potentially push the z away from the camera, depending on the look you want.

The Toony Colors Pro 2 asset works this way:

However keeping the outlines a fixed world space width has the side effect of getting really big outlines when you’re close to the object. A more complex system would have the outlines be scaled based on distance, but clamped to some max world space size, and maybe even fade out when really far away.