Search Unity

Alpha Mask shader Help

Discussion in 'Shaders' started by Kobu_HKS, May 9, 2013.

  1. Kobu_HKS

    Kobu_HKS

    Joined:
    May 9, 2013
    Posts:
    2
    Hello Friends. I need help making a shader that acts as an alpha mask.

    this is the effect i want to achieve
    $Glow2.gif

    And this is how i need the shader to work.

    $Example2.jpg

    I´d really appreciate your help :D
     

    Attached Files:

    Last edited: May 9, 2013
    Xepherys likes this.
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    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?
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That, and you need another mask to lerp to white before the alpha blend (I'd make it lerp a generic color);
     
  4. Jessespike

    Jessespike

    Joined:
    Jul 9, 2012
    Posts:
    44
  5. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Code (csharp):
    1. Shader "Separate Alpha Mask" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Alpha ("Alpha (A)", 2D) = "white" {}
    5.     }
    6.     SubShader {
    7.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent"}
    8.        
    9.         ZWrite Off
    10.        
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         ColorMask RGB
    13.        
    14.         Pass {
    15.             SetTexture[_MainTex] {
    16.                 Combine texture
    17.             }
    18.             SetTexture[_Alpha] {
    19.                 Combine previous, texture
    20.             }
    21.         }
    22.     }
    23. }
    Can you explain that in more detail? What would your lerp arguments be, and what is the purpose of the operation?
     
    Subcursion, Fajlworks and Rhandros like this.
  6. Kobu_HKS

    Kobu_HKS

    Joined:
    May 9, 2013
    Posts:
    2
    @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 :D
     
  7. redthrawn

    redthrawn

    Joined:
    May 8, 2013
    Posts:
    27
    @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.
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    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.
     
  9. redthrawn

    redthrawn

    Joined:
    May 8, 2013
    Posts:
    27
    I'm doing the same thing just instead of:

    Code (csharp):
    1.  
    2. SetTexture[_Alpha] {
    3.     Combine previous, texture
    4. }
    5.  
    I'm using:

    Code (csharp):
    1.  
    2. SetTexture[_Alpha] {
    3.     Combine previous * texture
    4. }
    5.  
    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?
     
    Rhandros likes this.
  10. sata3d

    sata3d

    Joined:
    Apr 3, 2014
    Posts:
    2
    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


    texture mask problem.jpg



    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



    texture mask problem shading.jpg


    how i can add to the code the controls of the standard bumped specular ?

    texture mask problem controls.jpg

    thanks for all :)
     
    Last edited: Feb 12, 2015
  11. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    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.
     
  12. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    Well, for the forum's sake, here's the shader if anyone happens to stumble across this:
    Code (ShaderLab):
    1. Shader "Alpha Mask/Bumped Specular" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _SpecCol ("Specular Color", Color) = (1,1,1,1)
    5.         _Spec ("Specularity", Range (0,1)) = 0.8
    6.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         _BumpMap ("Normal Map", 2D) = "bump" {}
    8.         _Mask ("Mask (A)", 2D) = "white" {}
    9.     }
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 200
    13.        
    14.         CGPROGRAM
    15.         #pragma surface surf Spec fullforwardshadows addshadow alpha
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.         sampler2D _BumpMap;
    22.         sampler2D _Mask;
    23.  
    24.         struct Input {
    25.             float2 uv_MainTex;
    26.             float2 uv_BumpMap;
    27.             float2 uv_Mask;
    28.         };
    29.  
    30.         half _Spec;
    31.         fixed4 _SpecCol;
    32.         fixed4 _Color;
    33.  
    34.         half4 LightingSpec (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
    35.             s.Normal = normalize (s.Normal);
    36.  
    37.             half3 h = normalize (lightDir + viewDir);
    38.  
    39.             half diff = max (0, dot (s.Normal, lightDir));
    40.  
    41.             float nh = max (0, dot (s.Normal, h));
    42.             float spec = pow (nh, 4096.0 * pow (s.Specular, 2)) * s.Specular * 2;
    43.  
    44.             half4 c;
    45.             c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec * _SpecCol) * atten;
    46.             c.a = s.Alpha;
    47.             return c;
    48.         }
    49.  
    50.         void surf (Input IN, inout SurfaceOutput o) {
    51.             // Albedo comes from a texture tinted by color
    52.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    53.             o.Albedo = c.rgb;
    54.             o.Specular = _Spec;
    55.             o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
    56.             o.Alpha = tex2D (_Mask, IN.uv_Mask).a;
    57.         }
    58.         ENDCG
    59.     }
    60.     FallBack "Diffuse"
    61. }
    62.  
     
    kerethros and porandojin1 like this.
  13. twiggyash

    twiggyash

    Joined:
    Nov 7, 2013
    Posts:
    45
    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!!!
     
    Last edited: Apr 30, 2016
  14. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    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):
    1. Shader "Alpha Mask/Unlit" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Mask ("Mask (A)", 2D) = "white" {}
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Opaque" }
    9.         LOD 200
    10.      
    11.         CGPROGRAM
    12.         #pragma surface surf Lambert fullforwardshadows addshadow alpha
    13.         // Use shader model 3.0 target, to get nicer looking lighting
    14.         #pragma target 3.0
    15.         sampler2D _MainTex;
    16.         sampler2D _Mask;
    17.         struct Input {
    18.             float2 uv_MainTex;
    19.             float2 uv_Mask;
    20.         };
    21.         fixed4 _Color;
    22.         void surf (Input IN, inout SurfaceOutput o) {
    23.             // Albedo comes from a texture tinted by color
    24.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    25.             o.Emission = c.rgb;
    26.             o.Alpha = tex2D (_Mask, IN.uv_Mask).a;
    27.         }
    28.         ENDCG
    29.     }
    30.     FallBack "Diffuse"
    31. }
     
    Grakou likes this.
  15. twiggyash

    twiggyash

    Joined:
    Nov 7, 2013
    Posts:
    45
    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!
     
  16. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    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.
     
  17. twiggyash

    twiggyash

    Joined:
    Nov 7, 2013
    Posts:
    45
    Thanks again. Here is my problem:

    This is with normal Unlit shader
    ScreenUnlit.png

    and this is with your shader using a mask only for hairs
    ScreenAlphaMaskUnlit.png

    Using a double sided unlit shader could be usefull to avoid this issue?
    Thank a lot for help!
     
  18. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    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):
    1. Shader "Custom/Standard Two Sided Soft Blend" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         [NoScaleOffset] _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         [Toggle] _UseMetallicMap ("Use Metallic Map", Float) = 0.0
    6.         [NoScaleOffset] _MetallicGlossMap("Metallic", 2D) = "black" {}
    7.         [Gamma] _Metallic ("Metallic", Range(0,1)) = 0.0
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _BumpScale("Scale", Float) = 1.0
    10.         [NoScaleOffset] _BumpMap("Normal Map", 2D) = "bump" {}
    11.         _Cutoff("Alpha Cutoff", Range(0.01,1)) = 0.5
    12.     }
    13.     SubShader {
    14.         Tags { "Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout" }
    15.         Blend SrcAlpha OneMinusSrcAlpha
    16.         LOD 200
    17.         ZWrite Off
    18.         Cull Off
    19.         Pass { // Prepass depth write
    20.             ColorMask 0
    21.             ZWrite On
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.      
    26.             #include "UnityCG.cginc"
    27.             struct v2f {
    28.                 float4 vertex : SV_POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.             };
    31.             sampler2D _MainTex;
    32.             fixed _Cutoff;
    33.             v2f vert (appdata_img v)
    34.             {
    35.                 v2f o;
    36.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    37.                 o.texcoord = v.texcoord;
    38.                 return o;
    39.             }
    40.             fixed4 frag (v2f i) : SV_Target
    41.             {
    42.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    43.                 clip(col.a - _Cutoff);
    44.                 return 0;
    45.             }
    46.             ENDCG
    47.         }
    48.         Pass {
    49.             Tags {"LightMode"="ShadowCaster"}
    50.             ZWrite On
    51.             Cull Off
    52.             CGPROGRAM
    53.             #pragma vertex vert
    54.             #pragma fragment frag
    55.             #pragma multi_compile_shadowcaster
    56.             #include "UnityCG.cginc"
    57.             struct v2f {
    58.                 V2F_SHADOW_CASTER;
    59.                 float2 texcoord : TEXCOORD1;
    60.             };
    61.             v2f vert(appdata_base v)
    62.             {
    63.                 v2f o;
    64.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    65.                 o.texcoord = v.texcoord;
    66.                 return o;
    67.             }
    68.      
    69.             sampler2D _MainTex;
    70.             fixed _Cutoff;
    71.             float4 frag(v2f i) : SV_Target
    72.             {
    73.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    74.                 clip(col.a - _Cutoff);
    75.                 SHADOW_CASTER_FRAGMENT(i)
    76.             }
    77.             ENDCG
    78.         }
    79.  
    80.         CGPROGRAM
    81.         #pragma surface surf Standard fullforwardshadows alpha:fade nolightmap
    82.         #pragma shader_feature _USEMETALLICMAP_ON
    83.         #pragma target 3.0
    84.         sampler2D _MainTex;
    85.         sampler2D _MetallicGlossMap;
    86.         sampler2D _BumpMap;
    87.         struct Input {
    88.             float2 uv_MainTex;
    89.             fixed facing : VFACE;
    90.         };
    91.         half _Glossiness;
    92.         half _Metallic;
    93.         fixed4 _Color;
    94.         half _BumpScale;
    95.         fixed _Cutoff;
    96.         void surf (Input IN, inout SurfaceOutputStandard o) {
    97.             // Albedo comes from a texture tinted by color
    98.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    99.             o.Albedo = c.rgb;
    100.             #ifdef _USEMETALLICMAP_ON
    101.             fixed4 mg = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    102.             o.Metallic = mg.r;
    103.             o.Smoothness = mg.a;
    104.             #else
    105.             o.Metallic = _Metallic;
    106.             o.Smoothness = _Glossiness;
    107.             #endif
    108.             // Rescales the alpha on the blended pass
    109.             o.Alpha = saturate(c.a / _Cutoff);
    110.             o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex), _BumpScale);
    111.             o.Normal.z *= IN.facing;
    112.         }
    113.         ENDCG
    114.     }
    115.     FallBack "Diffuse"
    116. }
    117.  
    That's a lit shader, but it kind of hacks around the issue of transparency sorting with a prepass depth write.
     
  19. reena

    reena

    Joined:
    Apr 15, 2015
    Posts:
    7
    Hi,
    I have tried all the shader about mention,
    The masking thing appears in scene view but not in game view.
     
  20. reena

    reena

    Joined:
    Apr 15, 2015
    Posts:
    7
    Hi,
    I have tried all the shader about mention,
    The masking thing appears in scene view but not in game view.
     

    Attached Files:

  21. Ash-Blue

    Ash-Blue

    Joined:
    Aug 18, 2013
    Posts:
    102
    Is it possible to use a shader like this in 2D? Trying to re-create this effect from Super Metroid.

     
  22. spatulaG

    spatulaG

    Joined:
    Dec 25, 2017
    Posts:
    1
    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!!!! : )
     
  23. DaipayanRoy

    DaipayanRoy

    Joined:
    Nov 4, 2015
    Posts:
    7
    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?
     
  24. Cetraxxx

    Cetraxxx

    Joined:
    Nov 29, 2021
    Posts:
    6
    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):
    1. Shader "Unlit/Rotation"
    2. {
    3.     Properties
    4.     {
    5.  
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.         _Alpha ("Alpha (A)", 2D) = "white" {}
    8.         _Tint ("Tint", Color) = (1,1,1,1)
    9.         _Brightness ("Brightness", Range(0,5)) =1
    10.         _Contrast ("Contrast", Range (0,10))=1
    11.         _RotationSpeed ("Rotation Speed", Float) = 2.0
    12.      
    13.     }
    14.  
    15.     SubShader
    16.     {
    17.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    18.         LOD 100
    19.        
    20.         ZWrite Off
    21.         Blend SrcAlpha OneMinusSrcAlpha
    22.          ColorMask RGB
    23.         Pass
    24.         {
    25.      
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.             // make fog work
    30.             #pragma multi_compile_fog
    31.          
    32.             #include "UnityCG.cginc"
    33.              uniform float4 _Tint;
    34.              uniform float _Contrast;
    35.              uniform float _Brightness;
    36.             struct appdata
    37.             {
    38.                 float4 vertex : POSITION;
    39.                 float2 uv : TEXCOORD0;
    40.             };
    41.             struct v2f
    42.             {
    43.                 float2 uv : TEXCOORD0;
    44.                 UNITY_FOG_COORDS(1)
    45.                 float4 vertex : SV_POSITION;
    46.             };
    47.             sampler2D _MainTex;
    48.             float4 _MainTex_ST;
    49.             struct Input {
    50.                 float2 uv_MainTex;
    51.             };
    52.          
    53.             float _RotationSpeed;
    54.          
    55.             v2f vert (appdata v)
    56.             {              
    57.                 v.uv.xy -=0.5;
    58.                 v2f o;
    59.                 o.vertex = mul(UNITY_MATRIX_MVP, float4(v.vertex));
    60.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    61.                 UNITY_TRANSFER_FOG(o,o.vertex);
    62.                 float s = sin ( _RotationSpeed * _Time );
    63.                 float c = cos ( _RotationSpeed * _Time );
    64.                 float2x2 rotationMatrix = float2x2( c, -s, s, c);
    65.                 rotationMatrix *=0.5;
    66.                 rotationMatrix +=0.5;
    67.                 rotationMatrix = rotationMatrix * 2-1;
    68.                 o.uv.xy = mul ( o.uv.xy, rotationMatrix );
    69.                 o.uv.xy += 0.5;
    70.                 return o;
    71.             }
    72.          
    73.             fixed4 frag (v2f i) : SV_Target
    74.             {
    75.                 // sample the texture
    76.                 fixed4 col = pow (tex2D(_MainTex, i.uv) * _Tint, _Contrast) * _Brightness;
    77.                 // apply fog
    78.                 UNITY_APPLY_FOG(i.fogCoord, col);
    79.                 return col;
    80.             }
    81.          
    82.  
    83.              
    84.             ENDCG
    85.  
    86.         }
    87.     }
    88. }