UTexture2D C++ sample at x,y coordinates returning incorrect RGBA values

ColorToolsBPLibrary.cpp (6.0 KB)

ColorToolsBPLibrary.h (2.4 KB)

Internally I gather the linear colors from the data and then manually convert them to full color (perhaps the kismet lib conversion would keep the rounding consistent)

Edit:
Updated version with “native” kismet color conversion method toggle.
Seems strange that the built in function seems to change the calculation and throw off the end result.

ColorToolsBPLibrary.cpp (6.3 KB)
ColorToolsBPLibrary.h (2.4 KB)

Would you be able to shed some light on a slightly separate, but related issue?
I’m using the same method of accessing textures to write into them so I can edit pixel values.

Here’s the function:
void UReadWriteTexture2D::SetPixelColor(int32 InX, int32 InY, FLinearColor InColor)
`{
// Clamp Input
const int32 X = FMath::Max(0, FMath::Min(InX, Texture->GetSizeX()-1)); // [0, Width]
const int32 Y = FMath::Max(0, FMath::Min(InY, Texture->GetSizeY()-1)); // [0, Height]

// Get Color Array
FColor* FormattedImageData = static_cast<FColor*>(Texture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_ONLY));

FColor ConvertedColor = FColor(
	static_cast<uint8>(FMath::Clamp(InColor.R * 255.0f, 0.0f, 255.0f)),
	static_cast<uint8>(FMath::Clamp(InColor.G * 255.0f, 0.0f, 255.0f)),
	static_cast<uint8>(FMath::Clamp(InColor.B * 255.0f, 0.0f, 255.0f)),
	static_cast<uint8>(FMath::Clamp(InColor.A * 255.0f, 0.0f, 255.0f))
);
FormattedImageData[(Y * Texture->GetSizeX()) + X] = ConvertedColor;

// Unlock Texture, we are no longer using it. 
Texture->GetPlatformData()->Mips[0].BulkData.Unlock();
Texture->UpdateResource();

}`

After running this function, the change is visible in the editor (Both by opening the texture and viewing it, and by sampling it with the other method), but upon exporting the texture it doesn’t propagate to the new copy. The changes also disappear after restarting the editor.

My guess is that somehow my approach is only changing the copy of the texture in memory and not the copy on the disk. I thought Texture->UpdateResource() might fix that, but it did not.

Hopefully you can shed some more light.

Thanks.

Edit:

Adding a line to save the source worked

// Insert color with the format: B, G, R, A
        // Pixel index is the distance along the 1D representation of the texture crossed with 4  because every pixel consumes 4 byte slots
	const int pixelIndex = (Y * Texture->GetSizeX()) + X;
	UnformattedImageData[(4 * pixelIndex) + 0] = ConvertedColor.B;
	UnformattedImageData[(4 * pixelIndex) + 1] = ConvertedColor.G;
	UnformattedImageData[(4 * pixelIndex) + 2] = ConvertedColor.R;
	UnformattedImageData[(4 * pixelIndex) + 3] = ConvertedColor.A;

	// Unlock Texture, we are no longer using it. 
	Texture->GetPlatformData()->Mips[0].BulkData.Unlock();
	Texture->Source.Init(Texture->GetSizeX(), Texture->GetSizeY(), 1, 1, TSF_BGRA8, UnformattedImageData); // This Line
	Texture->UpdateResource();

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.