Search Unity

Mix lightmaps and dynamic shadows

Discussion in 'Shaders' started by speps, Oct 19, 2009.

  1. speps

    speps

    Joined:
    Oct 13, 2009
    Posts:
    22


    Hello,

    On the image above, I'm using precomputed lightmaps on the models except the cube. I want to mix them together with dynamic shadows but as you can see there is a different tinting of the shadow. This is because the dynamic shadow under the barn is blended with the lightmap (the directional light has the same angle as the light used for precomputing the lightmap).

    What I would like to achieve is that the dynamic shadow of the cube can't be distinguished from the lightmap?

    Thanks.

    PS : I'm using Unity Pro 2.5.1
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Thats out of your control I fear.
    you can not affect the realtime shadow rendering
     
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Couldn't you just increase the light's shadow "strength"? That would darken it (assuming it's not already at its maximum setting).
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Unfortunately you can't mix light maps and dynamic shadows because Unity doesn't give you fine-grained enough control to have shadows cast by the player fall on the ground, but shadows cast by walls, etc. only fall on the player.

    What I recommend doing is using light maps only for ambient lighting (including ambient occlusion, etc.), and allow all directional light to be done dynamically, with dynamic shadows.
     
  5. speps

    speps

    Joined:
    Oct 13, 2009
    Posts:
    22
    Thanks for the answers.

    I tried the strength parameter of the shadows but it wasn't quite what I wanted.

    I read an example of a shader which makes dynamic shadows desaturated, wouldn't it possible to adapt it to change the color of the resulting shadow or maybe use the shadow map information to make dynamic shadows only appear where there is no lightmap.

    On a side question, when I looked at the builtin shaders sources, I didn't find information about how the shadows are displayed, is it a postprocess which can't be controlled?

    Thanks.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I fear that shader is of no use as Unity's shadow shader is unavailable for modification.

    Technology wise I would guess its a PSSM alike approach as unity isn't a deferred / forward rendering tech.
     
  7. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751
    It seems to actually be possible with a little bit of contortion. I'm not sure if this technique will just steal back the performance gains from lightmapping but in the interest of theorycrafting and if you really, REALLY need to do it:

    1. Split your directional light into two^H^H^Hthree^H^H^H^Htwo lights
    2. Call them LightNoLightmap and LightLightmap
    3. Create a layer for objects which have lightmaps and another layer for proxy objects
    4. Set your LightNoLightmap to light everything except for the lightmapped layer
    5. Set your LightLightmap to light only the lightmapped layer and proxy objects
    6. Now create a proxy object for your player, this will be an invisible object with the same geometry as your player but with a shader that only casts shadows

    It's... possible... but in most cases will probably run a lot slower than just using dynamic lights for everything.
     
  8. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Can you explain this a bit more precisely? Perhaps a matrix showing which layers are lit by which lights.
     
  9. speps

    speps

    Joined:
    Oct 13, 2009
    Posts:
    22
    Actually, for my original problem, I would have done something like using the dynamic shadow map where there is no lightmap (ie. where the lightmap is the whiter) and using the information from the lightmap where the two overlap.

    I tried some things with a custom shader which did something like that but it had weird borders where the two overlapped because of the difference in lighting, my lightmap has occlusion and global illumination information.

    Maybe I will try something like having additional information in my lightmap to modulate the dynamic shadow.
     
  10. cannon

    cannon

    Joined:
    Jun 5, 2009
    Posts:
    751
    Only 1 layer definition is required:
    - Lightmap layer

    There are 5 elements in my test scene:
    - Light for lightmap and proxies
    - Light for everything except for lightmaps
    - Player object
    - Player proxy object (the same geometry/animation/transform as the player, but with a shader that renders it invisible yet still casts a shadow)
    - Bench Object (Casts the lightmap)
    - Ground Object (Lightmapped)


    Place only the following objects in the lightmap layer:
    - Ground object
    - Player proxy object

    Now set up one light to light only the lightmap layer, and another light for everything except for the lightmap layer.

    The net effect is this:
    1. The "light everything but lightmaps" light will cause the bench to cast a dynamic shadow on the player but not on the ground
    2. The "light only lightmaps" light will cause the player proxy to cast a dynamic shadow on the ground

    Effectively you've removed the bench's dynamic shadow from the ground, leaving you free to use static lightmaps for the shadow. It's not really that slow now that I've simplified it from what I originally envisioned, as each object only gets lit by one light except for the "player+player proxy" combination.