Search Unity

Blend Lightmap

Discussion in 'Shaders' started by jcarpay, May 3, 2009.

  1. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    I''m trying to blend a lightmap but it doesn't seem to work, I'm not sure why.
    Here the code:

    Code (csharp):
    1. Shader "Example/LightMapped-Blend" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1)) = 0.5
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _LightMap ("Lightmap (RGB)", 2D) = "white" {}
    7.     }
    8.     SubShader {
    9.         UsePass "Lightmapped/VertexLit/BASE"
    10.         UsePass "Diffuse/PPL"
    11.         Pass {
    12.             SetTexture [_MainTex]
    13.            
    14.             SetTexture [_LightMap] {
    15.                 constantColor (0, 0, 0, [_Blend])
    16.                 combine texture lerp (constant) previous
    17.             }
    18.         }        
    19.     }
    20.     FallBack "Lightmapped/VertexLit", 1
    21. }
    Anyone got an idea?
     
  2. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    Fixed!
     
  3. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    Could you post the final shader? I need something like this as well.
     
  4. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    What the (modified) shader does is blending (using a slider) the strength of the lightmap, so it doesn't blend between different lightmaps at the moment, although this should be trivial to implement.

    I should be able to post the shader in the evening.
     
  5. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    I managed to piece it together myself, with blending between two lightmaps as well. And you were right, it was pretty trivial. Thanks for the start!

    Code (csharp):
    1. Shader "Lightmapped/Diffuse Blend" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1)) = 0
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _LightMap ("Lightmap From (RGB)", 2D) = "black" {}
    7.         _LightMap2 ("Lightmap To (RGB)", 2D) = "black" {}
    8.     }
    9.     SubShader {
    10.         Pass {
    11.             Name "BASE"
    12.             Tags {"LightMode" = "PixelOrNone"}
    13.             Color [_PPLAmbient]
    14.             BindChannels {
    15.                 Bind "Vertex", vertex
    16.                 Bind "normal", normal
    17.                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    18.                 Bind "texcoord", texcoord1 // main uses 1st uv
    19.             }
    20.             SetTexture [_LightMap] {
    21.                 constantColor [_Color]
    22.                 combine texture * constant
    23.             }
    24.             SetTexture [_LightMap2] {
    25.                 constantColor (0, 0, 0, [_Blend])
    26.                 combine texture lerp (constant) previous
    27.             }
    28.             SetTexture [_MainTex] {
    29.                 constantColor [_Color]
    30.                 combine texture * previous, texture * constant
    31.             }
    32.         }
    33.         UsePass "Diffuse/PPL"
    34.     }
    35.     FallBack "Lightmapped/VertexLit", 1
    36. }
     
  6. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    I was about to post the shader, good to see you've managed to do it by yourself :)

    One thing you might have overlooked are the fallback modes apparent in the original Lightmap-VertexLit shader. E.g. Your version supports three texture cards only. Older cards won't be supported this way. My main shader file looks like this:
    Code (csharp):
    1. Shader "Custom/Lightmapped/Diffuse-Blend" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1)) = 0.5
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _LightMap ("Lightmap (RGB)", 2D) = "black" {}
    7.     }
    8.     SubShader {
    9.         UsePass "Custom/Lightmapped/VertexLit-Blend/BASE"
    10.         UsePass "Diffuse/PPL"
    11.     }
    12.     FallBack "Custom/Lightmapped/VertexLit-Blend", 1
    13. }
    The modifications I've done are mainly in the VertexLit-Blend shader which is based on the VertexLit shader(which has fallback modes, so you can check it out).
     
  7. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    Herewith the shader with fallback modes/ support for older gfx cards (DirectX 7):
    Suggestions/improvements are welcome ;)

    Primary shader:
    Code (csharp):
    1. Shader "Custom/Lightmapped/Diffuse-Blend" {
    2.     Properties {
    3.         _Blend ("Blend", Range (0, 1)) = 0
    4.         _Color ("Main Color", Color) = (1,1,1,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _LightMap ("Lightmap From (RGB)", 2D) = "black" {}
    7.         _LightMap2 ("Lightmap To (RGB)", 2D) = "white" {}
    8.     }
    9.     SubShader {
    10.         UsePass "Custom/Lightmapped/VertexLit-Blend/BASE"
    11.         UsePass "Diffuse/PPL"
    12.     }
    13.     FallBack "Custom/Lightmapped/VertexLit-Blend", 1
    14. }
    Dependent shader:

    Code (csharp):
    1. Shader "Custom/Lightmapped/VertexLit-Blend" {
    2. // ------------------------------------------------------------------
    3. // Three texture cards (Radeons, GeForce3/4Ti and up)
    4.  
    5. SubShader {
    6.     Blend AppSrcAdd AppDstAdd
    7.     Fog { Color [_AddFog] }
    8.  
    9.     // Ambient pass
    10.     Pass {
    11.         Name "BASE"
    12.         Tags {"LightMode" = "PixelOrNone"}
    13.         Color [_PPLAmbient]
    14.         BindChannels {
    15.             Bind "Vertex", vertex
    16.             Bind "normal", normal
    17.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    18.             Bind "texcoord", texcoord1 // main uses 1st uv
    19.         }
    20.          SetTexture [_LightMap] {
    21.             constantColor [_Color]
    22.             combine texture * constant
    23.          }
    24.          SetTexture [_LightMap2] {
    25.             constantColor (0, 0, 0, [_Blend])
    26.             combine texture lerp (constant) previous
    27.          }
    28.         SetTexture [_MainTex] {
    29.             constantColor [_Color]
    30.             combine texture * previous, texture * constant
    31.         }
    32.     }
    33.    
    34.     // Vertex lights
    35.     Pass {
    36.         Name "BASE"
    37.         Tags {"LightMode" = "Vertex"}
    38.         Material {
    39.             Diffuse [_Color]
    40.             Shininess [_Shininess]
    41.             Specular [_SpecColor]
    42.         }
    43.  
    44.         Lighting On
    45.         SeparateSpecular On
    46.  
    47.         BindChannels {
    48.             Bind "Vertex", vertex
    49.             Bind "normal", normal
    50.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    51.             Bind "texcoord1", texcoord1 // lightmap uses 2nd uv
    52.             Bind "texcoord", texcoord2 // main uses 1st uv
    53.         }
    54.        
    55.         SetTexture [_LightMap] {
    56.             constantColor [_Color]
    57.             combine texture * constant
    58.         }
    59.         SetTexture [_LightMap] {
    60.             constantColor (0.5,0.5,0.5,0.5)
    61.             combine previous * constant + primary
    62.         }
    63.         SetTexture [_MainTex] {
    64.             combine texture * previous DOUBLE, texture * primary
    65.         }
    66.     }
    67. }
    68.  
    69. // ------------------------------------------------------------------
    70. // Dual texture cards - draw in two passes
    71.  
    72. SubShader {
    73.     Blend AppSrcAdd AppDstAdd
    74.     Fog { Color [_AddFog] }
    75.  
    76.     // Always drawn base pass: texture * lightmap
    77.     Pass {
    78.         Name "BASE"
    79.         Tags {"LightMode" = "Always"}
    80.         Color [_PPLAmbient]
    81.         BindChannels {
    82.             Bind "Vertex", vertex
    83.             Bind "normal", normal
    84.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    85.             Bind "texcoord", texcoord1 // main uses 1st uv
    86.         }
    87.          SetTexture [_LightMap] {
    88.             constantColor [_Color]
    89.             combine texture * constant
    90.          }
    91.          SetTexture [_LightMap2] {
    92.             constantColor (0, 0, 0, [_Blend])
    93.             combine texture lerp (constant) previous
    94.          }
    95.         SetTexture [_MainTex] {
    96.             combine texture * previous, texture * primary
    97.         }
    98.     }
    99.    
    100.     // Vertex lights: add lighting on top of base pass
    101.     Pass {
    102.         Name "BASE"
    103.         Tags {"LightMode" = "Vertex"}
    104.         Material {
    105.             Diffuse [_Color]
    106.             Shininess [_Shininess]
    107.             Specular [_SpecColor]
    108.         }
    109.  
    110.         Lighting On
    111.         SeparateSpecular On
    112.        
    113.         ColorMask RGB
    114.  
    115.         SetTexture [_MainTex] {
    116.             combine texture * primary DOUBLE, texture
    117.         }
    118.     }
    119. }
    120.  
    121. // ------------------------------------------------------------------
    122. // Single texture cards - lightmap and texture in two passes; no lighting
    123.  
    124. SubShader {
    125.     Blend AppSrcAdd AppDstAdd
    126.     Fog { Color [_AddFog] }
    127.  
    128.     // Base pass: lightmap
    129.     Pass {
    130.         Name "BASE"
    131.         Tags {"LightMode" = "Always"}
    132.         BindChannels {
    133.             Bind "Vertex", vertex
    134.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    135.         }
    136.          SetTexture [_LightMap] {
    137.             constantColor [_Color]
    138.             combine texture * constant
    139.          }
    140.          SetTexture [_LightMap2] {
    141.             constantColor (0, 0, 0, [_Blend])
    142.             combine texture lerp (constant) previous
    143.          }
    144.     }
    145.    
    146.     // Second pass: modulate with texture
    147.     Pass {
    148.         Name "BASE"
    149.         Tags {"LightMode" = "Always"}
    150.         BindChannels {
    151.             Bind "Vertex", vertex
    152.             Bind "texcoord", texcoord0 // main uses 1st uv
    153.         }
    154.         Blend Zero SrcColor
    155.         SetTexture [_MainTex] { combine texture }
    156.     }
    157. }
    158.  
    159. Fallback "VertexLit", 1
    160.  
    161. }
    162.  
     
  8. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    Yeah, I skipped all that since I didn't really know how to fit the second lightmap into all of them. :)

    Are you sure that this really supports all the cards of the original shader? It's using three textures in one pass in the dual texture subshader, and two textures in one pass in the single texture subshader. I'm no shader guru, but it sounds to me like a dual texture card only supports two textures in a pass. Also, shouldn't the second lightmap be somewhere in the vertex lighting pass of the three texture subshader as well? I don't know how, but it feels like it should be in there as well.
     
  9. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    I thought so as well, but I checked by going through the various emulation modes and it all appeared to work well... Perhaps a shader expert can step in here?

    yes, you assumed right.
     
  10. maxxoros

    maxxoros

    Joined:
    May 3, 2009
    Posts:
    52
    Sorry for my ignorance, I am not quite used to read shader code, but does your shader is solving this issue

    http://forum.unity3d.com/viewtopic.php?t=23940

    That mean we need a second lightmap to raise the exposure of texture.

    Thanks :wink: :roll:
     
  11. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    No, it doesn't.
     
  12. maxxoros

    maxxoros

    Joined:
    May 3, 2009
    Posts:
    52
    So what is the purpose of this shader, what is the second lightmap for
     
  13. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    This shader will blend/crossfade between 2 lightmaps.
     
  14. tolm

    tolm

    Joined:
    Feb 4, 2008
    Posts:
    61
    ...which is good if you are doing a day/night-cycle with baked lighting. For example.
     
  15. jcarpay

    jcarpay

    Joined:
    Aug 15, 2008
    Posts:
    561
    ...so yeah, it's cool :)