Search Unity

silhouette shader overlapping problem

Discussion in 'Shaders' started by dr-electro, Aug 12, 2015.

  1. dr-electro

    dr-electro

    Joined:
    Oct 3, 2011
    Posts:
    58
    Hey,

    i am working on a hack n slash and want to highlight units behind walls and trees etc.
    I modified a silhouette shader to resemble that effect but i am stuck with the problem of overlapping objects.

    In the screenshot you see the dwarf, his hammer (its a separate mesh) and a troll. They all have the silhouette shader so you can see them behind trees or walls.

    You can see that where the hammer overlaps the dwarf there are unwanted highlights.
    How can i modify the shader or what can i do so that (in this example) the hammer is drawn over the dwarfs highlights?

    Thanks! Alex



    Find the the shader attached
     

    Attached Files:

  2. dr-electro

    dr-electro

    Joined:
    Oct 3, 2011
    Posts:
    58
    Code (CSharp):
    1. Shader "Outlined/Silhouetted Diffuse" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,0.5)
    4.         _SpecColor ("Spec Color", Color) = (1,1,1,1)
    5.         _Emission ("Emmisive Color", Color) = (0,0,0,0)
    6.         _Shininess ("Shininess", Range (0.01, 1)) = 0.7
    7.         _MainTex ("Base (RGB)", 2D) = "white" { }
    8.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    9.     }
    10. CGINCLUDE
    11. #include "UnityCG.cginc"
    12. struct appdata {
    13.     float4 vertex : POSITION;
    14.     float3 normal : NORMAL;
    15. };
    16. struct v2f {
    17.     float4 pos : POSITION;
    18.     float4 color : COLOR;
    19. };
    20. //uniform float _Outline;
    21. uniform float4 _OutlineColor;
    22. v2f vert(appdata v) {
    23.     v2f o;
    24.     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    25.     o.color = _OutlineColor;
    26.     return o;
    27. }
    28. ENDCG
    29.     SubShader {
    30.         Tags { "Queue" = "Transparent" }
    31.         // note that a vertex shader is specified here but its using the one above
    32.         Pass {
    33.             Name "OUTLINE"
    34.             Tags { "LightMode" = "Always" }
    35.             Cull Off
    36.             ZWrite Off
    37.             ZTest Greater
    38.             ColorMask RGB // alpha not used
    39.             // you can choose what kind of blending mode you want for the outline
    40.             //Blend SrcAlpha OneMinusSrcAlpha // Normal
    41.             Blend One One // Additive
    42. CGPROGRAM
    43. #pragma vertex vert
    44. #pragma fragment frag
    45. half4 frag(v2f i) :COLOR {
    46.     return i.color;
    47. }
    48. ENDCG
    49.         }
    50.         Pass {
    51.        
    52.             ZWrite On
    53.             ZTest LEqual
    54.             Blend SrcAlpha OneMinusSrcAlpha
    55.            
    56.            
    57.             Material {
    58.                 Diffuse [_Color]
    59.                 Ambient [_Color]
    60.                 Shininess [_Shininess]
    61.                 Specular [_SpecColor]
    62.                 Emission [_Emission]
    63.             }
    64.             Lighting On
    65.             SeparateSpecular On
    66.             SetTexture [_MainTex] {
    67.                 constantColor [_Color]
    68.                 Combine texture * primary DOUBLE, texture * constant
    69.             }
    70.         }
    71.     }
    72.     Fallback "Diffuse"
    73. }
     
  3. dr-electro

    dr-electro

    Joined:
    Oct 3, 2011
    Posts:
    58
    ok, i got it
    Offset -100,-100 added to the Outline Pass did the trick.

    Not entirely sure why, but great nonetheless ;)