Shader "FZDL_Shader/TestEdgelight"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}


        _RimWidth("Rim Width",float) = 1 
        _ES_AvatarFrontRimColor("Front Rim Color",color) = (1,1,1,1)
        _ES_AvatarBackRimColor("Back Rim Color",color) = (1,1,1,1)
        _ES_AvatarFrontRimIntensity ("Front Rim Intensity",float) = 1
        _ES_AvatarBackRimIntensity ("Back Rim Intensity",float) = 1


        _EdgeStart("Edge start",range(0,1)) = 0.82
        _EdgeEnd("Edge end",range(0,1)) = 0.9
    }
    SubShader
    {
        Tags 
        {
         "RenderType"="Transparent" 
         "Queue" = "Transparent"
        }
        

        LOD 100

        Pass
        {


            Name "EdgeLight"
            Tags{"LightMode" = "UniversalForward"}


            ZTest off
            Cull Back
            Blend One One
            BlendOp Add

            Offset 0,-1

            HLSLPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            //#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float4 screen        : TEXCOORD1;
                float3 normal       : TEXCOORD2;
                float3 viewDir      : TEXCOORD3;

            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            float _RimWidth;
            half3 _ES_AvatarFrontRimColor;
            half3 _ES_AvatarBackRimColor;
            float _ES_AvatarFrontRimIntensity;
            float _ES_AvatarBackRimIntensity;
            float _EdgeStart;
            float _EdgeEnd;

            v2f vert (appdata v)
            {
                v2f o = (v2f)0;
                o.vertex = TransformObjectToHClip(v.vertex.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                o.screen = ComputeScreenPos(o.vertex);
                float3 worldPos = TransformObjectToWorld(v.vertex.xyz);
                o.viewDir = normalize(GetCameraPositionWS() - worldPos);
                o.normal = TransformObjectToWorldNormal(v.normal,true);


                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                half4 col = 0;

                float2 uv = UnityStereoTransformScreenSpaceTex(i.screen.xy / i.screen.w);

                float3 albedo = SAMPLE_TEXTURE2D_X(_CameraOpaqueTexture, sampler_CameraOpaqueTexture,uv).rgb;

                float thisZ = Linear01Depth(i.screen.z / i.screen.w, _ZBufferParams);

                float traceDir = cross(i.viewDir,i.normal).y > 0? -1 : 1;

                float uvx = uv.x; 
                //只管水平方向 效率效果更好
                uv.x += traceDir * _RimWidth / (thisZ * _ProjectionParams.z);

                float sceneZ = Linear01Depth(SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture,uv).r, _ZBufferParams);
                half depthDist = max(sceneZ - thisZ, 1e-6) /(thisZ * _ProjectionParams.z);
                // half edge = pow(depthDist, 0.015);
                half edge = pow(depthDist,0.015);
                edge = smoothstep(_EdgeStart, _EdgeEnd, edge);

                half nol = dot(i.normal, _MainLightPosition.xyz);
                half frontDiff = saturate(nol);
                half backDiff = saturate(-nol);
                // half frontDiff = smoothstep(0.8,0.99,nol);
                // half backDiff = smoothstep(0.5,0.99,-nol);

                half3 adjustAlbedo = saturate(albedo * 5 + 0.3);

                half3 frontEdgeColor = _ES_AvatarFrontRimColor * _ES_AvatarFrontRimIntensity * frontDiff;
                half3 backEdgeColor = _ES_AvatarBackRimColor * _ES_AvatarBackRimIntensity * backDiff;

                half3 edgeLight = (frontEdgeColor + backEdgeColor) * adjustAlbedo * edge;

                half viewLerp = dot( _MainLightPosition.xz, -i.viewDir.xz)*0.5 + 0.5;
                edgeLight *= viewLerp;


                // return 0;

                // return half4(((uv.x - uvx) * 0.5 + 0.5).xxx,1);
                // return half4((uv.x - uvx).xxx * 50,1);
                return half4(edgeLight ,1);
            }
            ENDHLSL
        }
    }
}
