如何使用着色器裁剪WebcamTexture?

时间:2018-12-18 08:31:36

标签: unity3d shader crop chromakey

我有一个抠像着色器可以在Unity 2018.3中完美运行。我需要向着色器添加top,bottom,left,right属性,并使用这些参数使纹理的不需要部分透明。这是来自纹理的图像;

chromakey

所有过程都需要在着色器中完成,因为这是一个网络摄像头,GetPixels()SetPixels()中的mainTextureUpdate()非常昂贵。

这是我的着色器代码:

shader "Custom/chroma"

{

Properties{
        _MainTex("Base (RGB)", 2D) = "white" {}
        _MaskCol("Mask Color", Color) = (1.0, 0.0, 0.0, 1.0)
        _Sensitivity("Threshold Sensitivity", Range(0,1)) = 0.5
        _Smooth("Smoothing", Range(0,1)) = 0.5
        _Top("Top", Range(0,1)) = 0
        _Bottom("Bottom", Range(0,1)) = 0
        _Left("Left", Range(0,1)) = 0
        _Right("Right", Range(0,1)) = 0
}
    SubShader{
            Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
            LOD 100
             Lighting Off
             ZWrite Off
              AlphaTest Off
             Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma surface surf Lambert alpha

            struct Input {
                float2 uv_MainTex;
                float4 screenPos;
                float3 worldPos;
            };

            sampler2D _MainTex;
            float4 _MaskCol;
            float _Sensitivity;
            float _Smooth;
            float _Top;
            float _Bottom;
            float _Left;
            float _Right;


            void surf(Input IN, inout SurfaceOutput o) {
                    half4 c = tex2D(_MainTex, IN.uv_MainTex);

                    float maskY = 0.2989 * _MaskCol.r + 0.5866 * _MaskCol.g + 0.1145 * _MaskCol.b;
                    float maskCr = 0.7132 * (_MaskCol.r - maskY);
                    float maskCb = 0.5647 * (_MaskCol.b - maskY);

                    float Y = 0.2989 * c.r + 0.5866 * c.g + 0.1145 * c.b;
                    float Cr = 0.7132 * (c.r - Y);
                    float Cb = 0.5647 * (c.b - Y);

                    float d = abs(length(abs(float2(Cr, Cb) - float2(maskCr, maskCb))));
                    float edge0 = _Sensitivity * (1 - _Smooth);
                    float alpha = smoothstep(edge0, _Sensitivity, d);

                    if (IN.uv_MainTex.x<_Left || IN.uv_MainTex.x>(1-_Right))
                    {
                        alpha = 0;
                    };
                    if (IN.uv_MainTex.y<_Bottom || IN.uv_MainTex.y>(1 - _Top))
                    {
                        alpha = 0;
                    };

                    o.Alpha = alpha * 1;
                    o.Emission = c.rgb * alpha;

            }
            ENDCG

        }
            FallBack "Unlit/Texture"

}

1 个答案:

答案 0 :(得分:1)

IN.uv_MainTex包含您的标准化屏幕位置。

if (IN.uv_MainTex.x<0.3||IN.uv_MainTex.x>0.8) return float4(0,0,0,0);

将在0.3-0.8范围之外将图像裁剪为透明-您可以通过材质属性对y和参数化进行相同操作