Unity视频透明显示Shader

最近有需求要透明显示视频,即使视频带Alpha通道,导入unity之后不会直接透明显示,写一个shader就可以解决这个
问题,shader代码如下,需要的直接copy 就可以了。

Shader "Custom/Example"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        //视频1,表示纹理颜色
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        //视频2,表示alpha通道,用颜色值表示alpha的值
        _AlphaVideo ("Alpha Video(R)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
    Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        //#pragma surface surf Standard alpha
        #pragma surface surf Lambert alpha

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _AlphaVideo;

        struct Input {
            float2 uv_MainTex;
            float2 uv_AlphaVideo;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        void surf (Input IN, inout SurfaceOutput o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            fixed4 _alpha = tex2D (_AlphaVideo, IN.uv_AlphaVideo);
            //用视频1的纹理颜色
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            //o.Metallic = _Metallic;
            //o.Smoothness = _Glossiness;
            //o.Alpha = _alpha.r;
            //用视频2的颜色值叠加表示alpha的值
            //o.Alpha = (_alpha.r + _alpha.g + _alpha.b)/3;
            o.Alpha = _alpha.b;

        }
        ENDCG
    }
    FallBack "Diffuse"
}

猜你喜欢

转载自blog.csdn.net/u011017980/article/details/79858429