Search Unity

Need help combining shaders

Discussion in 'Shaders' started by powerups, Aug 30, 2019.

  1. powerups

    powerups

    Joined:
    Dec 10, 2012
    Posts:
    8
    Hello - I'm trying to combine two shaders with dependencies into one shader (One Video and one Chroma Key) and I'm having trouble getting it to display. Not sure what I'm doing wrong.




    Shader "Custom/VideoShaderKey" {
    Properties{
    _MainTex1("MainTex", 2D) = "white" {}
    _BgColor("BgColor", Color) = (1,1,1,1)
    _KeyColor("KeyColor", Color) = (1,1,1,1)
    _DChroma("D Chroma", range(0.0, 1.0)) = 0.5
    _DChromaT("D Chroma Tolerance", range(0.0, 1.0)) = 0.05

    _Chroma("Chroma (Main -> Bg)", range(0.0, 1.0)) = 0.5
    _Luma("Luma (Main -> Bg)", range(0.0, 1.0)) = 0.5
    _Saturation("Saturation (0 -> Chroma)", range(0.0, 1.0)) = 1.0
    _Alpha("Alpha (Chroma -> Bg)", range(0.0, 1.0)) = 1.0
    }
    CGINCLUDE
    #include "UnityCG.cginc"
    struct VS_OUT {
    fixed4 position:pOSITION;
    fixed2 texcoord0:TEXCOORD0;
    };

    sampler2D _MainTex1;
    fixed4 _MainTex_ST;

    fixed4 _BgColor;

    fixed4 _KeyColor;
    fixed _DChroma;
    fixed _DChromaT;

    fixed _Chroma;
    fixed _Luma;
    fixed _Saturation;
    fixed _Alpha;

    VS_OUT vert(appdata_base input) {
    VS_OUT o;
    o.position = UnityObjectToClipPos(input.vertex);
    o.texcoord0 = TRANSFORM_TEX(input.texcoord, _MainTex);
    return o;
    }

    fixed3 RGB_To_YCbCr(fixed3 RGB) {
    fixed Y = 0.299 * RGB.r + 0.587 * RGB.g + 0.114 * RGB.b;
    fixed Cb = 0.564 * (RGB.b - Y);
    fixed Cr = 0.713 * (RGB.r - Y);
    return fixed3(Cb, Cr, Y);
    }

    fixed3 YCbCr_To_RGB(fixed3 YCbCr) {
    fixed R = YCbCr.z + 1.402 * YCbCr.y;
    fixed G = YCbCr.z - 0.334 * YCbCr.x - 0.714 * YCbCr.y;
    fixed B = YCbCr.z + 1.772 * YCbCr.x;
    return fixed3(R, G, B);
    }
    fixed2 lerp3_2(fixed2 A, fixed2 B, fixed2 C, fixed v) {
    if (v < 0.5) {
    return lerp(A, B, 2*v);
    }
    else {
    return lerp(B, C, 2*(v-0.5));
    }
    }
    fixed lerp3_1(fixed A, fixed B, fixed C, fixed v) {
    if (v < 0.5) {
    return lerp(A, B, 2 * v);
    }
    else {
    return lerp(B, C, 2 * (v - 0.5));
    }
    }
    fixed4 frag(VS_OUT input) : SV_Target {
    fixed4 c = tex2D(_MainTex1, input.texcoord0);
    if(c.a > 0) {
    fixed4 c_bg = _BgColor;

    fixed3 src_YCbCr = RGB_To_YCbCr(c.rgb);
    fixed3 target_YCbCr = RGB_To_YCbCr(c_bg.rgb);
    fixed3 key_YCbCr = RGB_To_YCbCr(_KeyColor);

    fixed dChroma = distance(src_YCbCr.xy, key_YCbCr.xy);
    if (dChroma < _DChroma) {
    fixed ta = 0;
    c.rgb = c_bg.rgb;
    if (dChroma > _DChroma - _DChromaT) {
    ta = (dChroma - _DChroma + _DChromaT) / _DChromaT;
    fixed2 cta = lerp(src_YCbCr.xy, target_YCbCr.xy, 1 - ta);
    fixed2 ct = lerp3_2(src_YCbCr.xy, cta, target_YCbCr.xy, _Chroma);

    fixed sa = length(cta);
    fixed s = lerp(0, sa, _Saturation);
    ct *= s / sa;

    fixed la = lerp(src_YCbCr.z, target_YCbCr.z, 1 - ta);
    fixed l = lerp3_1(src_YCbCr.z, la, target_YCbCr.z, _Luma);

    c.rgb = YCbCr_To_RGB(float3(ct.x, ct.y, l));
    }
    ta = lerp(ta, c.a, _Alpha);
    if(c.a > ta){
    c.a = ta;
    }
    }
    }
    return c;
    }
    ENDCG
    //////////////////////


    SubShader {
    Tags{ "Queue" = "Background" }
    Pass {

    Lighting Off
    ZWrite Off
    ZTest Off
    Cull Off

    CGPROGRAM
    // use "vert" function as the vertex shader
    #pragma vertex vert
    // use "frag" function as the pixel (fragment) shader
    #pragma fragment frag

    // vertex shader inputs
    struct appdata
    {
    float4 vertex : POSITION; // vertex position
    float2 uv : TEXCOORD0; // texture coordinate
    };

    // vertex shader outputs ("vertex to fragment")
    struct v2f
    {
    float2 uv : TEXCOORD0; // texture coordinate
    float4 vertex : SV_POSITION; // clip space position
    };
    int _Rotation = 0;
    float _ScaleX = 1.0;
    float _ScaleY = 1.0;
    int _Mirror = 0;
    int _VerticalMirror = 0;
    int _InvertTextureChannels = 0;

    // vertex shader
    v2f vert(appdata v)
    {
    v2f o;

    // transform position to clip space
    o.vertex = float4(v.vertex.x*_ScaleX, v.vertex.y*_ScaleY, 0.0, 1.0);

    if (_Rotation == 1)
    {
    float tmp = o.vertex.x;
    o.vertex.x = o.vertex.y;
    o.vertex.y = -tmp;
    }
    else if (_Rotation == 2)
    {
    o.vertex.x = -o.vertex.x;
    o.vertex.y = -o.vertex.y;
    }
    else if (_Rotation == 3)
    {
    float tmp = o.vertex.x;
    o.vertex.x = -o.vertex.y;
    o.vertex.y = tmp;
    }

    // pass the texture coordinate
    o.uv = v.uv;
    if (_Mirror == 1)
    o.uv.x = 1.0 - o.uv.x;
    if (_VerticalMirror == 1)
    o.uv.y = 1.0 - o.uv.y; // image is flipped upside down (depending on pixel formats and devices)

    return o;
    }



    fixed4 frag(v2f i) : SV_Target
    {
    // sample texture and return it
    fixed4 col = tex2D(_MainTex1, i.uv);
    if (_InvertTextureChannels == 1)
    col = fixed4(col.b, col.g, col.r, col.a);
    return col;
    }
    ENDCG
    }

    GrabPass {}

    ////CHROMA
    Pass {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    ENDCG
    }



    }

    Fallback Off
    }