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

Resolved Terrain with Dots (Physic, and global approach)

Discussion in 'DOTS Dev Blitz Day 2023 - Q&A' started by HBG-Mathieu, Aug 24, 2023.

  1. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Hello,

    I saw recently that Terrain Collider were supported. In fact it's on the built-in/compagnon list, but there is no such convertion, is that a manual error ?
    Anyway someone on Discord was nice enough to show me a code sample here.

    So I took it (only the needed part) and openned the Physic samples. I went to the parade scene (collisions folder if I'm right), created a small default terrain 10*10 that I put under the "objects". Obviously when I hit play the terrain disappear because it's not supported by default by Entities.Graphics. But the collider is there and the objects won't get through. That's nice, except... it's horribly slow ! I check in the profiler and the job of the narrowphase takes 50ms. Most of it comme from the mesh and convex collider, so does it mean that the terrain collider is not production ready?

    On a more general note, I always read on internet that unity terrain is less performant than a mesh. But I'm not sure that the people who say this, are thinking about a mesh with a splatmap shader and the hole support (quite tricky with the collider). In my mind, the terrain tool should should generate the same thing when the build is generated, but I don't find any information here.

    So, what's the good approach today with DOTS to handle and stream a big terrain (only limited by the computer), from a rendering and physic point of view ? I'm curious about a small one too.

    Thanks !
     
    UniqueCode likes this.
  2. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    213
    The lower level Unity Physics
    TerrainCollider
    allows you to create height patches which you can create on the fly. There is no tooling available yet for creating these easily within the editor during authoring unfortunately, but it's being looked at.

    If you have a specific performance issue with the use of this type of collider, feel free to share some specific information and content for it, such as a project or a profiler screenshot.
    For sending us a project to reproduce the performance issues, you can submit the project through the bug report intake system.
     
    HBG-Mathieu likes this.
  3. schembri-unity

    schembri-unity

    Unity Technologies

    Joined:
    Jul 8, 2020
    Posts:
    12
    Hello,
    Unity Physics currently doesn't support the conversion of the classic physics Terrain Collider - yet. There are components in the built-in component list that are not compatible with ECS, and within some of the more complex built-in components (ex: Configurable Joint), not all the of features are fully compatible yet. It is on our roadmap to improve this.

    That said, Unity Physics itself does support Terrain Collider and the best way to create a terrain collider is by calling TerrainCollider.Create(), similar to what that code sample shows. Then you can use the BlobAssetReference of this new collider to create a static body from it (ex: CreateStaticBody(position, orientation, collider). After a quick scan of that code, I'm not seeing any reference to the terrain being static in that example.

    Can you verify that the terrain being generated is static? Because dynamic - dynamic collisions between meshes will be slow.
     
    HBG-Mathieu likes this.
  4. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    Infortunately it is, I think, static flag, no PhysicMass, no PhysicVelocity... I just tried to add a kinematic mass just to check, same thing. I'll report it I guess then.

    PhysicsSamples - ColliderParade - Basic - Windows, Mac, Linux - Unity 2022.3.0 DX11.jpg Profiler.jpg

    But without this issue, is there a performance issue at the moment between the terrain approach and the meshes approach? In fact a height map collider looks like it could be smarter than a mesh collider, no? Even if I don't know about dots streaming and terrain.
     
  5. schembri-unity

    schembri-unity

    Unity Technologies

    Joined:
    Jul 8, 2020
    Posts:
    12
    Thanks for verifying it's static. It's strange that the narrowphase is taking so long for such a small terrain.

    If you go through the officially supported method to generate a terrain, can you make a comparison of the narrowphase between generating the terrain as a mesh collider vs as a terrain collider? How does that compare to generating the terrain from the built-in Terrain Collider?

    If you think that you found a performance issue, then I agree with Daniel and you should send us a bug report.
     
  6. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    I think I'm missing something isn't the "officially supported method to generate a terrain" the same as "generating the terrain from the built-in Terrain Collider" ? Or there is a doc somewhere I don't know about ?
    I mean, I would have done the same thing that in the code sample at the beginning.

    Oh I'm stupid, if the code ask for triangle collision method, it will probably be the same result as a mesh.
    I'll try tomorrow to convert a simple terrain (with some height changes) to a mesh, and compare both !
     
  7. schembri-unity

    schembri-unity

    Unity Technologies

    Joined:
    Jul 8, 2020
    Posts:
    12
    This is something we should have done a better job of communicating and the team will work to improve this. I searched through the Unity Physics documentation and there are few mentions of terrain. Internally, we have numerous tests for terrain collider, which could be helpful, but these aren't included in the publicly available PhysicsSamples.

    There also seems to be a typo in the documentation regarding what built-in features will work with ECS/Unity Physics and which ones aren't supported yet. The built-in Terrain Collider Component that has the tooling is one of these features that isn't supported by baking yet. This leaves authoring terrain in Unity Physics to be done via code through the TerrainCollider.Create() method (or to alternatively use a MeshCollider.Create() approach).
     
    HBG-Mathieu likes this.
  8. HBG-Mathieu

    HBG-Mathieu

    Joined:
    Feb 16, 2023
    Posts:
    59
    So , inside the ColliderParade - Basic scene (PhyscisSamples).
    I added a default flat square terrain of 30*30 with still the default resolution 1024 (that hurts, the mesh is the perfect duplicate). Also I removed the cylinder and the suzane to be fair with the builtin physic.
    Then I tried these :


    - No Dots config (mono + terrain) : when everything stop falling I have less tan 2ms of player loop
    - Dots with Terrain mesh (definitely stupid if you need the same geometry, I should have think 2sec) : when everything is down, 550ms on the narrowphase
    - Dots with Terrain mesh (hull convex) : player loop around 3ms with narrow phase at 0.15s, but it's pretty much just walking on a plane at this level
    - Dots with Terrain collifer (heighmap) : slow down like crazy when hitting the terrain -> 2 workers 42/62ms on narrowphase create contact job. Reducing the terrain details don't change much (I went down to 8/96 config), most of the time there is just one worker at 100ms.

    Well I guess it's time to wrap the unity package and create that bug report, except if you're telling me that's normal @schembri-unity .

    Thanks and have a nice day !
     
  9. schembri-unity

    schembri-unity

    Unity Technologies

    Joined:
    Jul 8, 2020
    Posts:
    12
    Thanks for this info. The 550ms slowdown isn't acceptable at all. Please send us that bug report.
     
    HBG-Mathieu likes this.