Search Unity

Is there culling sample for ecs jobs?

Discussion in 'Entity Component System' started by HeronHuang, May 23, 2018.

  1. HeronHuang

    HeronHuang

    Joined:
    Nov 30, 2012
    Posts:
    27
    I just do instancing render meshes in the jobsystem, how to do cull?Is there any samples?
    thx
     
  2. avvie

    avvie

    Joined:
    Jan 26, 2014
    Posts:
    74
    Probably at this stage you will have to write this yourself from what i understand. I am curious if there are plans for such systems to get implemented by the engine team into ECS
     
  3. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    We are working on some early prototypes of frustum culling and lod systems as part of the ecs rendering package, they should be part of the next release.
     
    Krajca, Orimay, TheGabelle and 10 others like this.
  4. HeronHuang

    HeronHuang

    Joined:
    Nov 30, 2012
    Posts:
    27
    very happy to hear that:)
     
  5. Afonso-Lage

    Afonso-Lage

    Joined:
    Jul 8, 2012
    Posts:
    70
    They are already available, you can just add MeshCullingComponent to and entity and it should be culled. I didn't tested it myself, but those systems are avaliable already:
    • MeshCullingComponent.cs
    • MeshFrustumCullingSystem.cs
    • MeshLODComponent.cs
    • MeshLODSystem.cs
    If you want to see the source, just read it on Unity cache folder. On windows 10 it's located on:
    %LOCALAPPDATA%\Unity\cache\packages\packages.unity.com\com.unity.entities@0.0.12-preview.5\Unity.Rendering.Hybrid\
     
  6. Enrico-Monese

    Enrico-Monese

    Joined:
    Dec 18, 2015
    Posts:
    77
    This is very cool news!
     
    recursive likes this.
  7. HeronHuang

    HeronHuang

    Joined:
    Nov 30, 2012
    Posts:
    27
    thx,i will give it a try
     
  8. jsestini

    jsestini

    Joined:
    May 15, 2017
    Posts:
    4
    wow, added the MeshCullingComponent to our entities and got a nice performance boost.

    I couldn't find and example of setting up the MeshLODComponent. It's unclear how to associate the 3 LOD with the component. Anyone have an example?
     
  9. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    You create multiple Entities with MeshLODComponent each one having a different LOD level assigned. Then you create an entity with MeshLODGroupComponent (or attach that to eg the first entity). All MeshLODComponent's must reference the entity of the MeshLODGroupComponent they belong to. The system will now calculate which LOD level should be used for a MeshLODGroup and attach MeshLODInactive to the ones that are currently not active. The MeshInstanceRendererSystem ignores all objects having a MeshLODInactive.

    I didn't use it yet, so no code to share.
     
  10. Roni92pl

    Roni92pl

    Joined:
    Jun 2, 2015
    Posts:
    396
    Imo it would make more sense to implement culling BEFORE calling rendering methods.
    Calling render everything and then culling not visible to camera seems like unnecessary work.
    Edit: unless this culling component cooperates with unity's internal renderInstanced method, then ignore my comment.
     
  11. jsestini

    jsestini

    Joined:
    May 15, 2017
    Posts:
    4
    thanks, that's enough to get started.
     
  12. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    Maybe these got removed or changed to something else. I was unable to find them in my cache (Unity 2018.2.3f1, com.unity.entities@0.0.12-preview.12).
     
  13. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    They did indeed get removed culling no longer attaches these components since that approach didn't scale to millions of renderers in a scene.
     
    Krajca likes this.
  14. starikcetin

    starikcetin

    Joined:
    Dec 7, 2017
    Posts:
    340
    What is the new approach?
     
  15. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Frustum culling is now taken care of in the MeshInstanceRendererSystem. If an entity has the MeshInstanceRenderer and LocalToWorld components, it will get rendered by the MeshInstanceRendererSystem (no culling). Additionally, if it also has a WorldMeshRenderBounds component, it should get culled by that same system.
     
  16. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    You say "Millions of renderers in a scene".. With a simple green cube, I'm already running into 50ms of rendertime for about 10k cubes (with a shared meshinstancerenderer).. Is there something I'm missing?
     
  17. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    You will not display all of them at once. Like in Megacity demo thy have to be culled and loaded and unloaded dynamicly.
     
  18. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    How would I go about culling these with regards to my camera('s)?

    In short: I'm trying to render stars around the user based on the user's (and the star's) actual location. This takes quite a bit of Sin/Cos/Tan-Math to do..
    I've currently got it working for 100 Stars, where the Positioning is taking roughly .1ms, and the Rendering is taking about 1-2 ms.. (On Unity 2018.2.13f1, with Entities 0.0.12-preview.19).. Increasing the # of stars leads to crashes & freezes though, with the Editor crashing once every 5 minutes atm..)
    The total # of stars I want to use is +/- 10k. This amount is spaced out across all 3 dimensions around the user/camera

    Edit: I'm currently at 4000 Entities, with the Positioning (the math) still only taking 0.1Ms, but the rendering already taking 12ms
     
    Last edited: Nov 2, 2018
  19. mikerz1985

    mikerz1985

    Joined:
    Oct 23, 2014
    Posts:
    79
    What are you rendering on? Are you using the meshinstancerenderer with GPU instancing on your material? Are the models complex? You'll want to LOD it, if there's some kind of zoom functionality.


     
  20. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    >What are you rendering on?
    How exactly do you mean? It's running on my laptop (an HP Elitebook 8740w), with a single, shared MeshInstanceRenderer
    >Are you using the meshinstancerenderer with GPU instancing on your material?
    Yes
    >Are the models complex?
    They are the Unity-Cube-Primitives.. I believe they have 24 Vertices per mesh.
    >You'll want to LOD it, if there's some kind of zoom functionality.
    There isn't. Only Frustum culling is what I'm looking for..

    I've also since found out that if I add a "Frozen"-component to my instances, the render-times drop from 25-30 ms to around 3-5 ms...
     
  21. Ylly

    Ylly

    Joined:
    May 15, 2015
    Posts:
    24
    Hi there. Itried to use this way and have some issue with MeshLODComponent. I have some SpawnsPoints. In first OnUpdate call i have init data and creates some SpawnsPoints entities and adding them MeshLODGroupComponent. Then in IJobParallelFor spawning Units from SpawnPointData and seth evry unit
    MeshLODComponent with ling to SpawnPoint entity with MeshLODComponent. When i hit Play i got crash immediately. May be this way doesn't work or how can i use LODs with MeshInstanceRenderer? Some code example will be awesome? So i saw topic about custom system getting from Unity.Rendering.Hybrid and anyway should CopyPaste system from this package and use it in my project or how should i do? Should LODs work by defaults?
     
  22. AdamGoodrich

    AdamGoodrich

    Joined:
    Feb 12, 2013
    Posts:
    3,782
    Are there any samples on how to set this up. I added the component but was unable to get any culling - so no doubt there is a bit more to it than this.
     
    T-Zee likes this.
  23. junglemason

    junglemason

    Joined:
    Dec 30, 2010
    Posts:
    69
    I read a comment in another forum that the Scene camera is included when doing frustum culling, so therefore you wouldn't be able to troubleshoot frustum culling using the Scene window. I don't know if this is true, I haven't had a chance to confirm it yet.
     
  24. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Setup WorldMeshRenderBounds offset on some point not in center of an object and you see that mesh culled :)

    It's not true as you can see in video above :)
     
    AdamGoodrich and Chiv like this.
  25. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    Frustum culling is definitely NOT in the MeshInstanceRendererSystem yet.
    I was able to do my own implementation though, by adding a [Frozen]-Component to the entities I wanted to have culled, and adjusting the code for the MeshInstanceRendererSystem (which can be found under %LOCALAPPDATA%/Unity/Cache/packages/packages.unity.com/com.unity.entities@0.0.12-preview.19/Unity.Rendering.Hybrid ) and commenting out any rendering of Frozen Entities.
    This dropped my timing (for the rendersystem) from 15ms to about 2ms
     
    AdamGoodrich likes this.
  26. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    You wrong WorldMeshRenderBounds it's part of MeshInstanceRendererSystem, if you look at source code, it's do frustrum culling. And it works, look at my sample above.
     
  27. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    It is but it's chunk based. Each chunk is culled as a whole. CullLODToVisible is doing that.

    Frozen is there to prevent the TransformSystem from updating the position and for some chunk based culling optimizations. Objects having Frozen set (by adding Static) should still be rendered.
    You should rather add a new Culled component.
     
  28. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Today I'm first :cool: (and it doesn't matter that the answers complement each other :D)
     
    julian-moschuering likes this.
  29. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
  30. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    >You should rather add a new Culled component.
    I tried to.. Tried to copy/paste the MeshInstanceRenderSystem so I could disable it, then extend my own version..
    Turns out Unity doesn't like 'unsafe'-code in the project (but is fine with having it in a package)
     
  31. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Wrong. It's not render behind camera, it uses frustrum planes for clipping, look at source code - CullLODToVisible parallel job.
    upload_2018-11-8_16-19-55.png

    Where Planes it's FrustrumPlanes
    upload_2018-11-8_16-20-19.png

    WorldMeshRenderBounds is entity bounding sphere for frustrum culling
     
  32. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Rendering and Culling both require an entity to have the MeshInstanceRenderer and LocalToWorld components. Culling also requires the WorldMeshRenderBounds component. The system culls and renders to each active camera (game camera, scene camera, etc), so you won't be able to see the results of the culling.

    To get around the unsafe code issue, create an assembly definition file and check to allow unsafe code.
     
  33. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    And as you can see in source code, it's use only one active camera for getting frustrum planes :)
    upload_2018-11-8_16-33-15.png
     
  34. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258

    Output without my own culling:
    150k verts, 78.6k tris, 7.5-15ms of rendertime, 75 batches
    https://gyazo.com/765c7f81bb3a775c69d4cc1b94c51730

    Output with my own culling:
    81k verts, 44k tris, 2-5 ms of rendertime, 54 batches
    https://gyazo.com/4044efc1eb6e3a24c972d4d85fee7dad

    Note: Scene tab is closed/nonexistent for both
    Tell me again how it's not rendering objects behind the camera?
     
  35. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Yes, but the renderer system is updated from the RendererBootstrapSystem, which uses Camera.onPreCull (which i believe is used on every camera?).
    Code (CSharp):
    1. [ExecuteInEditMode]
    2.     public class RenderingSystemBootstrap : ComponentSystem
    3.     {
    4.         protected override void OnCreateManager()
    5.         {
    6.             RenderPipeline.beginCameraRendering += OnBeforeCull;
    7.             Camera.onPreCull += OnBeforeCull;
    8.         }
    9.  
    10.         protected override void OnUpdate()
    11.         {
    12.         }
    13.  
    14.         [Inject]
    15. #pragma warning disable 649
    16.         MeshInstanceRendererSystem m_MeshRendererSystem;
    17.  
    18.         [Inject]
    19.         LODGroupSystem m_LODSystem;
    20. #pragma warning restore 649    
    21.         public void OnBeforeCull(Camera camera)
    22.         {
    23. #if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
    24.             var prefabEditMode = UnityEditor.SceneManagement.StageUtility.GetCurrentStageHandle() !=
    25.                                  UnityEditor.SceneManagement.StageUtility.GetMainStageHandle();
    26.             var gameCamera = (camera.hideFlags & HideFlags.DontSave) == 0;
    27.             if (prefabEditMode && !gameCamera)
    28.                 return;
    29. #endif
    30.            
    31.             m_LODSystem.ActiveCamera = camera;
    32.             m_LODSystem.Update();
    33.             m_LODSystem.ActiveCamera = null;
    34.  
    35.            
    36.             m_MeshRendererSystem.ActiveCamera = camera;
    37.             m_MeshRendererSystem.Update();
    38.             m_MeshRendererSystem.ActiveCamera = null;
    39.            
    40.         }
    41.     }
     
  36. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Your numbers not important, while you not show code, and how you setup WorldMeshRenderBounds, I can show you absoluteley different numbers, and with your numbers I just can say you don't understand how it works. I shown you engine source code which show HOW it works, and it works perfect.
     
  37. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep, but it's not affect Scene camera as you can see in video and it can be bug may be :) Also your code old? :) It's from preview.19 and b8
    Code (CSharp):
    1. using System;
    2. using Unity.Entities;
    3. using UnityEditor.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.Experimental.Rendering;
    6.  
    7. namespace Unity.Rendering
    8. {
    9.   [ExecuteInEditMode]
    10.   public class RenderingSystemBootstrap : ComponentSystem
    11.   {
    12.     [Inject]
    13.     private MeshInstanceRendererSystem m_MeshRendererSystem;
    14.     [Inject]
    15.     private LODGroupSystem m_LODSystem;
    16.  
    17.     protected override void OnCreateManager()
    18.     {
    19.       RenderPipeline.beginCameraRendering += new Action<Camera>(this.OnBeforeCull);
    20.       Camera.onPreCull += new Camera.CameraCallback(this.OnBeforeCull);
    21.     }
    22.  
    23.     protected override void OnUpdate()
    24.     {
    25.     }
    26.  
    27.     public void OnBeforeCull(Camera camera)
    28.     {
    29.       if (StageUtility.GetCurrentStageHandle() != StageUtility.GetMainStageHandle() && (camera.hideFlags & HideFlags.DontSave) != HideFlags.None)
    30.         return;
    31.       this.m_LODSystem.ActiveCamera = camera;
    32.       this.m_LODSystem.Update();
    33.       this.m_LODSystem.ActiveCamera = (Camera) null;
    34.       this.m_MeshRendererSystem.ActiveCamera = camera;
    35.       this.m_MeshRendererSystem.Update();
    36.       this.m_MeshRendererSystem.ActiveCamera = (Camera) null;
    37.     }
    38.   }
    39. }
     
  38. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Maybe... i don't know. o_O
    If I throw in a Debug.Log(camera.gameObject.name) in that OnBeforeCull method, it logs both Main Camera and Scene Camera. I'm on 2018.3.0b8 if that makes a difference.
     
  39. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Look video, I show special for you that it's not render behind camera, and problem only in your code.
     
    Antypodish likes this.
  40. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    Turns out my MWRB-radius was being reset to 0 somehow.. After deleting & reinstalling Unity (and the packages), frustum culling does appear to be working now..
    Verts: 65k
    Tris: 35k
    Batches: 35
     
  41. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Good to know you'll see truth now :) Enjoy :)
     
  42. FrankvHoof

    FrankvHoof

    Joined:
    Nov 3, 2014
    Posts:
    258
    Hahaha thx.. Weird btw that you have to update the MWRB-Center if the Entity moves, instead of having the MIRS use Position as well...
     
  43. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep it works only by manual updating Center (using Position good only if use Offset instead center in cases where you don't need Center in object pivot)
     
    Last edited: Nov 8, 2018
  44. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    There is a 'Allow unsafe Code' in the Player settings for loose scripts and each asmdef has this option too.
     
  45. Alex1337rus

    Alex1337rus

    Joined:
    Mar 30, 2016
    Posts:
    37
    How can i add WorldMeshRenderBounds component? I don't have it in menu. Version of Unity - 2018.3.0b10. Entities version 0.0.12-preview.20.
     
  46. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    I don't think it has a ComponentDataWrapper<T> associated with it (it's just an IComponentData). You might have to make one if you're using hybrid.
     
  47. Alex1337rus

    Alex1337rus

    Joined:
    Mar 30, 2016
    Posts:
    37
  48. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    If you are converting Prefabs to Entities with
    GameObjectConversionUtility.ConvertGameObjectHierarchy
    you can setup your Prefabs using non-ecs LODGroup and it will be converted to ECS version automatically.
     
    Last edited: Jul 19, 2019
  49. Davood_Kharmanzar

    Davood_Kharmanzar

    Joined:
    Sep 20, 2017
    Posts:
    411
    seems that these are not exists in Hybrid 0.3.3 / Entities 0.5.1 :(
     
    Last edited: Feb 10, 2020
  50. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    687
    Anyone have a working culling process for entities in ECS 0.5.1?

    I have spent the day looking through old post, chasing down old data... so I though I would bug people on this thread.

    Thanks