Shader "Course/Particles/Firefly Emission"
{
    Properties
    {
        [HDR] _BaseColor("Glow color", Color)=(10,13,2.5,1)
        _CoreStrength("Core brightness", Range(0,5))=2
        _HaloStrength("Halo brightness", Range(0,2))=.24
        _FogInfluence("Fog attenuation", Range(0,1))=.18
        _SoftFade("Surface fade distance", Float)=.08
    }
    SubShader
    {
        Tags { "RenderPipeline"="UniversalPipeline" "Queue"="Transparent" "RenderType"="Transparent" }
        Pass
        {
            Tags { "LightMode"="UniversalForward" }
            Blend One One
            ZWrite Off
            ZTest LEqual
            Cull Off
            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag
            #pragma multi_compile_fog
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
            struct Attributes { float4 positionOS:POSITION;float2 uv:TEXCOORD0;half4 color:COLOR; };
            struct Varyings { float4 positionCS:SV_POSITION;float2 uv:TEXCOORD0;half4 color:COLOR;float fog:TEXCOORD1;float eyeDepth:TEXCOORD2; };
            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                float _CoreStrength,_HaloStrength,_FogInfluence,_SoftFade;
            CBUFFER_END
            Varyings Vert(Attributes input)
            {
                Varyings output;
                float3 world=TransformObjectToWorld(input.positionOS.xyz);
                output.positionCS=TransformWorldToHClip(world);
                output.eyeDepth=-TransformWorldToView(world).z;
                output.uv=input.uv;output.color=input.color;
                output.fog=ComputeFogFactor(output.positionCS.z);
                return output;
            }
            half4 Frag(Varyings input):SV_Target
            {
                float r=length(input.uv*2-1);
                float core=exp2(-r*r*48)*_CoreStrength;
                float halo=exp2(-r*r*5.5)*_HaloStrength*(1-smoothstep(.65,1,r));
                float sceneDepth=LinearEyeDepth(SampleSceneDepth(GetNormalizedScreenSpaceUV(input.positionCS)),_ZBufferParams);
                float surfaceFade=saturate((sceneDepth-input.eyeDepth)/max(.001,_SoftFade));
                // Emission is attenuated, without adding the background fog color to the sprite.
                float visibility=lerp(1,ComputeFogIntensity(input.fog),_FogInfluence);
                half3 glow=_BaseColor.rgb*input.color.rgb*input.color.a*(core+halo)*surfaceFade*visibility;
                return half4(glow,0);
            }
            ENDHLSL
        }
    }
}
