Search Unity

2D performance tips for this case

Discussion in '2D' started by Sonoshee, Sep 2, 2015.

  1. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Hi everyone!
    I made a script that randomly places 2D clouds within a given 3D box collider ( note that I'm using a perspective camera to achieve a natural parallax effect by altering the z-position, so a 3D box collider is needed).



    In order for the clouds-plane to look dense the script is told to instantiate 400 cloud objects.
    The script also sets clouds' color based on their Z-position.
    The FPS however went from a glorious 90 frames to 30.

    This was after I tried Sprite Batching for the first time, the batch number has decreased but the FPS didn't seem to improve much.
    I also set the clouds prefabs to static ( there are 7 of these prefabs, all picked and placed randomly by the script).
    I STILL NEED MORE FPS, so what do you guys suggest to improve performance?
     
  2. keely

    keely

    Joined:
    Sep 9, 2010
    Posts:
    967
    I'm just guessing, but one reason could be that you are suffering from lot of overdraw. Overdraw means that Unity needs to render all the clouds from back to front, which means a lot of pixels in your case.

    One way to combat this is to use a special cutout shader that allows you to use something called the z-buffer for sprite rendering. Here seems to be a good example of a shader that does that: https://gist.github.com/InfiniteAmmoInc/9823907 (haven't personally tested)

    Make a new material that uses that shader and assign it to your SpriteRenderer. It may or may not get your perf up, but worth a try.
     
    theANMATOR2b likes this.
  3. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Thanks for the suggestion keely. I tried using that shader on the clouds but I get this weird appearance ( despite changing the Shadow Alpha Cutoff):



    And the FPS doesn't seem to improve much, so I guess I'm gonna look into some other optimization if I can.

    I wanted to ask if occlusion culling can be achieved in my case, since many small clouds are spawned behind big ones, and thus never appear on screen. I think either preventing them from spawning or using culling might solve this.
     
  4. keely

    keely

    Joined:
    Sep 9, 2010
    Posts:
    967
    You should probably use the profiler. Run it without clouds and then with it and see what changes.
     
  5. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    This helped, thanks!

    I thought the slowdown was solely due to the many objects being drawn, but when I checked the profiler as you suggested, I found out that a small script attached to each cloud object causes most of the lag, it's a very simple script but when run for each object it accumulates slowdown.
    I optimized the script and did some tweaks and now the fps went from 30 to 50.

    Any other advice is welcome :)
     
  6. dworm

    dworm

    Joined:
    Jun 17, 2015
    Posts:
    74
    well 400 sprites isnt that much tbh... i think you have something wrong still unless very old machine

    also collider is big mistake, that surely slows down and i dont see a reason to use a collider on clouds...

    try and figure out a way to not do that
     
  7. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    Thanks for the answer. At first I used 400, but now clouds are in thousands.
    And no I don't use colliders, it's only a small script that adjusts the scale so that the cloud is kept pixel-perfect no matter how far it is from the camera. I moved the code from Update() to Start() ( in Update I was able to check if the scale has changed in runtime, but that if statement run for those numerous clouds slows down the game).
    And yes I do have a crappy machine.
     
  8. dworm

    dworm

    Joined:
    Jun 17, 2015
    Posts:
    74
    also i had thought you could not really need to do it at runtime maybe its worth just doing it all in some inizialization process and precalculating the clouds so at runtime you only draw them or slightly modify them

    btw i think your are overkilling the game with thousands and pixel perfect just for clouds :D
     
    theANMATOR2b likes this.
  9. Sonoshee

    Sonoshee

    Joined:
    Jul 8, 2014
    Posts:
    77
    haha I can't help it, I need clouds to look thick and volumetric. But don't worry the game still holds its pixel art style.