Shader "RedNightWorks/SyringeViewJack"
{
    Properties
    {
        _Strength("Overall Effect Strength", Range(0,1)) = 1.0
        [Space(20)]

        [Header(Colors)]
        _Color1("Color1", Color) = (1, 0.5, 0.8, 1)
        _Color2("Color2", Color) = (1, 0.5, 0.8, 1)
        _ColorShiftFreq("Color Shift Frequency", Float) = 1
        [Space(20)]

        [Header(Chromatic aberration)]
        _ShiftX("Shift X", Range(0,0.02)) = 0.005
        _FreqX("Frequenxy X", Range(0,30)) = 0.5
        _SpeedX("Speed X", Float) = 1
        _ShiftY("Shift Y", Range(0,0.02)) = 0.005
        _FreqY("Frequenxy Y", Range(0,30)) = 0.5
        _SpeedY("Speed Y", Float) = 1
        [Space(20)]

        [Header(Texture Scroll)]
        [NoScaleOffset] _EffectTex("Shape Texture", 2D) = "white"{}
        _ShapeColor("ShapeColor", Color) = (1, 0.7, 0.95, 0.5)
        _ShapeSize("Shape Size", Float) = 0.2
        _ScrollSpeed("Shape Scroll Speed", Float) = 0.2
        [Space(30)]

        [Toggle]
        _Debug("Debug", int) = 0
    }
    SubShader
    {
        Tags{"Queue"="Transparent+2000" "VRCFallback"="Hidden"}

        GrabPass
        {
            "_BackgroundTexture"
        }

        Pass
        {
            Cull Front
            ZWrite Off
            ZTest Always

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;

                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float4 screenPos : TEXCORD0;
                float3 worldPos : TEXCORD2;
                float3 objPos : TEXCORD3;

                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _EffectTex;
            fixed4 _Color1;
            fixed4 _Color2;
            fixed _ColorShiftFreq;
            float _Strength;
            half _ShiftX;
            half _FreqX;
            half _SpeedX;
            half _ShiftY;
            half _FreqY;
            half _SpeedY;
            fixed _ShapeSize;
            fixed4 _ShapeColor;
            fixed _ScrollSpeed;
            int _Debug;
            UNITY_DECLARE_SCREENSPACE_TEXTURE(_BackgroundTexture);
            //UNITY_DECLARE_SCREENSPACE_TEXTURE(_CameraDepthTexture);

            v2f vert(appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                o.pos = UnityObjectToClipPos(v.vertex);
                o.screenPos = ComputeGrabScreenPos(o.pos);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                o.objPos = unity_ObjectToWorld._m03_m13_m23;
                return o;
            }

            float random(float2 st)
            {
                return frac(sin(dot(st.xy, float2(12.9898, 78.233))) * 43758.5453);
            }


            fixed4 frag(v2f i) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

                //球の半径
                fixed radius = length(i.worldPos - i.objPos);

                //GrabPassテクスチャ取得
                fixed2 screenSpaceUV = i.screenPos.xy / i.screenPos.w;
                fixed4 bgColor = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BackgroundTexture, screenSpaceUV);
                //float depth = LinearEyeDepth(UNITY_SAMPLE_SCREENSPACE_TEXTURE(_CameraDepthTexture, screenSpaceUV)).x;

                //X方向シフト
                fixed2 shiftUV = screenSpaceUV;
                fixed shift = sin(_Time.y*_SpeedX + shiftUV.y * _FreqX) * _ShiftX;
                #if defined(USING_STEREO_MATRICES)
                shift *= 0.25;
                #endif
                shiftUV.x += shift;
                fixed shiftR = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BackgroundTexture, shiftUV).r;

                //Y方向シフト
                shiftUV = screenSpaceUV;
                shift = sin(_Time.y*_SpeedY + shiftUV.x * _FreqY) * _ShiftY;
                #if defined(USING_STEREO_MATRICES)
                shift *= 0.25;
                #endif
                shiftUV.y += shift;
                fixed shiftB = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BackgroundTexture, shiftUV).b;

                fixed4 shiftColor = fixed4(shiftR, bgColor.g, shiftB, 1);

                //球の内側に制限
                half dist = length(_WorldSpaceCameraPos.xyz - i.objPos.xyz);
                fixed distMask = step(dist, radius);

                fixed4 mulColor = lerp(_Color1, _Color2, sin(_Time.y*_ColorShiftFreq)*0.5+0.5);


                //ハートがスクロールするエフェクト
                fixed aspect = _ScreenParams.x / _ScreenParams.y;
                #if defined(USING_STEREO_MATRICES)
                aspect *= 2;
                #endif

                fixed4 effectColor = fixed4(0,0,0,0);
                fixed scroll = _Time.y * _ScrollSpeed;
                for(int i=0; i<15; i++){
                    float2 a = screenSpaceUV;
                    fixed iterRand = random(float2(i, i*i));
                    fixed iterRand2 = random(float2(iterRand, i));

                    //描画するy座標を決める(画面サイズより大きめにスクロールする)
                    a.y -= frac(scroll + iterRand)*1.2-0.1;
                    //Xはランダム座標
                    a.x -= random(float2(floor(scroll + iterRand)+i, i));
                    //描画するサイズ
                    a /= _ShapeSize;
                    a.x *= aspect;
                    //0.5オフセットでテクスチャの中心を描画ポイントに合わせる
                    a += 0.5;

                    //テクスチャアトラス内のどれを描画するか
                    fixed2 b;
                    b.x = iterRand;
                    b.y = iterRand2;
                    b = step(b, 0.5)*0.5;
                    a = clamp(a, b, b+0.5);

                    effectColor += tex2D(_EffectTex, a) * tex2D(_EffectTex, a).a * _ShapeColor.a;
                }
                effectColor = lerp(0, effectColor, distMask);

                fixed4 output = lerp(bgColor, mulColor*shiftColor, distMask*mulColor.a);
                output += effectColor * _ShapeColor;
                output = lerp(bgColor, output, saturate(_Strength)); //strength適用
                
                //debug
                output = _Debug ? fixed4(1,1,1,1) : output;



                return output;
            }
            ENDCG
        }
    }
}
