Search Unity

Transparent Cutoff Cg Shader with Unity lightmaps

Discussion in 'Shaders' started by Dolmen007, Aug 26, 2013.

  1. Dolmen007

    Dolmen007

    Joined:
    Nov 25, 2012
    Posts:
    18
    Hello everybody,

    actually learning programming shaders in Nvidia Cg and working with Unity,
    I try in vain to implement good lightmaps with transparency.

    So I have lightmaps but without the alpha test from the texture.
    $transparentLM.PNG

    And I really don't know how to fix this problem.

    Is that Cg allow to convert : the alpha channel (from texture with coord1) to another with the coordinates from lightmaps(coord2) ?
    Like a rebake of a texture with another uv set.

    I would try to multiply the lightmap with the basic texture alpha, to remove some shadows in the lightmap.
    But now I can't because these textures don't have the same uv coordinates.

    I hope i'm clear enough.

    Thanks in advance.
     
  2. David L

    David L

    Joined:
    Jul 22, 2013
    Posts:
    11
    What do you use for your transparent shader? What does it look like?
     
  3. Dolmen007

    Dolmen007

    Joined:
    Nov 25, 2012
    Posts:
    18
    Hi David,

    Edit : I don't know if there is another way to implement transparency and lightmaps.
    For Gles I simply use clip() instead of discard.

    this is my code :

    Code (csharp):
    1.  
    2. Shader "HeliumGames/Unlit RGB(A) D3D-GLES (Lightmap)"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Diffuse (RGB)(A)", 2D) = "white" {}
    7.         _Range ("Alpha Test Cutoff", Range(0,1)) = 0.5
    8.         _LMPower("Lightmap power", Range(1,5)) = 2
    9.         _Color ("Color" , Color) = (1,1,1,1)
    10.     }
    11.    
    12.     //Direct3D 9.0/11.0 Shader Version
    13.     SubShader
    14.     {
    15.         Tags { "Queue" = "Transparent" "IgnoreProjector"="True" "RenderType"="TransparentCutoff"}
    16.         LOD 300
    17.         Pass
    18.         {
    19.             Cull Off
    20.             Lighting Off
    21.        
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #pragma target 3.0
    26.             #pragma only_renderers d3d9 d3d11
    27.  
    28.             #include "UnityCG.cginc"
    29.            
    30.             uniform sampler2D _MainTex;
    31.             uniform float4 _MainTex_ST;
    32.             uniform float _Range;
    33.             uniform float _LMPower;
    34.             uniform float4 _Color;
    35.            
    36.             struct vertexInput
    37.             {
    38.                 float4 vertex       : POSITION;
    39.                 float2 texcoord     : TEXCOORD0;
    40.                 float2 texcoord1    : TEXCOORD1;
    41.             };
    42.            
    43.             #ifdef LIGHTMAP_OFF
    44.             struct vertexOutput
    45.             {
    46.                 float4 position     : SV_POSITION;
    47.                 float2 tex1         : TEXCOORD0;
    48.             };
    49.             #endif
    50.        
    51.             #ifndef LIGHTMAP_OFF
    52.             struct vertexOutput
    53.             {
    54.                 float4 position     : SV_POSITION;
    55.                 float2 tex1         : TEXCOORD0;
    56.                 float2 tex2         : TEXCOORD1;
    57.                 //float2 tex3           : TEXCOORD2;
    58.             };
    59.             sampler2D unity_Lightmap;
    60.             sampler2D unity_LightmapInd;
    61.             float4 unity_LightmapST;
    62.             #endif
    63.    
    64.             vertexOutput vert (vertexInput input)
    65.             {
    66.                 vertexOutput OUT;
    67.                
    68.                 OUT.position = mul(UNITY_MATRIX_MVP, input.vertex);
    69.                 OUT.tex1 = TRANSFORM_TEX(input.texcoord, _MainTex);
    70.                
    71.                 #ifndef LIGHTMAP_OFF
    72.                     OUT.tex2.xy = ((input.texcoord1.xy * unity_LightmapST.xy) + unity_LightmapST.zw);
    73.                     //OUT.tex3.xy = mul(UNITY_MATRIX_TEXTURE1, input.texcoord);
    74.                 #endif
    75.                
    76.                 return OUT;
    77.             }
    78.            
    79.             float4 frag (vertexOutput input) : COLOR
    80.             {
    81.                 float4 c;
    82.                 float4 texColor = tex2D(_MainTex, float2(input.tex1.xy)) * _Color;
    83.                 c.rgb = texColor.rgb;
    84.                 c.a = texColor.a;
    85.                
    86.                 #ifndef LIGHTMAP_OFF
    87.                     float4 lightmapTex = tex2D(unity_Lightmap, input.tex2.xy);
    88.                     float3 lightmap = DecodeLightmap(lightmapTex);
    89.                
    90.                     //float4 transparentLM = tex2D(_MainTex, input.tex3.xy);
    91.                     //lightmap = step(lightmap, c.a)/_LMPower;
    92.                    
    93.                     c.rgb += texColor * lightmap;
    94.                     c.a += texColor.a;
    95.                 #endif // LIGHTMAP_OFF
    96.                
    97.                 if(c.a < _Range)
    98.                 {
    99.                     discard;
    100.                 }
    101.                 return c;
    102.             }
    103.             ENDCG
    104.         }
    105.     }
    106.     FallBack "Transparent/Cutout/VertexLit"
    107. }
    108.  
    109.  
     
    Last edited: Aug 27, 2013
    sogum likes this.
  4. David L

    David L

    Joined:
    Jul 22, 2013
    Posts:
    11
    I don't have experience writing custom frag/vert shader to work along with lightmap. My obvious question is why aren't you using the standard Transparent/Cutout/Diffuse or specular?