Hello Friends. I need help making a shader that acts as an alpha mask. this is the effect i want to achieve And this is how i need the shader to work. I´d really appreciate your help
It looks like you want your colour and alpha values to come from separate textures (optionally with separate offsets), and you want the result to be alpha blended?
That, and you need another mask to lerp to white before the alpha blend (I'd make it lerp a generic color);
Have you tried the TextureMask shader at the Unify Community Wiki? http://wiki.unity3d.com/index.php/TextureMask
Code (csharp): Shader "Separate Alpha Mask" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Alpha ("Alpha (A)", 2D) = "white" {} } SubShader { Tags { "RenderType" = "Transparent" "Queue" = "Transparent"} ZWrite Off Blend SrcAlpha OneMinusSrcAlpha ColorMask RGB Pass { SetTexture[_MainTex] { Combine texture } SetTexture[_Alpha] { Combine previous, texture } } } } Can you explain that in more detail? What would your lerp arguments be, and what is the purpose of the operation?
@Daniel Thanks So much man! that´s exactly what I was looking for. I´ll credit you when the game is finished and if you ever need anything design-wise just let me know
@Daniel thank you for the shader! Do you think it would be possible to modify this texture so that the base texture could also have transparency on it? It would just be a cutout.
Sorry, I don't know why I missed your questions. :-/ The original image is sort of a lightsaber effect, with white in the center of some other color.
I'm doing the same thing just instead of: Code (csharp): SetTexture[_Alpha] { Combine previous, texture } I'm using: Code (csharp): SetTexture[_Alpha] { Combine previous * texture } for my effect. One problem I'm having is that the black in the transparency texture is being multiplied to the main color texture, making the resulting texture turn dark as it is fading. Anyone have any ideas how to fix this?
hi guys i have a problem whit the shader i need to make a soft transition from the terrain to the sky, i think whit a transparency is the better way i cant control the specular color and the main color or the shyness, when i use the shader "Separate Alpha Mask" this cannot make shadows how i can add to the code the controls of the standard bumped specular ? thanks for all
So you want an alpha mask shader combined w/ built-in bumped specular? And you want shadows enabled as well, I take it. For one, as far as I'm aware, due to the way semi-transparency is rendered in Unity, shadows are not supported. As for the other steps, I'd be fine writing a shader for that, I just need clarification.
Well, for the forum's sake, here's the shader if anyone happens to stumble across this: Code (ShaderLab): Shader "Alpha Mask/Bumped Specular" { Properties { _Color ("Color", Color) = (1,1,1,1) _SpecCol ("Specular Color", Color) = (1,1,1,1) _Spec ("Specularity", Range (0,1)) = 0.8 _MainTex ("Albedo (RGB)", 2D) = "white" {} _BumpMap ("Normal Map", 2D) = "bump" {} _Mask ("Mask (A)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Spec fullforwardshadows addshadow alpha // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; sampler2D _BumpMap; sampler2D _Mask; struct Input { float2 uv_MainTex; float2 uv_BumpMap; float2 uv_Mask; }; half _Spec; fixed4 _SpecCol; fixed4 _Color; half4 LightingSpec (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) { s.Normal = normalize (s.Normal); half3 h = normalize (lightDir + viewDir); half diff = max (0, dot (s.Normal, lightDir)); float nh = max (0, dot (s.Normal, h)); float spec = pow (nh, 4096.0 * pow (s.Specular, 2)) * s.Specular * 2; half4 c; c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * _SpecCol) * atten; c.a = s.Alpha; return c; } void surf (Input IN, inout SurfaceOutput o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; o.Specular = _Spec; o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap)); o.Alpha = tex2D (_Mask, IN.uv_Mask).a; } ENDCG } FallBack "Diffuse" }
Thank you vey much!!! I was looking for this for long time I'm noob with shaders.. is it possible a VERTEX LIT or UNLIT version? I'm using this shader to hide a part of texure on a 3d model. Is it normal that the opaque part is "semi-transparent"? Thanks a lot!!!
The opaque parts of the shader should be fully opaque if the alpha of the mask is 1 at that point. If that's not the problem, due to the way unity handles transparency, some things may look a bit transparent because of the order in which they are drawn. As for unlit, it's moderately simple; you just have to return the emission output value from the surface shader. Code (ShaderLab): Shader "Alpha Mask/Unlit" { Properties { _Color ("Color", Color) = (1,1,1,1) _MainTex ("Albedo (RGB)", 2D) = "white" {} _Mask ("Mask (A)", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Lambert fullforwardshadows addshadow alpha // Use shader model 3.0 target, to get nicer looking lighting #pragma target 3.0 sampler2D _MainTex; sampler2D _Mask; struct Input { float2 uv_MainTex; float2 uv_Mask; }; fixed4 _Color; void surf (Input IN, inout SurfaceOutput o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Emission = c.rgb; o.Alpha = tex2D (_Mask, IN.uv_Mask).a; } ENDCG } FallBack "Diffuse" }
Thank you very much for unlit version you're great!! The issue with transparency of opaque part is a real problem.. basically I can see throught my character mesh.. sadly I do not really know what can I do.. any ideas? Thanks for help!
Ah, right, I know what you mean now. Unfortunately, this is just a problem with the way transparency is drawn. Because it isn't in the same order as other objects, some things may be drawn in the wrong order. There's nothing we can really do with this, apart from mucking around with z-testing, etc. which bring their own problems.
Thanks again. Here is my problem: This is with normal Unlit shader and this is with your shader using a mask only for hairs Using a double sided unlit shader could be usefull to avoid this issue? Thank a lot for help!
See my post here: http://forum.unity3d.com/threads/render-mode-transparent-doesnt-work-see-video.357853/#post-2315934 Then try this shader: Code (CSharp): Shader "Custom/Standard Two Sided Soft Blend" { Properties { _Color ("Color", Color) = (1,1,1,1) [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {} [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0 [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {} [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0 _Glossiness ("Smoothness", Range(0,1)) = 0.5 _BumpScale("Scale", Float) = 1.0 [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {} _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5 } SubShader { Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" } Blend SrcAlpha OneMinusSrcAlpha LOD 200 ZWrite Off Cull Off Pass { // Prepass depth write ColorMask 0 ZWrite On CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct v2f { float4 vertex : SV_POSITION; float2 texcoord : TEXCOORD0; }; sampler2D _MainTex; fixed _Cutoff; v2f vert (appdata_img v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.texcoord = v.texcoord; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); clip(col.a - _Cutoff); return 0; } ENDCG } Pass { Tags {"LightMode"="ShadowCaster"} ZWrite On Cull Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct v2f { V2F_SHADOW_CASTER; float2 texcoord : TEXCOORD1; }; v2f vert(appdata_base v) { v2f o; TRANSFER_SHADOW_CASTER_NORMALOFFSET(o) o.texcoord = v.texcoord; return o; } sampler2D _MainTex; fixed _Cutoff; float4 frag(v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.texcoord); clip(col.a - _Cutoff); SHADOW_CASTER_FRAGMENT(i) } ENDCG } CGPROGRAM #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap #pragma shader_feature _USEMETALLICMAP_ON #pragma target 3.0 sampler2D _MainTex; sampler2D _MetallicGlossMap; sampler2D _BumpMap; struct Input { float2 uv_MainTex; fixed facing : VFACE; }; half _Glossiness; half _Metallic; fixed4 _Color; half _BumpScale; fixed _Cutoff; void surf (Input IN, inout SurfaceOutputStandard o) { // Albedo comes from a texture tinted by color fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color; o.Albedo = c.rgb; #ifdef _USEMETALLICMAP_ON fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex); o.Metallic = mg.r; o.Smoothness = mg.a; #else o.Metallic = _Metallic; o.Smoothness = _Glossiness; #endif // Rescales the alpha on the blended pass o.Alpha = saturate(c.a / _Cutoff); o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale); o.Normal.z *= IN.facing; } ENDCG } FallBack "Diffuse" } That's a lit shader, but it kind of hacks around the issue of transparency sorting with a prepass depth write.
Hi, I have tried all the shader about mention, The masking thing appears in scene view but not in game view.
Hi, I have tried all the shader about mention, The masking thing appears in scene view but not in game view.
Hello there, I was wondering how to make position of mask based on the position of player. How do I change the input of this shader? Thanks!!!! : )
Will these shaders work for Unity 2019. I used the shader code provided. I can see it work in the scene view but nothing is visible in the game view. Can anyone tell me why that might be happenig?
Hello guys, i want to combine this alpha mask with an animated texure to simulate some kind of fire with smoother edges. Here is the animated script developed in part from bgolus, i ll'be so grateful because im trying to combine them but this throws pink texture (error) thanks in advice Code (CSharp): Shader "Unlit/Rotation" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _Alpha ("Alpha (A)", 2D) = "white" {} _Tint ("Tint", Color) = (1,1,1,1) _Brightness ("Brightness", Range(0,5)) =1 _Contrast ("Contrast", Range (0,10))=1 _RotationSpeed ("Rotation Speed", Float) = 2.0 } SubShader { Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } LOD 100 ZWrite Off Blend SrcAlpha OneMinusSrcAlpha ColorMask RGB Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fog #include "UnityCG.cginc" uniform float4 _Tint; uniform float _Contrast; uniform float _Brightness; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; struct Input { float2 uv_MainTex; }; float _RotationSpeed; v2f vert (appdata v) { v.uv.xy -=0.5; v2f o; o.vertex = mul(UNITY_MATRIX_MVP, float4(v.vertex)); o.uv = TRANSFORM_TEX(v.uv, _MainTex); UNITY_TRANSFER_FOG(o,o.vertex); float s = sin ( _RotationSpeed * _Time ); float c = cos ( _RotationSpeed * _Time ); float2x2 rotationMatrix = float2x2( c, -s, s, c); rotationMatrix *=0.5; rotationMatrix +=0.5; rotationMatrix = rotationMatrix * 2-1; o.uv.xy = mul ( o.uv.xy, rotationMatrix ); o.uv.xy += 0.5; return o; } fixed4 frag (v2f i) : SV_Target { // sample the texture fixed4 col = pow (tex2D(_MainTex, i.uv) * _Tint, _Contrast) * _Brightness; // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col; } ENDCG } } }