天天看點

Unity播放透明視訊的三種方式

Unity支援播放衆多格式視訊,在這就不一一列舉了,接下來說下如何播放透明視訊,通過查閱資料與請教高手,總結了幾種方式,與大家記錄分享一下。

首先想到的是直接将視訊拖入面闆,Unity自動添加VedioPlayer元件

播放一下,确實能實作效果,但是卻出現一個問題,雖然可以調該物體Scale,但是并不能實作改變其大小

效果如下圖:

Unity播放透明視訊的三種方式
Unity播放透明視訊的三種方式

那麼,如何播放透明視訊,且可以自由控制呢?接下來進入正題。

要播放透明視訊,首先視訊具有 alpha通道。

1,第一種方式:通過Plane+shader

先建立一個Plane,把視訊拖到Plane上面,通過控制Plane來控制視訊的大小,但是卻發現視訊周邊是黑色的,播放看看效果:

Unity播放透明視訊的三種方式

我們可以使用一個shader,剔除黑色

shader代碼如下:

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" 
}      

再添加一個播放視訊的腳本:

public MovieTexture moveTexture;
   // Use this for initialization
   void Start () {
       GetComponent<Renderer>().material.mainTexture = moveTexture;
       moveTexture.loop = true;
       moveTexture.Play();
   }
    
   // Update is called once per frame
   void Update () {
        
   }      

效果圖如下:

馬上注冊,結交更多好友,享用更多功能,讓你輕松玩轉社群。

您需要 登入 才可以下載下傳或檢視,沒有帳号?注冊帳号 
Unity播放透明視訊的三種方式

x

Unity支援播放衆多格式視訊,在這就不一一列舉了,接下來說下如何播放透明視訊,通過查閱資料與請教高手,總結了幾種方式,與大家記錄分享一下。

首先想到的是直接将視訊拖入面闆,Unity自動添加VedioPlayer元件

播放一下,确實能實作效果,但是卻出現一個問題,雖然可以調該物體Scale,但是并不能實作改變其大小

效果如下圖:

Unity播放透明視訊的三種方式
Unity播放透明視訊的三種方式

那麼,如何播放透明視訊,且可以自由控制呢?接下來進入正題。

要播放透明視訊,首先視訊具有alpha通道。

1,第一種方式:通過Plane+shader

先建立一個Plane,把視訊拖到Plane上面,通過控制Plane來控制視訊的大小,但是卻發現視訊周邊是黑色的,播放看看效果:

Unity播放透明視訊的三種方式

我們可以使用一個shader,剔除黑色

shader代碼如下:

[C#]  純文字檢視  複制代碼 ?

  01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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"

}

再添加一個播放視訊的腳本:

[C#]  純文字檢視  複制代碼 ?

  01 02 03 04 05 06 07 08 09 10 11

public

MovieTexture moveTexture;

// Use this for initialization

void

Start () {

GetComponent<Renderer>().material.mainTexture = moveTexture;

moveTexture.loop =

true

;

moveTexture.Play();

}

// Update is called once per frame

void

Update () {

}

效果圖如下:
Unity播放透明視訊的三種方式
Unity播放透明視訊的三種方式

總結: 這種方式需要視訊格式為MOV、MP4等,視訊的Importer Version改為 MovieTexTure。但是,使用MovieTexture播放視訊的話,不能導入移動端,至于什麼原因,呵呵,這個,在下也不知道啊。

2,第二種方式:Plane+使用Unity自帶的shader,如下圖

視訊格式為:webm

添加元件:Vedio Player

效果圖如下:

Unity播放透明視訊的三種方式

總結:這種方式比較簡單,也可自由控制大小,但是有時會出現視訊微卡現象。

3,第三種方式:通過UI:RawImage +Vedio Player

視訊格式:webm

添加UI:RawImage 

添加元件:Vedio Player

Project面闆添加:Render Texture

Render Texture的size參數改為視訊的長度和寬度,如下圖:

Unity播放透明視訊的三種方式
将Render Texture拖入RawImage 和 Vedio Player,播放,效果圖如下:
Unity播放透明視訊的三種方式

總結:這種方式也挺簡單,效果也不錯。

到此就結束了,以上為播放透明視訊的三種方式,根據個人喜好自行選擇吧,鄙人學習Unity也是純屬瞎摸索,有錯誤之處,還望衆位大神不吝指出。

4.png (48.35 KB, 下載下傳次數: 1)
Unity播放透明視訊的三種方式
7.png (23.97 KB, 下載下傳次數: 1)
Unity播放透明視訊的三種方式

轉自: http://www.manew.com/thread-134897-1-1.html