Strategy game world map like Risk?

Here is an implementation to get the specific pixel color at the texture x,y coordinates.

FColor UColorToolsBPLibrary::ProcessTexture(UTexture2D* InTexture, int32 PixelX, int32 PixelY)
{	
	FTexture2DMipMap* MyMipMap = &InTexture->PlatformData->Mips[0];
	FByteBulkData* RawImageData = &MyMipMap->BulkData;
	FColor* FormatedImageData = static_cast<FColor*>(InTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY));
	
	uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
	FColor PixelColor;

	if (PixelX >= 0 && (uint32)PixelX < TextureWidth && PixelY >= 0 && (uint32)PixelY < TextureHeight)
	{
		PixelColor = FormatedImageData[PixelY * TextureWidth + PixelX];
	}		
	RawImageData->Unlock();

	return PixelColor;
}

For an RT in theory this conversion should work. Haven’t tested it out in a project though, just mixing some code for RT to utexture conversion.

FColor UColorToolsBPLibrary::ProcessTextureRT(UObject* WorldContextObject, UTextureRenderTarget2D* RTarget, int32 PixelX, int32 PixelY)
{
	UTexture2D* InTexture = RTarget->ConstructTexture2D(WorldContextObject, "texture", EObjectFlags::RF_NoFlags, CTF_DeferCompression);
	FTexture2DMipMap* MyMipMap = &InTexture->PlatformData->Mips[0];
	FByteBulkData* RawImageData = &MyMipMap->BulkData;
	FColor* FormatedImageData = static_cast<FColor*>(InTexture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_ONLY));

	uint32 TextureWidth = MyMipMap->SizeX, TextureHeight = MyMipMap->SizeY;
	FColor PixelColor;

	if (PixelX >= 0 && (uint32)PixelX < TextureWidth && PixelY >= 0 && (uint32)PixelY < TextureHeight)
	{
		PixelColor = FormatedImageData[PixelY * TextureWidth + PixelX];
	}

	RawImageData->Unlock();
	return PixelColor;
}

It would probably be wise to cache the render target’s utexture conversion and use it as a lookup instead of constantly writing it per each check. (maybe adding a bool function if you want to update it or something)

Here is an old post where I was getting the values from an SRGB version of a texture for the OP.
If you want to use this on a map then you would need to do a line trace to the map and turn on get uv coordinates for hit detection and then use that information to get the clicked areas uv coordinates and feed it into the function to get the needed value.

1 Like