[TUTORIAL] Understanding lightmaps

Discussion in 'Tutorials & Resources' started by Shmitz, Apr 2, 2008.

  1. Shmitz

    aa Shmitz Old Hat

    Messages:
    1,129
    Positive Ratings:
    742
    The goal of this tutorial is to provide a basic understanding of lightmaps, how they work, and how one can minimize their load on texture memory.

    You can find the map and associated assets here: Tutorial Map and Model

    Lightmaps are an often forgotten part of map optimization. It's true that they are less important than other methods of optimizing performance, but a mapper who pays attention to his/her lightmaps can make a significant difference in their size, which not only has a positive impact on performance but decreases the size of the bsp.

    What is a lightmap?
    A lightmap is a grayscale image created for a brush face that creates the light shading on that face. Without lightmaps, everything would be fullbright. When the lightmap is overlayed on top of the brush face, it shows us how well (or poorly) lit that face is, if it has shadows cast across it, etc.

    Because they are images, they take up a considerable amount of space, both as data in the bsp and in memory as you're playing the map. The first thing we want to do is find out just how much space. To do that we need to compile the map.

    We don't need any fancy compile options, just use the "Normal" compile options for bsp, vis, and rad. When it completes, look in the summary table for "LDR lightdata", as shown in this image:
    [​IMG]

    It's a tutorial map, so it's going to be a pretty small amount. As we look at actual differences, keep in mind that in a real map these differences will be multiplied greatly.

    Lightmap Scale
    The first thing we want to consider is lightmap scale, which is a face property that can be found here:
    [​IMG]

    The number represents how much of a face gets condensed into a single pixel of the lightmap image. Higher scaling will result in a smaller image with less definition. Lower scaling will result in a larger image with more definition.
    [​IMG]

    The thing to pay attention to when determining scale is how much definition is actually required for that face. If we look at the two walls that are in the shade, they both look almost identical.

    [​IMG]

    Since the lower scale of 16 does not bring much of an advantage, both walls should be using 64. Visual detail will not be lost, but lightdata will greatly decrease. Consider the following image:

    [​IMG]

    We can see that when a shadow falls across a surface, a lower lightmap is better. The top rectangles should probably all be using 16 at least, if not 8. However, the bottom rectangles are all very flat with little to no shading across them. They should all be using 64.

    Eliminating Excess
    Another way to decrease lightdata is to remove lightmaps from significantly large hidden areas. You can see the columns standing along each wall. These columns are func_detail, which means the face of the wall they are in front of is not cut by them. The area of the wall that is hidden is getting unneeded lightmap data generated for it. If we cut the wall and make the hidden portion of it nodraw, we can remove a considerable amount of lightdata. Look at the visgroups tool on the right side of Hammer:

    [​IMG]

    Uncheck "Wall A" to hide that group and check "Wall B" to make that group visible. Wall B in this case has the areas behind the columns cut and made nodraw. If we compile again and look at the lightdata, we will see that we have just removed 101080 from the map.

    [​IMG]

    We also gain another advantage from doing this. This method also removes the shadow leak and makes the place where the column meets the wall more seamless.

    [​IMG]

    Hidden Detail Faces
    It is important to note that while the compiler will compute lightmap data for world geometry that is behind detail brushes, it is actually smart enough to NOT do this for other detail brushes. An easy example is steps, which you can see a set of in the middle of the map. There are two methods of creating steps: A series of rectangular brushes, each higher than the previous (visgroup Steps A), or a nodraw triangular brush covered by triangular step brushes (visgroup Steps B).
    [​IMG]

    In A, each step has a lot of surface area covered by the brush in front of it (the orange lines). In B, no parts of the textured surfaces (blue lines) are hidden behind anything. Yet if we compile each one separately and compare lightdata, we will find that they are identical in size. This indicates that the covered areas are not having lightdata calculated for them.

    Lightmaps are Rectangles and Pixels
    Yes, we said this earlier, but we should consider the implications of this and how it relates to the shape of your face. A face can be nearly any valid polygon. A lightmap, however, will always be rectangular. Find the flat hovering square brush in the example map. This is visgroup Square A. If we switch to visgroup Square B, we'll see a triangular brush with exactly half the surface area of A. Yet if we compile them and look at the lightdata, it is again exactly the same.

    Lightmaps also can't be a fraction of a pixel in dimensions. If your lightmap scale is 16 and the brush is 17 units wide, it's going to be the same size as if you brush were 32 units wide. It's important to note this in view of the section on eliminating excess. If the area you are trying to cut out is small or irregularly shaped, you may actually be increasing your lightdata rather than decreasing it.

    [​IMG]

    The blue lines represent the actual lightmap shape. In the case of Strip A and Strip B, it's important to note that in Strip B it does not apply the same lightmap to both strips. It creates two separate lightmaps, one for each face.

    Using Props
    Props are vertex lit, and so their contribution to bsp size and memory usage is the size of the model and skin. If you have brushwork that is repeated many times throughout the level, you may consider turning it into a prop. Not only is it easier to render the same mesh multiple times, but it eliminates all of the lightmap data required for those brushes.

    Even if you are not a modeler, creating basic shapes is not difficult, and already existing Valve textures can be used for the model's skin (as it would be if it were still brushwork). In this example we use the I-beams scattered around the map.

    [​IMG]

    If we compile and compare, the use of the I-beam models cuts out 32192 bytes of lightdata. The model itself contributes 7709 bytes of data, but we're still at a profit of 24483 bytes removed.
     
    • Thanks Thanks x 61
  2. Dox

    Dox L8: Fancy Shmancy Member

    Messages:
    588
    Positive Ratings:
    62
    Great info, thanks, hopefully I can get my filesize down a bit :)
     
  3. l3eeron

    l3eeron L8: Fancy Shmancy Member

    Messages:
    594
    Positive Ratings:
    86
    I learned alot... great images and explaination. Id thank you twice if I could.
     
  4. Jimbomcb

    Jimbomcb L1: Registered

    Messages:
    27
    Positive Ratings:
    2
    Awesome tutorial, really helped in understanding them
     
  5. TotalMark

    TotalMark L6: Sharp Member

    Messages:
    331
    Positive Ratings:
    40
    The func_detail thing I had no idea about, now I gotta go fix some of my maps!
    :lol:
     
  6. YM

    aa YM LVL100 YM

    Messages:
    7,099
    Positive Ratings:
    5,920
    Does anyone know why the 'lightmap grid' setting in hammer no longer shows a grid? I was really pissed off to find it now only shows three colours, one for normal scale, one for bigger than and one for less than. its now really hard to tell what brush has what scale but before you could see because it had a nice blue grid of the right size on it :(
     
  7. Shmitz

    aa Shmitz Old Hat

    Messages:
    1,129
    Positive Ratings:
    742
    Maybe you've accidentally toggled some Hammer setting. Mine still shows blue gridlines.
     
  8. YM

    aa YM LVL100 YM

    Messages:
    7,099
    Positive Ratings:
    5,920
    :O I hate it when I toggle random settings without realising it! damnation!
     
  9. phatal

    phatal L6: Sharp Member

    Messages:
    259
    Positive Ratings:
    21
    Shmitz you make some great tutorials. Very easy to follow with great imagery. Big thumbs up. Look forward to your next tutorial. ;) Thanks.
     
  10. Spacemonkeynz

    Spacemonkeynz L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    52
    Is there anyway to set the lightmap grid of the whole map to 64 in one go?

    I would like to do that, then go through and reduce it where necessary.
     
  11. grazr

    aa grazr Old Man Mutant Ninja Turtle

    Messages:
    5,436
    Positive Ratings:
    3,767
    edit > select all

    texture tool; type in 64 to lightmap option.

    click apply.

    Edit: You can set/apply light map scales without changing the texture underneath. If you have a wall texture and right click to apply a lightmap scale of 64 to a metal texture, it wont effect the texture material itself, it will remain metal (only if you are in lightmap camera mode). Still, you can edit texture appearences on mass scale anyway whilst using multiple materials. It wont revert all your selected faces to the selected material. Handy, that.
     
    • Thanks Thanks x 1
    Last edited: Jul 18, 2008
  12. Altaco

    Altaco L7: Fancy Member

    Messages:
    485
    Positive Ratings:
    121
    Oh, thanks, I was wondering about that.
     
  13. Spacemonkeynz

    Spacemonkeynz L5: Dapper Member

    Messages:
    234
    Positive Ratings:
    52
    Thanks for that too.

    Going through my map atm lowering the lightmap scale where all the shadows are. Lots of work, wish their was an easier way to do it.

    I've got TF2 running in windowed mode, so I just spectate through, when I find a face with contrasting shadows, I alt-tab to hammer and change the scale.


    What would be awesome if you could edit the lightmaps right in hammer, and see the results.
     
  14. Sgt Frag

    Sgt Frag L14: Epic Member

    Messages:
    1,443
    Positive Ratings:
    356
    I never saw this tut but it is well done and has good info. Figured it could use a bump and a little more info (see bottom)

    I was aware that changing lightmap scale could shrink file size but never looked into it too closely, just kindof did it as a last minute optimizing technique.

    Anyway, after reading this and knowing I needed to do it for Orient I went through yesterday and changed scale from 16 to 24 on about half the building trims and some roofs that didn't get much shadow anyway. Changed alot of the barely seen details and roofs to 32.

    That shrank the 44 mb file down by 2.5 MB's and I'll probably get another 2-4 MB in the future. Maybe only 2 MB zipped but it does help with that. Can't say I've noticed framerate changes but haven't looked into it that far.

    So anyone looking to shave file size this can have a pretty good impact on that, and I suppose there is tex memory benefits for your graphic card (I've got 512 ram on mine, maybe it's more noticeable on 256).

    Also, in game you can use
    mat_fullbright 2
    and it will render the map in greyscale, just the shadow maps so you can see which faces aren't shading much anyway. That helps decide where you can benefit from this.
    mat_luxel 1
    will actually show you the lightmap luxels (grid scale of maps)
     
    • Thanks Thanks x 1
  15. pitto

    pitto L3: Member

    Messages:
    109
    Positive Ratings:
    73
    Great tutorial
    I did know about the light map stuff before, with the shadows being better, and better shadows taking longer 2 compile, but I didn't know much about the file size and some of the other techniques.
     
  16. Ravidge

    aa Ravidge Grand Vizier

    Messages:
    1,544
    Positive Ratings:
    2,517
    Bumping this, because it's a very nice tutorial!
     
    • Thanks Thanks x 3
  17. Nutomic

    Nutomic L11: Posh Member

    Messages:
    888
    Positive Ratings:
    177
    Never heard of that. How do i get there?
     
  18. DaBeatzProject

    aa DaBeatzProject

    Messages:
    1,276
    Positive Ratings:
    1,141
    Select your camera view - click on the 'camera' word - select lightmap grid
     
    • Thanks Thanks x 2
  19. The Political Gamer

    aa The Political Gamer

    Messages:
    4,468
    Positive Ratings:
    1,704
    Agreed!