Rendering heightmap directly on UTexture 2D from C++.

Hi there,

Question about UTexture2D. I want to render ripples from a table on the UTexture2D. As a test I’ve managed to put these ripples on a RuntimeMeshComponent and they work.
When I tried to use UTexture2D to render these ripples from an array the texture did sometimes update but then it changed to full white again.

I used this https://wiki.unrealengine.com/Dynamic_Textures

So, how to properly set up UTexture2D to update it every frame from a data array?

I have used the code from the wiki and it works, you should probably figure out why it only ‘updated sometimes’.

Here is another snippet of code that I’m using (this one uses a UTextureRenderTarget2D though).


void CopyArrayToRenderTarget(UTextureRenderTarget2D* Target, TArray<FFloat16Color>& Colors)
{
	ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER(
		FWriteArrayToRT,
		TArray<FFloat16Color>, data, Colors,
		FTextureRenderTargetResource*, source, Target->GameThread_GetRenderTargetResource(),
		{
			uint32 SizeX = source->GetRenderTargetTexture()->GetSizeX();
			uint32 SizeY = source->GetRenderTargetTexture()->GetSizeY();

			uint32 Stride;
			uint8* DestTextureData = (uint8*)RHILockTexture2D(source->GetRenderTargetTexture(), 0, RLM_WriteOnly, Stride, false);
			const uint8* SourceTextureData = (uint8*)data.GetData();
			const uint32 DataStride = SizeX * sizeof(FFloat16Color);

			if (Stride == DataStride)
			{
				FMemory::Memcpy(DestTextureData, SourceTextureData, DataStride * SizeY);
			}
			else
			{
				//log
			}

			RHIUnlockTexture2D(source->GetRenderTargetTexture(), 0, false);
		});
}

Make sure the FFloat16Color array you feed into it is the same size as the render target or it might not work.