- Home /
Movie GUI Texture Alpha?
I've searched but never found a real solution on how to get a GUI movie in unity to have an Alpha matte applied. Does anyone know a easy way to do this? Right now my movie is being created by:
public MovieTexture Loading_Movie;
void OnGUI()
{
GUI.DrawTexture(new Rect(295, 200, 147, 143), Loading_Movie, ScaleMode.ScaleToFit, true, 0F);
Loading_Movie.Play();
Loading_Movie.loop = true;
}
I created a separate movie that is a grayscale for an Alpha, but not sure how to apply this to the code so it makes my "Loading_Movie" have an Alpha.
So basically I have 2 movies. One is an RGB and is currently on my "Loading_Movie" public movieTexture. The second which is the grayscale for Alpha is not currently being called because Im not sure how to make it treat my RGB version as the alpha. Any suggestions?
Answer by simon.broggi · Aug 21, 2013 at 02:28 PM
Create a GUITexture and attach this script (uses Graphics.Blit which requires Unity Pro):
using UnityEngine;
using System.Collections;
public class GUIVideoWithAlpha : MonoBehaviour {
private Texture colorTexture;
private Texture matteTexture;
public Shader blitShader;
private Material blitMat;
void Start () {
//make the gui texture a render texture
guiTexture.texture = new RenderTexture(colorTexture.width, colorTexture.height, 16, RenderTextureFormat.ARGB32);
blitMat = new Material(blitShader);
blitMat.SetTexture("_AlphaTex", matteTexture);
if(colorTexture is MovieTexture){
(colorTexture as MovieTexture).Play();
}
if(matteTexture is MovieTexture){
(matteTexture as MovieTexture).Play();
}
}
void Update () {
if(guiTexture.enabled){
Graphics.Blit(colorTexture, (RenderTexture)guiTexture.texture, blitMat);
}
}
}
Drop your video into the Color Texture, and the alpha matte into Mate Texture. Use this as the blitShader:
Shader "Alpha Matte" {
Properties {
_MainTex ("Base (RGB)", Rect) = "white" {}
_AlphaTex ("Base (RGB)", Rect) = "white" {}
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _AlphaTex;
fixed4 frag (v2f_img i) : COLOR
{
fixed4 output = tex2D(_MainTex, i.uv);
fixed4 alpha = tex2D(_AlphaTex, i.uv);
output.a = Luminance(alpha.rgb);
return output;
}
ENDCG
}
}
Fallback off
}
Hi, I would like to also know this. I tried the above solution but I get an 'InvalidCastException' when casting the Texture to a RenderTexture!?
Woops, sorry. You need to assign a new render texture to the gui texture. I just added the missing line to the code in the start function. Hope it works.
Your answer
Follow this Question
Related Questions
Help with destroying guiRect? 0 Answers
Move Texture From Plane To GUI And Back 0 Answers
Fading a GUI.DrawTexture? 2 Answers
Alpha cutoff for a GUI Texture? 2 Answers
Make a movie texture rewind (play again) 3 Answers