3D Game Programming All in One (Course Technology PTR Game Development Series)
Okay, enough talk. Time for some action—let's create some terrain. We'll use the Torque Engine and its internal Terrain Manager to create the terrain, and we'll employ the height-map method using the in-game Terrain Editor. There is another method, direct manipulation, that we'll use later in Chapter 18.
The Height-Map Method
For this section, you will need to fire up Paint Shop Pro. You should be fairly familiar with the basics, so I won't hold your hand too much with respect to PSP operations.
Note | The default size for a terrain in Torque (when the squareSize property in a MIS mission file is set to 8) is 65,536 world units (WU). One WU in Torque is equal to one unit in most third-party map editors. A WU is equivalent to one scaled inch. |
-
Start with a drawing of the contours to create the height-map image.
If you have a source for colored contour drawings for a section of land drawn at full scale (1:1), such as shown in Figure 12.9, get one that suits your needs. If not, you can use the images shown here, but in their colored format, which you will find at C:\3DGPAi1\RESOURCES\CH12. Each image has the same name as the figure number used here.
Figure 12.9: Contour map. -
Clip out the portion you want and save it as a PNG image, as shown in Figure 12.10.
Figure 12.10: Cropped and resized contour map. -
Now you need to do a little noodling over scale and unit numbers.
In Torque each terrain square is made of two terrain triangles sized at 256 WU by 256 WU; as mentioned earlier, the default squareSize property in a Torque mission file equals 8 by default. The terrain has 256 of these squares per side for a total of 65,536 world units (inches) per side.
256 WU 256 squares=65,536 WU (inches)
If we convert the units, we get 5,641.3 feet, or 1,664.6 meters (1.034 miles, or 1.6646 kilometers).
65,536 inches 12 inches=5,461.33 feet 1 mile=5,280 feet 5,461.33 feet 5,280 feet per mile=1.034 miles 1 mile=1,609 meters 1,664.6177 meters 1,609 meters per mile=1.034 miles
The value 8 (for squareSize) and the value 65,536 (for terrain size) are not accidental; they are powers of 2. This works nicely with our images as well as the software. The size for our height-map image must be 256 pixels by 256 pixels. This means that when the image is stretched to fit our terrain of 65,536 inches by 65,536 inches, each texture pixel (texel) determines the horizontal distance of 256 inches (or 6.504 meters) of terrain. Because each terrain square is 256 WU, each height-map texel is used to determine the height of one terrain square.
256 pixels 256 WU (inches)=65,536 WU (inches) 256 inches 39.37 inches per meter=6.5024 meters 6.5024 meters 256 pixels=1,664.6177 meters=1.664 kilometers=1.034 miles
-
Based on the preceding calculations, we can get the equivalent area in the image—crop the image just inside the line box created in the Figure 12.10 drawing representing 1.034 square miles.
-
Resize the image to 256 by 256 pixels.
-
Save the image as a PNG file to preserve the original colors for the contours.
In a moment you will paint over this contour image using gray color values representing the heights of the contour lines. In this case the contours range from an elevation of 410 feet to 485 feet. This information is available from the source of the contour maps. The grayscale can be any sequence of gray RGB values within the 256 colors ranging from 0,0,0 (black) to 255,255,255 (white).
-
Establish your scale, keeping in mind that it's best to have some separation between the incremental values so they can be easily seen as you paint the contours.
Examination reveals that there are 16 elevation increments in the contour range of 410 to 485. Divide the 256 colors for the grayscale range by 16, and you will get the values in Table 12.2, which starts at the color (0,0,0) and works up.
Table 12.2: Elevation RGB Values Elevation
RGB
Increment
485
240,240,240
1
480
224,224,224
2
475
208,208,208
3
470
192,192,192
4
465
176,176,176
5
460
160,160,160
6
455
144,144,144
7
450
128,128,128
8
445
112,112,112
9
440
96, 96, 96
10
435
80, 80, 80
11
430
64, 64, 64
12
425
48, 48, 48
13
420
32, 32, 32
14
415
16, 16, 16
15
410
0, 0, 0
16
Now that we have the values, we need to create what Paint Shop Pro calls swatches. We need to make a different swatch for each increment.
-
Make sure that you have the Materials palette visible in Paint Shop Pro by choosing View, Palettes, Materials and clicking on the Swatches tab in the Materials frame.
-
Delete any existing swatches by clicking on each one and then clicking on the trash can icon below the swatches. If you think you can keep track of your custom swatches and the ones already there, then you can skip this step.
Next we will create a new swatch for our first increment.
-
Click the Create New Swatch button at the bottom of the Swatch frame.
-
Type in a name for your swatch. I suggest you use either the increment number or the elevation value for the name. You could also use both, as in 1-485 for the topmost entry from Table 12.2.
-
The Color dialog box will appear. Here you type in your RGB values, referring to Table 12.2 for your numbers. Click OK to close the dialog box.
-
Repeat steps 1, 2, and 3 for each increment in the table.
-
Now fill in your image following the contour lines as shown in Figure 12.11. Use a combination of the Brush and Fill tools, at your discretion, to complete the task.
Figure 12.11: Contour map with grayscale values. Notice that in Figure 12.11 the grayscale value is the same at all the edges. This is because we want the edges to match when the terrain repeats itself, if it is tiled—and in this case that's what we will be dealing with. The edges could be different values; you would then just match them at the top and bottom or left and right sides.
-
When you have finished the "paint-by-number" process, convert the image to grayscale by choosing Image, Grayscale.
-
Save your image as a PNG file.
-
Flip the image around its X-axis—this flips the top with the bottom—by choosing Image, Flip. You should get an image like that in Figure 12.12. Make sure you save your work.
Figure 12.12: Terraced height map. Notice the terrace effect in Figure 12.12. If you import this into Torque as is, you will have a set of terraced, or stepped, surfaces. If this is what you want, then you're good to go already. However, let's go a bit farther.
-
Make a copy of the image you created in step 7 to work with.
-
Choose Adjust, Blur, Gaussian Blur to smooth out the edges a bit. Use a radius of four and then save your changes to this new image as a PNG file. You should get an image much like the one shown in Figure 12.13.
Figure 12.13: Blurred height map. Tip It can be more difficult to locate your original contour features after smoothing with Gaussian Blur. A quick work-around is to try reducing the radius or use the original image unblurred and smooth the terrain in Torque using the Terrain Editor (covered later).
A more time-consuming technique (but much more accurate and rewarding) is to create the terrain image at a much larger scale and reduce it to 256 by 256. For example, you might try constructing the image at around 2,048 by 2,048 or 4,096 by 4,096; this means much more painting time, but after reducing the image size again, the blending information is retained (although somewhat smoothed) by the resize algorithms. The resulting terrain is much more accurate than the Gaussian Blur process.
This last height-map image is the one you will work with to create the terrain.
Next, we will import these images into Torque.
-
Place the images in Torque's C:\3DGPAi1\common\editor\heightscripts folder. If the folder does not already exist, create it.
-
Use the Run fps Demo shortcut to launch Torque.
-
Run any existing mission to which you'd like to add this terrain or choose File, New Mission.
-
Press F11 to open the World and Terrain Editor.
-
Choose Window, Terrain Terraform Editor (as shown in Figure 12.14) to open the Terrain Terraform Editor.
Figure 12.14: World Editor Window menu with Terrain Terraform Editor checked. -
On the right side of the screen, in the General Settings area (see Figure 12.15), set Min Terrain Height and Height Range in meters.
Figure 12.15: Terraform Editor. The maximum elevation in the terrain we are modeling is to be used for Minimum Terrain Height (the Minimum Terrain Height box is mislabeled in the Editor). You will recall that the highest elevation is 485 feet; this translates to a Minimum Terrain Height value of approximately 148 meters.
485 feet 3.281 feet per meter=147.8208 (148) meters
Height Range represents the distance from our lowest to highest elevation. The grayscale color values of our height-map image will be interpolated between these values. We need to calculate the difference and multiply that by the ratio of highest color number divided by total number of grayscale colors (256) and convert to meters. Clear as mud?
485 feet−410 feet=75 feet 240 256 75 feet=70.3 feet (240 is our highest color number in Table 12.2) 70.3 feet 3.281 feet per meter=21.4 (21) meters
-
Now click the Operation box to roll out the Operation dialog box, as shown in Figure 12.16.
Figure 12.16: The Operation dialog box. -
Select Bitmap from this dialog box—this brings up a bitmap Load File dialog box, as shown in Figure 12.17.
Figure 12.17: The Load File dialog box. -
Highlight the image you want translated to a new terrain and click the Load button. You should find the height-map image you saved earlier in C:\3DGPAi1\common\editor\heightscripts, from Paint Shop Pro.
-
Click the Apply button at the right side of the menu. You will see the terrain change. To relight the scene, choose Edit, Relight Scene. There will be a slight pause in input response while the relighting occurs.
-
In the lower left of the screen the overhead map view of the terrain will change to show the contours imported from the height-map image.
Notice that this image, as depicted in Figure 12.18, has the same orientation as the original one before we flipped it around the X-axis in step 8 of this list.
Figure 12.18: The overhead view. The white line in the map shows the terrain boundary, representing the extents of your terrain before repeating. In the main 3D view, a green translucent box illustrates this boundary, as you can see in Figure 12.19. The terrain boundary is a fixed dimension—you can't change it.
Figure 12.19: The terrain boundary. Figure 12.20 illustrates where to find the inner red box that represents the mission area. You can change the extents of the mission area boundary by using the Mission Editor.
Figure 12.20: The mission area. -
Choose File, Save As to save your mission with your own unique name. You should save your new file in the directory C:\3DGPAi1\fps\data\missions.
When you save your mission, the terrain data is also saved as a TER file in the C:\3DGPAi1\fps\data\missions directory. If you want, you can also import previously saved TER files rather than re-creating height maps.
Note | Reference to the newly created terrain file is stored in the mission file in a TerrainBlock that needs to be named "Terrain": new TerrainBlock(Terrain) { rotation = "1 0 0 0"; scale = "1 1 1"; detailTexture = "~/data/terrains/details/detail1"; terrainFile = "./myterrain.ter"; squareSize = "8"; locked = "true"; position = "-1024 -1024 0"; }; |
The units displayed in the Mission Editor map (x, y, w, h) represent the (x,y) distance of the upper-left corner of the mission area (in red) from the image center and the (w,h) width and height of the area in terrain texture units. Note that the position parameter in the mission file also uses the terrain texture units to position one terrain repetition.There are 32 repetitions of the terrain textures (don't confuse these with the height-map images) with each terrain texture image being 256 by 256 pixels.
32 reps 256 pixels=2,048 texture units 65,536 WU 2,048 texture units=32 WU per texel
This information will be useful when creating terrain textures. Convert these values to inches by multiplying by 32. (The total area represented ranges from −1,024 to +1,024 when the terrain squareSize=8 for a total 2,048. And 2,048 32=65,536.)
If your contour area needs to be other than 1.034 miles, you can change the terrain squareSize. This will determine the area available before the terrain repeats. As you can see in Table 12.3, you must adjust the squareSize parameter in powers of 2.
Terrain SquareSize | Texels +/, total | Texels 32 = WU | Feet | Miles | Meters |
---|---|---|---|---|---|
32 | + −4,096=8192 | 8,192 32=262,144 | 21,845.33 | 4.137 | 6,658.13 |
16 | + −2,048=4,096 | 4,096 32=131,072 | 10,922.66 | 2.068 | 3,329.06 |
8 | + −1,024=2,048 | 2,048 32=65,536 | 5,461.33 | 1.034 | 1,664.53 |
4 | + −512=1,024 | 1,024 32=32,768 | 2,730.66 | 0.517 | 832.26 |
2 | + −256=512 | 512 32=16,384 | 1,365.33 | 0.258 | 416.13 |
Changing the terrain squareSize in the mission file also affects the control in the Terrain Editor and terrain material painter; you will have more control at smaller sizes. Be sure to change the position values of the terrain to correspond to the worldSize also. For example, if you want more control of the terrain editing, set the squareSize to 4 and the position to −512 −512 0:
new TerrainBlock(Terrain) { rotation = "1 0 0 0"; scale = "1 1 1"; detailTexture = "~/data/terrains/details/detail1"; terrainFile = "./myterrain.ter"; squareSize = "4"; locked = "true"; position = "-512 -512 0"; };
Applying Terrain Cover
Terrain textures must be PNG format images and must be 256 by 256 pixels in size. These textures should be placed in a subdirectory under C:\3DGPAi1\fps\data\terrains; they will also work directly in the terrains folder.
Terrain textures are stretched to 2,048 world units (WU) if the terrain squareSize is 8. This means there are 32 repetitions of a terrain texture across one terrain width or depth (1 terrain rep). This also means there are 8 WU per texture pixel (texel).
65,536 WU 32 texture reps=2,048 WU per texture rep 2,048 WU 256 pixels=8 WU per texel
If the terrain squareSize is set to 4 in the mission file, there will still be 32 terrain texture repetitions, but each repetition will only cover 1,024 WU of the terrain.
And although not a requirement, terrain cover textures will look best when they are created to be tiled, as discussed earlier; opposite edges should match so that when they are tiled you won't be able to see the edges. The images in Figure 12.21 and 12.22 are test textures that are 256 by 256 pixels. The checkerboard pattern in Figure 12.21 has each white or black section sized at 128 by 128 pixels.
The grid texture in Figure 12.22 has white lines every 32 pixels and red lines at 128 pixels. You can use these images to calculate the total terrain size with respect to the dimensions of objects created in a map editor as well as to calculate the terrain square size with respect to terrain textures. In addition, you can use the image in Figure 12.22 to create sightlines when manually adjusting terrain heights.
To paint our terrain cover:
-
Place these images in a subdirectory under C:\3DGPAi1\fps\data\terrains.
-
Use the Run fps Demo shortcut to launch Torque.
-
Select the mission you created in the previous section.
-
Press the F8 function key to switch to "fly" mode.
-
Fly up above the terrain a bit using the arrow keys to move and the mouse to aim and look down. You can use F7 to switch out of fly mode when you want.
-
Press F11 to open the World and Terrain Editor.
-
Choose Window, Terrain Texture Painter, as shown in Figure 12.23.
You will now see the Material Selection dialog box (as shown in Figure 12.24) to the right. You can highlight the material you want to paint with or change or add new textures here.
-
To add a new material, click an Add or Change button and you will get a new texture image Load File dialog box, as shown in Figure 12.25.
Figure 12.25: Image Load File dialog box. -
Highlight the image you want and click the Load button. That image is now in your selection set.
-
From the Action pull-down menu, make sure Paint Material is checked.
-
Now go up to the Brush pull-down menu and select your desired brush size.
Remember that we are using the default terrain squareSize set to 8. Table 12.4 lists the area of the terrain that is influenced based on brush size.
Brush Size | Texel | World Units (texels squareSize) |
---|---|---|
1 | 1 32=32 | 32 8=256 |
3 | 3 32=96 | 96 8=768 |
5 | 5 32=160 | 160 8=1,280 |
9 | 9 32=288 | 288 8=2,304 |
15 | 15 32=480 | 480 8=3,840 |
25 | 25 32=800 | 800 8=6,400 |
Figure 12.26 depicts a texture applied with the brush size set to 1, and Figure 12.27 shows the corresponding terrain grid.
You can see the 32 by 32 texel influence area for one brush that corresponds to 256 WU for the terrain squares shown in the grid view.
So take your trusty Terrain Paint Brush and go nuts!
Категории