Shader "Custom/URPRefraction2D_Shimmer"
{
    Properties
    {
        _NoiseTex("Noise Texture 1", 2D) = "white" {}
        _NoiseTex2("Noise Texture 2", 2D) = "white" {}
        _DistortionStrength("Distortion Strength", float) = 0.2
        _TopColor("Top Light Color", Color) = (1, 1, 1, 1)
        _LightStart("Light Start", float) = 0.275
        _LightEnd("Light End", float) = 0.4
        _Scroll("Scroll", Vector) = (0.05, 0.05, 0, 0)
        _Scroll2("Scroll2", Vector) = (-0.05, -0.05, 0, 0)
    }

    SubShader
    {
        Tags { "RenderType"="Transparent" "RenderPipeline"="UniversalRenderPipeline" }
        LOD 100

        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off
        Cull Off

        Pass
        {
            Name "Refraction"
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

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

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 screenUV : TEXCOORD1;
            };

            TEXTURE2D(_NoiseTex);
            TEXTURE2D(_NoiseTex2);
            TEXTURE2D(_CameraSortingLayerTexture);
            
            SAMPLER(sampler_NoiseTex);
            SAMPLER(sampler_NoiseTex2);
            SAMPLER(sampler_CameraSortingLayerTexture);

            float2 _Scroll;
            float2 _Scroll2;
            float _DistortionStrength;
            float _LightStart;
            float _LightEnd;
            half4 _TopColor;

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = IN.uv;
                
                float4 clipPosition = OUT.positionHCS;
                float2 screenUV = (clipPosition.xy / clipPosition.w) * 0.5 + 0.5;
                screenUV.y = 1.0 - screenUV.y;
                OUT.screenUV = screenUV;

                return OUT;
            }
            
            half4 frag(Varyings IN) : SV_Target
            {
                float depth1 = SAMPLE_TEXTURE2D(_NoiseTex, sampler_NoiseTex, IN.uv + _Scroll * _Time.y).r;
                float depth2 = SAMPLE_TEXTURE2D(_NoiseTex2, sampler_NoiseTex2, IN.uv + _Scroll2 * _Time.y).r;

                float combinedDepth = depth1 * depth2;
                float2 distortion = _DistortionStrength * 0.001 * float2(combinedDepth, combinedDepth);
                float2 distortedUV = IN.screenUV + distortion;

                half4 sceneColor = SAMPLE_TEXTURE2D(_CameraSortingLayerTexture, sampler_CameraSortingLayerTexture, distortedUV);
                
                half topLight = smoothstep(_LightStart, _LightEnd, combinedDepth);
                half4 topLightColor = _TopColor * topLight;

                return sceneColor + topLightColor;
            }

            ENDHLSL
        }
    }
}

+ Recent posts