Trainz/Creating Normal Maps with Blender

Acknowledgments
This tutorial was inspired by a number of discussions on the Trainz Content Creation Forum and the author's interest in creating normal maps for use in Trainz. Paul Hobbs discusses normal mapping in his excellent Blender tutorials that can be found on his website. Those tutorials are written for an earlier version of Blender and the Blender 2.5 interface is rather different.

There are several You Tube videos on normal mapping in Blender but the following are recommended viewing:


 * - by Blender Nerd


 * - by d123s404

Introduction
This tutorial demonstrates how to simulate rivets on a flat plate using a material created from a normal map texture and a simple diffuse (coloured) texture. The material is then applied to the sides of a simple cube that is used as a scenery asset in Trainz.

There are some actions in the steps below that may differ from your normal approach to content creation in Trainz. That's OK. Just use whatever method suits you. The tutorial method of copying asset files from a working folder into a Trainz editing folder is only for simplicity. The author uses Andi06's AssetX program to manage that process.

The tutorial uses layers in Blender to move objects out of the way temporarily. The numbering of these layers is contained in the Blender Manual. Basically the 10 layer boxes on the top row are numbered 1 to 10 and the bottom row from 11 to 20.

Some actions in the tutorial require you to be in Object Mode or Edit Mode. To ensure you are in the correct mode, the tutorial will remind you of the correct mode.

Assumptions and Prerequisite Knowledge

 * Experience with creating objects in Blender and use of the Blender exporter for Trainz.
 * Experience with creating assets in Trainz.

Before You Start
You will need a rivet mesh for this exercise. For Trainz purposes a rivet can simply be a half sphere and the easiest way to get a suitable rivet is to download the Bits Box (Cab Controls) from Paul Hobbs’ site. There is a rivet.blend file in the collection that is suitable. Place a copy of rivet.blend in a known location.

Purpose
To demonstrate how to create a normal mapped object in Blender using the Blender Bake option. The object is exported from Blender and imported into Trainz as a new scenery asset.

Essential

 * Blender 2.5 (2.57.x or 2.58.0)


 * Blender Trainz Exporter (0.96 version)


 * Trainz Simulator 12 (build 47059) (TS9 and TS10 should also work but have not been tested by the author)


 * A colour texture file in uncompressed TGA format. Just a black, red or blue file will suffice.  It must comply with the standard for texture files for Trainz.

Desirable

 * Objects from Paul Hobbs’ Bits Box http://www.44090digitalmodels.co.uk/bitsbox/cab_controls.html

Expected Result
At the completion of this tutorial you should have the following asset in Trainz.



Step 1 – Create Trainz asset

 * Open Trainz Content Manager and create a new scenery asset.  Change the asset name to “Normal Test”.   Keep a note of the KUID number and save the asset.  You can ignore the errors since we do not yet have a mesh.  Exit Content Creator Plus.


 * Commit the asset, ignore the errors and open it for edit again. This will update the asset name and ensure the asset is correctly named in the editing folder.

Step 2 – Create Blender environment

 * In your Blender user area create a new folder and call it “Normal Test”.
 * Copy the config.txt from the “…\Auran\...\UserData\Editing\Normal Test” folder into your new Blender folder. We will edit the config.txt later.

Create the basic meshes

 * Start Blender and save the default project as Normal_Test.blend in your Blender Normal_Test folder.
 *  Object Mode 
 * The normal cube is a bit big so scale it to 0.5 in all axes or just resize the X,Y and Z dimensions to 1.
 * Change the Z location value to bring the bottom of the cube to the level of the grid floor.
 * Apply scale and location to the cube (Ctrl A).
 * Move the cube to layer 6. We will use it later.
 * Position the 3D cursor at zero (0,0,0) (Shift C).
 * Add a new plane mesh and call it “lo poly”.
 * Change to top view using the 7 numpad key.
 * Resize using the dimensions values or scale the plane to a one Blender unit square.
 * Apply the scale. This is also required if you resized the plane using dimension values.
 * In the Properties Editor, select the Object Data icon (the triangle), open the Normals twisty and deselect Double Sided. This isn’t strictly necessary but the exporter will warn about it.
 * In the 3D Editor select the lo poly mesh and create a duplicate with Shift D. Press escape to avoid moving the new mesh.
 * Rename the duplicate mesh from “lo poly.001” to “hi poly”.
 * Move the hi poly mesh to layer 1 and then select layer 1 so we can work on the hi poly mesh.
 * Save the Blender file.



Modify the hi poly mesh

 *  Object Mode 
 * Import the rivet mesh into your working Blender file. In detail:
 * File->Append
 * Select the rivet.blend file
 * Object->Rivet
 * Link/Append from Library


 * The append command places the rivet at location 0,0,0 but it will need to be rotated minus 90 degrees on the Y axis.
 * Select the rivet and change the Y Rotation value to -90.
 * Then press Ctrl A and then Apply Rotation to set this placement as the standard orientation.
 * The imported rivet is 0.02 Blender units which is roughly ¾ inch. You could rescale the rivet to about 0.025 which is roughly 1 inch.  This might make the rivet more visible in Trainz.  Experiment with different sizes to find a rivet size that suits you.  The dimension values for a one inch rivet are X = 0.025, Y = 0.25 and Z = 0.0125.




 * Move the rivet to the top left corner of the plate and use it to create additional rivets in any pattern you prefer. You can duplicate the rivet by selecting the rivet, followed by Shift D then either X or Y.  The X or Y allows you to move the duplicated rivet along either of those axes.   Once you have 2 or 3 rivets just select all of them and duplicate as required.




 * When you have created all the required rivets, select them, but not the plane, and form a single mesh using Ctrl J.

Warning!  You also need to join the rivets mesh with the hi poly mesh but, if you select the hi poly mesh last, all the rivets will be flattened. This is probably because the hi poly mesh is a simple plane.


 * Select the hi poly mesh first and then the rivets mesh and join using Ctrl J. Check the join is OK by looking at a side view (numpad 3).  The rivets should be visible above the plane.
 * Change back to top down view.
 * The mesh name will have changed so rename the mesh back to “hi poly”.
 * Save the Blender file.

We are now ready to create the material and normal texture.

Step 4 – Create material and normal texture

 *  Object Mode 
 * Change to layer 1 and select the lo poly mesh.
 * Open the UV/Image Editor in another panel. Create one if you do not have one visible.  Create a new image called “rivets_normal” (Image->New Image) using a 512 * 512 size.  Leave alpha checked and check 32 bit float.
 * Select the lo poly mesh and select the material icon in the Properties Editor.


 * Material Settings
 * Create a new material called “rivets.m.tbumptex”.
 * Change the Specular settings to Blinn shader and intensity 0.2.
 * Change the Shading->Emit to 0.5.
 * Leave other settings unchanged.
 * Note: These settings gave a reasonable result in Trainz. This is one area you could experiment with different settings to improve your result.
 * In the Properties Editor select the textures icon.


 * Normal Texture Settings
 * Create a new texture called “normal” using the Image or Movie type.
 * In the Image twisty, select the leftmost button (Browse image to be linked) and select rivets_normal from the list.
 * Note: This uses the “generated” image within Blender.  This may need to be changed to an external file once the final normal map has been created.
 * In Image Sampling select Normal map and leave other settings unchanged.
 * In Mapping change the Coordinates setting to UV.
 * In Influence deselect Diffuse->Color and select Geometry->Normal.
 * Save the Blender file.

We are now ready to commence the baking process.

Step 5 – Bake normal map

 *  Object Mode 
 * Go to layer 2 and move the hi poly mesh to layer 1.
 * Move to Layer 1 and select the lo poly mesh.
 * Move to Edit Mode (tab) and ensure you have Face Select Mode selected.
 *  Edit Mode 
 * Unwrap the mesh (U->Unwrap) and the mesh will be projected onto the rivets_normal image. As there is only one face on the lo poly mesh it just fills the image.
 * Return to Object Mode.


 *  Object Mode 
 * Ensure you are in Blender Render mode. This mode is displayed in the Blender Information panel – usually at the top of the main Blender window.
 * Select the Render icon (the camera) in the Properties Editor.
 * Close off all the sub window “twisties” and open up the Bake sub window.


 * The settings for the Bake window are:
 * Bake Mode -> Normals
 * Bake from multires -> unchecked
 * Normal Space -> Tangent
 * Clear -> checked
 * Selected to Active -> checked
 * Margin -> 2
 * Distance -> 0
 * Split -> Automatic
 * Bias -> 0.08 (see note 2)


 * Select the hi poly mesh and then Shift select the lo poly mesh.  Ensure that the lo poly mesh is the active mesh as we will be baking from the selected mesh to the active mesh.




 * Select the Bake option and you should see the rivets_normal image converted to a normal map.




 * Go to the UV/Image Editor window and save the normal map image (save as image) as a raw Targa file with the name rivets_normal.tga. The "raw Targa" file type can be selected from the file types in the Save as Image twisty on the left hand side of the window.  Check the save location as Blender tends to try and save the file in the root directory of your C: drive.
 * Move the hi poly mesh to layer 2. This is done so the mesh is not included in the exported IM file for Trainz.
 * Select the lo poly mesh. Note we cannot see the rivets on the object yet.
 * In the Properties Editor, select the normal texture and reload the rivets_normal file from disk. See Note 1.

Note 1: If you have packed the image into Blender the reload option will be unavailable. If this has occurred you will need to unlock the data block. But if you followed the instructions above this will not be necessary.

Note 2: The default Bias value is zero which, if unchanged may result in a splatter effect of bumps. This was the value chosen by video tutorials that the author observed. You can experiment with different values to see which gives the best effect. The result probably depends on the type of bumps and depressions being modelled.

Step 6 – Create diffuse material

 * Select the lo poly mesh in the 3D Editor.
 * In the Properties Editor select the textures icon.
 * Create a new texture called “diffuse” using the Image or Movie type. Load your simple one colour texture file.  This example uses the black.tga file common to many models.
 * Change the Mapping->Coordinates to UV.
 * The Influence->Color should be checked which is the default.

At this point you could just export the lo poly mesh to Trainz but it would appear as a plane. A more interesting object is a normal mapped cube so we will use the cube for our export.

Step 7 – Create riveted box

 *  Object Mode 
 * Move to layer 6 or the layer containing the cube.
 * Select the cube and then go to the Properties Editor and choose the Material icon.  If there is an existing material then remove it.
 * Apply the rivets.m.tbumptex material to the cube.
 * Change to edit mode.
 *  Edit Mode 
 * Select all faces of the cube and unwrap the cube to map against the rivets_normal image.
 * If necessary, move and resize each cube face to take up the entire rivets_normal image.
 * Change to object mode.

Step 8 – Export model

 *  Object Mode 
 * Save the Blender file.
 * Export using the Blender Trainz exporter. You don’t need to change any of the exporter settings but make sure it is exporting into your Normal_Test folder.

Assuming you have no errors, you are now ready to set up for importing into Trainz.

Step 9 – Create or modify the config.txt file

 * Open the config.txt file within the normal_test folder using your preferred editor. Add or change the file to reflect the sample config.txt below.  Remember not to remove or change the first line of the file.

Note: It is assumed you have a suitable thumbnail image. You could delete or omit that section but TS12 will complain. PEV’s AssetX program has a thumbnail generation facility.

Step 10 – Asset file check
We now need to import the asset files into Trainz Content Manager. There are several ways of doing this but for the purpose of this tutorial we will just copy the files using Windows copy.
 * Ensure the asset is “Open for Edit” in Content Manager. Open a window to folder …\Auran\TSxx\UserData\editing\Normal Test.
 * Open the development folder where the Blender files are located.
 * The following files are to be copied (not moved) from the development folder to the Trainz editing folder.
 * Config.txt
 * Normal_test.im
 * Rivets_normal.tga
 * Rivets_normal.texture.txt
 * Black.tga (this was my diffuse colour file – substitute whatever file you used)
 * Black.texture.txt

The following pictures show my source folder, with files to be copied highlighted, and the destination folder.





After copying the files, move to Trainz Content Manager, select the Normal Test asset and commit. If you have errors check you have all the files and that tga files are not compressed.

Step 11 – Display in Trainz
Launch Trainz from Content Manager (Ctrl L), find a suitable test route and session and add the “Normal Test” scenery object to your session. The lighting affects the display of the normal map texture and seems to work best on horizontal surfaces. In some lights the rivets are difficult to see. Below is how the asset looks in the author’s Trainz session (TS12). You may need to experiment with different colours and settings to get the effect that works best for you. Since the rivets are rather small, the effect is subtle and, in some lighting conditions, almost invisible.



This final picture shows the normal image in the UV/Image Editor, the textured cube in the 3D Editor and part of the attributes for the normal material in the Properties->Texture Editor. Note that the GLSL option is selected in the Shading panel in the 3D Editor Properties panel. This is only done for taking this picture so both textures are visible on the model.



Other Reading
The official Blender website at http://www.blender.org

Van Gumster, Jason (2011). Blender For Dummies 2nd Ed. Indianapolis, Indiana: Wiley Publishing, Inc. SBN 978-0470-58446-0.

WiKiBooks also has a Blender specific collection at Blender 3D including the "Blender: Noob to Pro" at Blender 3D: Noob to Pro that has sections on bump and normal mapping.