Search Unity

Outline hull shader - line thickness inconsistency between objects

Discussion in 'Shaders' started by Senshi, Jun 26, 2017.

  1. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    I have no idea what changed since a day ago, but some of my outlined objects suddenly have thicker outlines than others; a 100 times thicker, to be exact.

    Attached is a screenshot to illustrate the issue. Both objects have identical scale, have the same parent, same material, and no "per renderer" properties. And indeed, if I simply swap out the "mesh" property the outline changes accordingly.

    I am at a complete loss as to what might be the culprit here. If anyone could give this a crack I would very much appreciate it. Below is the outline pass of the shader:

    Code (CSharp):
    1. Pass
    2.         {
    3.             Name "OUTLINE"
    4.             Lighting Off
    5.             Cull Front
    6.  
    7.             CGPROGRAM
    8.             #pragma vertex vert
    9.             #pragma fragment frag
    10.  
    11.             #include "UnityCG.cginc"
    12.  
    13.             struct appdata
    14.             {
    15.                 float4 vertex : POSITION;
    16.                 float3 normal : NORMAL;
    17.             };
    18.  
    19.             struct v2f
    20.             {
    21.                 float4 vertex : SV_POSITION;
    22.             };
    23.  
    24.             fixed4 _LineColor;
    25.             float _LineThickness;
    26.        
    27.             v2f vert (appdata i)
    28.             {
    29.                 float dist = distance(_WorldSpaceCameraPos, mul(_Object2World, i.vertex));
    30.  
    31.                 v2f o;
    32.                 o.vertex = mul(UNITY_MATRIX_MVP, i.vertex + i.normal * pow(dist, .6) * .001 * _LineThickness);
    33.                 return o;
    34.             }
    35.        
    36.             fixed4 frag (v2f i) : SV_Target
    37.             {
    38.                 return _LineColor;
    39.             }
    40.             ENDCG
    41.         }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    If you look at the model asset settings I suspect you'll find some have a "file scale" (usually 0.01), and some don't. Basically it means some meshes were exported with a different scene scale setting (like one was using meters, and the other using millimeters).

    The way Unity handles this isn't by changing the mesh data, but instead applying a scale to the object to world transform the shader uses. The solution is to re-export those meshes with the same scene scale, or by not using the mesh data directly and instead transform the positions and normal into world space, view, or idealing clip space, prior to doing the expansion. Look at Unity's built in toon outline shader for an example that uses clip space.
     
  3. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Prepare to have your mind blown, they're both meshes from the same, single FBX! =P I also double-checked my scene scales in Blender and they're all neatly (1,1,1).

    I'll besure to have a look a clip space though; that's a new one to me, thanks!
     
    neoshaman likes this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    ... o_O ...​