最近讨论在unity播放带alpha通道的视频,一开始一点效果也没有,然后搜索各种解决方案,总结出三种不太好的方案,有更好的方案的希望大家提出来。
方案重点两个方面:
1.能否播放带alpha通道的视频
2.播放的视频和三维场景的层级关系
1.剔除
使用一个剔除的shader,不渲染黑色部分
shader代码如下:
Shader "AlphaVideo/CullingVideo"
{
Properties
{
_MainTex("Base (RGB)", 2D) = "white" {}
_AlphaOffsetX("alpha offset x", float) = 0.5
_AlphaOffsetY("alpha offset y", float) = 0
_Cutoff("Cutoff", Range(-1,0)) = -0.3
}
SubShader
{
//Lighting Off
AlphaTest Less[_Cutoff]
Tags{ "Queue" = "Transparent-20" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
LOD 300
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainTex;
float _AlphaOffsetX;
float _AlphaOffsetY;
struct Input
{
float2 uv_MainTex;
};
void surf(Input IN, inout SurfaceOutput o)
{
half4 c = tex2D(_MainTex, IN.uv_MainTex);
IN.uv_MainTex.x += _AlphaOffsetX;
IN.uv_MainTex.y += _AlphaOffsetY;
half4 d = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = ((d.r*-1) + 1)*-1;
}
ENDCG
}
// FallBack "Diffuse"
FallBack "Transparent/Diffuse"
}
编写一个播放视频的C#脚本如下:
using UnityEngine;
using Sy