Search Unity

Per Object Shadow Distance?

Discussion in 'Global Illumination' started by TKDHayk, Oct 18, 2018.

  1. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    130
    Hello Unity community!

    Is it possible to set shadow distance per object? I want only my terrain to cast a long distance dynamic shadow. All my other other objects (enemies, Rocks, Trees) can have a much lower shadow distance.

    The only way I have thought to achieve this is to increase my global shadow distance in quality settings, and turn shadow casting on and off per object (enemy, Rock, tree) based on OnTriggerEnter/Exit. However, this would require a very large number of Trigger calls and I think there should be a more performant way to achieve this.
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    What's the objective here? an optimisation or a visual effect?
     
  3. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    130
    Anthony,

    The objective is to have the Terrain Render shadows from a very large distance. This shadow needs to be Dynamic as I have a day/night cycle. The other objects should only render when they are close to the Player.
     
  4. kristijonas_unity

    kristijonas_unity

    Unity Technologies

    Joined:
    Feb 8, 2018
    Posts:
    1,080
    protopop likes this.
  5. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    130

    Thank you, but I am not using Baked GI, because I have a day/night cycle (the directional light revolves around the terrain). I need my terrain to cast a dynamic shadow which is visible form very large distances. Since it is only 1 object, performance is not too bad even with very large shadow distance. However, all of my other objects, such as rocks and trees and enemies, also need to cast dynamic shadows which should only be visible from up close. Is there a way to set visible shadow distance per object? Please advise. thank you
     
  6. CraigGraff

    CraigGraff

    Joined:
    May 7, 2013
    Posts:
    44
    If you are looking to increase the quality of the shadow at large distances, you should change the Resolution property (under Shadow Type) to Very High Resolution.

    You can also make your directional light only affect the Terrain layer (you will need to add a Terrain layer, of course). Then set that light to have Render Mode: Important, and the light that affects everything else as Not Important. This is not a solution I have tested, however.
     
    Last edited: Nov 10, 2018
  7. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    130
    Hello Craig, thank you for you suggestion. My shadow resolution is already set to very high but the quality of the shadow is still very blurry when I set the shadow distance to 2000. It only looks sharp only when I set shadow distance to 100 or lower.

    Creating 2 Dynamic lights seems like a plausible solution. One for the terrain, and another for all objects. But how would I set my other objects (Boulders, enemies, player) to cast shadows only within a short distance form the player? The shadow distance in quality settings is a global setting. how can I change the shadow distance per directional light?
     
  8. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Easiest way is just to use the LOD Group component and make your LOD1/LOD2 mesh not cast shadows (Mesh Renderer component). You could even use the same mesh for both LOD's. The LOD component just switches different mesh renderers on/off.

    Another way is to script your own collision-based shadow remover, or have a script that is calculating their distance to the camera and switching the shadows on/off.
     
    ROBYER1, TKDHayk and nathanielp like this.
  9. sewy

    sewy

    Joined:
    Oct 11, 2015
    Posts:
    150
    Hey, i would like to reopen this questions.

    We would like to have:
    - realtime shadows close to the camera for every object - distance shadowmask
    - baked shadows for static objects
    - realtime shadows for dynamic objects (! this is what I am up to !)

    Is it somehow possible please?

    I wonder if anyone who works on fly simulator doesnt need it?
     
  10. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Try the Subtractive mode.
     
  11. TKDHayk

    TKDHayk

    Joined:
    Dec 22, 2015
    Posts:
    130
    For the record, I went with Stardog's suggestion of turning off shadow casting after a certain distance from the player, using a sphere trigger collider. This seems to be the cheapest/most efficient solution.

    I don't remember the exact syntax off the top of my head, but its something like:

    void OnTriggerExit()
    {
    Mesh.ShadowCastingMode= false;
    }

    void OnTriggerEnter()
    {
    Mesh.ShadowCastingMode= true;
    }