Maya tutorial – Terrain Displacement

In this tutorial you will learn how to create the image seen below using displacement in Mental Ray for Maya.
To follow along with this tutorial you will need the following software installed on your machine:

* Maya with the Mental Ray plug-in loaded
* Adobe Photoshop CS* or higher – For image stitching
* WinZip or WinRar – To extract the files
* Pull SDTS – To convert the DEM data to Raw files
* Python – Required to run PullSDTS

 

* Note: You can use an older version of Photoshop, but you will be limited to 8-bit TIFF file output. With Photoshop CS you can export 16-bit TIFF files for finer displacement.

We are going to be building a digital version of Mount Rainier using satellite digital elevation maps (DEM) and displacement inside of Maya with the Mental Ray plug-in. One key factor to remember while following this tutorial is ORGANIZATION! There will be a LOT of information covered here and you can get lost really quickly. Keeping things like file names and subfolders organized will help you keep things manageable along the way.

DEMs are greyscale photographs taken by satellites in orbit of the Earth capturing terrain elevation values. These DEMs are broken down into approximately 6.5 mile by 7.5 mile sections called quads.

Before we get started you will need to create two (free) accounts to gather the images needed for this project. One account will be at the USGS Quads site which will be used to determine what quads make up the area we will be building. The other account will be at the Geocomm website which is where we will be downloading the quads from. You have the option of setting up a pay account at Geocomm. When you pay for a premium account you get a faster connection speed for downloading. Or you can just use a free account and the downloads will take longer. The typical connection speed is limited to 3-5 KB/sec for the free account. I am not 100% sure if the download speed is based on IP address or your log on account. When I download with several machines, that are behind a single IP address router, the total speed is spread over each download and you can achieve blazing speeds of 0.2 KB/sec. So, I just find it better to let one download have all the bandwidth and find something else to do while you are downloading. The average file size is 2-4 MB usually resulting in around 10 min to download each quad. The other option would be to have Geocomm burn the DEM data to a CD and mail it to you for a fee.

The quads are sorted by county on the Geocomm site so you will also need to locate the county names for your target location. I usually do a search on Google for ” county map” and within the first few hits I can find a good image to go with. This is the county map image I found for Washington State.

Washington County Map

Sometimes, as with this project, your quads may span two or more counties depending on their location. This is where it’s helpful to have a map of what counties butt up next to each other.

After you have your counties image, go to the USGS Quad finder site and under the “1:24k Search” navigate down to Washington in the list and click it.

USGSquads

This is where you will need to have an account created. When you locate the section of quads we will be needing you will find that some of our quads are located in Pierce County and some are in Lewis County.

I’ve added the red line and county names to our map for illustration. This is where your research skills come into play in determining county lines.

Now you are ready to jump over to the Geocomm website and start the download process.

* Click the GIS Data tab across the top of the page.

* Click the Download GIS Data button.

Geo community

* Click the Washington State image on the U.S. map.

Geo Community

* Click the Pierce County link.

Pierce County link.

* Click the 24K DEM link.

This next page is where you will find the quad names to download. Search the list and you will come to our first quad, Golden Lakes.

On the right side you will see two download links. A green one and a tan one. The green one is the free download, and the tan one is for the premium download accounts. The only difference is download speed. You also have the option to add them to the cart and purchase them on CD via the mail. However you get the files is completely up to you. I will be using the free method for this tutorial. Once you get to the download page you may have two options. The 10 meter and 30 meter image. This has to do with resolution of the image. The lower the number the finer the detail and better overall quality. For this tutorial we will be using 10 meter DEM data for all quads. Sometimes you will only have the 30 meter option. Always opt for the best quality possible, for obvious reason. There may also be cases where you have all 10 meter DEMs for most of your location but one quad will only have 30 meter available. That’s a judgment call on your part. I’ve mixed and matched resolutions before and just made sure to keep that area out of the focal point of the rendered frame. It looked fine for the shot I was doing…again, your call.

For clarification issues I tend to rename the files being downloaded. As you can see in this case the default name does not represent something logical that we can distinguish quickly.

I would append the file name to be something like:

* “GoldenLakes – 1675707.DEM.SDTS.TAR.GZ”

Again, this is just me but I find with all the organization that will be needed in later steps it helps to keep quad names straight. Go ahead and repeat this process for downloading all 15 quads shown in this image.

Now that you have all of your DEMs downloaded you can extract the files. The *.gz file format is simply a compressed file that WinZip or WinRar should be able to extract with no problem. When extracting the files I tend to use the “Extract To” method and accept the default sub folder name. This is why you want to append the quad name to the beginning of the files when you are downloading them. That way you are left with a subfolder structure that is easily recognizable when browsing your project. When you extract the file you should have about 18 *.ddf files and a README for each quad.

NOTE: Occasionally I’ve ran into an issue where I download the quad *.gz file and when I extract it there is only one file inside. I have not yet figured out what causes this, but usually re-downloading and extracting the file again will fix it. You may need to try downloading from a different machine. That’s usually what I do.

Now you are ready to start converting all of these files to something that Photoshop can read. For this we will be using a program called PullSDTS. In order to use this program you must have the Python plug-in installed on your machine. Launch PullSDTS and click the [Select File] button.

Locate the first quad folder and you will note that only one *.ddf file is listed. Select and load that file. Now click the [Copy] button to copy the header text to the clipboard.

Launch a text editor like notepad and paste the text into the new document. Repeat this process of copying and pasting the headers into this new document. This header information makes up what will be our rangeStatistics used in the conversion process. Information such as elevation, quad size, and world coordinates are in this file. I’ve attached a copy of my completed text file rangeStatistics.zip for you to download. What you need to do is scan through all 15 headers and determine what the lowest (min) elevation is and the highest (max) elevation for our entire range. You also will need to figure out what the Northwest and Southeast coordinates are for later. We will use the east and west coordinates to determine how wide our range is, too. I’ve done this for you in the text file and added this to the top of our rangeStatistics file. After you get all of this required information, it’s now time to convert the data. Go back into PullSDTS and click the [Select File] button again. Re-load our first quad, then click the [Load DEM] button. You will now see a greyscale image of that quad in the left side window. Click the [Save RAW] button.

Digital Elevation Model (DEM) Terrain Displacement

In the save options window you want to set:

* * Units = Feet
* * Byte Order = “PC” (Intel)
* * Header = None
* * Grays = Equalize to Range*
* * Bit Depth = 16
* * Fill = Black

*Note: In the “Range” you need to enter your “Min Elevation” and “Max Elevation” from the rangeStatistics created on the previous page. This will equalize all of the greyscale images to the same black-to-white elevation ratios. That way they match up seamlessly when stitching them together. If you did not do it this way, the images would have drastic variances when stitched together and the displacement would have holes in it where neighboring images met. Being that we set the Units to Feet, you must enter the feet values for the elevation. If you need a calculator to convert meters to feet, you can download my script

Click the [SAVE] button and accept the default name for the file. It will look something like:

* “GoldenLakes_971x1401_PC.raw”

The 971×1401 is the image size. You will need these numbers when reading the file into Photoshop later, so I just find it easier to leave them as part of the *.raw file name for reference. Go ahead and repeat this process to convert the rest of our 15 quads.

Here comes the fun part! Once you have all of your quads converted to *.raw files crack open Adobe Photoshop. Currently in Maya v. 6.5, Mental Ray has a texture limitation of 8k (8192 x 8192). Even though Mental Ray says it can handle 8k textures, I’ve had problems when using exactly 8k images, so I would just go a little smaller like around 8100 x 8100 resolution. Create a new document in Photoshop and set:

* * Width = 8100
* * Height = 8100
* * Color = RGB 16 bit
* * Background color = Black

If you are using different DEMs than we are downloading for this project you may need to have a larger image. Just make sure it is square so when we reduce it later it will fit evenly on your NURBS displacement plane, which will be square. Remember the final image has a max of 8k for Mental Ray.

Now you want to open all of the *.raw files you created with PullSDTS. When you select a *.raw file to open you are greeted with an options box.

For the “Dimensions” you need to enter the Width and Height values from the quad names we saved while exporting from PullSDTS. (…971×1401…) Then you want to set:

* Depth = 16 Bits
* Byte Order = IBM PC

Repeat this process opening all 15 *.raw files into Photoshop. Next you need to drag the “Background” layers for each of these files into our 8100 x 8100 image we created earlier. Lay the images out so that they match the quad layout from our USGS Quad diagram. As you are dragging them over you may want to name each new layer apropriatly.

I also drag them in order from the top left quad across to the bottom right quad. This way it keeps the layers neatly in order inside the new image. You will need to set the “blending mode” to Lighten for each layer. This is so that when you start butting the images the black border will disappear leaving you with only the greyscale image. This is also why we set the background color of the new image to Black.

Now you will need to zoom into the streets between your quads and move them all together.

When you get in closer you will note the edges of each quad have a stair step pattern to them. These “steps” mate together and form a seamless image.

After you have butted your quads together, you want to merge all of your layers into one for our final output.

You will also note that we are now using less than our image size of 8100 x 8100. You always want to keep your image square and a power of 2x. So, you can crop the image size down to 6k (6144 x 6144). I find it easier to start large and then crop it down later. Save the image as a TIFF file format.

The output file will be fairly large. This one should be about 215 MB which is average for a 6k, RGB16-bit TIFF file.

OK, you are finally at the point where you are ready to launch Maya and start displacing. Open Maya and create a NURBS plane with these settings:

* * scaleX = 614.4
* * scaleY = 614.4
* * scaleZ = 614.4
* * patchesU = 25
* * patchesV = 25
* * degree = Cubic

This is why we need to make sure the Photoshop image we are using for displacement remains square.

You will notice that we scaled the flat NURBS plane in all 3 axis. (X, Y, and Z) The reason for this is because Maya calculates displacement as if it were a volume. If you have a flat plane and you leave it’s scaleY value at 1, it will not displace properly because it only has 1 unit of volume to work with. You need to scale your plane in Y to a value at least that of your target displacement. Since we do not know that number yet, I just scale all 3 values the same amount. The reason for 614.4 is because I prefer to work in 1/10th scale. This eliminates all the potential problems Maya has when dealing with larger scaled scenes. So, since our TIFF image is 6k (6144 x 6144) it’s a straight conversion to 614.4 x 614.4. The patches 25 and cubic are going to come into play when we convert the displacement to polygons later.

With your plane selected use the pull down: “Window > Rendering Editors > mental ray > Approximation Editor…”

In the Approximation Editor you want to click the [Create] button next to “Displace Approx.”

Next click the [Edit] button and change the Presets to Fine View High Quality.

This is just a default preset for the Displacement Approximation and works well for our purposes.

Next:

* * Create a new Lambert shader and click the “Output Connection” button located at the top of the Attribute Editor.

* Then click the map button next to “Displacement Mat.”

* Then click the “File” button in the Create Render Node window.

* Click the file tab and then map our TIFF image to the “Image Name” field.

Now you can render your image to see how things look.

You will see a small amount of displacement in the image, but not quite what we are going for. Open the Attribute Editor for our file node and expand the Color Balance folder.

This is where you dial-in your displacement height. Enter a value of 10 for “Alpha Gain” and then select your NURBS plane. In the Attribute Editor for your plane, expand the Tessellation folder and check the box for “Display Render Tessellation”.

Check the box for “Enable Advanced Tessellation” and change the Number U and V to a value of 5. What this will do is increase the number of faces your NURBS plane has based on its spanU and spanV values that we earlier set to 25. The Attribute Editor says that the “Triangle Count” will be 31250 for our plane, but that is WAY off. With your NURBS plane selected use the pull down “Modify > Convert > Displacement to Polygons”.

This could take a very long time depending on your machine. If your machine crashes, change the Number U and V to a lower value in the “Advanced Tessellation” section we set to 5 earlier. These numbers control how many polys Maya generates when converting the displacement to polys. Lower numbers = less polygons = less memory intensive on your machine.

This is still not enough detail for what we are going for, but it’s required to get the scale proper. Next you want to create two locators and snap them to the lower left and lower right corners of the polygon displacement.

Then using the measure tool (Create > Measure Tools > Distance Tool) you want to measure the distance between these two locators.

The resulting value is 479.26 units. Back in the rangeStatistics rangeStatistics.zip we noted that the total width of our range is 48,378.5 Meters and the Max elevation is 4393 Meters. Well, the “Alpha Gain” that we set to 10 before means that our displacement is currently 10 units high. Therefore we can calculate out how much we need to set this alpha gain to get a true scale for our scene.

The formula is:

scene distance / desired distance = alpha gain / max elevation

Thus:

479.26 / 48,378.5 = X / 4393
X = 43.5

We now need to increase our Alpha Gain for the displacement file node to 43.5. Since our original “Displacement to Polygon” conversion was done with Alpha Gain set to 10, we need to delete that poly surface and run the conversion again. This polygonal surface will never be rendered, but it is needed to have something physical in the scene to align our camera to.

You will also note that if you used the original numbers of 5 for Number U and V in the tessellation settings, your poly count for the surface is roughly 1.1 million polygons. A far cry from the 31250 that Maya estimated. But even with the high poly count, Maya still has decent performance when tumbling your camera into position.

Now it’s just a matter of selecting a nice view poit for your camera and firing off a final render.

From: Mike Harris