Hello, I am making a game that involves a character going into stealth. When in stealth, the character is semi-transparent, but is undetected by AI, yada, yada, yada. I was having problems with Unity's transparent shader, so I looked around and found a great shader from the community at: http://www.unifycommunity.com/wiki/index.php?title=AlphaVertexLitZ Unfortunately, I am having two problems: 1) This shader does not take Unity's Depth of Field camera shader into account. 2) At times, my character's hood is more transparent than my character's face (depending on the angle), which allows the player to see my character's face. I have attached two images that better describe my situation. If anyone knows how to fix this, I am willing to pay for the help. Thank you in advance!
Do you have multiple meshes/submeshes? If not, it can only be because of ColorMask 0 not working, which a couple people told me was a problem, on Windows – I don't know if this bug has been fixed yet. If so, then put your hood into an earlier queue than your head. Also, this method is inefficient if you're just going to have the same alpha value over the whole mesh, like the wiki shows (not the best example). Do you actually need the alpha channel UV mapped, or will a single value suffice? Unfortunately, I don't know how to help you with depth of field.
Hey Jessy, thanks for responding! I do have multiple meshes -- I believe 4: cape, body, face, swords. I must admit I am a tad bit lost, but I think I will only need one single value. The entire character just needs to be semi-transparent -- I'm not trying to do anything *too* fancy! Out of curiosity, would you (or anyone else) know if it's Unity's D.O.F. shader that would need tweaking, or the transparent shader that would need tweaking? Thanks for all the help. I sincerely appreciate you taking your time to help out!
In that case, you need to use two materials on each. It shouldn't be any slower; you'll still use the same number of passes; you just need to utilize different queues, and a shader can only be in one. Make a single material from this shader, and use that material on each of the meshes: Code (csharp): Shader "Transparent/VertexLit with Z/ZWrite" { SubShader { Tags {"Queue" = "Transparent-1"} Pass {ColorMask 0} } } Then, make a different material for each mesh, using this shader: Code (csharp): Shader "Transparent/VertexLit with Z/RGB" { Properties { _Color ("Color (A = Opacity)", Color) = (1,1,1,1) _MainTex ("Texture", 2D) = "white" } SubShader { Tags {"RenderType"="Transparent" Queue = Transparent} ZWrite Off Blend SrcAlpha OneMinusSrcAlpha ColorMask RGB Lighting On Material { Diffuse [_Color] Ambient [_Color] } Pass { SetTexture [_MainTex] {Combine texture * primary Double, primary} } } } I also modified the second shader to use the alpha of "Color". Just use an RGB texture now instead of texture alpha. I'm sure somebody else might.
Wow! Thank you SO much, Jessy. You really helped me out there. I definitely owe you one! If anyone else has a clue regarding the D.O.F. shader, feel free to chime in Again, thank you a lot, Jessy. That was very, very, very helpful!
I was browsing through the DepthOfField.js script I am using for my camera (Unity Pro only), and I came across this: // we use the alpha channel for storing the COC // which also means, that unfortunately, alpha based // image effects such as sun shafts, bloom or glow // won't work if placed *after* the DOF image effect Does this mean that my alpha shader is happening AFTER the DepthOfField script is run? If so, is there a way to change this?
I don't know. Find out if depth of field has anything to do with rendering queues or the alpha already written to the screen. We can easily modify those shaders to deal with either.
The depth of field shader uses the Z buffer to determine what to blur. Because it is a postprocess, it can only "see" the front-most depth for any given pixel. Because your character is drawn with Z writing enabled, those pixels are considered in focus. If your character did not write to the Z buffer, his pixels would be considered part of the background, and blurred heavily. What you want in this situation is actually to composite your character after the DOF effect. This would mean writing an image effect that came afterwards, and rendered your character in a separate buffer. That would probably work quite well, assuming your character is always in focus, but it means learning about image effects and compositing with shaders.
Wow, that sounds a little too complex for me (as I said before, I barely know syntax for shaders!) Jessy, is there a way to combine the Vertex Lit with RGB with a bump map? My character has a normal map for his armor, which I would like to use. If it is complicated, though, then I'd rather not fix what is not broken. Thanks for the help guys!
hey gamesurgeon, maybe this shader can help you. It's a Diff-Tex, NormMap, Alpha-Slider, pretty easy. Please give a response, if it fits your need - i'm quite new into coding. But for me this one helped. Would be great to hear if it works for chars as well.
Thanks a bunch MarkPixel! That works! I just need to adjust it to work with my code. Again, thank you very much!
I think my situation falls pretty much into this topic. Could you guys please take a look to this post on Unity Answers? http://answers.unity3d.com/questions/438556/my-shader-brakes-depth-of-field.html Basically I have some objects with UV alpha (it has to be so) and I need depth of field on the full scene. Find more details in the post. Any help is very much appreciated!! Thank you
You sort of answered your own question in the post there; ZWrite Off That means your shader doesn't write to your zbuffer, so Unity won't know it's in the Z buffer in order to blur it. You can try adding a pass before (or after - if you want to see bits of your character through other bits of your character) your current pass that reads; Code (csharp): Pass { ColorMask 0 } That pass will write to the ZBuffer but nothing else. Then change the Tags { "RenderType"="Transparent" "Queue"="Transparent" } to Tags { "RenderType"="TransparentCutout" "Queue"="Transparent" }
Hi Farfarer, thank you very much for your answer. Unfortunately the Cutout doesn't work for me. I really need the alpha to be smooth and based on UV mapping. I tried also to add the pass you suggest but I had really weird "shuttered" transparencies. The shader I shared in the post is not the only one doing this. Most of the Transparent built in shaders do the same thing. Any futher ideas?
Anything transparent will not write to the ZBuffer and therefore will not be blurred by DOF accurately as it uses the depth buffer to do so. Does the Unity DOF use a replacement shader for that stuff? If so, you might need to add your own passes to it to render things correctly.