Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Calculating optimal lightmap resolution for given maximum light map size

Discussion in 'General Graphics' started by sosi_san, Feb 22, 2023.

  1. sosi_san

    sosi_san

    Joined:
    Jun 20, 2019
    Posts:
    4
    Hey. I'm working on an automated lightmapping baking process and I'm trying to figure out a formula for getting the biggest Texels per Unit value without going out of the specified Lightmap Resolution.
    If I understand it correctly the following equation tells how many texels can fit into a given resolution. But I can't find the relationship between map resolution and Texels per Unit.
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    Without going out of specified "light map size" and not resolution, maybe?

    Light map resolution is texels per unit.
    Light map SIZE is size of lightmap texture.

    So, if your unit is meter, and "light map resolution" is 10, while light map size is 1024x1024, then your texture can cover
    104.8576 square meters. In theory. Because that does not take padding into account. Also square meters of surfaces that can be lit, and not structures that occupy 104 square meters on the ground.
     
  3. sosi_san

    sosi_san

    Joined:
    Jun 20, 2019
    Posts:
    4
    Yes in order not to exceed the "light map size" . Thanks for the reply. I still don't understand how you arrived at the 104.8576 square meters figure. Could you get me a bit more context on that?
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,877
    Its not 104.8576 its 1 048 576m2 BUT with the resolution of 10 that neg calculated, i believe his calulcation becomes correct - but not 100% sure
     
  5. sosi_san

    sosi_san

    Joined:
    Jun 20, 2019
    Posts:
    4
    I think I figured out the right solution. It calculates correctly if there was zero padding.

    Code (CSharp):
    1.  
    2. /// <summary>
    3. /// Calculates the maximum Texels per Unit (TPU) value that will fit within a specified lightmap size, given the dimensions of the lightmap and object, as well as the desired number of pixels per lightmap texel (PixelsPerTexel).
    4. /// </summary>
    5. /// <param name="lightmapWidth">The width of the lightmap texture in pixels.</param>
    6. /// <param name="lightmapHeight">The height of the lightmap texture in pixels.</param>
    7. /// <param name="objectWidth">The width of the object being lightmapped in world units.</param>
    8. /// <param name="objectHeight">The height of the object being lightmapped in world units.</param>
    9. /// <param name="pixelsPerTexel">The desired number of pixels per lightmap texel.</param>
    10. /// <returns>The calculated maximum TPU value as a float.</returns>
    11. public static float CalculateTPU(float lightmapWidth, float lightmapHeight, float objectWidth, float objectHeight, float pixelsPerTexel)
    12. {
    13.     float tpu = Mathf.Sqrt((lightmapWidth * lightmapHeight) / (objectWidth * objectHeight * pixelsPerTexel));
    14.     return tpu;
    15. }
    16.  
    If its very close to right answer. You just have to subtract some number to account for the padding.

    Thanks for help!
     
  6. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    Lightmap resolution is linear, meaning texels per unit, not square unit.

    Meaning 1024 texels becomes 1024/10 - 102.4 units. So the texture is 102.4 x 102.4 units, which is 104898.56, so yeah, I made an error.

    The amount of space an object takes in lightmap is not based on object dimensions, but on total area size of all triangles. That's not taking padding into account.

    You cannot calculate it with width/height, unless you have a flat plane. Also, your estimation seems to be off, as it is unclear what 'pixels per texel' are supposed to represent.
     
    MadeFromPolygons likes this.
  7. sosi_san

    sosi_san

    Joined:
    Jun 20, 2019
    Posts:
    4
    Oh, sorry, I forgot to mention. How do I actually calculate this. I calculate the surface area of all the objects in the scene that are affected by the light. Which gives me some surface area. So I'm actually calculating the "height" and "width" of the plane.
     
  8. Chipboard

    Chipboard

    Joined:
    Aug 26, 2017
    Posts:
    4
    Has anyone found a solution to this yet?
     
  9. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,324
    The information I provided still stands. To roughly guess required resolution, calculate surface area of all lit triangles in your level, and use that as a rough estimate. Note that geometry complexity and topology will affect required area.