Search Unity

Feedback Optimization of many objects beyond just using a spatial hasher

Discussion in 'Scripting' started by xytor500, Feb 4, 2023.

  1. xytor500

    xytor500

    Joined:
    Mar 29, 2017
    Posts:
    73
    I am making a game which will have a lot of small objects that all move around, and every frame I need to know the closest few objects to the player, who also moves around.

    I created a spatial hasher, which gets me from 30fps to 60fps for around 1500 objects. But that is about half the objects I plan to have, so I need further optimization.

    I have uploaded the spatial hasher to this post. Is there any way I can improve it? Are there other techniques I could use to boost my fps further?

    Thanks in advance!
     

    Attached Files:

  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    I'm unable to look at the code rn, but do you already use DOTS?
    Sounds like it could be beneficial for you
     
  3. xytor500

    xytor500

    Joined:
    Mar 29, 2017
    Posts:
    73
    I haven't heard of it before today. I'm not exactly a seasoned Unity developer, so I would benefit from a discussion as to how DOTS can help me, and maybe an example or two. :)
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,048
    Look up examples on YouTube.
    It's a different programming pattern optimized for extremely performance and scalable systems. Can be harder to develop for, but might be worth the uplift when working with thousands of objects
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Looks like your hasher is a linear grid of cells, and you have to compute a "shell" every time the object moves.

    The Physics engine uses an octree. Just for giggles, you could TRY adding a new layer, put a very small sphere collider on the object, and use SphereCastAll or SphereCastNonAlloc to find things within the appropriate radius. I have no idea if it would be faster or slower for you, but it's worth running some tests in the profiler just because it's so easy to implement.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You can find all of these things in the Unity documentation for ECS / DOTS.

    This subsystem has been around / in development at least five years and is tailored to exactly this sort of high numbers of objects count.

    However, it is NOT a drop in replacement at all, and is in fact a completely different way of doing things than the existing MonoBehaviour approach to things. You will need to rewrite 100% of anything you decide needs to live in DOTS-land.
     
  8. xytor500

    xytor500

    Joined:
    Mar 29, 2017
    Posts:
    73
    The shell is pre-calculated, so we're just doing fast index math to get the surrounding buckets. I already tried using the physics system, but it didn't give appreciable fps gains.

    I looked at ECS, and it looks perfect for what I need. My original design was essentially using that philosophy, and shoehorned it into GameObjects, so the refactor will be easy.
     
    DevDunk likes this.