Search Unity

Architecting a scene containing 500 rotating Quads

Discussion in 'Scripting' started by eco_bach, Nov 29, 2015.

  1. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    This is strictly speaking a code optimization related question. I have an array of 500 float values that is constantly updating in realtime. There are 500 quads corresponding to these float values. Each one of these float values should directly effect(change the rotation of) its corresponding quad.

    My question is, from a code runtime efficiency point of view, what is the BEST way of architecting this?

    Should I use an eventmanager that each quad object subscribes to? Or should a forget events and keep a reference to every quad in the same class where the float values are updated? or??
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    Optimization is a hard game. Are you optimizing for memory, computation time, heat and power usage?

    Assuming that your desire is a high framerate, then, if each float values update every frame, and you want those values reflected in the transform of each quad every frame, then the best you can do is to iterate over each quad and update its transform every frame. Some micro-optimizations will help, such as re-using a Quaternion for your inner loop. Pre-sorting the array by predictable branching values can also provide a boost.

    Using events and delegates creates a lot of unnecessary overhead in this case.
     
  3. BrianND

    BrianND

    Joined:
    May 14, 2015
    Posts:
    82
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You would want to avoid getparticles and just set them instead if that was your goal for speed. But i'd look at a compute shader or geometry shader. With a geometry shader you can create a vertex shader which will read vertex streams from this meaning you only need to update a list of points for positions and use vertex attribute to control rotations. This would be faster than shuriken by miles.
     
    zombiegorilla likes this.
  5. eco_bach

    eco_bach

    Joined:
    Jul 8, 2013
    Posts:
    1,601
    Thanks for the great info. kru> computation time.

    Noob question> If I have all 500 Quads on my stage what is the easiest way of adding them to an array?
    There is

    Code (csharp):
    1.  GameObject[] objects =GameObject.FindGameObjectsWithTag("myTag");
    but I need these Quads to be in sequential order.
    hippocoder, I'm a shader noob, any chance you could point me to examples using the compute or geometry shader solution you suggested?
     
    Last edited: Nov 30, 2015
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723