Help Wanted: Combining 2 off-the-shelf Shaders: surf and frag ones

Hello,
I have 2 shaders that I’ve found online - each works well independently - but I want to combine them, and cen’t get it to work.
It should be pretty simple: The first is a basic Chroma Key shader, and the second is a Parallax shader (using a mask).
I have very little experience with shaders, but think that this should be do-able. I’ve searched online for over a day, and mostly found answers that this isn’t possible, since Unity can’t run another shader once you have a “surf” shader. I’ve tried separate passes, code blocks, subshaders - nothing works.

The first shader is:

Shader "Custom/shaderCK_1"
// https://discussions.unity.com/t/600196 ?_ga=2.114736389.341870837.1608642344-105392366.1584566490
{
    Properties
    {
        //_Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
        //_Glossiness ("Smoothness", Range(0,1)) = 0.5
        //_Metallic ("Metallic", Range(0,1)) = 0.0
        _thresh("Threshold", Range(0, 16)) = 0.65
        _slope ("Slope", Range (0, 1)) = 0.63
        _keyingColor ("KeyingColour", Color) = (1,1,1,1)
    }
    SubShader
        {
            Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
            LOD 100

            Lighting Off
            ZWrite Off
            AlphaTest Off
            Blend SrcAlpha OneMinusSrcAlpha

            Pass {
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag
            #pragma fragmentoption ARB_precision_hint_fastest

        sampler2D _MainTex;
        float3 _keyingColor;
        float _thresh; //0.8
        float _slope; //0.2

        #include "UnityCG.cginc"

        float4 frag(v2f_img i) : COLOR {
        float3 input_color = tex2D(_MainTex,i.uv).rgb;  // * _Color;
        float d = abs(length(abs(_keyingColor.rgb - input_color.rgb)));
        float edge0 = _thresh * (1 - _slope);
        float alpha = smoothstep(edge0, _thresh, d);
        return float4(input_color, alpha);


        }

            ENDCG
        }
        }
    FallBack "Unlit/Texture"
}

The second is just a modification of the standard Sprites-Diffuse shader:

Shader "Sprites/DiffuseP"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
        // add these 2 lines:
        _HeightTex("Heightmap (R)", 2D) = "gray" {}
        _Scale("Scale", Vector) = (0,0,0,0)
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        CGPROGRAM
        #pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
        #pragma multi_compile _ PIXELSNAP_ON
        #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
        #include "UnitySprites.cginc"

        // add these two variables
        sampler2D _HeightTex;
        fixed2 _Scale;

        struct Input
        {
            float2 uv_MainTex;
            fixed4 color;
        };

        void vert (inout appdata_full v, out Input o)
        {
            v.vertex = UnityFlipSprite(v.vertex, _Flip);

            #if defined(PIXELSNAP_ON)
            v.vertex = UnityPixelSnap (v.vertex);
            #endif

            UNITY_INITIALIZE_OUTPUT(Input, o);
            o.color = v.color * _Color * _RendererColor;
        }

        void surf (Input IN, inout SurfaceOutput o)
        {
            // Displacement -- add these lines
            fixed height = tex2D(_HeightTex, IN.uv_MainTex).r;
            fixed2 displacement = _Scale * ((height - 0.5) * 2);
            fixed4 c = SampleSpriteTexture(IN.uv_MainTex - displacement) * IN.color;
            //remove the next one?
            //fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;
            o.Albedo = c.rgb * c.a;
            o.Alpha = c.a;
        }
        ENDCG
    }

Fallback "Transparent/VertexLit"
}

*EDIT - mistakenly copy-pasted the same shader twice

I’d be happy for some help!

You’ve posted the same shader twice, but I expect it would be possible to add this to an existing surface shader. Just convert the frag shader block to a function, call it on the value you sample from the albedo/diffuse texture, and use the return value as the alpha value for the surface shader.

Eureka!
Not that I actually did what you said, but you gave me the push to try and put the functions WITHIN the “surf” section.

And I spent 4 hours trying to get it to work, until finally realizing that there was one more modification needed: The Chroma Key requires the line
Blend SrcAlpha OneMinusSrcAlpha
which needed to replace the original (Sprite/Diffuse)
Blend One OneMinusSrcAlpha

And then it works!
Full code:

Shader "Custom/GreenParallax"
// https://discussions.unity.com/t/600196 ?_ga=2.114736389.341870837.1608642344-105392366.1584566490
{
    Properties
    {
        [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
        _Color("Tint", Color) = (1,1,1,1)
        [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
        // add these 2 lines:
        _HeightTex("Heightmap (R)", 2D) = "gray" {}
        _Scale("Scale", Vector) = (0,0,0,0)

        // add these 3 from the GreenScreen:
        //_MainTex2("Base (RGB)", 2D) = "white" {}
        _thresh("Threshold", Range(0, 16)) = 0.65
        _slope("Slope", Range(0, 1)) = 0.63
        _keyingColor("KeyingColour", Color) = (1,1,1,1)
    }

        SubShader
        {
            Tags
            {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
            "PreviewType" = "Plane"
            "CanUseSpriteAtlas" = "True"
            }

            Cull Off
            Lighting Off
            ZWrite Off
            //Blend One OneMinusSrcAlpha  // NEEDS TO BE TURNED OFF!!!
            //these are from the GreenScreen:
            LOD 100
            AlphaTest Off
            Blend SrcAlpha OneMinusSrcAlpha


                CGPROGRAM
                    #pragma surface surf Lambert vertex:vert nofog nolightmap nodynlightmap keepalpha noinstancing
                    #pragma multi_compile _ PIXELSNAP_ON
                    #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
                    #include "UnitySprites.cginc"
   
                    // add these two variables for Parallax
                    sampler2D _HeightTex;
                    fixed2 _Scale;

                    ////Chroma additions:
                    float3 _keyingColor;
                    float _thresh; //0.8
                    float _slope; //0.2
                    #include "UnityCG.cginc"

                    struct Input
                    {
                        float2 uv_MainTex;
                        fixed4 color;
                        //float2 _MainTex;
                    };

                    void vert(inout appdata_full v, out Input o)
                    {
                        v.vertex = UnityFlipSprite(v.vertex, _Flip);

                        #if defined(PIXELSNAP_ON)
                                    v.vertex = UnityPixelSnap(v.vertex);
                        #endif

                        UNITY_INITIALIZE_OUTPUT(Input, o);
                        o.color = v.color * _Color * _RendererColor;
                    }

                    void surf(Input IN, inout SurfaceOutput o)
                    {
                        // Displacement -- add thes lines
                        fixed height = tex2D(_HeightTex, IN.uv_MainTex).r;
                        fixed2 displacement = _Scale * ((height - 0.5) * 2);
                        fixed4 c = SampleSpriteTexture(IN.uv_MainTex - displacement) *IN.color;

                        //remove the next one?
                        //fixed4 c = SampleSpriteTexture (IN.uv_MainTex) * IN.color;

                        // now try and manually insert the Greenscreen functionality
                        float d = abs(length(abs(_keyingColor.rgb - c.rgb)));
                        float edge0 = _thresh * (1 - _slope);
                        float alpha = smoothstep(edge0, _thresh, d);

                        o.Albedo = c.rgb*c.a;
                        o.Alpha = alpha; //c.a;
                    }
                    ENDCG


   

        }
            Fallback "Transparent/VertexLit"
}