Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unlit Transparent with another transparency from Vertex Alpha - Please help...

Discussion in 'Shaders' started by wkone, Apr 8, 2014.

  1. wkone

    wkone

    Joined:
    Mar 31, 2014
    Posts:
    4
    Hello,

    I am not really at ease with shaders and I almost have what I need... Almost :S
    I would like to have an Unlit Transparent shader, meaning that the material is transparent according to the alpha from the texture, that is also transparent where the vertex alpha is black.
    In other words the resulting transparency is a combination of transparency from the texture multiplied by the transparency from the vertex alpha channel.

    I found this, which is pretty close except that it doen(t take in account the vertex alpha and I didn't manage to make it work.

    Thank you very much for any help!


    Code (csharp):
    1.  
    2.    Shader "Unlit/AlphaSelfIllum" {
    3.         Properties {
    4.              _Color ("Color Tint", Color) = (1,1,1,1)
    5.              _MainTex ("SelfIllum Color (RGB) Alpha (A)", 2D) = "white"
    6.         }
    7.        
    8.         Category {
    9.             Lighting On
    10.             ZWrite Off
    11.             Cull Back
    12.             Blend SrcAlpha OneMinusSrcAlpha
    13.             Tags {Queue=Transparent}
    14.            
    15.             SubShader {
    16.                 Material {
    17.                     Emission [_Color]
    18.                 }
    19.                
    20.                 Pass {
    21.                     SetTexture [_MainTex] {
    22.                         Combine Texture * Primary, Texture * Primary
    23.                     }
    24.                 }
    25.             }
    26.         }
    27.     }
    28.  
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    I think this is what you're looking for:
    Code (csharp):
    1.  
    2. // Unlit alpha-blended shader.
    3. // - no lighting
    4. // - no lightmap support
    5. // - no per-material color
    6.  
    7. Shader "Unlit/Transparent 2" {
    8. Properties {
    9.     _Color ("Main Color", Color) = (1,1,1,1)
    10.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    15.     LOD 100
    16.    
    17.     ZWrite Off
    18.     Blend SrcAlpha OneMinusSrcAlpha
    19.    
    20.     Pass {  
    21.         CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.            
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata_t {
    28.                 float4 vertex : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f {
    33.                 float4 vertex : SV_POSITION;
    34.                 half2 texcoord : TEXCOORD0;
    35.             };
    36.  
    37.             sampler2D _MainTex;
    38.             float4 _MainTex_ST;
    39.             fixed4 _Color;
    40.            
    41.             v2f vert (appdata_t v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    45.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    46.                 return o;
    47.             }
    48.            
    49.             fixed4 frag (v2f i) : COLOR
    50.             {
    51.                 fixed4 col = tex2D(_MainTex, i.texcoord)*_Color;
    52.                 return col;
    53.             }
    54.         ENDCG
    55.     }
    56. }
    57. }
    58.  
     
  3. wkone

    wkone

    Joined:
    Mar 31, 2014
    Posts:
    4
    Thank you Peter.
    I applied it and, well, the vertex alpha of the object is not taken into account.
    It is great to be able to control the overall alpha though.

    Let me introduce my process, maybe my need will be clearer;
    I have an object on which I painted some vertices in black in some area, this in the vertex alpha channel of the object.

    Once this object imported I would like to apply an unlit texture on it and blend the alpha of the texture with the black areas of the vertex alpha channel so that the texture also appears transparent on these areas.


    I tweaked a bit my shader and got almost what I want with it... except that it doesn't blend right with the background now :S
    I am really not far but not there yet...

    Code (csharp):
    1.  
    2. Shader "WkLibrary/WkCloudsVertexAlpha" {
    3.     Properties {
    4.         _Color ("Color Tint", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.     }
    7.    
    8.     Category {
    9.             ZWrite On
    10.             Lighting Off
    11.             Cull Off
    12.             ZTest Always
    13.  
    14.         SubShader {
    15.          Tags {"Queue" = "Transparent" }
    16.          
    17.             Pass {
    18.                 ColorMaterial AmbientAndDiffuse
    19.                
    20.                 Blend SrcAlpha OneMinusDstAlpha
    21.                
    22.                 SetTexture [_MainTex] {
    23.                 // vertex alpha used here
    24.                     Combine texture * primary, texture * primary
    25.                 }
    26.                
    27.                 SetTexture [_MainTex] {
    28.                     constantColor [_Color]
    29.                     Combine previous * constant DOUBLE, previous * constant
    30.                 }
    31.             }
    32.         }
    33.      }
    34.      
    35.     FallBack "Diffuse"
    36. }
    37.  
    38.  
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    Oh sorry, I didn't read you want to take the vertex alpha into account and it even says it in the topic.

    This one should take vertex alpha into account, I'm not able to test because I don't have a mesh with vertex colors. It basically passes 'color' from the vertex shader to the pixel shader and multiplies the color with the vertex color. Vertex RGB to tint and Alpha to blend. Hope it works!
    Code (csharp):
    1.  
    2. // Unlit alpha-blended shader.
    3. // - no lighting
    4. // - no lightmap support
    5. // - no per-material color
    6.  
    7. Shader "Unlit/Transparent 2" {
    8. Properties {
    9.     _Color ("Main Color", Color) = (1,1,1,1)
    10.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    11. }
    12.  
    13. SubShader {
    14.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    15.     LOD 100
    16.    
    17.     ZWrite Off
    18.     Blend SrcAlpha OneMinusSrcAlpha
    19.    
    20.     Pass {  
    21.         CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.            
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata_t {
    28.                 float4 vertex : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.                 float4 color : COLOR;
    31.             };
    32.  
    33.             struct v2f {
    34.                 float4 vertex : SV_POSITION;
    35.                 half2 texcoord : TEXCOORD0;
    36.                 float4 color : COLOR;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             fixed4 _Color;
    42.            
    43.             v2f vert (appdata_t v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    47.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    48.                 o.color = v.color;
    49.                 return o;
    50.             }
    51.            
    52.             fixed4 frag (v2f i) : COLOR
    53.             {
    54.                 fixed4 col = tex2D(_MainTex, i.texcoord) * _Color * i.color;
    55.                 return col;
    56.             }
    57.         ENDCG
    58.     }
    59. }
    60.  
    61. }
    62.  
     
  5. wkone

    wkone

    Joined:
    Mar 31, 2014
    Posts:
    4
    We are in the right direction but it is still not exactly that: here the vertex alpha is visible as a black color on the texture.

    Here is my sample mesh with vertex alpha. It is for a cloud dome.
     

    Attached Files:

  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,415
    I checked the mesh and the issue is the mesh uses white as alpha for every vertex and the fade is stored in RGB. What you actually want to do is set RGB to white and use Alpha for the blend.
     
  7. wkone

    wkone

    Joined:
    Mar 31, 2014
    Posts:
    4
    Indeed :S
    Geee, I didn't see that.

    So it should be fixed now (cf attach). The shader is working far better.

    There is still something strange though: on the vertex alpha area the texture appears darker. It is not fading so well.
    Can it be fixed in the shader or do you think this comes from the texture, like a premult that should be applied for example?

    As my textures come from Photoshop there is no premult on them. I checked with tga and png and the result is the same.

    Thanks again for your help ;)
     

    Attached Files: