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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Bake Shadows at Runtime

Discussion in 'Universal Render Pipeline' started by solsnare, Feb 11, 2023.

  1. solsnare

    solsnare

    Joined:
    Dec 7, 2016
    Posts:
    20
    I was wondering what actually makes it difficult for a game to sort of "flash bake" shadows at runtime?

    I notice that on mobile platforms having shadows coming from lights kills performance, but it seems like at most it takes like 10-20 ms before it renders the shadows.

    Isn't it possible to render these shadows and then store them in a cache?

    I have heard of "Shadow Caching" in Unity HDRP, is this close to it?

    My game is procedurally generated and I'm playing around with even procedurally placing objects within, and would love to have that feature.

    Any ideas on the best way to "flash bake"? What do you think the problems are with this that I'm not realizing? Is it the writing of a texture to store baked lighting info that slows everything down?

    I guess one obvious thing is that shadows generating as a result of a depth buffer is not saved in world space, but rather screenspace making it so you'd have to take a lot of shadow snapshots to build a comprehensive shadow map?

    Tl;dr
    Basically I'm wondering if there is a really quick shadow baking solution out there that would be practical to use in a loading step for a procedural level on mobile VR?
    (Bonus points if ambient occlusion can be baked quickly too somehow, I know, I'm asking a lot)

    I'm also open to the idea of storing SDFs per object to help with the generation of these maps! (No idea if that would help with the generation of shadows n ambient occlusion)
     
    AydinDeveloper likes this.
  2. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    681
    Shadow caching is exactly what you want! Unfortunately, URP does not yet have this. It would be possible to make an asset/plugin that does this, though! Hmm, I might actually, unless URP is already planning on implementing it?

    The only real downsides to shadow caching are obviously the increased video memory usage from the texture and the increased cost/latency of the shadow texture sample in your fragment shaders. However, these costs are quite minimal compared to realtime shadow rendering, for sure!
     
    solsnare and AydinDeveloper like this.
  3. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    681
  4. solsnare

    solsnare

    Joined:
    Dec 7, 2016
    Posts:
    20
    Omg you work fast, how did you implement that?
    Does it work at scale and on mobile?

    Incredible, exactly what I was looking for!

    Btw yes, URP is planning on implementing it, there's a thread about this:
    https://forum.unity.com/threads/urp-cached-shadows.1342265/

    Still would be interested in understanding how this works!
     
    JesOb likes this.
  5. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    681
    I implemented it as a RenderFeature/RenderPass, which has similar functionality to normal shadowmapping. In the RenderFeature there's some settings for "how often" and stuff like that, as well as a manual mode. I might go ahead and put this on the Asset Store anyways, because "unity time" means it might take them 3+ years to actually get to it lol.

    And yes, it should work on mobile just fine, that was the point.
     
    DevDunk likes this.
  6. solsnare

    solsnare

    Joined:
    Dec 7, 2016
    Posts:
    20
    Awesome, please keep me in the loop for whenever you have it up if you can?

    Also what is your technical background if you don't mind me asking? Are you available for work? I'm building a team and have funding incoming and need a technical artists/graphics engineer. If at all interested, contact me on discord: Soltar#4159 (or DM? Not sure if unity has that)
     
    funkyCoty likes this.
  7. funkyCoty

    funkyCoty

    Joined:
    May 22, 2018
    Posts:
    681
    Hey, I finally had a chance this weekend to wrap up making this plugin haha. Had a really crazy last couple of months, was kept away for awhile

    forum post here: https://forum.unity.com/threads/com...r-real-time-shadows-for-mobile-games.1420124/

    @solsnare when unity's asset store team finally blesses me with their approval i'll send you a key! the approval time could be anywhere from days to months, in my experience. If you'd like a preview sooner, DM me on discord and i'll send you a preview package