Blender Raytrace Baker

Overview
This tutorial will show you one way how to bake textures with Blender to create lightmaps. Lightmaps are use to generate static shadows at e.g. terrains or buildings.

Requirements

 * Blender 2.42a
 * Blender Raytrace Baker 3.4 from AlienHelpdesk
 * 30 minutes of time

Gettings Started
First lets get the Python Script which will help you to bake better than the procedural Texure Baker shipped with Blender.

Visit this site and download the Blender Raytrace Baker 3.4. For now you can ignore the rest of the content of the alienhelpdesk site. I'll explain all important settings later.

After you downloaded the script copy it to the 'scripts' folder of Blender.

If you don't know how to access this folder do the following:


 * Use the Finder's "Go" menu from the menu bar on the top of your screen. Here you enter the name of the folder in "Go to Folder" .  On my system it is here: /Applications/Blender/blender.app/Contents/MacOS/.blender/scripts

Now start Blender and lets begin.

Create Meshes and UVmaps

 * Create a Cube with Add -> Mesh -> Cube. (If there isn't already a cube created in the new file.)
 * Create a Plane with Add -> Mesh -> Plane and place it (Key G then Key Z then move the mouse) below the Cube and Scale it (Key S then move the mouse). The result should look like this:



(Perspective View)


 * Select the Plane and switch to the Edit Mode. Press the key W and click 'Subdivide Multi', set the number of cuts to 3 and click 'OK'. -We want to keep the example as simple as possible but it is useful to use a plane with more than 2 faces because this is the typical setup of e.g. a terrain.
 * Press the Space Key and convert the quads into triangles like shown here:



Or just press the Keys Ctrl+T.
 * Now switch to View -> Orthographic if it isn't already set. Then to View -> Top.
 * We should now create the UVmap of the plane. Split the window with your 3D objects that you have two windows. Let's use the right window for the UVmap by changing the window type to 'UV/Image Editor'.
 * Switch from the 'Edit Mode' to 'UV Face Select' in the left window. Press the key U and click 'Project from View' (left window). You can see the UVmap in the right window. Mouse over the right window, press the key A to select the whole map and press the key S and scale the UVmap that it is a little bit smaller than the squared grey grid. Press the key G to move the UVmap if you have to. The result should look like this:



Now (multi-)select (LMB + Shift) the following edges (shown yellow):
 * In the left window switch to Object Mode and select the cube. Switch to Edit mode. Enable the 'Edge Select Mode'.



(Perspective View)


 * Press Space in the left window (mouse over) and select Mark Seam:



or just press Ctrl + E and click 'Mark Seam'. These Seams help Blender to know where you want to cut the UVmap to get it from 3D to 2D. The Seams should now appear orange.
 * Press the Space Key and convert the quads into triangles like shown here:



Or just press the Keys Ctrl+T.
 * Switch to the 'UV Face Select Mode' in the left window. Press A to select all faces if they aren't already selected.
 * Press the key U and click 'Unwrap' in the left window. Select the whole UVmap in the right window by pressing the key A if it isn't already selected. Scale, change the position and rotate (key R) it that it looks like this:



Set up the lights
Click Add -> Lamp -> Area to add an area light which is able to cast soft shadows. Manipulate the 2 lights like shown here:
 * Now it's time to set up some lights. One light should already exist in the scene by default so lets add another.



(Perspective View)


 * Now we have to change some settings of the Arealight to get a soft shadow effect so select the Arealight. Change to the Shading Panel by pressing the Key F5 and select the Lamp Buttons.




 * Next to the red dots all important setting are marked:



Rendering Settings

 * Switch to the Scene Panel with Key F10 and click Image Buttons.




 * Make sure that all your settings are the same as shown here:



Prepare Baking
First save your project... (Have a look at Important Notes if you want to know why.)
 * Go back to Object Mode and select the plane.
 * In the right window switch to Scripts Window Type.




 * You don't have to make big changes. The only thing you have to do is to activate 'to UV coords'.



If you're using Blender on Windows, and you have installed Blender on (for example) drive E: and you fill in /bakedTextures/letsTest then the baked texture is saved to E:/bakedTextures with the filename letsTest.png (if you selected .png as filetype).
 * Fill in something useful as 'path' - the location to which your baked texture will be saved.
 * Let's save the baked texture as .PNG.

Bake

 * Hit RUN SCRIPT... and lounge back (if your chair has a back rest^^).
 * After it has finished, click EXIT in the Raytrace Baker window.

You should get a result like this:



It's not finished yet... so be cool. ;)


 * Now let's do the same for the cube. Select the cube in Object Mode, open the Raytrace Baker script, change the settings and click RUN SCRIPT again to create the lightmap of the cube.

You should get this result:



Polish the Result
The shadows don't look very convincing yet.


 * Open the pane texture with a picture editor of your choice. I'll use The Gimp in this tutorial.
 * Now use Filter -> Blur -> Gaussian Blur. Set the value to 15 or so and click OK. Now the shadow should look softer than before.

It's a pity, but I really don't know how to fix this. Even a talk with macouno, the creator of the script, didn't help... So we have to fix this by hand. (This can also happen for the plane texture... but only as very rare 1x1 pixel impurities if you did nothing wrong.) Just paint out the one pixel lines using an appropriate color.
 * Open the Cubetexture and with a close look you will find one pixel wide lines between the sides of the cube.

See a result here. It uses Unity's built in Lightmapped/BumpedSpecular Shader.

Important Notes

 * Re-open the Raytrace Baker script every time after you change the next object you want to bake.
 * The Raytrace Baker script will change some things in the project that you won't be able to bake the same object 2 or more times without to change back some settings. If you bake the same object twice without changing the settings, the result will be pretty crappy.
 * Scale all textures in your picture editor to have power of two dimension, or Unity won't import them.