# Procedural Landscapes - editing the heightmap in C++?

**URL:** <https://forums.unrealengine.com/t/procedural-landscapes-editing-the-heightmap-in-c/2880>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [April 19, 2014, 3:14pm UTC](https://forums.unrealengine.com/t/procedural-landscapes-editing-the-heightmap-in-c/2880 "2014-04-19T15:14:56Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![anonymous\_user\_061af0cc1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_061af0cc1](https://forums.unrealengine.com/u/anonymous_user_061af0cc1)\
**Post date:** [August 18, 2022, 2:13am UTC](https://forums.unrealengine.com/t/procedural-landscapes-editing-the-heightmap-in-c/2880/12 "2022-08-18T02:13:30Z")

</div>

I recently managed to write custom heightmap data to the Landscape using the helper class [FHeightmapAccessor](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Landscape/FHeightmapAccessor/). I haven’t tried it in runtime yet though, but I think it might be possible.

1. As a starting point, have a look at the following function, found in _/Engine/Source/Editor/LandscapeEditor/Private/LandscapeEdMode.cpp_

```auto
bool LandscapeEditorUtils::SetHeightmapData(ALandscapeProxy* Landscape, const TArray<uint16>& Data)
{
	FIntRect ComponentsRect = Landscape->GetBoundingRect() + Landscape->LandscapeSectionOffset;

	if (Data.Num() == (1 + ComponentsRect.Width())*(1 + ComponentsRect.Height()))
	{
		FHeightmapAccessor<false> HeightmapAccessor(Landscape->GetLandscapeInfo());
		HeightmapAccessor.SetData(ComponentsRect.Min.X, ComponentsRect.Min.Y, ComponentsRect.Max.X, ComponentsRect.Max.Y, Data.GetData());
		return true;
	}

	return false;
}

```

…and just replicate that code. You don’t want to use that function directly since it is in an Editor module. The FHeightmapAccessor will update the internal textures, includig mips, and recalculate the normals for you.

_ComponentsRect_ corresponds to the number of quads, meaning that for example Width() + 1 is the number of vertices along the X-axis.

If you want to manually set normals etc, just look inside the implementation of SetData() and replicate what it does but pass also normal data etc to `LandscapeEdit->SetHeightData(...)`.

1. Though, you probably first need to convert your floating point heights to uint16 packed for the heightmaps. You can do it like this, per height in your array:

```auto
#include "LandscapeDataAccess.h"

...

float HeightCm = /* height in centimeter */
float HeightLocal = HeightCm / Landscape->GetTransform().GetScale3D().Z;
uint16 HeightTex = LandscapeDataAccess::GetTexHeight(HeightLocal);

```

---

_[View the full topic](https://forums.unrealengine.com/t/procedural-landscapes-editing-the-heightmap-in-c/2880)._
