Search Unity

NVIDIA PhysX plugin - preview

Discussion in 'Physics Previews' started by HerrVR, Mar 15, 2019.

  1. voidnull_

    voidnull_

    Joined:
    Jun 7, 2019
    Posts:
    1
    Exposing of native physx API to Unity -- possible, is the best stuff ever. At least for people who every time using standard unity components, that can't provide wide functionality as, for example, Physix Immediate Mode. And i have questions about ability to contribute for this project. It's possible for community members?
     
  2. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Yes, when it's published on GitHub anyone will be able to contribute. I can't tell the estimate for this right now, but it should be the nearest future.
     
    rizu, Vytek and yant like this.
  3. blackgun

    blackgun

    Joined:
    May 27, 2014
    Posts:
    9
    I can't see anything in the unitypackage sample.
    My env: Unity 2019.1.12f1 on MacOS 10.14.5. Anything I missed?

    upload_2019-7-30_18-4-45.png
     
  4. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Forgot to mention, the only supported platform atm is Windows x64.
     
    AlanMattano likes this.
  5. MicCode

    MicCode

    Joined:
    Nov 19, 2018
    Posts:
    10
    2019.2
    DllNotFoundException: PhysX.Native

    Works in 2019.1
     
    Last edited: Aug 1, 2019
  6. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Yep. I confirm. 2019.2 can't find native dll if its name contains dots.
    @yant is it known?
     
  7. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    354
    I ran a quick search and looks nobody reported this dll naming thing yet. May I kindly ask you @HerrVR report one please? (using the Editor)
     
  8. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    354
    Aghh, posted back too fast. Apparently, another search in Fogbugz revealed a case exactly like this, currently sitting in triage. It's #1174403, but it's not public as of yet.
     
  9. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Yes, I know about this one :)
     
  10. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    So, Unity answer is

    "It seems to be already fixed on Unity version 2019.3.0a6 and above. The fix should be backported to 2019.2 sometime in the future."
     
    AlanMattano likes this.
  11. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    354
    True. I understand that the wording here might make it sound like the fix will be back-ported right after non-trivial life is discovered at the centre of the Sun, but it's simply because we can't commit to dates as of yet. The fix is upcoming, please stay tuned.
     
    nxrighthere and HerrVR like this.
  12. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Hi all

    I've hit one annoying bug in Unity, here's the issue page:
    https://issuetracker.unity3d.com/is...eobject-is-deleted-manually-in-project-window

    As I understand Unity engineers will only start to look at it when it collects at least 10 votes (Update: No).
    Could you guys vote for it to motivate them to fix it asap?

    Thanks

    Update: Well, about at least 10 votes, it was my misunderstanding. There's no such thing, so I apologize. But you guys can still vote :)
     
    Last edited: Aug 8, 2019
  13. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    354
    Just to clarify, it's not correct to assume bugs are getting looked at only after acquiring 10 votes. We use user pain (https://blogs.unity3d.com/2016/08/17/a-look-inside-tracking-bugs-by-user-pain/) to prioritise fixes, but before scheduling, each incoming incident is getting checked and verified, independently of the votes count.

    Anthony
     
  14. Sposito

    Sposito

    Joined:
    May 4, 2014
    Posts:
    16
    @HerrVR any projection on how long until we have it on on Linux / MacOSX, I am coming from. machine learning environment most tools we use just work better on unix platforms. Linux would actually be ideal for us.
     
  15. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    I plan to build it for Linux when it's ready (a couple of months if all goes well).
     
  16. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    110
    Any ETA on Android? And how can i create PxInputStream for PxConvexMesh? What is the easy way to convert unity mesh to Pxconvexmesh?
     
    Last edited: Sep 1, 2019
  17. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    No ETA yet. Check SampleBase.createGemConvexMesh() function for PxConvexMesh creation. I'll expose stream classes later.
     
  18. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    110
    Hi, In sample scene CreateGemConvexMesh function used with m_cooking
    Code (CSharp):
    1. m_cooking = foundation.createCooking(PxVersion.PX_PHYSICS_VERSION, new PxCookingParams(physics.getTolerancesScale()));
    Is this some sort of data that is already in your sdk? How can i insert unity mesh data to PxCooking or PxConvexMesh?

    I have found some information in here and will try later.
    https://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/guide/Manual/Geometry.html
    If you will support android that would be good for me. I have lots of creative works in my mind.
     
  19. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    That's how it's done in PhysX. You always use cooking to create convex mesh (as well as triangle mesh and height field). You can use 'cooking.cookConvexMesh(desc, outStream)' to cook a convex mesh into a stream and then use that data to create convex mesh into PxPhysics by 'physics.createConvexMesh(inSteam)', or you can use cooking to directly create convex mesh into PxPhysics - 'cooking.createConvexMesh(desc, physics.getPhysicsInsertionCallback(), ...)'.
    But, as I didn't expose streams yet, only second option is available.
    It, most likely, will support all platforms supported by PhysX. Including Android.
     
    segant likes this.
  20. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    110
    Thanks for answer. I appreciate your work. I have managed to write some script and accessing them with dynamic or static rigidbody and rendering them with Universal RP Lit shader. Srp batcher is on and getting higher performance and can change everything even for one specific object in engine. This would be masterpiece if it will be released on all platforms.
    This is what i got with 8 years laptop.
     
    Last edited: Sep 2, 2019
    florianhanke and HerrVR like this.
  21. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    110
    I was trying to code MeshDescription from nvidia site when i was getting Intptr wih GCHandle i discovered you have already coded that in SampleBase :) . I just changed vertice list and triangle from mesh. But triangle mesh doesn't support dynamic rb. Is there any plan to support? And when will you support android? I'm looking forward to it. And unity's capsule is different than yours or different when it's radius and half height. Maybe it's vice-versa with your code. But i have multiplied it with
    Code (CSharp):
    1. Quarnetion.Euler(0,0,90f)
    then it worked.
     
    Last edited: Sep 5, 2019
  22. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    As far as I know PhysX doesn't support TriMesh shapes on dynamic RBs, because TriMesh-TriMesh collision is slow and never works reliable. In most cases it's enough to use convex mesh shape for your dynamic RB. If not, you'll need to compose your dynamic RB shape from several convex meshes.
    I plan to build it for Android (and Linux) when I finish making physics assets and components.
    PhysX capsule shape is oriented along X-axis. To match it with Unity builtin capsule mesh, which is oriented along Y-axis, you need to rotate it around Z-axis on 90 degrees. Btw, PhysX plane shape normal is also oriented along X-axis - it's YZ-plane. To use it as ground you also need to rotate it.
     
  23. MicCode

    MicCode

    Joined:
    Nov 19, 2018
    Posts:
    10
    Any update to the plugin?
     
  24. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Still in progress. Hope to show smth soon.
     
    mjc33, Vytek and hippocoder like this.
  25. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    27
    How will it be possible to connect those to Unity GameObjects or components? Or is this even possible in current state?
    In the demo all meshes seem to be build by Px objects, can we somehow use meshes, or even better a SkinnedMesRenderer?
     
  26. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    The simple way---just take the position of your rigid body and set it to your game object.
    That should work well if the number of rigid bodies is few.
    But---while PhysX can easily simulate 10K+ rigid bodies---updating 10K+ game objects will be too expensive.
    To have bigger number of rigid bodies on the scene, the particle-system--like approach should be used---single game object should spawn multiple rigid bodies, and then use their positions to draw something on the screen.
    In the PhysX Component System (PCS) I'm currently working on. I'm implementing single-body actors (for simple static and dynamic rigid bodies), as well as multi-body actor (for articulation), as well as the possibility for user to create custom actors with multiple rigid bodies and joints inside. That's the idea.
    As for SkinnedMeshRenderer---you just move the game objects used as it's skinning bones.
    Though, to make it efficient and don't create all this bone game objects, one will need to write a custom skinning shader and pass rigid bodies positions there directly. It's a bit advanced but still doable.
     
  27. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    27
    makes sense; but then, for my use cases, this would probably be okay to move some 50 bones Game Objects, the physics (rigidbody + collision + joints) are the expensive operation, having thousands of primitive colliders active.
    Are compound colliders somehow possible (having multiple primitive colliders attached to one rigidbody)? If yes, I'd give it a try even in this early state.
     
  28. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    292
    Not arguing with HerrVR , but in recent Unity versions transforms became really fast, even with Unity's api usage, in my tests updating even few k of transforms in loop every frame wasn't performance problem.
    Im sure physx calculations for 10k rigidbodies will become bottleneck much faster than updating their Unity transforms, unless we talk about gpu rigidbodies, as I never tested them ;)
     
  29. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    PhysX doesn't have compound shapes, but a rigid body can have several shapes attached. For PCS I made a PxCompoundShape asset which holds an array of references to other shape assets and attaches them to a rigid body internally.
     
  30. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Sure simulating 10K rigid bodies on CPU is tough. But applying the result of the simulation to game objects takes even longer. C# is too slow :)
     
  31. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    27
    So I guess PCS is Physics Component System? And I assume PxCompoundShape is only available with next build (did not find it in current build)?
     
  32. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Yes. Current build only has PhysX API exposed. With just API you can use PxRigidActor.attachShape(...) or PxRigidActor.createExclusiveShape(...) to make a rigid body with several shapes.
     
  33. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    110
    I tried havok and unity dots physics. This one is the best in my old computer even that i use amd graphic processor. I may continue develop after you completed Android build.
     
  34. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    27
    Sounds like I should give it a try. @HerrVR will the principals stay the same in later releases? Just don't want to sped too much time, if things change completely. Besides this, I am very excited about this project!
     
  35. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    It should.
     
  36. MicCode

    MicCode

    Joined:
    Nov 19, 2018
    Posts:
    10
    upload_2019-11-4_18-24-53.png
    I tried using the TGS solver for the articulation, it quickly goes crazy.
    Only adding this line to the SampleArticulation.cs before physics.createScene(sceneDesc);

    sceneDesc.solverType = PxSolverType.TGS;


    Any idea what happened?
     
  37. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    I asked PhysX team. They will check it. Thanks.
     
  38. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    292
    Hey is it possible to mess with Unity's created Physx scenes using your api? That would give some awesome opportunities.
     
  39. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    No, it's different instance of PhysX. It doesn't give access to Unity's PhysX.
     
  40. Kobix

    Kobix

    Joined:
    Jan 23, 2014
    Posts:
    65
    TGS is un-stable in Unitys Physx for me too, just on simple box colliders / rigidbodies.
     
  41. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    27
    Made a small test with simulating everything in PhsyX, and using the position and rotation of Px objects to move Unity GameObjects. Works quite well, get around 1K fps with around 500 rigid bodies.
    I do sync like that, not sure if this is the most efficient way:

    myTransform.position = pxRigidDynamic.getGlobalPose().p.ToVector3();
    myTransform.rotation = pxRigidDynamic.getGlobalPose().q.ToQuaternion();

    Also, one observation was that using GPU makes it slower (opposed to the included GPU demo scene); not really sure why this is the case.
     
  42. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    292
    You can save one Unity api call by using Transform.SetPositionAndRotation
     
    Kobix likes this.
  43. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    As Roni92pl said, plus no need to call getGlobalPose() twice. It's not free.

    var pose = pxRigidDynamic.getGlobalPose();
    ...

    500 rigid bodies is probably too small number to get advantage of GPU simulation.
     
  44. stuartiannaylor

    stuartiannaylor

    Joined:
    Oct 20, 2019
    Posts:
    18
    HerrVR firstly apologies as I am a terrible noob, but already with some trial and error beginning to think the Unity Wheel collider is a poor choice for tracked vehicles, in comparison to PxVehicleDriveTank.

    Whats the current state of play with devel and release for an awful noob like me :)

    PS many, many thanks for what I am presuming will be awesome.
     
  45. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    292
    Hey, I have suggestion to make api that would allow to access multiple Physx objects in one call, like SetPositions(Vector3[] positions) or with dictionary where key would be unique id and value would be it's position.
    That way you could 'ask'(and set) for position array of only objects you need, instead of calling pxRigidDynamic.getGlobalPose() for every Physx object to sync Unity transform position with Physx object.
    Other useful api would be for exmaple GetAllRigidbodiesPositions(out Vector3[] positions(or list?)).
     
  46. HerrVR

    HerrVR

    Joined:
    Mar 15, 2019
    Posts:
    41
    Sure. Users will be able to add their own functions to move heavy tasks from C# to C++.
    Check for example PxRigidActorList helper class used in SampleHelloGRB.
     
    Roni92pl likes this.
  47. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    292
    Yup, finally got some time to play with this new implementation, and I must admit I love this 'low-level' api approach, feels like such a relief, like this is how Physx should be integrated by Unity from the beginning, Unity should take notes.
    So I take the opportunity and quickly wrote few 'high-level' components to imitate Unity's Physx implementation rigidbody component and succesfully run Physx simulation on another thread.
    And here comes my other question - because I see it's nicely protected from race conditions when tried to use this api from both threads simultanously - is there chance to make it thread-safe? Im asking because I'd like to run Physx on own dedicated thread, and logic in other, but still being able to communicate between them without forced thread synchronization.
    Also are you going to support some layer system for collisions and raycasts like Unitys implementation? Because from what I can see it's not something trivial that can be done in few hours.
    Great work on this library, works great in editor and windows build and api is clear and pretty intuitive, didn't need any documentation to actually work with it, and that's pretty big, can't wait for the next update, and good luck :)
     
    Last edited: Nov 25, 2019
  48. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    354
    I would argue that considering when the Component-based PhysX integration was made and what state the industry was at back then, it wasn't actually realistic to have the raw APIs exposed then. Unity was basically just beginning to onboard users, and ease of use was #1 priority I imagine. On top of that, some PhysX upgrades wouldn't be directly possible if we didn't have a model that hides most of the internals (eg. PhysX 3.3 upgrade in Unity 5)

    To build up on this, C#/native interop via P/Invoke has always been a big trouble. Marshalling data back and forth in an efficient way has been a super tricky area up until NativeArray was exposed. Priorly, there were copies created all over the place, and they had to be collected by GC later on. For instance, look at how SphereCastAll evolved into SphereCastNonAlloc. It wasn't simply a problem in the early days, but had become one once bigger projects started being created.

    Finally, my personal take on the design here is -- a decent, modern, PhysX integration should be based on the Immediate Mode directly, with data owned and stored in C#. Immediate Mode API should acquire direct pointers to C# data and work with them. I don't believe PxActor/PxShape/PxScene/etc stuff gets anywhere far. It's so interlocked and black-boxy internally. Eg: can't call addForce off the main thread because it writes to islands directly, PxActor::setGlobalPose triggers immediate internal callbacks writing everywhere, etc. If you try to untangle the PxScene stuff, you get Immediate Mode basically.

    Hope that makes sense.

    Anthony
     
    JoNax97 likes this.
  49. MicCode

    MicCode

    Joined:
    Nov 19, 2018
    Posts:
    10
    I've been using Bullet physics for my physics based game because of the low level access, most importantly being able to create multiple physics world and featherstone/articulation. Then I saw this plugin, I immediately feel this is the future, physX simple outperform and better documented than bullet, using this plugin feels like what we are doing with bullet physics right now, so we might even be able to write a wrapper that allow us to switch the underlying engine between bullet and physx easily. Really looking forward to this.
     
    z26 and goncalo-vasconcelos like this.