Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Regarding the shadow, how can I tell if the player enters the shadow

Discussion in 'General Discussion' started by jikeking, Jul 13, 2023.

  1. jikeking

    jikeking

    Joined:
    Jan 20, 2023
    Posts:
    2
    Hello everyone, some events will be triggered when the player enters the shadow, how can I judge whether the player enters the shadow
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    If you're just worried about specific places where shadows happen to be, I would just put some colliders that trigger at the most likely spots. That's going to be the most efficient.

    Otherwise, if you mean any shadow anywhere including those cast by other moving objects, you will end up doing raycasts from multiple parts of the player's body to multiple light sources, which can get complicated quickly.
     
  3. jikeking

    jikeking

    Joined:
    Jan 20, 2023
    Posts:
    2
    Thanks for the reply, unfortunately I'm referring to shadows anywhere, and if it's too difficult to implement, I'll just give up.
     
  4. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    If you are looking solely for the directional shadow (sun) and if it only matters around the camera, you could use the shadow map of the directional light. You could attach a custom command buffer on the light that blits the shadow map into your own custom render texture, and then you could sample that render texture to see if the player is in shadow or not.

    It's quite technical but definitely possible.
     
  5. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    9,724
    Attach colliders to the bones of the player model. Use RaycastNonAlloc and cast from the lights to these bones. If the raycast produces collision results, they're in shadow. Not only does this work for any light, be it directional, spot, or point, but it can also determine how much the player is in shadow, and is considerably faster than using command buffers and render textures
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    Raycast from player towards light source. If it reaches light source, there is probably no shadow. You'll need to raycast from multiple points of the player to have more believable result.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    - Raycast in the direction of every neighboring light (you will need to partition your world if you want to stay performant)
    - calculate if they are turned on or off and only measure if they are on
    - calculate light attenuation (distance and strength of light from the source)
    - keep tabs on emissive materials nearby and do the same with those
    - the directional lights are more tricky, you need a vector parallel to the light direction but from the bone and check if it collides with anything (is anything covering the character in the direction of directional light?)
    - and you still can't handle GI which means you will have questionable results

    - if you have static lighting you're way better off
     
  8. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    I wonder if an alternate approach could be to use baked static lighting and raycast downard where the player is standing to get coordinates for sampling the lightmap. I haven't looked into sampling lightmaps but it should theoretically be possible.
     
  9. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I mean if I remember correctly there were ways to query light probes, I don't remember though, last I investigated the ways to make a Thief-like gameplay (as in infiltration and stealth) was years ago. Also the new light probe system should be investigated as well, that may work even better. If there is a way to query the light probe field somehow for a specific point in space (which is the sole purpose of them, the question is, is there a developer-facing API?)
     
    SunnySunshine likes this.
  10. RyanCantCode

    RyanCantCode

    Joined:
    Oct 22, 2018
    Posts:
    6
  11. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,784
    I like this attitude! :D