Search Unity

Feature Request: Rigidbody OnSleep and OnWake callbacks

Discussion in 'Physics' started by Orangy-Tang, Jan 19, 2016.

  1. Orangy-Tang

    Orangy-Tang

    Joined:
    Jul 24, 2013
    Posts:
    39
    Hello!

    I'm working with a large number of dynamic objects and for various optimisations[1] need to know when a rigidbody is put to sleep or woken up. At the moment there doesn't seem to be a way to do this other than polling all of the Rigidbody.IsSleeping methods, which is going to add a lot of overhead due to sheer number of objects.

    Physx has onSleep and onWake, can these be exposed to C# code please? I imagine it'd work like OnCollisionEnter etc. but if need be a single callback or list I can query each frame with the newly slept/awoken objects would also work.

    Thanks.

    [1] Super simplified version: I need to perform various baking operations (geometry merging, texture merging, lighting calculations) on a certain class of rigid bodies. There'll be a large number of them in any given scene, but most will be asleep, so these can be optimised to save draw calls / general rendering perf, with the remaining awake ones being rendered normally. Dynamic batching helps, but is still not enough for the results we need. And by doing these optimisations we can ease the load on dynamic batching so it can work it's magic on the other classes of objects where we need/expect it.
     
  2. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Hello,

    We'll add those eventually (added to the backlog), but can't say when exactly I'm afraid.
    Out of curiosity -- do you have any scripts added to those bodies currently? Do you already run FixedUpdate on them? In that specific case there would be no added performance penalty if you detect the sleep flag changes manually. In general case - you're right, callbacks need to be exposed.

    Anthony
     
  3. Orangy-Tang

    Orangy-Tang

    Joined:
    Jul 24, 2013
    Posts:
    39
    None of these objects are using Update or FixedUpdate, all of the gameplay code is event driven to allow us to support large numbers of them, which is why not having Sleep / Awake callbacks are such a pain.

    Even if I did have have a FixedUpdate, adding the extra memory accesses, cache misses and branch to check IsSleeping every frame would *definitely* not be 'no added penalty'. (again, think *lots* of objects in a perf-critical VR game).

    Is there some kind of feature backlog where I can track progress?

    Thanks.
     
    Marc-Saubion likes this.
  4. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Right, I see. It's not on the public roadmap yet. We're pretty packed for the first quarter of 2016 right now, so there's no estimate I could share with you I'm afraid. Thanks for your feedback.
     
  5. Orangy-Tang

    Orangy-Tang

    Joined:
    Jul 24, 2013
    Posts:
    39
    Hello!

    Has there been any update to this?
     
    Marc-Saubion likes this.
  6. roboman5000

    roboman5000

    Joined:
    Sep 27, 2016
    Posts:
    19
    This thread is about a year old, but I wanted to check if there have been any changes. There have been so many great updates in the past few releases - wondering if you have a better idea when it would move out of the backlog?

    With so many objects in the scene, our team is using coroutines as a workaround to check if the rigidbody has fallen asleep. It works, but with potentially hundreds/thousands of coroutines it doesn't scale really well.
     
  7. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Unfortunately, there are no dates to share with you yet, but I hope we'd be able to address it this year. Clearly, the feature is needed for bigger projects.
     
    MelvMay likes this.
  8. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Yes please, very much needed!
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Any movement on this? I needed them today, and assumed they already existed somewhere but no joy.
     
    Marc-Saubion likes this.
  10. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    I'm afraid not. I think we'd needed to approach the performance of the callbacks first. If we exposed it as an actual callback we'd get an analog of OnContactStay that very few people can actually afford using.
     
    hippocoder likes this.
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Currently I track stay per object by checking enter and exit, and it's here I wake up the script which periodically checks for sleep condition. It's okay, and not too bad, was a quality of life thing more than anything. I would much rather you focus on the big and bad jobs!

    thanks for reply :)
     
  12. standardending

    standardending

    Joined:
    Dec 2, 2017
    Posts:
    1
    I really need this feature, in my case it's still possible to wake up a rigidbody without invoking the OnCollisionExit and OnCollisionEnter callback.:(:(:(
     
  13. Dinamytes

    Dinamytes

    Joined:
    Nov 3, 2016
    Posts:
    51
    Sad this is not going to happen, in order to detect possible OnWakes must depend on a MonoBehaviour with
    OnCollisionEnter.
     
    Marc-Saubion likes this.
  14. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Any word on this? Or is it correct to assume this will not happen?
     
    Marc-Saubion and chadfranklin47 like this.
  15. wimachtendink

    wimachtendink

    Joined:
    Jan 21, 2018
    Posts:
    8
  16. StCost

    StCost

    Joined:
    Mar 1, 2017
    Posts:
    30
    One more person, who needs this.
    I need to switch lights to cached shadows, when nothing is moving around. And switch to real time shadows, when active rigidbody is here.

    I workarounded this with script, which creates sphere trigger. And trackes every rigidbody, which enters the trigger (and ofc removes, which exited). And every 7th Update() it checks list of rigidbodies, who's asleep who's awoken.

    I don't know how good it is performance wise. I have about 30 objects with this script. But I removed 1.5ms from my main thread, by doing this.
     
    Last edited: Sep 16, 2021
    PutridEx likes this.
  17. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    upload_2021-9-16_13-56-38.png

    It kinda is supported by Physx but I'm not sure how efficient that is crossing over to C# land. (from https://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/guide/Manual/RigidBodyDynamics.html)

    A lot of callbacks are potentially fired - no problem in C++ land but in Unity it probably might be better to have a native array that's populated with results like the current contact mod method. You'd subscribe to just one Unity callback and then parse the results. Just a thought really, because the old OnEnter/Exit etc is horribly inefficient and not designed for scalability in the slightest.
     
  18. EJSainz

    EJSainz

    Joined:
    Mar 24, 2015
    Posts:
    30
    I need this also.

    I'm working on a simple example of a wall of boxes falling down when an explosion hits, for the sake of teaching the Sleep() and WakeUp() methods. The students are supposed to Sleep() all the bricks, then wake up them just before applying an AddExplosionForce(). It works ok, but some boxes will stay floating when the ones below move, as there is a little distance between them.

    I'm waking up the whole wall when a single box receives the explosion force, but an OnWakeUp() event would be great to do the same when one of the boxes in the wall gets awaken by some mean other than the explosion.
     
  19. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,136
    Been quite a few years, still nothing :D

    Any hope for this to land in 2022?
     
    Marc-Saubion likes this.
  20. kookyoo

    kookyoo

    Joined:
    Apr 19, 2010
    Posts:
    53
    Any hope for this to land in 2023 ? xD
     
    Marc-Saubion likes this.
  21. PS_SuperJura

    PS_SuperJura

    Joined:
    Apr 3, 2018
    Posts:
    7
    Still need this feature :)
     
    Marc-Saubion likes this.
  22. kookyoo

    kookyoo

    Joined:
    Apr 19, 2010
    Posts:
    53
    Any hope for this to land in 2024 ? 2025 ? ;)
     
  23. Armegalo

    Armegalo

    Joined:
    May 14, 2018
    Posts:
    30
    Purleeeeease!