Movie Texture Blending

When writing a shader, for some reason if I try to blend using a MovieTexture, the blending gets ignored. I'm trying to use one movie as an alpha channel to another movie.

I've tried all the built in shaders Unity comes with, but none seem to work properly with MovieTextures. Is this even possible?

Is there a shader out there that's known to blend movie textures properly?

Thanks for heading us in the right direction Kevin. Now, there's a little drawback in your implementation, it uses the same UV for both images, which might not be what everyone needs. I've especially noticed that if you use a regular image as the main texture and a movie texture/quicktime for the alpha texture (great for custom transitions, you can have the alpha QT at a much lower resolution), they do NOT coincide because movie textures apparently use just the bottom left corner of the UV space. To remedy that, I had to make a custom object with two UV sets, the second taking double the size of the first and anchored at the bottom left corner (0,0 in UV space). That plus the fixed script did the trick. Here it goes:

Shader "Particles/SplitAlpha"
{ 
Properties 
{ 
    _MainTex ("Base (RGB)", 2D) = "white" {}
    _Mask ("Culling Mask", 2D) = "white" {}
    _Cutoff ("Cutoff", Range (0,1)) = .5 
} 

SubShader 
{
    Tags {"Queue"="Transparent"}

    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass 
    {    
        CGPROGRAM 
            #pragma fragment frag 
            #include "UnityCG.cginc"  

            sampler2D _MainTex; 
            sampler2D _Mask; 

            struct v2f 
            { 
                float4 pos : POSITION; 
                float4 uv : TEXCOORD0;
                float4 uv2 : TEXCOORD1;                 
            }; 

            half4 frag (v2f i) : COLOR 
            { 
                half4 color = tex2D(_MainTex, i.uv.xy); 
                half4 color2 = tex2D(_Mask, i.uv2.xy); 

                return half4(color.r, color.g, color.b, color2.r); 
            } 
        ENDCG         
    }    
}

Fallback "Transparent/Diffuse" 
}

Now, as for actually playing the quicktime placed in the _mask texture, here's the js you need attached to your object:

var mask;
function Start () {
    mask = renderer.material.GetTexture ("_Mask");
}

function Update () {
    if (Input.GetButtonDown ("Jump")) {
            mask.Play();
    }
}

GLHF! :)

After alot of digging and experimentation with other people's examples, I came up with a shader that seems to work.

The solution involved writing a fragment program. I'm not sure if I'm taking a performance hit by doing it this way, and I'm not sure that it's really impossible to do this without one.

Shader "MovieWithSeperateAlpha" 
{ 
    Properties 
    { 
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _Mask ("Culling Mask", 2D) = "white" {}
        _Cutoff ("Cutoff", Range (0,1)) = .5 
    } 

    SubShader 
    {
        Tags {"Queue"="Transparent"}

        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass 
        {    
            CGPROGRAM 
                #pragma fragment frag 
                #include "UnityCG.cginc"  

                sampler2D _MainTex; 
                sampler2D _Mask; 

                struct v2f 
                { 
                    float4 pos : POSITION; 
                    float4 uv : TEXCOORD0; 
                }; 

                half4 frag (v2f i) : COLOR 
                { 
                    half4 color = tex2D(_MainTex, i.uv.xy); 
                    half4 color2 = tex2D(_Mask, i.uv.xy); 

                    return half4(color.r, color.g, color.b, color2.r); 
                } 
            ENDCG         
        }    
    }

    Fallback "Transparent/Diffuse" 
}

I made this shader in shaderforge works pretty well

// Shader created with Shader Forge v1.21 
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge

Shader "0_CD/VideoMaskedColored" {
    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 = mul(UNITY_MATRIX_MVP, v.vertex );
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
/////// Vectors:
////// Lighting:
////// Emissive:
                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"
}

Thanks for the solution. Nevertheless the two movies do not always run perfectly synchronized. So I tried to have one movie only that includes both: it is double height, has the normal video on top and the grayscale video for the mask on bottom. The material uses this one movie for both textures, while the tilings and offsets are adapted. But for the culling mask the tiling and offset values are ignored - it looks like I need to adapt the shader, but sadly I have no idea about shader scripting. Any help?

“it does’nt work with CSharp. Do it in Javascript it will work ;)”

You can do it in C#. For playing the movie texture and the movie mask just drop this script onto the same object that uses the shader above:

public class MovieTextureWithAlphaMask: MonoBehaviour {

  private MovieTexture mask;
  private MovieTexture mainTex;

void Start () {
mask = (MovieTexture)renderer.material.GetTexture(“_Mask”);
mainTex = (MovieTexture)renderer.material.GetTexture(“_MainTex”);
}

void Update () {
if (Input.GetButtonDown(“Jump”))
{
mainTex.Play();
mask.Play();
}
}
}

One thing I noticed, however, is that there seems to be a tiny delay between the movie and the mask so they may not line up perfectly when they are played.

Anyone with a sample project file? I tried the script but get total transparent material even in preview within Unity Inspector. I use After Effects to output the Alpha quicktime in H264.

Just to confirm in case it is reverse, White is to show and Black is transparent, right?