Search Unity

Static Lightmap baker

Discussion in 'Assets and Asset Store' started by newjerseyrunner, Jun 25, 2020.

  1. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I came across a situation recently where Unity's light mapper couldn't do what I needed it to with regard to teleporters transferring light, so I wrote my own. I figured it might be useful for other people as well. It does most calculations on the GPU, can handle dozens or even hundreds of bounces, runs in the build. It'll give you a static texture of the lightmap.

    The way it works is that everything is unlit and using a shader that's built for lighting. It makes everything black, as though it has no light, and the only light sources are anything else in the scene that is visible. The way I create light maps is that I determine what my brightest light is, and make it a white sphere, then scale the grayness of each subsequent light. The light emits from surfaces, so the bigger the surface, the more light it will put out. You don’t have a variable for the power of the light because it doesn’t matter, only their relative power does. This is because the lightmapper has a parameter for how bright you want the room, it will keep adding light until it reaches this value, then will hover around it and keep bouncing until you stop it.

    There is a light scalar for the first few bounces that help pump lots of photons into the scene, since the first few bounces will be the mainly lit areas. Subsequent bounces smooth this out.

    This example, I bounces light around 50 times and the only initial source was a small white lightbulb hidden underneath the lampshade. The lampshade itself is translucent because both sides map to the same UV coord, this allows it to then spread the light out from there. It's also pulled a lot of color from the room itself.





    Put the shader and script on an object you want to bake (make sure it has a UV2) then just hit makeLightmap, or play with the debugging options to see what your scene looks like in checkerboard, white, grey...

    Please note, when you tell it to make your light map you won’t see any change right away. As long as your tiangleIterator is incrementing, it’s working. You’ll see progress when it loops back over and the bounce counter ticks up (but even then the first few bounces may be quite dark, around 6-10 bounces the light will suddenly amplify dramatically so don’t worry.). This was the first loop of my test scene.
     

    Attached Files:

    Last edited: Jun 25, 2020
    DebugLogError likes this.
  2. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Here are the options and their functions:
    Screen Shot 2020-06-25 at 12.07.25 PM.png

    Configure Lightmap
    • Resolution - The resolution of the lightmap. Use the checkerboard to figure out what the right value is, but 512 is decent and 128 is recommended for fast renders just to get an idea
    • Coordinate Set - Which UV the lightmap information resides on in the mesh. I use blender to model, and adding a second UV is as easy as duplicating the first and re-unwrapping using lightmap settings.
    Configure Baking
    • Bake Resolution - The resolution of the camera that is being used to render. 32 is useful for quick ones, but 64 provides better precision. 128 is extremely high res, and anything beyond that would likely make your computer move glacially.
    • Use skybox in render - Should the camera add in the skybox? This is a lot of light most likely.
    • Background color - If the skybox is not rendered, what color should the background be?
    • Light Magnifier - A scalar with amplifies the amount of light hitting a surface, speeding up the king process. It will decrement itself when it's close to the requested brightness.
    • Magnify Scale Per Step - Is a step that the magnifier will decrease by once it's passed a certain initial threshold. It'll reduce it by this amount until it hits the requested amount, then drop extremely low to fine tune.
    • Log When Writing File - When a file is saved, Debug.Log it.
    • Camera Prefab - The camera that you use to render. Add any post-processing you want onto it.
    • Second Correction Render - The main pass will render with a FOV of 170, which stretches the edges and gives them extra surface area, this second pass redoes the render at FOV of 85, preferring whats directly in front of it rather than the peripherals. Leave off for quick renders, but is necessary for realistic final renders.
    Debug Lightmap
    • Make Checkboard - Puts a 1x1 checkerboard pattern on your lightmap, allowing you to see it's resolution
    • Make Black - See your scene in total darkness
    • Make Grey - See your scene in 50% light
    • Make White - See your scene in 100% light
    • Make UV Map - Will output the wireframe of the UV map
    • Make UV Map Fill - Will make every pixel in the UV map green, but if it gets rendered more than once, it'll turn blue or red. This helps you know that your UV map doesn't overlap itself (unless you want it to.)
    Render
    • Make All Lightmaps - Will coordinate baking all light maps in your scene at once. It's important for them to say synced if they are in sight of each other.
    • Make Lightmap - Bake the lightmap for this object
    • Target Brightness - How bright you want the scene to end up. It'll continue to add light until it reaches this point, at which point it'll scale the light back uniformly and do it some more and repeat until you stop it.
    Info
    • Triangle Count - Used internally, but I made it public to give you an idea of progress. I think it's actually vertex count?
    • Triangle Iterator - This is the triangle it's currently baking, gives you an idea of how far into the scene it is.
    • Current Brightness - How bright the scene currently is
    • Time per bounce - How long it takes to do a single bounce, this value will be empty until one bounce is done
    • Current Bounces - The number of times that we've bounced the light. 25 to 50 is recommended.
     
  3. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I just uploaded a new version. The old one had a problem where the rectangular nature of the camera meant weird hard edges in the light, so I took a circle cutout of it instead, and now it works properly.

    I rerendered my test scene with it after removing the lampshade to see the difference. I also removed the texture, so it's pure light bouncing.
    Screen Shot 2020-06-25 at 2.17.45 PM.png
     
    Last edited: Jun 25, 2020
    DebugLogError likes this.