Shader "Unlit/Highlight"
{
    Properties{
    _HighlightColor("Highlight Color", Color) = (1, 1, 1, 1)
    _HighlightWidth("Highlight Width", Range(0, 10)) = 2
    _HighlightPower("HighlightPower",Range(0,10)) = 0.0
    }

        SubShader{
          Tags {
            "Queue" = "Transparent+110"
            "RenderType" = "Transparent"
            "DisableBatching" = "True"
          }

          Pass {
            Cull Off
            ZTest Less
            ZWrite Off //关闭深度写入
            Blend SrcAlpha OneMinusSrcAlpha //透明混合
            ColorMask RGB//写入默认渲染目标的给定通道

            CGPROGRAM
            #include "UnityCG.cginc"

            #pragma vertex vert
            #pragma fragment frag

            struct appdata {
              float4 vertex : POSITION;
              float3 normal : NORMAL;
              float3 smoothNormal : TEXCOORD3;
              UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f {
              float4 position : SV_POSITION;
              fixed4 color : COLOR;
              float3 worldNormal : NORMAL;
              float3 worldPos : TEXCOORD1;
              UNITY_VERTEX_OUTPUT_STEREO
            };

            uniform fixed4 _HighlightColor;
            uniform float _HighlightWidth;
            float _HighlightPower;

            v2f vert(appdata input) {
              v2f output;

              UNITY_SETUP_INSTANCE_ID(input);
              UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

              //input.smoothNormal！=0？input.smoothNormal：input.normal
              float3 normal = any(input.smoothNormal) ? input.smoothNormal : input.normal;
              float3 viewPosition = UnityObjectToViewPos(input.vertex);
              float3 viewNormal = normalize(mul((float3x3)UNITY_MATRIX_IT_MV, normal));

              output.position = UnityViewToClipPos(viewPosition + viewNormal * -viewPosition.z * _HighlightWidth / 1000.0);
              output.worldNormal = UnityObjectToWorldNormal(input.normal);
              output.worldPos = mul(unity_ObjectToWorld, input.vertex).xyz;
              output.color = _HighlightColor;
              return output;
            }

            fixed4 frag(v2f input) : SV_Target {
                fixed3 worldNormal = normalize(input.worldNormal);
                fixed3 viewDir = normalize(UnityWorldSpaceViewDir(input.worldPos));
                fixed A = pow(saturate(1-dot(worldNormal, viewDir)), _HighlightPower);
                input.color.a *= A;
              return input.color;
            }
            ENDCG
          }
    }
}
