Hi, new here. Working on a game for class. Were trying to overlay video onto our game as a sort of meta game element. I pulled some shader code from this http://forum.unity3d.com/viewtopic.php?t=12228&highlight=colbert+green+screen+video thread to do basic binary chroma keying. It works but I only get boolean values of alpha out of it. By that I mean return half4(color.r, color.g, color.b, .49595); turns things complete opaque whereas return half4(color.r, color.g, color.b, .5); turns things completely transparent. I'd like a smooth alpha value based on the amount of blueness in each pixel. Is there any way to do this? I'm a complete shader noob but good at coding in general. Thanks!
I'm applying the shader to a video texture which afaik does not support alpha channels correct me if I'm wrong.
Yeah, they do. Most videos don't use them, so most players don't play them. Alpha video channels are used all the time for video compositing, and of course special effects in games.
Okay thanks, I'll give that a try. For general reference though, I'm still curious as to how to get the full range of alpha values from a shader.
so I did some research and according to this thread http://answers.unity3d.com/questions/1600/how-do-i-use-movie-texture-with-alpha-channel unity does not support video alpha. btw I did try and export video with alpha and confirmed that it did not work. Of course it is possible I just did not export it right. I could use a second video as alpha channel as suggested in the video but that involves writing a shader as well (which brings me back to the same problem as before). In any case, that's still less than desirable as I'd much rather do the keying process directly in unity with a shader to save time.
Okay seems like I resolved this problem. The shader I copied was using alpha testing instead of alpha blending. I'm still figuring out how shaderlab works together with cg but its slowly starting to make sense. Thanks!
I've messed about with matting shaders before. The video + mask video route goes too slowly since theres two video files playing, might depend on your video size though. I've got another matting shader that does a simple color threshold and depending on your video it could make the matting look nicer. If you manage to make a matte shader this good please post your code in the wiki! Anyway, here's the matte shader with a simple threshold: Code (csharp): Shader "Matte/RGB Color" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Matte ("Matte Color", Color) = (1,0,1,1) _Thresh ("Threshold", Range (0,1)) = 0.0 _Cutoff ("Cutoff", Range (0,1)) = .5 } SubShader { Pass { AlphaTest Less [_Cutoff] CGPROGRAM #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 pos : POSITION; float4 uv : TEXCOORD0; }; sampler2D _MainTex; uniform half4 _Matte; uniform float _Thresh; uniform float _Cutoff; half4 frag (v2f i) : COLOR { //return _Matte; half4 color = tex2D(_MainTex, i.uv.xy); half3 keyMinus = half3( (_Matte.r - _Thresh), (_Matte.g - _Thresh), (_Matte.b - _Thresh) ); half3 keyPlus = half3( (_Matte.r + _Thresh), (_Matte.g + _Thresh), (_Matte.b + _Thresh) ); if ( ((color.r > keyMinus.r) (color.r < keyPlus.r)) ((color.g > keyMinus.g) (color.g < keyPlus.g)) ((color.b > keyMinus.b) (color.b < keyPlus.b)) ) { return _Cutoff; } return float4(color.rgb, max(0, _Cutoff-0.01)); } ENDCG } // pass } // subshader //Fallback "Transparent/Diffuse" } // shader
Hello... I really need to display a video with transparency into my Unity project, could you please post a sample project doing that ? I don't know how to use the shaders you are talking about Thanks a lot BloodArk
Double image with Alpha for Chroma Key in Unity 3D Hello, Maybe here is a trick I imagined, that could be usefull... it is not really chroma key, but it works fine... Make a movie with both, the foreground and the alpha key image on the same picture with After Effects or other compositing soft. Import this movie in Unity3D create a shader with the script written by LouLou from the french forum http://www.unity3d-france.com http://www.unity3d-france.com/unity/phpBB3/viewtopic.php?f=8&t=901 Apply this shader to a material, and adjust the Tile and offset of the texture of this material to hide the white alpha image. Code (csharp): Shader "Custom/VideoAlpha" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _AlphaOffsetX ("alpha offset x", float) = 0.5 _AlphaOffsetY ("alpha offset y", float) = 0 _Cutoff ("Cutoff", Range (0,1)) = .5 } SubShader { AlphaTest Less [_Cutoff] CGPROGRAM #pragma surface surf Lambert 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; } ENDCG } FallBack "Diffuse" } It works fine, it is much more better than hiding the green by script. A 600 pixel high sequence is fine. If you want to test here are some demo sequences to test http://www.noomuseum.net/mov/YannSteadyDoubleAlpha_674.mov (the .ogv file for Unity http://www.noomuseum.net/mov/YannSteadyDoubleAlpha_674.ogv Model Soizic Hess http://soizic-hess.com/ http://www.noomuseum.net/mov/SoizicDoubleAlpha-674.mov (.ogv file for Unity http://www.noomuseum.net/mov/SoizicRotatekey-2-800ogv.ogv 1) How could it be possible to make this texture "Unlit" ? does someone have the answer ? 2) Is it possible to do the same, but with a video movie store in a web server, or in a local computer, because the videos are really too heavy to put inside the project himself... Yes, just put this video player script in the screen object, and put a similar jpg image from the movie in the material window. Code (csharp): private var url = "http://www.noomuseum.net/mov/YannSteadyDoubleAlpha_674.ogv"; // Lien de la video public var object:GameObject; // Objet sur lequel doit être afficher la video function Start () { var www = new WWW(url); var movieTexture = www.movie; while (!movieTexture.isReadyToPlay) yield; object.renderer.material.mainTexture = movieTexture; audio.clip = movieTexture.audioClip; movieTexture.Play(); movieTexture.Loop = true; audio.Play(); } @script RequireComponent (AudioSource) If you want to store the movie on a local URL on your Mac or PC, replace the URL address in the script by that kind of address : file:///Users/YannSteadyDoubleAlpha_674.ogv Here a level I have done with two video inside.. [/url] avec deux videos incrustées à l'intérieur en démo. (les vidéos sont des .ogv hébergés sur le web.) http://www.cyberesthesie.com/cyberesthesia-3d/Cyberesthesia.html it is a multiplayer test, click "Heberger" and "entrez dans les Noobunkers" Sorry, it is a heavy level.. Yann, NooKeyer
If i understand your question right, you want to make a mask selectively based on a defined color right? Something like this will do: Code (csharp): Shader "Hidden/Aubergine/Mask" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _MaskCol ("Mask Color", Color) = (1.0, 0.0, 0.0, 1.0) } SubShader { CGINCLUDE #include "UnityCG.cginc" half3 NormalizeColor (half3 color) { //return color / max(dot(color, half3(1.0f/3.0f)), 0.0001); return color / dot(color, fixed3(0.22, 0.707, 0.071)); } half4 MaskColor (half3 mCol, half3 cCol) { half4 d = distance(NormalizeColor(mCol.rgb), NormalizeColor(cCol.rgb)); return (d > 1f) ? half4(0.0) : half4(1.0); } ENDCG Pass { ZTest Always Cull Off ZWrite Off Lighting Off Fog { Mode off } CGPROGRAM #pragma vertex vert_img #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest uniform sampler2D _MainTex; uniform float4 _MaskCol; half4 frag (v2f_img i) : COLOR { half4 col = tex2D(_MainTex, i.uv); half4 mask = MaskColor(col.rgb, _MaskCol.rgb); //return col * mask; return mask; } ENDCG } } Fallback off }
Hello Aubergine, thanks very much for your shader script, Iam sorry, I just try it, but I don't understand how it works... I have made a Shader with it, and use this shader on my video material.. but nothing happen... But it doesn't matter, because, as I explained just before I found a way to solve this problem of chroma key in unity... Using scripts to make a chroma key in Unity by using a color like green as mask is not fine, because they are green pixels around the foreground image... (very ugly) The best way, I found to make a "chroma key" effect, is to use the old video art technic of making the chromakey with a powerful device, like Ultimate, or Soft like After Effect, specialy built to generate a special video file, with the foreground image and a white mask. Loulou, from the french unity forum, wrote a script that works fine.. Thanks very much... Yann
It is a full screen image effect, you define the mask color and a threshold (its hard coded in there as return (d > 1f), 1 is the threshold here) then if any color is near or close to the value you defined comes out as white and any other colors come out as black and you get your mask After that you can do whatever you want with that mask. Anyhow, its not like using after effects offcourse.
Hey there! We have just released our new Chroma System for Unity. You can check this thread for further details. If you're interested in chroma key features in Unity you have to check it out! Thanks!
Hello I appreciate your work aubergine, the shader you provided works great for me but it only works on windows build can you help me how can i get it to work on mobile. when i try to use it on android it doesnt work. Can you please help me with this
This is great! Thank you very much. Can anyone tell me what to change in order to make this shader use alpha blending instead of alpha testing?
Sorry to bump up this old thread. Just wonder if this script work with current version of Unity? Coz I cant seem to get it to work. Any help would be appreciated. Thanks in advance. edit: Eventually it doesn't works on Android. Just too bad.
I know that i'm a little late but maybe someone could be interested. You just need to modify line 26 in your shader script from "o.Albedo = c.rgb;" to "o.Emission = c.rgb;" Hope this could help someone.
I'm using it with the new Unity 4.5 (Pro) and it seems to work fine. I think "Pro" is required because it use MovieTexture to play the movie. On Android it couldn't work because MovieTexture is not supported on that platform (nor iOS). If you need to reproduce movie on mobile you can check out this plugin: http://u3d.as/content/defiant-development-pty-ltd/mobile-movie-texture/2w1
This shader of mine http://u3d.as/6A4 does the job. It works in android, but only with images, since MovieTexture is not supported. Cheers.
This is an old thread but this script does the job : http://forum.unity3d.com/threads/chroma-key-in-unity-5.359119/ it works on mobile and with video . Just key out the color you don't want.
@BabilinApps how do you use it in mobile? what class do you use instead of MovieTexture? I have working the plane with custom shader for alpha Movie Texture, everything ok, but it fails when tryied to deploy to Mobile: "error CS0246: The type or namespace name `MovieTexture' could not be found. Are you missing a using directive or an assembly reference?"
The shader is for alpha clipping for green screen. It doesn't play a video by itself. You can use plugins like https://www.assetstore.unity3d.com/en/#!/content/10032 to render the video on mobile.
More info here: http://docs.zerodensity.tv/reality25/getting-started/other-control-applications/keyer-control Image based keyer: http://docs.zerodensity.tv/reality25/appendices/appendix-d-reality-keyer/what-is-image-based-keying http://docs.zerodensity.tv/reality25/appendices/appendix-d-reality-keyer/reality-keyer-pipeline