Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vertexlit gone after lightmapping

Discussion in 'Shaders' started by Eyeofgod, Sep 13, 2011.

  1. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Hi there,

    I have been having problems with lightmaps (using Beast) since I started using it seriously. First, there is the unresolved problem with the batching (see this post: http://forum.unity3d.com/threads/78...hing-from-working?highlight=lightmap+batching ) and now I have another weird problem with the Vertexlit shaders. First, Im using the shader Mobile/Diffuse. If I put a cube with 4 points lights around it and a directional light, all the lights illuminates perfectly it using vertex (as expected). Those lights, except the directional, are configured as "RealTime only". As soon as I launch Beast, and the lightmap process is over, the vertexlit lights are gone. If I remove the lightmap they come back.
    I have tried with all the default shaders and the effect is always the same: if you put a Beast lighmap you will loose vertex lights, only per-pixel light works. This has no sense. Anyone has info about this?

    Thanks in advance

    I have read this post (http://forum.unity3d.com/threads/74504-Lightmapping-in-iOS-VFX-Lighting) but the answer is not there.
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Is the cube lightmapped in the above scenario? If so then it will no longer receive realtime lighting (the 4 point lights) after the lightmaps are baked. An object can't be realtime lit and lightmapped at the same time unless you're using dual lightmaps.
     
  3. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Thx for the reply. Yes the cube is lightmapped and I'm using single lightmaps (I'm on iOS). The cube STILL receive per-pixel lights after been baked (if I use a per-pixel lighting shader, like the standar diffuse), but the per-vertex lights don't work. I have also tried with the no-mobile vertexlit shaders with same results: No per-vertex lighting
     
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yeah, the limitation Ethan talked about only affects VertexLit shaders. A vertex lit object, once lightmapped, does not receive any realtime lighting. We kind of chose this by design; using vertex lit almost always means "I want to run this as fast as possible" that's why we opted for no lighting once it's lightmapped.

    Per-pixel lit shaders, once lightmapped, are affected by non-lightmapped lights.
     
  5. Eyeofgod

    Eyeofgod

    Joined:
    Jun 25, 2010
    Posts:
    126
    Thanks both ;)

    Maybe you can help me with this problem with lightmapping and static batching (it's from another post that got lost)

    ".., Im using static batching, all meshes are marked as static and all have the same material. Now the tricky part: If the lightmapper only generates one texture for all the objects they are batched correctly. BUT, if it generates more than one, the batching don't work as spected. This are the numbers for 100 objects with same material (just planes) with one directional light and diffuse shader:
    Without lightmap: 1 Draw, 100 batched
    With 1 lightmap: 1 Draw 100 batched
    With 2 lightmaps: 18 draws, 86 batched
    With 3 lightmaps: 26 draws, 82 batched
    An so on...
    "
     
  6. serpin

    serpin

    Joined:
    Nov 13, 2011
    Posts:
    54
    Hi, Aras,

    It is possible to enable force-processing of vertex shaders on lightmapped objects? Via shader instruction or by any other means?

    Because it's is nice of you to take care of us by making lightmapped scenes fast by omitting vertex lighting, but wouldn't it be a lot better to give us a chioce?

    It is perfectly normal to use any lighting with any mapping if one knows that his target will be able to process it.
    Imagine how good would games look if they utilized both beast lightmapping and vertex lighting (in the dark areas, for example).
    Vertex lighting is extremely cheap, and you know it better than any one else, so...

    Please, advise.

    Cheers.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    forwardbase should do it I think
     
  8. serpin

    serpin

    Joined:
    Nov 13, 2011
    Posts:
    54
    Sorry, I forgot to mention that I'm an iOS developer. Won't work on iOS this way.
     
  9. serpin

    serpin

    Joined:
    Nov 13, 2011
    Posts:
    54
    Never mind. I've modified the standard Vertex Lit shader to multiply the resulting pass by PRIMARY and then add PREVIOUS as an albedo while still keeping the number of draw calls at 2.
    Now anything can be vertex lit even with the lightmap on.
    Cheers.
     
    Last edited: Nov 13, 2011
  10. Leo-Bounce

    Leo-Bounce

    Joined:
    Mar 2, 2011
    Posts:
    12
    Can you please provide this shader? Thanks!
     
  11. KarolD

    KarolD

    Joined:
    Oct 23, 2012
    Posts:
    12
    I'd like to see that shader too. Please :)
     
  12. Bovine

    Bovine

    Joined:
    Oct 13, 2010
    Posts:
    177
    Yes *bump* please supply this shader
     
  13. elias_t

    elias_t

    Joined:
    Sep 17, 2010
    Posts:
    1,367
    For any one interested I coded such a shader...

    I added one extra combine to use the color as the ambient light from the render settings. So if you don't need it delete it.

    These are the lines:

    SetTexture [_MainTex] {
    constantColor [_Color]
    combine previous*constant DOUBLE
    }

    you have to feed the ambient light through code though.

    And while it works very good on desktop it is very slow on some mobile devices.

    Code (csharp):
    1. //change the _Color to use the Ambient light !!!!
    2.  
    3. Shader "VertexLit_Lightmap" {
    4.  
    5. Properties {
    6.     _Color ("Main Color", Color) = (1,1,1,1)
    7.     //_Specular ("Specular", Color) = (1,1,1,1)
    8.     //_Shininess ("Shininess", Color) = (1,1,1,1)
    9.     //_Emission ("Emission", Color) = (1,1,1,1)
    10.     _MainTex ("Base (RGB)", 2D) = "white" {}
    11. }
    12.  
    13. // 2/3 texture stage GPUs
    14. SubShader {
    15.     Tags { "RenderType"="Opaque" }
    16.     LOD 100
    17.    
    18.     // Non-lightmapped
    19.     Pass {
    20.         Tags { "LightMode" = "Vertex" }
    21.  
    22.         Material {
    23.             Diffuse [_Color]
    24.             //Specular [_Specular]
    25.             //Shininess [_Shininess]
    26.             //Emission [_Color]
    27.         }
    28.         Lighting On
    29.         //SeparateSpecular On
    30.         SetTexture [_MainTex] {
    31.             Combine texture * primary DOUBLE, texture * primary
    32.         }
    33.     }
    34.    
    35.     // Lightmapped, encoded as dLDR
    36.     Pass {
    37.         Tags { "LightMode" = "VertexLM" }
    38.         Lighting On
    39.        
    40.         BindChannels {
    41.             Bind "Vertex", vertex
    42.             Bind "normal", normal
    43.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    44.             Bind "texcoord", texcoord1 // main uses 1st uv
    45.         }
    46.  
    47.         Material {
    48.             Diffuse [_Color]
    49.             //Specular [_Specular]
    50.             //Shininess [_Shininess]
    51.             //Emission [_Color]
    52.         }
    53.        
    54.         SetTexture [unity_Lightmap] {
    55.             matrix [unity_LightmapMatrix]
    56.             constantColor (1,1,1,1)
    57.             combine texture * constant
    58.         }
    59.         SetTexture [unity_Lightmap] {
    60.             combine previous + primary
    61.         }
    62.         SetTexture [_MainTex] {
    63.             combine texture * previous, texture * primary
    64.         }
    65.        
    66.         SetTexture [_MainTex] {
    67.             constantColor [_Color]
    68.             combine previous*constant DOUBLE
    69.         }
    70.        
    71.         SetTexture [_MainTex] {
    72.             combine previous+primary
    73.         }
    74.        
    75.        
    76.     }
    77.    
    78.     // Lightmapped, encoded as RGBM
    79.     Pass {
    80.         Tags { "LightMode" = "VertexLMRGBM" }
    81.         Lighting On
    82.        
    83.         //Blend SrcColor    SrcColor
    84.        
    85.         BindChannels {
    86.             Bind "Vertex", vertex
    87.             Bind "normal", normal
    88.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    89.             Bind "texcoord", texcoord1 // main uses 1st uv
    90.         }
    91.  
    92.         Material {
    93.             Diffuse [_Color]
    94.             //Specular [_Specular]
    95.             //Shininess [_Shininess]
    96.             //Emission [_Emission]
    97.         }
    98.        
    99.         SetTexture [unity_Lightmap] {
    100.             matrix [unity_LightmapMatrix]
    101.             combine texture * texture alpha DOUBLE
    102.         }
    103.         SetTexture [unity_Lightmap] {
    104.             combine previous + primary
    105.         }
    106.         SetTexture [_MainTex] {
    107.             combine texture * previous
    108.         }
    109.        
    110.         SetTexture [_MainTex] {
    111.             constantColor [_Color]
    112.             combine previous*constant  QUAD
    113.         }
    114.  
    115.         SetTexture [_MainTex] {
    116.             constantColor [_Color]
    117.             combine previous+primary
    118.         }
    119.                
    120.     }
    121.  
    122.    
    123.  
    124. }
    125.  
    126.  
    127. }
    128.  
     
  14. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    3 years on from the creation of this thread, mobile GPUs are a fair bit faster, but per-pixel/pass-per-light still isn't viable for most mobile developers - still needing to support at least the iPhone 4S. It'd still be nice to be able to real-time vertex lighting on lightmapped objects - even if it's just for a couple of effects - such as the occasional flickering light or big explosion

    Has anyone figured out a solution? - when I last tried messing with shaders, it seemed that the vertex light parameters simply don't get set for lightmapped objects, so custom shaders won't help much (unless you do your own lighting entirely, which isn't ideal)

    It is possible to trick Unity into thinking that lightmapped meshes aren't lightmapped, and are regular meshes with two UV sets?, so the lighting won't be disabled?
     
  15. gwiazdorrr

    gwiazdorrr

    Joined:
    Sep 29, 2014
    Posts:
    102
    Bump.

    If think it's heavily disappointing, for same reasons as bluescrn. It seems that when LIGHTMAP_ON keyword is enabled, runtime will simply ignore VERTEXLIGHT_ON, even if present.

    Does anyone have a workaround for this?
     
  16. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    642
    Our current workaround is to use a set of entirely custom shaders which always calculate 4 per-vertex diffuse-only point lights.

    It does the job, but it's not the greatest solution, as we have to manage the lights/shader parameters ourselves, and are limited to 4 global lights (rather than the 4 closest/most important lights per-object). But these lights are purely for special effects at the moment, rather than a fundamental part of the game's lighting.
     
  17. gwiazdorrr

    gwiazdorrr

    Joined:
    Sep 29, 2014
    Posts:
    102
    Exactly what I came with in my project, but because of the pain of having to manage these lights yourself, it's hardly a workaround.

    How do you calculate attenuation for these lights, i.e. the equivalent of unity_4LightAtten0? I use the inverted square of each light's range, but I'm not entirely sure whether that's what Unity is doing.
     
  18. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    456
    We still really need the ability to enable vertex light information without additional passes and without hacky solutions like suggested towards the end of this thread. Surely shaders can provide this information, since they do when lightmaps aren't being used, so why is it so difficult to introduce a feature that allows us to enable receiving vertex light information when lightmaps are present?

    There are still important scenarios for being able to use vertex lights on objects that are lightmapped. One of the most important being adding some realtime lighting without performance penalty in forward rendering.

    Unless I'm misguided then this is still an important unsolved issue 7 years after the original post. What can be done? When, if ever, can we expect the ability to do this?