Main Content

Generate Unreal Engine Landscape

This example shows how to use the Unreal® Editor to create a landscape map by importing a heightmap file. Supported file formats include:

  • 16-bit, grayscale PNG file

  • 16-bit, grayscale RAW file in little-endian byte order

This example uses the unsigned 16-bit integer (uint16) format.

Create Data

Use 65-by-65 mesh points to create a new landscape using the MATLAB® shape membrane. The size of the mesh determines the size of the final landscape in the Unreal Editor. By default, the Unreal Editor uses a mesh of one meter by one meter, so a 65-by-65 mesh translates to a landscape size of 64 meters by 64 meters at default scale.

npoints = 65;
m = (npoints - 1)/2;
L = membrane(1,m);
x = -1:1/m:1;

If you use sampled data instead of an analytical function, sample the heightmap data on a mesh of one by one meters. The maximum span is 8128 meters by 8128 meters. To use sampled data, choose npoints equal to the number of sampled points along X or Y.

Convert to Grayscale

For maximum resolution, set the range of heightmap values from the smallest (0) to the largest (65535) integer possible. Create a uint16 grayscale heightmap using this data, and display the heightmap as an image.

maxL = max(L,[],"all");
minL = min(L,[],"all");
range = maxL - minL;
scale = range / 65535;
Lgray = uint16((L - minL)/scale);
imagesc(Lgray); colormap(gray)

Save File

Save the heightmap in a 16-bit PNG format file that you can import to the Unreal Editor.

Note: If you impose a 0:255 grayscale colormap in the export setting, you risk losing resolution.

Lfile = 'Lgray_uint16.png';

Create Landscape in Unreal Editor

To create the landscape in Unreal Editor, create the landscape environment, import the heightmap file, apply materials to the heightmap, and reparent the level blueprint for use in MATLAB.

Create Landscape Environment

  1. Open the Unreal Editor. In the Content Browser, navigate to the desired location for the new landscape.

  2. Right-click on the desired folder and select Create Basic Asset > Level.

  3. Name and save the level, then double-click to open it. In the World Outliner, the level appears all black and contains no items.

  4. Open an existing level, such as Airport. In the World Outliner, shift-select all nine items in the Environment folder, then right-click to Edit > Copy.

  5. Open the new level again and paste the copied items into the World Outliner. The level now includes lighting and an atmosphere.


Import Heightmap File

  1. On top of the viewport, select Modes > Landscape to add the Landscape mode toolbar to the left and bring up the New Landscape dialog box.

  2. Click Import from file and select the file you created for Heightmap File. This action sets the Heightmap Resolution to the size of the loaded image, which is 65 by 65.

  3. The Scale defaults to 100 times in all three directions. The Unreal Editor converts meters into centimeters.

  4. Set Overall Resolution to the same value as the Heightmap Resolution for 1:1 scaling, with consideration for Section Size. The options for Section Size are 7x7, 15x15, 31x31, 63x63, 127x127, and 255x255 quads. The Overall Resolution is a product of the Section Size, Sections Per Component, and Number of Components in each direction. In this example, these values are 63-by-63 sections, one section per component and one component in each direction. The overall resolution is 64-by-64.

  5. Click the Import. A Landscape element is added to the World Outliner. Close the Landscape mode toolbar.

  6. Change the location and scale of the landscape as desired, then press F to fit the selection to the window. If you set Location and Scale to [0,0,0] and [100,100,10], respectively, and place a sphere in the far corner of the landscape, the coordinates of the sphere are [6300,6300,-1840] centimeters.

  7. Adjust the LightmassImportanceVolume location and scale to encompass the new terrain.


Apply Materials

In the World Outliner, select Landscape. Then, in the Details pane, find Landscape Material. An empty Landscape Material value results in a checkered gray image. To change the landscape appearance, select a material to use, for example, MathWorksAerospaceContent > Environment > Apple_Hill > Materials > Grass_1. Click the material to select it, then click the left-pointing the Landscape Material arrow to apply the material.


Reparent Blueprint

Click Blueprints at the top of the viewing window and select Open Level Blueprint. In the dialog box, click Class Settings. The first item in the Details pane is Class Options > Parent Class. Set Parent Class to Sim 3d Level Script Actor. This setting enables this level to be used in MATLAB. Save and exit the dialog.


The new landscape map is now ready to use.


External Websites