Unity中带有alpha通道的视频叠加播放

问题:

      如何让两个透明视频叠加播放

解决播放:

      1:使用Unity自带的shader,shader代码如下所示

Shader "Unlit/MaskVideo"
{
	Properties
	{
		_MainTex("MainTex", 2D) = "white" {}
		_Mask("Mask", 2D) = "white" {}
		_Transparency("Transparency", Range(0, 1)) = 0
		_Color("Color", Color) = (0.4485294,0.310013,0.310013,1)
		[HideInInspector]_Cutoff("Alpha cutoff", Range(0,1)) = 0.5
	}

		SubShader
		{
			Tags
			{
				"IgnoreProjector" = "True"
				"Queue" = "Transparent"
				"RenderType" = "Transparent"
			}

			Pass
			{
				Name "FORWARD"
				Tags {"LightMode" = "ForwardBase"}
				Blend SrcAlpha OneMinusSrcAlpha
				ZWrite Off

				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#define UNITY_PASS_FORWARDBASE
				#include "UnityCG.cginc"
				#include "UnityPBSLighting.cginc"
				#include "UnityStandardBRDF.cginc"
				#pragma multi_compile_fwdbase
				#pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2 
				#pragma target 3.0

				uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
				uniform sampler2D _Mask; uniform float4 _Mask_ST;
				uniform float _Transparency;
				uniform float4 _Color;

				struct VertexInput
				{
					float4 vertex : POSITION;
					float2 texcoord0 : TEXCOORD0;
				};

				struct VertexOutput
				{
					float4 pos : SV_POSITION;
					float2 uv0 : TEXCOORD0;
				};

				VertexOutput vert(VertexInput v)
				{
					VertexOutput o = (VertexOutput)0;
					o.uv0 = v.texcoord0;
					o.pos = UnityObjectToClipPos(v.vertex);
					return o;
				}

				float4 frag(VertexOutput i) : COLOR
				{
					float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
					float3 emissive = (_Color.rgb*_MainTex_var.rgb);
					float3 finalColor = emissive;
					float4 _Mask_var = tex2D(_Mask,TRANSFORM_TEX(i.uv0, _Mask));
					return fixed4(finalColor,((lerp(lerp(lerp(_Mask_var.b, _Mask_var.r, _Mask_var.rgb.r), _Mask_var.g, _Mask_var.rgb.g), _Mask_var.b, _Mask_var.rgb.b))*_Transparency));
				}

				ENDCG
			}

		}

			FallBack "Diffuse"
					CustomEditor "ShaderForgeMaterialInspector"
}

 2:准备好格式为两个mp4的视频文件,并且提前下载安装好QuickTime,导入Unity当中,将视频文件由Videoclip改为MovieTexture

 

3:建立新的Material材质,将编写好的shader使用到材质中去,并将处理好的视频拖入当中

注意 : 上面的视频会遮盖住下面的视频

 

4:建立plane模型,X旋转90度,将建立好的材质拖到plane模型当中去,用代码控制视频的播放

5:控制代码如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovieTextureWithAlphaMask : MonoBehaviour {

    private MovieTexture mask;
    private MovieTexture mainTex;

    // Use this for initialization
    void Start()
    {
        mask = (MovieTexture)GetComponent<Renderer>().material.GetTexture("_Mask");
        mainTex = (MovieTexture)GetComponent<Renderer>().material.GetTexture("_MainTex");
        mainTex.Play();
        mainTex.loop = true;
        mask.Play();
        mask.loop = true;
    }
}

 

posted @ 2019-06-25 14:46  玄~轩逸  阅读(949)  评论(0编辑  收藏  举报