Documentation – External Lightmapping Tool

How it works under the hood?

Below I will present how the simplified process looks like. I will omit classes like StorePreferences.cs since the idea is quite obvious.

The class LightmappingTool.cs displays the main window and when the “Export Scene” button is pressed it first colapses the unused lightmaps and just after that prepares for FBX exporting.

The first stage of that process is making a set of unique materials so that only one instance of each material gets exported and this is actually done in LightmappingTool.cs, next the SaveFBX is called where all the verticles, normals, uv, uv2 (no uv1 or uv0) get exported into ASCII fbx file.

After that the transformed to the World Space verticles are transfered to CalculateArea method from CalcArea.cs which calculates the actual object area and gives that data to PackObjects method.

PackObjects uses it to pack the objects into a lightmap texture atlas in correct proportions and returns Rect[] which tells the FBX exporter how to offset and scale UV3 in the FBX file. The FBX exporter stores the offset&scale data.

It’s accessed by later (after succesful exporting) LightmappingTool.cs which sets the lightmapTillingOffset attribute of each lightmapped object.

Before opening the 3dsmax instance the LightmappingTool first searches for an opened one with the scene loaded and switches to it using SwitchWindows.cs class

If an instance is found then the already loaded maxscript reloads the scene (it’s observing the file modification date)

Else it’s opening a new 3dsmax window and loading maxscript which transfers information from Unity to 3dsmax (such as the lightmap resolution) and trigers to open which holds the main GUI and some other onetime functions.

More details how the fbx loading looks in 3dsmax:

At first a check for existance of SceneName.max is performed. If it doesn’t exist then a new one is created and loaded. This file is used for holding presets such as lightning and rendering preferences.

After that all the previously imported geometry which exist in this file gets deleted, but their materials are backup’ed in the material library.

Then a fbx merge import is performed (including lights) and assignMaterials function gets called which iterates over all materials imported with the fbx and if a material with the same name exists in the library then the object material switches to it.

There’s also one important class called LookForLightmaps.cs which extends OnPostprocessAllAssets and wraps and assigns the found lightmaps to the LightmapSettings.lightmaps, so when user comes back to Unity after the rendering they all get set.

What makes the system?

The system consists of 8 classes:

Class Name Description Methods
LightmappingTool.cs Holding main window gui and logic Init

















SaveFBX.cs Preparing and saving the .fbx file ExportFBX



CalcArea.cs Making some operation on verticles and UV’s CalculateArea



SwitchWindows.cs Class using methods from user32.dll for finding and setting the focus to 3dsmax window after exporting EnumDesktopWindows







LookForLightmaps.cs A class that extends AssetPostprocessor and assignes found lightmaps to the objects OnPostprocessAllAssets
Prepare BatchScript.cs Prepares a maxscript communication file called as the scene name Prepare
StorePreferences.cs Simply save and restores user preferences Load


Help.cs A simple help class Manual

%d bloggers like this: