Contents

Rendering terrain dynamically with argument buffers

Use argument buffers to render terrain in real time with a GPU-driven pipeline.

Overview

This sample demonstrates dynamic terrain generation on an outdoor landscape, using argument buffers to select terrain materials, vegetation geometry, and particle effects within a GPU-driven pipeline. The sample creates a landscape with visually distinct areas, called habitats, that differ based on the land’s elevation. These are the habitats in the sample, ordered from highest to lowest elevation:

  • Snow

  • Rock

  • Grass

  • Sand

[Image]

Getting started

The Xcode project contains schemes for running the sample in macOS and iOS. Metal isn’t supported in the iOS Simulator, so the iOS scheme requires a physical device that supports GPU family 4 to run the sample. The default scheme is macOS, which runs the sample as is on your Mac.

In macOS, use these controls to navigate the scene:

  • W, S, A, and D keys. Move the camera body.

  • Arrow keys. Move the camera view.

  • Mouse or trackpad drag. Move the camera view.

  • Mouse or trackpad primary click. Raise the terrain.

  • Mouse or trackpad secondary click. Lower the terrain.

In iOS, use these controls to navigate the scene:

  • Pan gesture. Move the camera view.

  • modify terrain button. Cycle through a predefined terrain manipulation sequence.

Respond to landscape alterations

The app determines the landscape’s initial topology from a static height map, TerrainHeightMap.png.

At runtime, as you alter the landscape with the provided controls, the sample evaluates the latest topology to determine whether it should apply a new habitat to the land based on its new elevation. If so, the sample updates the argument buffer corresponding to the land with the correct materials and vegetation geometry for the new habitat. The sample renders this new habitat by passing the land elevation value to the EvaluateTerrainAtLocation function.

Define an argument buffer for terrain habitats

The sample defines a custom argument buffer structure, TerrainHabitat, that defines the elements of a terrain habitat.

Among these elements, elevationStrength and elevationThreshold determine the elevation range in which the habitat is active. Additionally, diffSpecTextureArray and normalTextureArray determine the textures used to render the habitat.

The app nests TerrainHabitat within another argument buffer, TerrainParams, that provides many slight visual variations for added realism.

TerrainHabitat is the specific argument buffer definition for a terrain habitat. However, because the app nests its definition within TerrainParams, the app sends the TerrainParams objects to the GPU pipeline.

Render terrain

The sample provides the GPU with the textures corresponding to various habitats. First, the sample calls the useResource:usage: method to indicate which textures the GPU uses.

Then, the sample calls the setFragmentBuffer:offset:atIndex: method to set the argument buffer, terrainParamsBuffer, that contains those textures.

The sample accesses the argument buffer in the fragment function, terrain_fragment, to output the correct material for the terrain. First, the sample passes the mat parameter into the fragment function.

Then, the sample passes the current land elevation into the EvaluateTerrainAtLocation function, where the fragment samples the texture corresponding to that elevation.

Render vegetation

The sample passes the terrainParamsBuffer argument buffer to the vegetation render pass through an instance of AAPLTerrainRenderer. This data determines which type of vegetation to render at a given location. First, the sample calls the setBuffer:offset:atIndex: method to set the argument buffer for the vegetation render pass.

Then, the sample passes the argument buffer into the EvaluateTerrainAtLocation function, which produces a habitatPercentages value.

The habitat percentages are processed to select a specific index into the vegetation geometries, determined by the value of pop_idx.

Finally, the sample uses this population index to render an instance of a particular vegetation geometry onto the landscape.

Render particles

The sample passes the terrainParamsBuffer argument buffer to the particle render pass through an instance of AAPLTerrainRenderer. This data determines which type of particles to render at a given location. First, the sample calls the setBuffer:offset:atIndex: method to set the argument buffer for the particle render pass.

Then, the sample checks the relative percentages of habitat coverage in the altered landscape with the EvaluateTerrainAtLocation function, where the sample passes the 3D position of the particle.

The sample chooses the appropriate habitat by selecting the terrain with the highest percentage of habitat coverage.

Finally, the app retrieves the particle’s corresponding habitat material from the argument buffer and sets it to the new particle.

See Also

Argument buffers