Documentation version: 1.0 - 5 Jan 08
References:
Assumptions:
This tutorial shows how to create stencil shadows in Rail Sim using Blender. We'll create a simple cube object and see what that the resulting shadow looks like, and then make some adjustments to get it to display optimally. This is a quite detailed tutorial, so may look rather lengthy and daunting, but the process it is actually quite fast and simple once you know how. An example .blend file is available too: shadow_tutorial.zip.
Firstly we will create a simple cube to form the basis for our shadow. We don't even need a 'parent' object to go with the shadow, Rail Sim will let us just display the shadow all by itself :). So, for this tutorial we will create only a single shadow object to keep things straight forward.
Firstly let's create a cube, correctly set it up for Rail Sim, and then export it into the asset editor to see what it looks like. Start up Blender, and select front view (numpad 1 or view/front from the 3D view lower menu bar) in the 3D view. If the 3D cursor is not at the origin, then press shift-c to make it so. I recommend using orthographic view (numpad 5 or from the view menu). Add a cube to the 3D view (spacebar, add, mesh, cube).
We are now going to move the cube up so the bottom sits on the the ground plane, otherwise we will only see half of the cubes shadow. There are two ways to move or transform objects in Blender, either 'drag' using the mouse or more precisely by using the transform properties. We'll use the more exact method here to ensure consistent results. As mentioned in the Blender exporter docs, we should generally aim to transform the vertexes (ie transform in edit mode) versus transforming the object matrix (ie transforming in object mode). The main reason for this is rendering efficiency. Enter 'edit' mode by pressing tab or select edit mode from the menu - the object should be fully selected by default, which is what we want.
Open the transform properties box (n key). Note the median x,y, and z values are 0.00 as expected. Make sure the global/local button is selected to global. Click the left mouse over the centre of the 0.00 in the z box. Enter 1 and press enter. The cube should move up so that the base is sitting in the horizon plane. Get rid of the transform properties box (n key or click close 'x')
Now we want to name the cube correctly and set up the material. In the buttons pane select the 'Object' icon (or press F7). Under the 'Objects and Links' pane you should see the mesh object name 'OB:Cube'. Click in the box and change the name to '1_0000_shadow_test' and press enter. Blender will prefix with 'OB:' so you should have 'OB:1_0000_shadow_test'. Note that because of the small size of the name window some of the name may not display, but it is all there.
The '1_0000' part of the name refers to 'level of detail' (LOD) 1 and '0000' display distance in meters. Since we used 0000 for display distance, Rail Sim will render it from 0 metres to infinity (as per Rail Sim docs). We could also choose a distance, say '0100' meters and it would display, but either choice will work.
The 'test' part of the name can be anything you like, but keep the name short or Blender may truncate. A shortened 'OB:1_0000_shadow' will also work, but 'OB:1_0000_test_shadow' will not, ie 'shadow' has to be straight after the LOD and distance.
Now click on the 'Texture buttons' icon (or press F6). Under texture pane click 'add new'. By default the name will be 'TE:Tex'. We set the shader for Rail Sim here by naming the texture according to the shader we want. For shadows, the shader is always 'StencilShadow.fx', but importantly in Blender we abbreviate the shader name according to the table in the Blender exporter docs. The table shows we should use 'Shadow' for the texture / shader name. So enter 'Shadow' in the texture name and we should se 'TE:Shadow'. Note the upper case 'S' in this case.
Almost there. Under the 'texture type' selector change it from 'None' to 'Image'. Select 'Load' under the 'Image' pane and load a texture. Any valid Rail Sim texture will do because it is not actually displayed, but used as a 'place holder' to make sure the igs file is correct. So if you haven't done so create a 32x32 'dummy' texture and save as a .png/.bmp/.tga, it doesn't matter about the name or type. Convert the texture to ace format and store in the textures directory of object you are exporting to in the 'Source' directory of Rail Sim. So you should have the texture you created loaded in Blender as a .png/.bmp/.tga and you should see it in the preview window.
Now we need to add UV's to the object and then export. The UV's don't matter because the texture isn't displayed, so we just add generic ones. In the 3D view select 'UV Face select' from the mode menu. All faces will be selected by default. In the 'UV/Image Editor' window click on the lower menu bar 'image/open' and load the same texture. This is all it takes to assign generic UV coordinates to the cube. Change the window back to 3D view. Select 'Object mode' from the Mode menu drop box. If you want to see the texture displayed you will have to select 'Texture' from the viewport shading menu drop box (default is solid).
Let's export now. From the upper menu bar select 'File/Export/RailSim_iga and export the object. Check the Blender console/text output window to make sure everything went okay and there are no warnings. You should have an advisory about UV coordinates set to UV from 'ORCO'. Good, the exporter did this automatically for you and saved some work.
Fire up the asset editor and preview the object. What ??! That doesn't look right does it ? The shadow is there, but looks a bit odd. Certainly not what we'd expect or want.
The result in asset editor of our first effort (bounding box is selected & moved up out of the way):
What's going on is that by default 3D modellers export the vertexes and faces you created, but the vertex normals are averaged between the faces. See below, Blender is displaying vertex normals of the cube we created (select 'Editing' mode or F7 from the buttons pane, select 'Edit mode' in the 3D view and on the 'Mesh Tools 1' pane select 'draw VNormals'). See how the normals point off at an angle to all the faces, because there is only one shared vertex at each corner ? Well to fix this we have to create vertex normals that point in the same direction as the faces. This is a really easy task in Blender.
The cube with 'averaged' vertex normals - the blue lines at each corner represent the direction of each vertex normal:
Modifiers are really cool features, they allow real time preview of the effect(s) we want, but we don't have to 'apply' them until just before export. This gives excellent flexibility and 'undo-ability'. If you have not done so, select 'Edit' Mode in the 3D view and 'Editing' Pane in the buttons window (or press F6). Find the modifiers pane. It should say 'add modifier'. We now select one face of the cube ready to apply our modifier. We could also select multiple faces if it was a more complex model.
In the 3D view find the 'Face select mode' icon, it looks like a triangle with a yellow dot in the centre (or ctrl-Tab then 3 key). Press 'a' key to deselct all the faces (they are purple if selected). Right click on one of the cubes faces to select it. If we have all faces selected and apply the modifier it will still work in this case, but normally we apply the modifier per 'smoothing group' in Max parlance or in Blender, per faces we want to be smoothed together. Now in the modifiers panel add a 'edge split' modifier. An edge split details box will be added to the modifiers list at the top. Notice in the 3D view all of a sudden each face has vertex normals that point perpendicular to each face. There are now three normals per corner, or more accurately three vertexs at each corner. Beauty.
The edge split modifier added, but not 'applied':
We can add and remove, combine and manipulate modifiers parameters until we are happy. In this case (and most cases) the defaults will do nicely. So press 'apply' in the modifier's data box. We may get the error message 'Modifiers cannot be applied in edit mode'. Easy fixed, press the 'tab' key to go to object mode, or select 'Object mode' from the 3D view 'Mode' menu bar drop box. Now press 'apply' to the modifier. The data box will disappear and the modifier is applied to the model. Go back to edit mode and have a look at the vertexes and normals now. This is what the vertex normals look like now.
The cube with 'split' vertex normals - the blue lines at each corner represent the direction of each vertex normal:
The end result is that up to 3 times the vertexes are created for the object, one at each faces corner, but this is what is needed to work properly. We end up with 3 coincident vertexes at the corners of the cube. Visually we cannot tell the difference and the object/shadow is displayed correctly. In fact the above process is needed for many models not just for shadows. Whenever we want a flat surface we have to do an 'edge split'. This is because directx, the Rail Sim renderer (and OpenGL for that matter) always smooth normals between faces by default. This is good because curved surfaces look a lot smoother. If we want 'hard' corners or edges however, we have to created more 'geometry'. The process is the equivalent of 'smoothing groups' in Max, but perhaps easier. Later, the Blender exporter many include a script to emulate Max's smoothing groups, but for now we use Blender's Edge Modifier.
Now re-export the model and fire up the asset editor again. Note also that the Blender exporter log reports 24 vertexs exported vice the previous 8, as we would expect. Fantastic, the shadow looks correct now.
The proper shadow (the gray box is just the default collision bounding box and not the cube object):
Hopefully you enjoyed this tutorial. Happy shadowing.
© SteamPSI, 2008