Pixel Lightmapper

After reading about Yoggy’s approach to lightmapping in http://forum.unity3d.com/viewtopic.php?t=9920&start=0 I really wanted to try it to see what the speed difference would be. And it’s a lot! Heres a new lightmapper (Thanks to Yoggy) Attached example rendered in 3 sec.

Instructions:
Place the script in the Editor folder and run the wizard from GameObject/Lightmap Wizard (Pixel).

The light mapper supports both point, spot and directional lights and while it doesn’t match them completely it’s usually a fairly good approximation.

The objects 2nd UV set should be uniquely laid out (or the 1st UV set if the 2nd isn’t used) (Note that most of Unity’s primitives aren’t uniquely UV mapped)

Options:

  • lightmapMode (default ALL): Determines what objects are lightmapped.
    ALL: All objects in the scene with a lightmap shader is lightmapped
    SELECTION: Only directly selected objects are lightmapped
    SELECTION_OR_CHILD: Only selected objects and thier children are lightmapped

  • Smoothing (default 1): Determines how much the final lightmaps should be smoothed. When smoothing a texel tcenter the lightmapper looks at all texels that are at within smoothing distance from tcenter (including tcenter) and averages the color (texels that have recieved no light is ignored).

  • Save Light Maps (default false): Should the lightmaps be saved after being generated. If false all lightmaps will revert on reimport which is good if your testing out difference light setups and doesn’t want to loose the old lightmaps just yet.

  • Alpha Lookup (default false): If true the light mapper whill look at the alpha of all the surfaces from a texel and to the light source and modify the light intensity accordingly. (E.g. if the light passes through a surface with and alpha of 0.4 only 60% of the light will get through to the surface behind). Also surfaces with an alpha value will only receive the light that doesn’t pass through. (E.g. a surface with an alpha of 0.25 will only recieve 25% of the light hitting it);

  • No Directional Shadow (default false): If this is true directional light will always be applied regardless of colliders. Use this for indoor scene where the “roof” whould block all directional light.

  • Layer Mask (default everything): The layers that light rays should be blocked by/test alpha against.

  • Ambient Color (defaut black): A color that is added to all texels.[/list]

Updated: 07/04/08



67968–2541–$pixellightmapper_148.cs (14.7 KB)

holy cow this looks sweet! don’t have time to try it out now but will later.

very cool talzor forest - thanks for sharing : )

Thank you guys!

This will save me so much work and add lots of fun to my upcoming work with Unity.

Cheers

Frank

FYI
I’ve Uploaded a few minor bug fixes to the script.

  1. The progress bar now works
  2. It doesn’t slow to a crawl if a lightmap has the wrong texture format.

Just curious, I have been closely following the progress of the various lightmapping utilities people have been writing…why is it that they all only support omni lights? Is there something inherently difficult in adding support for spot/directional lighting? As much as I would love to use a built in lightmapper, this seems like a limitation that would keep me from doing so…

I think the goal is to approximate things like radiosity and occlusion by “baking” the area lighting of a scene into the textures on the objects.

Radiosity/Occlusion = slow to render :frowning:
LightMapping = realtime! :slight_smile:

Your Mileage May Vary…

Right, I get that, but why wouldn’t you just do this in your modeling app (say, Maya) and composite the lighting/occlusion/radiosity into a texture to be applied in a lightmap? It would work with more light types and allow more flexibility (you can adjust the effect of each contribution in PS)…don’t get me wrong, I would love to just do all these things in Unity; is there something I am missing here that outweighs doing all this in your modeling/texturing app?

there are a handfull of apps that just cant bake lightmaps. And if people are using Unity primatives to build level geometry, then they’d need Unity to lightmap it.

Due to popular demand (well, noradninja anyway). I’ve uploaded a new version of the lightmapper that also supports spot and directional lights.

68355--2565--$lightmap_339.png
68355--2566--$spot_lights_201.png

Talzor, you’re the man. UT should give you a job
:smile:

Ok, I feel dumb…how do I use this? I can’t figure out where the .cs file needs to go so I can run the wizard…also, do I have to have Unity Pro to have this calculate shadows?

EDIT: NM, I figured it out how to get it to work, but the question about the shadows I am still wondering about…also, where do the lightmaps get saved to?

Awesome :smile:

Excellent. There is just a minor bug which causes a runtime error. Line 52 should read:

Vector2[] uv = mesh.uv2.Length > 0 ? mesh.uv2 : mesh.uv;

Yes, you are right. Change uploaded.

FYI: I’ve uploaded a new version that can do AlphaLookup and directional shadows. (Directional shadows can be disabled, which is especially useful for indoor scenes)

I was working on my own pixel lightmapper last week and encountered the following pitfalls:
1.) It’s quite helpfull to make the shadow color editable and initially fill the lightmap texture with that color
2.) Using a layermask in all Physic.Raycasts makes the lightmapper more flexible
3.) Using RaycastAll is slower but gives you the opportunity to ignore certain hits (eg hitting trigger or disabled renderers)

All good suggestions and all taken to heart. New version with layer mask and an option to specify an ambient color added. All raycast now ignore triggers and disabled renderers, which makes a lot of sense. (I’ve also removed a few debug lines that I forgot to remove from the last version :sweat_smile:)

(I’ve also added a little explanation of the options on the wiki)

Talzor you are the best!

Yes, we’re seriously impressed :slight_smile:

d.

I can’t get this to work. I’ve been trying with the built-in plane and a single light source, but the resulting maps don’t reflect the position of the light source and I can’t figure out exactly how they’re broken. Just so I’ve got this right:

You set up a scene, select the object you want lightmapped, make sure it has a lightmap material with a non-null lightmap texture, and then you choose the Lightmap Wizard from the GameObject menu. Is there anything I’m missing?

Here’s an example of what I’m seeing, before and after lightmapping: