OK, I need to edit the color of texture and I’ve followed the tutorial from Rama…unfortunately it turns everything white…and I have no idea why. Help! Here is what I have (MyMat is the name of my texture):
void ABatteryCollecterCharacter::GrabMat(AActor* mesh) {
if (mDynamicTexture == NULL) {
int32 w, h;
bool valid;
UTexture2D* textureToReadFrom = Victory_LoadTexture2D_FromFile("C:\\Users\\Toshiba Q\\Desktop\\rock_x0_y0.png", valid, w, h);
AStaticMeshActor* m = Cast<AStaticMeshActor>(mesh);
//Convert the static material in our mesh into a dynamic one, and store it (please note that if you have more than one material that you wish to mark dynamic, do so here).
if (m) {
UMaterialInstanceDynamic* newMat = m->GetStaticMeshComponent()->CreateAndSetMaterialInstanceDynamic(0);
mDynamicMaterials.Add(newMat);
//Create a dynamic texture with the default compression (B8G8R8A8)
mDynamicTexture = UTexture2D::CreateTransient(w, h);
//Make sure it won't be compressed
mDynamicTexture->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
//Turn off Gamma-correction
mDynamicTexture->SRGB = 0;
//Guarantee no garbage collection by adding it as a root reference
mDynamicTexture->AddToRoot();
//Update the texture with new variable values.
mDynamicTexture->UpdateResource();
//Grab the colorvalues from our existing texture (the one we created at '''Texture Setup''') and copy it into a uint8* mTextureColors variable.
FTexture2DMipMap& readMip = textureToReadFrom->PlatformData->Mips[0];
readMip.BulkData.GetCopy((void**)&mTextureColors);
// Initalize our dynamic pixel array with data size
mDynamicColors = new uint8[w * h * 4];
mDataSize = w * h * 4;
mDataSqrtSize = w * 4;
// Copy our current texture's colors into our dynamic colors
FMemory::Memcpy(mDynamicColors, mTextureColors, w * h * 4);
// Create a new texture region with the width and height of our dynamic texture
mUpdateTextureRegion = new FUpdateTextureRegion2D(0, 0, 0, 0, w, h);
// Set the Paramater in our material to our texture
mDynamicMaterials[0]->SetTextureParameterValue("MyMat", mDynamicTexture);
}
}
}
void ABatteryCollecterCharacter::UpdateMat() {
int pixelAmount = mDataSize / 4;
for (int i = 0; i < pixelAmount; ++i)
{
int blue = i * 4 + 0;
int green = i * 4 + 1;
int red = i * 4 + 2;
int alpha = i * 4 + 3;
mDynamicColors[red] =0;
mDynamicColors[blue]=255;
mDynamicColors[green] = 255;
}
if (mDynamicTexture == NULL ){
UE_LOG(LogClass, Log, TEXT("uh-oh, not texture loaded"));
}
else {
UpdateTextureRegions(mDynamicTexture, 0, 1, mUpdateTextureRegion, mDataSqrtSize, (uint32)4, mDynamicColors, false);
mDynamicMaterials[0]->SetTextureParameterValue("MyMat", mDynamicTexture);
}
}
void ABatteryCollecterCharacter::IncreaseRock() {
TextureLevel += 0.1;
UE_LOG(LogClass, Log, TEXT("up"));
if (TextureLevel > 1)
TextureLevel = 1;
UpdateMat();
}