Unity plays video with Alpha channel (Unity play channel movie)

Source: Internet
Author: User

Recently discussed in unity to play with the alpha channel video, at the beginning of a little effect, and then search for various solutions, summed up three kinds of less good solutions, there is a better plan for everyone to put forward.

The programme focuses on two areas:

1. Can I play a video with an alpha channel?

2. Hierarchical relationship of video and three-dimensional scenes played


1. CullingUse a stripped-out shader without rendering the black partThe shader code is as follows:
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 Offalpha Test less[_cutoff]tags{"Queue" = "Transparent-20" "ignoreprojector" = "True" "rendertype" = "Transparent"}lod 300CGPROGR Am#pragma Surface surf Lambert alphatest:_cutoffsampler2d _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.u V_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"}
Write a C # script that plays the video as follows:
Using unityengine;using System.collections;public class moviecontrol:monobehaviour{public    movietexture movie;    Public AudioClip Moviesound;    void Start ()    {        Play ();    }    public void Play ()    {        if (movie! = null)        {            Movietexture temp = (movietexture) gameobject.getcomponent <MeshRenderer> (). sharedmaterial.maintexture;            if (temp! = null)            {                temp. Stop ();            }            Gameobject.getcomponent<meshrenderer> (). sharedmaterial.maintexture = movie;            Movie. Play ();            if (moviesound! = null)            {                getcomponent<audiosource> (). clip = Moviesound;                Getcomponent<audiosource> (). Play ();}}    }    public void Stop ()    {        movie. Stop ();    }}
In the scene of the panel before and after the resume two small squares, test video and 3d order problem: Effectthe first scheme satisfies condition 2, and the effect of condition 1 is not good. 2.shader Overlay MatteThis scenario requires two video overlays, here is the test video downloaded on the web, and the video and mask graphs are as follows:  the shader used are as follows:
Shader "Alphavideo/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 oneminussrcalphazwrite Offcgprogram#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.0uniform sampler2d _maintex; Uniform float4 _maintex_st;uniform sampler2d _mask; Uniform float4 _mask_st;uniform float _transparency;uniform float4 _color;struct vertexinput {float4 VERTEX:POSITION;FL Oat2 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 = Mul (UNITY_MATRIX_MVP, v.ve Rtex); 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.RGB.G), _mask_var.b, _mask_var.rgb.b)) (*_transparency));} Endcg}}fallback "Diffuse" Customeditor "Shaderforgematerialinspector"}

Achieve results

This effect can meet both conditions 1 and 2, but to two video, it is troublesome, do not know if there is no big God know other solutions, masks into pictures also line.
3.unity comes with a shaderfirst, with Untiy's fx/flare shader, two video playback effects  found to meet condition 1, unable to meet condition 2, and then found too bright
then unity comes with the shader particles/addtive Effect brightness can be lowered down, lost the original color of the video, which is also drunk, so, friends have other better solutions to welcome the exchange.
Project Engineering Files directory: Movietexturealpha

Unity plays video with Alpha channel (Unity play channel movie)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.