Search Unity

(Question) Some newbee question related to performance (i guess)

Discussion in 'General Graphics' started by darksilence, Jan 2, 2020.

  1. darksilence

    darksilence

    Joined:
    Oct 16, 2014
    Posts:
    16
    I am very inexperienced developer only worked through some tutorials so far. Right now i am building my own project for learning purposes. This is very primitive rts i started today with only functionality of selected units moving towards to clicked point and very basic camera controller. For testing purposes i am spawning a test bullet from my test tanks. Bullet has only one sphere and one point light and tanks are only made of one cube, and two cylinders.

    Lets skip into question, even with so few objects point lights doesnt work correctly (Second picture) if bullets have lifetime of 1.5 seconds (12 max bullets exist in one frame in 30, and 9 bullets exist every other frame) but if i reduce lifetime to 0.5 seconds (6 max bullets with average closer to 3) it works perfectly (First picture). So i thought it must be an optimization problem. Obviously creating and destroying lights constantly is not a good practice but i dont know any alternative way yet and i didnt think it would have that much impact.

    I am also making coloring on the fly:
    Code (CSharp):
    1.  void ColorUnit()
    2.     {
    3.  
    4.         switch (m_player)
    5.         {
    6.             case Player.Blue:
    7.                 m_mat = m_blue;
    8.                 break;
    9.             case Player.Red:
    10.                 m_mat = m_red;
    11.                 break;
    12.  
    13.         }
    14.         m_childMeshes = GetComponentsInChildren<MeshRenderer>();
    15.         if (m_mat != null && m_childMeshes != null)
    16.         {
    17.             foreach (MeshRenderer m_mesh in m_childMeshes)
    18.             {
    19.                 m_mesh.material = m_mat;
    20.             }
    21.         }
    22.     }
    I am open to any suggestions how i can do thing better and what i am doing wrong. Thank you for your time.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    There's a maximum number of pointlights that will render in a frame at any given time. I think this is determined by quality settings.

    You should use lights more sparingly, though. If you want your bullets to glow, a much more efficient way is give the bullets an HDR emissive color (turn on "emission in the material properties") and add "bloom" to your post processing stack.

    https://docs.unity3d.com/Packages/com.unity.postprocessing@2.1/manual/Bloom.html
     
  3. darksilence

    darksilence

    Joined:
    Oct 16, 2014
    Posts:
    16
    Thank you very much for the response. I learned two things at once. But i can't apply bloom effect correctly, either i get no effect or everything glows. I created seperate layer and put bullet prefab in that. When i apply bloom to the bullet layer nothing happens and when i apply it to default layer everything "including bullets" glows.

    (Edit: setting threshold to 2 seems to solve it, thanks)
     
    Last edited: Jan 2, 2020
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    Post effects are not applied until after the frame is rendered so they can't be applied to only specific layers or objects without complicated masking or multiple cameras. I think that you should be able to get the effect you want with the proper settings, though.

    The way HDR (High dynamic range lighting) works is this:
    normal rendering light levels are on a scale of 0 to1 where 0 is black and 1 is the maximum white your monitor can produce. With HDR enabled, though, the renderer will store light values greater than 1. They won't actually be brighter on the monitor since 1 is already the max, but special effects like bloom can access these values to see how the effect should be applied.

    Try setting the threshold for the bloom higher. like 0.9 or 1 to reduce the effects on normal objects.

    Then make sure the emissive color on the bullet material is an HDR color.

    Also make sure is HDR rendering is enabled in general. I think there is a check-box on the camera component for this.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    If you're rendering using the built in forward rendering path, there's no limit on the number of lights per-scene, but there is a limit on the number of lights that can affect each object. That number is set in the Quality settings with the Per Pixel Count. Since your ground plane is one big object, it can only show that many lights at a time.

    You can increase that number to something much larger, but it's bad for performance. The way Unity's built in forward renderer handles multiple lights is by rendering an object multiple times, once for each light that affects it. This makes a lot of lights expensive, but has the benefit of working on basically every GPU ever made.

    The alternative if you want to use a lot more lights is you could also play with using the deferred rendering path for your project. That removes the light limits entirely. Deferred rendering exists specifically to make rendering multiple lights much more efficiently. However this isn't an option for all platforms as deferred rendering uses a lot more GPU memory which either may not be an option or may have a significant performance impact. Specifically mobile GPUs from a few years ago and earlier.

    The newer LWRP/URP work different, but still have per-object light limits, as well as fairly low per scene light limits.


    Note the light limit is purely active lights. If there's a light in the scene that's disabled or has its brightness set to 0.0 it won't affect the light limits. Baked lights also may not impact the limit, depending on your lightmap settings.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Bloom works best if HDR is enabled on the camera & project. But note that very small objects (like bullets) may not work well. Bloom works by down scaling and blurring the image in multiple passes and adding the resulting blurred image back on top. However for efficiency reasons not all pixels are necessarily factored in during the down scaling and blurring meaning small or thin objects may not show up in the bloom at all regardless of how bright they are if they happen to be in pixels that get skipped. So even with bloom it's often beneficial to a use "faux bloom" glowing particle on projectiles to increase the size they are on screen. The bloom will work off of those just as well as any other bright material.