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:
Respond to Landscape Alterations
The initial topology of the landscape is determined by a static height map, TerrainHeightMap.png.
At run time, as you alter the landscape with the provided controls, the sample evaluates the latest topology to determine whether a new habitat should be applied to a new land 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.
TerrainHabitat is nested within another argument buffer, TerrainParams, that provides a number of slight visual variations for added realism.
TerrainHabitat is the specific argument buffer definition for a terrain habitat. However, because this definition is nested within TerrainParams, the TerrainParams objects are the ones passed along the GPU pipeline.
The sample provides the GPU with the textures corresponding to various habitats. First, the sample calls the useResource:usage: method to indicate which textures are used by the GPU.
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.
The sample passes the terrainParamsBuffer argument buffer to the vegetation render pass via 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 outputs 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:
The sample passes the terrainParamsBuffer argument buffer to the particle render pass via 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 particles 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 particle’s corresponding habitat material is retrieved from the argument buffer and set onto the new particle.