Search Unity

M2HCulling: Optimize your game - culling system for Unity

Discussion in 'Scripting' started by MikeHergaarden, Jun 21, 2010.

  1. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Thanks Mike, i will try it now since i don´t own pro..

    ---
    Miguel
     
  2. Demostenes

    Demostenes

    Joined:
    Sep 10, 2010
    Posts:
    1,106
    Probably that this system works ;) Umbra implementation is bugged, so we are waiting to promised rewrite, which should be in Unity 3.5.
     
  3. Spectre9000

    Spectre9000

    Joined:
    Aug 30, 2010
    Posts:
    170
  4. jeffro11

    jeffro11

    Joined:
    Jan 7, 2010
    Posts:
    185
    Unity's built in Occlusion Culling is... subpar at best. Having full control like this system has is awesome to have! It also works with the free versions!
     
  5. Spectre9000

    Spectre9000

    Joined:
    Aug 30, 2010
    Posts:
    170
    I didn't know how it stacked up performance-wise, I just new it seemed lots of people were under the impression Unity only had fulcrum culling, when in fact it has occlusion culling. Like many things in Unity, their system can be improved upon I'm sure, and I imagine this is a system that does just that.
     
  6. alln2themusic

    alln2themusic

    Joined:
    Feb 3, 2011
    Posts:
    46
    Thanks for the Reply Leepo, I'll try out Unity's first and see if I can't get it to work. Your's seems a little simpler to get up and running, as this is my first attempt at using occlusion culling in a game, starting out with white line grids all over your scene to start with is a little overwhelming. I guess I just need to go in and create different boxes for each area of the map.

    Any tips you can give on setting up culling in an open world town environment where the insides of buildings aren't use very much?
    Any good tutorials (other than the Unity reference manual) that you could point me to would be an amazing help? Thanks for all your help!!
     
  7. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    I'm getting a wierd error when I run: "Instance of CullingAreaEditor_Manual couldn't be created because there is no script with that name." I have no clue what this means? It does run and build though, but its a wierd error I see in the console.
     
  8. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    That sounds like the script is renamed or something. This warning/error will most likely stop the editor from working.
    However, it only occurs at run time? possible put #if UNITY_EDITOR at the very beginning and #endif at the very end of that file...that might surpress the error at runtime.
     
  9. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    Wow, it's awesome you still follow up in here sir! Here is a screengrab of the contents in my Standard Assets folder for my project...I mean it appears to have everything that came in the downloaded package?

     
  10. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Your image doesn't work.
    You can check the correct contents of the package at the asset store page. If it did mess up; importing the package in an empty project could help.
     
  11. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    Ah my image link was broken.
     
  12. pragmascript

    pragmascript

    Joined:
    Dec 31, 2010
    Posts:
    107
    hey would this work also with dynamically instantiated prefabs?

    greetings
     
  13. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I'm not entirely sure..but I believe that it doesn't work on dynamic objects per default. This should be an easy enough extension though.
     
  14. NTDC-DEV

    NTDC-DEV

    Joined:
    Jul 22, 2010
    Posts:
    593
    I'm still using it and it is the back-bone of our optimization pipeline =)

    One question though; with over 4000 objects, dozens of zones... the delay when the camera enters a zone and the objects appearing gets quite high (up to 4 sec in some cases)... I'm wondering if there is a way to send the bottle-neck line of code to another thread, have you experimented with this? (I know Unity doesn't support multithreading officially, but the possibility is here for logic...)
     
  15. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    PolishRenegade : hah nice to hear its so usefull.
    The enable/disable calls are done in one frame currently. You could try to spread this out over several frames by using a coroutine(yield in-between).
     
  16. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    If you simply attach the auto culling script to a camera, does it work right out of the box?
     
  17. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    How do I add objects to the GameObject areas that I have made? I made a GameObject, named it CullingObjects, and put all objects in the gameobject. Then I made 4 other Gameobjects and attached the CullingArea_Auto script, but how do I add objects to each of these gameobjects? And what do you do with the Editor script? I am missing where to apply the editor script I believe.
     
    Last edited: Sep 27, 2011
  18. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    occullusion culling showing >> strange result >>

    http://www.youtube.com/watch?v=ns3AW6KUraY

    look at the video, why is this happening in my scene ?

    i have around .6 million [6 lakh] polygons in my room or lobby...my fps is coming 9-10...can i use this system to improve my fps ?

    Please suggest
     
  19. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I just watched your scene and I'd say pretty much not, you can't use this to optimize your scene.
    You will have to optimize your polycount on your meshes.
     
  20. tiagomelo

    tiagomelo

    Joined:
    Jun 25, 2010
    Posts:
    18

    Leepo, have you tried to use Camera culling mask, and divide objects in layers? This way you won't need to disable the mesh renderer component.

    It should be faster to enable/disable groups...
     
  21. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Sure, but this does not allow you the level of culling that you really need; culling objects that are hidden behind walls etc. Culling based on the layers only culls on distance, this will be visible by your players.
     
  22. tiagomelo

    tiagomelo

    Joined:
    Jun 25, 2010
    Posts:
    18
    Are you sure? If you set different layers for 2 groups of objects, and change the culling mask of your camera to show only one of them, the second one won't be visible at all, even if it's in the front of the camera.
     
    Last edited: Nov 16, 2011
  23. SirManGuy

    SirManGuy

    Joined:
    Sep 16, 2010
    Posts:
    33
    Nice work as usual Mike! Just a quick note I also got "Instance of CullingAreaEditor_Manual couldn't be created because there is no script with that name." -- which was because the unityPackage has CullingAreaEditor_Manual class in the CullingColliderEditor_Manual.cs file. A quick rename of the file fixes the issue.
     
  24. SirManGuy

    SirManGuy

    Joined:
    Sep 16, 2010
    Posts:
    33
    A quick question on using this in combination with CombineChildren.cs. I'm using the automatic culling with areas around a large number of objects that have been bound in a game object and have CombineChildren attached.
    With Culling On + Combine On:
    ~490 draw calls, 78K triangles
    With Culling Off + Combine On:
    ~110 draw calls, 174K triangles
    With Culling On + Combine Off:
    ~501 draw calls, 49.2K triangles
    With Culling Off + Combine Off:
    ~3036 draw calls, 161K triangles

    Any way to get the combine effort to work with culling properly? I'm guessing it is an order of operations and I'll take a look tonight if I get a chance. I just figured someone might have a quick pointer.
     
  25. SirManGuy

    SirManGuy

    Joined:
    Sep 16, 2010
    Posts:
    33
    Ok I get it, CombineChildern.cs disables the renderers but doesn't remove them. When the culling comes across them it re-enables them. You end up with all the meshes that have been combined turned back on. I'll come up with a fix and post it on here at some point.
     
  26. SirManGuy

    SirManGuy

    Joined:
    Sep 16, 2010
    Posts:
    33
    Added a tag: Combined into Unity. Inside CombineChildren.cs - added: curRenderer.tag = "Combined"; under line 48. Added if (ren.tag != "Combined") check into SetRendererSetting() in CullingCamera_Auto.cs.
     
  27. jdwieber

    jdwieber

    Joined:
    Mar 20, 2012
    Posts:
    28
    Hello Leepo,

    I have a game in which every level is procedurally generated from a handful of prefabs. Thus the unity occlusion system is useless to me. Each resulting level is rectangular in shape with tunnels, and passageways carved out of it in a maze like fashion. The typical size of a level is roughly ( 1000, 300, 1000 ) game units. Do you think that I would be able to apply the automatic culling system to such a scenario? I can easily gather the bounds of the level as it's created. Could I use that to define a single area for the culling system?

    Thanks.
     
  28. anshul-bhardwaj

    anshul-bhardwaj

    Joined:
    Feb 28, 2012
    Posts:
    34
    Hey,


    I tried to run your example code in unity 3.5 version. It run and build fine but getting one error "Instance of CullingAreaEditor_Manual couldn't be created because there is no script with that name." and while selecting culling areas getting "multi-object editing not supported" in inspector view.

    EDIT: Solved it by reading earlier posts. :)
     
    Last edited: Jul 20, 2012
  29. stoner221

    stoner221

    Joined:
    Aug 11, 2012
    Posts:
    3
    Hi i just picked up your script for integration in my game and i noticed that the culling area cubes can not be rotated is there something im missing or is this how the code works?
     
  30. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Yeah rotation doesnt work, I believe thats simply because Unitys debug/gizmo cube drawing cannot be rotated.
     
  31. stoner221

    stoner221

    Joined:
    Aug 11, 2012
    Posts:
    3
    So instead of having a gizmo cube drawn why not something else? maybe with a tag or w/e?
     
  32. stoner221

    stoner221

    Joined:
    Aug 11, 2012
    Posts:
    3
    Actually my brother just stumbled upon this:

    void OnDrawGizmosSelected()
    {
    Gizmos .matrix = transform.localToWorldMatrix;
    Gizmos.color = gizmoColor;
    Gizmos /DrawCube (Vector3 .zero ,Vector3, .one );
    //Gizmos.DrawCube(transform.position, transform.localScale);
    }
    which still draws the cube but allows for rotations.
     
  33. rmele09

    rmele09

    Joined:
    Nov 8, 2010
    Posts:
    716
    For some reason my camera doesnt ever render the objects in my second area, it only renders the objects in my first area. I am using the auto script. Game starts and the first area appears fine, turn the corner and the other area is not rendering? What should I do to fix this?
     
  34. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    Will this be useful/required for Unity 4 or does Unity4 have it's own culling system? (Sorry, beginner question).
     
  35. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    It's a pro feature on Unity 3 and 4. This package would be needed by indie/free versions and possibly iOS.
     
  36. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    Is m2h culling still required/useful now that 4.3 is out? Does 4.3 offer culling features?
    Thanks
     
  37. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    The Pro versions of Unity have an occlusion system built in...occlusion culling. M2H culling is still very much relevant as a culling solution for users of the free licenses. It's very useful for mobile development, not needed so much for PC development these days (if you're efficient). I use the M2H culling system in my racing game projects and it works well.
     
  38. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    Great. thanks for the reply. I think this system (M2H) works very well. I bought and tried InstantOC but couldn't get to grip with it and found it too fiddly and unreliable (although that was probably my own inexperience playing a part). The only thing I would really like to see in M2H is the ability to rotate the culling area boxes. I notice someone (above) has mentioned it was possible but again I wouldn't know how to add that tweak in order to get area rotations.
     
  39. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    Yeah I bought InstantOC as well, but for my purposes it became a bit too cumbersome and not really usable. I found it much easier to just create volumes in M2H system and simply assign geometry to volumes. Plus, didn't have to mess with any raycasts or things like that.
     
  40. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    stoner221, thanks for that tweak to provide rotation of CullingAreas.
    There is an error in that bit of code though. Here is the correct tweek:
    This works great. Thanks again.

     
  41. JamesArndt

    JamesArndt

    Joined:
    Dec 1, 2009
    Posts:
    2,932
    Wow you guys rock! Adding in rotation!
     
  42. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    Question:
    When you set up an new area, the culling areas (where you choose to either show or hide) are blank ie they have neither 'show' or 'hide'. Once you choose to either show or hide, it is not possible to revert to NEITHER (blank). What does leaving the option set at 'blank' do? and why can't I choose 'blank' as a third option rather than toggle between 'show' 'hide' ?

    ta
     
    Last edited: Dec 5, 2013
  43. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    Also, I can't get the Manual system to work!
    In the example factory scene, if you click on an area, the 'culling_area_manual.cs script says:
    If I add a new area and attach the script, a gizmo colored box doesn't show up. There is no way to show the defined area as there is no gizmo box showing. Is this an error on my part or maybe a problem with unity 4.2 ?

    ta
     
  44. richardh

    richardh

    Joined:
    Mar 7, 2012
    Posts:
    226
    URGENT!!!
    That tweak (above) to allow GIZMOS rotations DOESN'T WORK. Don't use the tweak. It will allow you to rotate the gizmos BUT the rotations won't take effect when the script runs. You will THINK the gizmos match the rotations when in fact the original gizmo postion is maintained and the NEW rotation is a false representation.

    I have spent 3 hours pulling my hair out wondering what has happened, until it occurred to me the tweak might be the culprit, and it was!

    So please don't use this tweak (the one in the thread which I've also quoted below).

    sorry.

     
  45. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Hi - curious if this system works with Unity v4+ ?

    I'm attempting to set up the manual system, but when I add the CullingArea_Manual script to an empty GameObject, I get an error:

    The empty GameObject shows the component added, but also shows the "Multi-object editing not supported" message, even though only the single object is selected.
     
  46. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    All good. Just figured out that the CullingAreaEditor_Manual script isn't named correctly in the package.