Creating textures at runtime in C++ makes them fuzzy between colors.

I need to create textures at runtime. So im using this code to create a checkerboard of squares type of texture with blue and red.
It works okay but If you look up close the transition between the squares as a fuzzy line of pinkish color. Is it not possible to remove this, and just make solid pixels?

const int32 Width = 32;
	const int32 Height = 32;
	const int32 SquareSize = 16;

	UTexture2D* Texture = UTexture2D::CreateTransient(Width, Height, PF_B8G8R8A8);
	Texture->MipGenSettings = TMGS_NoMipmaps;
	FColor* Pixels = (FColor*)Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);

	for (int32 y = 0; y < Height; y++) {
		for (int32 x = 0; x < Width; x++) {
			if (((x / SquareSize) + (y / SquareSize)) % 2 == 0) {
				// Set to blue
				Pixels[x + y * Width] = FColor(0, 0, 255, 255);
			}
			else {
				// Set to red
				Pixels[x + y * Width] = FColor(255, 0, 0, 255);
			}
		}
	}

	Texture->PlatformData->Mips[0].BulkData.Unlock();

	Texture->UpdateResource();
	UMaterialInterface* Material = Cast<UMaterialInterface>(StaticLoadObject(UMaterialInterface::StaticClass(), nullptr, *MaterialPath));

	UMaterialInstanceDynamic* DynamicMaterial = UMaterialInstanceDynamic::Create(Material, this);
	DynamicMaterial->SetTextureParameterValue(FName(TEXT("Texture")), Texture);
	// Set the dynamic material instance on the mesh component
	CubeMesh->SetMaterial(0, DynamicMaterial);

I need the pixels to be 100% solid because i will use this texture as a mask to hide parts of my mesh, and or move parts of my mesh using the World Position Offset.

Pls help.

2 Likes

Hi SophiaWolfie,

Add the line:

Texture->Filter=TF_Nearest;

That will stop any of the pixel interpolation.

3 Likes

Thank you RecourseDesign that worked like a charm.

2 Likes