unity3d播放透明mov格式視頻的一種方式

首先說明這個是利用shader來實現透明,並不是使用含有透明通道的mov視頻


網上查了unity好像沒有自帶的方法來支持透明通道的mov格式視頻

但是可以用shader實現,mov如果帶了透明通道也是和沒有透明一樣是黑色的背景

這裏需要兩個視頻,AE裏面按如下格式輸出,注意Channels一個是RGB 另一個是ALPHA 不需要其他調整

(也不要壓縮,因爲unity會自動壓縮mov格式,如果mov導入出錯首先看看你的視頻名字是不是中文的再說)


網上找到了一個 把下面寫入shader文件然後放材質上,材質放物體上,然後按如下圖調整

albedo(漫反射)放RGB那個 alpha放alpha那個視頻

Shader "Custom/Example"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _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
 
        // 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 SurfaceOutputStandard o) {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            fixed4 _alpha = tex2D (_AlphaVideo, IN.uv_AlphaVideo);
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = _alpha.r;
           
        }
        ENDCG
    }
    FallBack "Diffuse"

}

(參考網址 http://forum.unity3d.com/threads/video-with-alpha.338072/)
注意如果你還是看到黑色沒有看到動畫那是因爲視頻沒有播放

記得兩個視頻都要播放Play(); 纔會正確

這種方式適合GUI的,但是會有淡淡的透明背景色存在

shader可以改成

#pragma surface surf Lambert  alpha

SurfaceOutputStandard

也改成SurfaceOutput就能獲得如圖的效果了,注意這種的放GUI會全黑或者全白

其他快捷的方法還在探索中,希望有好的效果能分享哈,共同進步


原文章地址:https://blog.csdn.net/shenmifangke/article/details/48025909

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章