Search Unity

Wiki Silhouette Only Shader Not Working

Discussion in 'Shaders' started by ConorArup, Jan 23, 2020.

  1. ConorArup

    ConorArup

    Joined:
    Feb 15, 2018
    Posts:
    17
    Hi all,

    I'm trying to just create an outline of a plane object (ideally a solid outline, with a transparent coloured centre), and found the silhouette only shader on the wiki (bottom shader found here) which would satisfy my needs temporarily.

    When I apply this shader to my plane object, however, the object turns magenta (assuming because something is now not working in the shader).

    Can anyone point me in the right direction?

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Outlined/Silhouette Only" {
    4.     Properties {
    5.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    6.         _Outline ("Outline width", Range (0.0, 0.03)) = .005
    7.     }
    8. CGINCLUDE
    9. #include "UnityCG.cginc"
    10. struct appdata {
    11.     float4 vertex : POSITION;
    12.     float3 normal : NORMAL;
    13. };
    14. struct v2f {
    15.     float4 pos : POSITION;
    16.     float4 color : COLOR;
    17. };
    18. uniform float _Outline;
    19. uniform float4 _OutlineColor;
    20. v2f vert(appdata v) {
    21.     // just make a copy of incoming vertex data but scaled according to normal direction
    22.     v2f o;
    23.     o.pos = UnityObjectToClipPos(v.vertex);
    24.     float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    25.     float2 offset = TransformViewToProjection(norm.xy);
    26.     o.pos.xy += offset * o.pos.z * _Outline;
    27.     o.color = _OutlineColor;
    28.     return o;
    29. }
    30. ENDCG
    31.     SubShader {
    32.         Tags { "Queue" = "Transparent" }
    33.         Pass {
    34.             Name "BASE"
    35.             Cull Back
    36.             Blend Zero One
    37.             // uncomment this to hide inner details:
    38.             //Offset -8, -8
    39.             SetTexture [_OutlineColor] {
    40.                 ConstantColor (0,0,0,0)
    41.                 Combine constant
    42.             }
    43.         }
    44.         // note that a vertex shader is specified here but its using the one above
    45.         Pass {
    46.             Name "OUTLINE"
    47.             Tags { "LightMode" = "Always" }
    48.             Cull Front
    49.             // you can choose what kind of blending mode you want for the outline
    50.             //Blend SrcAlpha OneMinusSrcAlpha // Normal
    51.             //Blend One One // Additive
    52.             Blend One OneMinusDstColor // Soft Additive
    53.             //Blend DstColor Zero // Multiplicative
    54.             //Blend DstColor SrcColor // 2x Multiplicative
    55. CGPROGRAM
    56. #pragma vertex vert
    57. #pragma fragment frag
    58. half4 frag(v2f i) :COLOR {
    59.     return i.color;
    60. }
    61. ENDCG
    62.         }
    63.     }
    64.     Fallback "Diffuse"
    65. }