Search Unity

[Released] Ambient Character Shadows - A Capsule soft shadow implementation

Discussion in 'Assets and Asset Store' started by cgDoofus, Dec 27, 2021.

  1. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Screenshot_5.png



    I've found many implementations and many people discussing this famous technique, so I decided to have my own take at it and to also play around with compute shaders. This is a technique used in many AAA titles that greatly helps with making characters feel more grounded in shadowed areas and allows them to have area light shadows. With my implementation, it seemlessly integrates with the existing light probes system, as the shadows' directions are sampled per character from its interpolated light probe.

    Features:
    • Uses a single compute shader for everything.
    • There's no limit on the number of characters, and the screenspace implementation makes it way more performant than classic shadow mapping.
    • Easy setup, only 1 component needs to be added to a character gameobject that has capsule colliders as children.
    • Renders a mask for character meshes so that they don't have self occlusion artifacts.
    • Samples the dominant light direction from the interpolated light probe the character is at, and each character works independantly.
    • Uses an axis aligned bounding box based screen space culling per character to optimize an already very performant shader, while smoothly blending out the shadows as they reach their specified radius.
    • Allows for rendering ambient shadows only in already shadowed areas by directional light (Built in implementation, forward path only).
    • Built in render pipeline support (requires no dependencies).
    • HDRP Support through custom pass, supports VR multipass.
    • Post Processing V2 custom effect implementation, supports VR multipass and single pass instanced through XR plugin management.
    • Works in the editor as well with all implementations!
    Roadmap:
    • URP support
    • Adding spheres and boxes.
    • Adding more ray marching based shadowing algorithms, maybe even emission.
    • The option to render at half resolution to support very low end hardware.
     
    Last edited: Mar 27, 2022
  2. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
  3. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    This looks really nice!
    Will check it out :)
     
  4. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Thank you!

    New update waiting for approval!
    • Added full support for Forward path
    • Added a new option in Shadow Renderer to only render ambient shadows in shadowed areas as shown below.
     
  5. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Another update awaiting approval!

    • Added support for multipass VR
    • Bug fixes and performance optimizations, now it computes 0.25x the pixels it used to so it should theoretically have a 4x performance boost in the GPU part.
    Next up I'll try to tackle HDRP support.
     
    PutridEx and Ferapod like this.
  6. CastryGames

    CastryGames

    Joined:
    Oct 1, 2014
    Posts:
    77
    urp please
     
    Lars-Steenhoff likes this.
  7. Ferapod

    Ferapod

    Joined:
    Dec 8, 2017
    Posts:
    1
    looking forward to HDRP support.
    thank you very much for your work
     
  8. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48

    Major update pending approval!

    V2.0
    • Added HDRP Support through custom pass
    • Added Post Processing V2 implementation
    • Now it works in the editor too with those implementations!
    • The old implementation is still there as it's the most suitable for mobile devices and requires no dependencies.
    • Some optimizations and bug fixes and rewriting the fetching capsule colliders code since this now has to work in the editor dynamically.
    Anytime!

    gave it a shot but decided to release this first, definitely working on this now
     
    Lars-Steenhoff likes this.
  9. jmgek

    jmgek

    Joined:
    Dec 9, 2012
    Posts:
    177
    Any update for urp?
     
    Lars-Steenhoff likes this.
  10. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    I've postponed URP support because as of right now there's no camera normals support, there are workarounds but they seem "too hacky" so I think it'd be a better deal to wait for proper support, if any.
     
    jmgek likes this.
  11. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48


    Update V2.1:

    • Added Luminance Blend, that controls how much luminance "hides" shadows, can be adjusted so that shadows are only shown in shadowed areas. This is much cleaner than the previous implementation that relied on sampling the shadow map from a single light which in this case was the main directional light.
    • Rewrote the built-in render pipeline version to work with a command buffer for everything, much cleaner this way and now also works in the scene view.
     
    Last edited: Mar 26, 2022
    Lars-Steenhoff likes this.
  12. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    I'm trying out the 2.1 version on Mac with unity 2022.b13. and after I return to the scene when exiting playmode the scene becomes black. Is this a known error?

    Default RP

    Metal: Compute shader missing buffer binding at index 1 (capsules)
     
    Last edited: Mar 26, 2022
  13. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    That's weird, try resizing the window and it'll reset buffers, though I'll handle that in the next release.
     
    Lars-Steenhoff likes this.
  14. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    I have a feeling its actually not working on my computer, because when I change the radius of the capsule shadow in the demo scene nothing changes, I will try it in 2021 and see if that helps, have you had ang users that were able to run on MacOS?
     
  15. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    I just tried it on 2022.1.0b13 and it works normally on Windows, I haven't had someone test it for me on MacOS, but I'll try to get access to a macbook.
    Does the previous version (2.0) work fine?

    Edit: actually there's a weird behaviour with shadow radius on 2022, it updates only when the mouse hovers over scene/game view not interactively with the slider, will look into that as well
     
    Last edited: Mar 27, 2022
  16. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,527
    Thanks for looking into this, I actually never used v2 as I bought it few days ago and 2.1 was the first version I could download.
     
  17. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Try downgrading to 2.0, the built in RP implementation is fundamentally different, though I don't see why the current version wouldn't work on a mac, will get to it as soon as I get my hands on one.

    On other news, I just pushed a new minor release 2.1.1 with a fix to a memory leak that was happening in built in RP version caused by CommandBuffer.SetFloatParam.
     
    Lars-Steenhoff likes this.
  18. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    633
    Can I use the capsule shadows for direct light as well, not just ambient?
     
    Last edited: Apr 15, 2022
  19. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Sure you can just bump up shadow sharpness value and modify the light direction passed to the shader, however unless your character is a bunch of capsules it will look odd
     
    blueivy likes this.
  20. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    633
    I'm actually fine with having soft direct shadows, just wanted to see if I would need to make any changes to she shader which you answered! Thanks!
     
  21. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    You'll find what you need to change in Character Shadow script, you can modify the light direction vector that gets passed in a struct to the shader
     
    Last edited: Apr 18, 2022
    blueivy likes this.
  22. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    633
    Thanks for the information, asset bought! Excited for everything on your roadmap. Specifically the spheres and box support.
     
    cgDoofus likes this.
  23. atomicjoe

    atomicjoe

    Joined:
    Apr 10, 2013
    Posts:
    1,869
    Hi @cgDoofus ! I love your ambient shadows!
    Will you be implementing box shadows along with capsule shadows? I would really need them for doors. :)
     
  24. pojpitak

    pojpitak

    Joined:
    Mar 7, 2018
    Posts:
    2
    Hello,

    It's exactly what I need for my game!

    However, I'm facing issues where shadows only appear on box and planes.

    I'm having issues where the shadows won't appear on light-baked meshes?

    Also, I can't seem to change the direction of the shadow, even while moving the directional light into another direction.

    Any help would really be appreciated!

    Thanks!
     
  25. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Hello! This doesn't work too well when shadows overlap, so if a character's capsule shadows are over a door's box shadow it will have some artifacts, also the door's shadow probably wouldn't blend too well with the rest of the environment.
    I'll try to figure out a good way to support arbitrary shape shadows with the current capsule groups (characters) system though :)

    That's odd, it's a screenspace effect therefore it should show up on all sorts of materials, even unlit ones, maybe it has to do with Luminance Blend factor? also which version are you using?
    The shadow direction isn't controlled by directional lights, it approximates the dominant light direction from light probes. However it's easy to change this behaviour and pass in your own shadow direction vector, there's a method in CharacterShadow script named lightDirection, you can replace its logic by whatever you want.
     
  26. pojpitak

    pojpitak

    Joined:
    Mar 7, 2018
    Posts:
    2
    Yeah, I'm having issues with it. While the demo scenes work. The other scenes doesn't sometimes, would you be able to help trouble shoot or understand it better by any chance.
     
  27. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    Sure, send me a PM.
     
  28. Onat-H

    Onat-H

    Joined:
    Mar 11, 2015
    Posts:
    195
    Hi, the asset doesn't seem to work on metal (macOs). the capsule shadows compute shader throws an error in line 157 (cannot implicitly convert from uint2 to uint3 at kernel DeferredPath)
     
  29. cgDoofus

    cgDoofus

    Joined:
    Jun 15, 2021
    Posts:
    48
    I'm going to push an update with a fix once I can test this on a mac, in the mean time though just change
    uint3 coord
    to
    uint2
     
  30. Onat-H

    Onat-H

    Joined:
    Mar 11, 2015
    Posts:
    195
    Sorry for the late reply, I tried that, but all the other params like color, depth and normal need uint3, so that doesn't work.
     
  31. shma_unity

    shma_unity

    Joined:
    Jun 13, 2018
    Posts:
    118
    Hello,
    I've been integrating ambient shadows for my project and they are working quite nicely. However, I stumbled upon a bit of a glitch.

    When DLSS (Nvidia's deep learning super sampling) is enabled for dynamic resolution on the camera settings, the character itself starts to get a black flicker effect. When going close to it.

    The effect is even slightly there when DLSS is off, but only when going very very close to the object.

    It looks like the flicker is coming from the excluded mesh itself.

    upload_2022-9-26_4-23-59.png
    upload_2022-9-26_4-24-24.png


    Edit: I was able to fix the issue by changing line 175 in CapsuleShadows.compute

    from

    uint mask = 1? depth - exc_marching[coord].x < 0.00001 : 0;


    to

    uint mask = 1? depth - exc_marching[coord].x < 0.001 : 0;
     
    Last edited: Sep 26, 2022
  32. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    Seems like this requires light probes to work - reflection probes won't work?
     
  33. DarknessBlade

    DarknessBlade

    Joined:
    Apr 21, 2014
    Posts:
    11
    Hello, does this work with unity 2019.4 lts built-in pipeline & deffered rendering mode ?