Search Unity

Question 2D Grass objects and Performance

Discussion in 'World Building' started by DrSpoutnik, Dec 1, 2023.

  1. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    Hi !
    i'm a newbie, i'm trying to create my own platformer game in 2.5D
    i want to create a level in a meadow with grass swaying in the wind.
    i have a grass sprite for that, with a shader animated for the sway. it works quite well, so far.
    So i duplicate my grass object to fill my level with it. Since the game is in perspective not flat/orthographic, i add ranges in the foreground and the background too.
    But in the end, it's a lot of grass objects. and the performance drop down when i play.

    without any grass (removed for the test) i'm at 390~400FPS / 2.5ms in the stats
    with the grass, i'm at 35~40FPS / 27ms

    So i created a little script to desactivate the SpriteRenderer Component of any grass objects distant to the player enough to be just outside the camera frame.
    it works, but it's not really efficient enough i think :
    with the script, i'm at 50~70FPS / 20ms

    Considering i'm at the very begining of my level building (i will ad trees, rocks, plants, etc.) it could get even heavier...

    1) Is it important to bother about FPS ? if yes what would be correct values for having a fluid game ?
    2) Desactivation of the SpriteRenderer isn't maybe the best way to reduce the load on the CPU ?
    3) Maybe there another way to fill my level with grass that the way id do it, duplicating an objects (in the end the full level could have 1000 grass objects i think....)

    Any advices would be welcome ! :)
    Thanks !
     
  2. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    Oh, i forgot, my project is in Built In pipeline, if it matters.
     
  3. ShilohGames

    ShilohGames

    Joined:
    Mar 24, 2014
    Posts:
    3,023
    Are you using a game object for each grass item? Try using GPU Instancing instead of game objects. Search for Graphics.DrawMeshInstancedIndirect and Graphics.DrawMeshInstanced.

    The best way to render lots of grass with Unity is with Graphics.DrawMeshInstancedIndirect.
     
  4. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    yes a game object for each grass item, in deed. each game object has a sprite renderer with a sprite, a material with a shader to animate them (swaying)

    thanks for your answer.
    graphics.drawMeshInstancedIndirect, is it usable for a 2d platformer ? i dont want to have grass everywhere, just exactly where i want, on areas smaller than open-world meadows. i’ll try that anyway !