How to reade and write pixel value of UTexture2D?

Hi everybody, I capture a scene with OpenCV and I want to write pixel value of captured image to UTexture2D but I can’t I already setup a material which has a texture parameter and I also created an instance Material of that then I create a C++ class but when I trying to change pixel value it does not work properly it’s change just top part of mesh, and I already read Ginku’s wiki but I don’t understant that it’s to messy, here is my function code.

void ADynamicTexture::capture()
{
	cv::Mat img = cv::imread("D:\\img.jpg");

	UTexture2D *ut2 = UTexture2D::CreateTransient(img.size().width, img.size().height);
	ut2->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
	ut2->SRGB = 0;
	ut2->AddToRoot();
	ut2->UpdateResource();
	FTexture2DMipMap &mip = ut2->PlatformData->Mips[0];
	void *data = mip.BulkData.Lock(LOCK_READ_WRITE);
	FMemory::Memcpy(data, img.data, img.size().width * img.size().height);
	mip.BulkData.Unlock();
	ut2->UpdateResource();
	matInst->SetTextureParameterValue(FName("DTexture"), ut2);
	mesh->SetMaterial(0, matInst);
}

Your not copying enough information you need to provide a stride to your mem copy.

auto stride = (int32)( sizeof(uint8) * 4 ); // for r, g, b, a
FMemory::Memcpy(data, img.data, img.size().width * img.size().height * stride);
1 Like

Thanks! Solved my problem!