Hi all,
Here is what I’m trying to do:
- I have a UV distortion map with floating values(32bits) stored in it
- I would like to apply this on the final rendering of the Engine to create real-time non linear lens distortions
- the UV map is changing every frame
I have currently created a PostProcess Material in the UE4 Editor which has the following connectivity:
Param_DistortionTexture(TextureSample converted using right click to Parameter2D)->Mask(R,G)->SceneTexture:SceneColor(UV in, Color out)->MyMaterial(Emissive in).
The TextureSampleParameter2D contains a HDR Texture (associated with a Linear Color Sampler). I haven’t found any other format than can take floating point values, please advise if there is a better format to use. When I drag/drop a distortion texture (dds format, 32bits per pixel) to the Texture Sample Parameter of UE4 Editor, it seems to apply the distortion in the viewer even if I actually notice some serious aliasing.
So my idea was to modify dynamically that parameter Param_DistortionTexture by updating it in dynamically in the C++ source code with other UV float textures.
Here is how I did that.
In the C++ source code, I have MyPlayerController.h which has in the class declaration:
UPROPERTY(EditDefaultsOnly, Category = Materials)
UMaterialInterface *mMaterialInterface;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Textures)
UTexture2D *mTexture;
in the constructor of MyPlayerController.cpp
static ConstructorHelpers::FObjectFinder<UMaterial> lPostProcessMaterial(TEXT("Material'/Game/Materials/DistortionPostProcess.DistortionPostProcess'"));
mMaterialInterface = lPostProcessMaterial.Object;
In the CalcCamera() method of MyPlayerController.cpp (I also modify the position/orientation of the camera here so I thought it was the best place for updating the distortion texture -let me know if not):
// TheWidth=TheHeight=32 here
// I have tried many different formats here including PF_FLOATRGBA, ... no change
mTexture = UTexture2D::CreateTransient(theWidth, theHeight, PF_A32B32G32R32F);
if(mTexture)
{
FTexture2DMipMap& lMip = mTexture->PlatformData->Mips[0];
float *lData = static_cast<float*>(lMip.BulkData.Lock(LOCK_READ_WRITE));
if (Data)
{
// lUVmap is declared as float *lUVmap= new float[theWidth * theHeight *4];
// and filled out with floating point vallues between 0.0 and 1.0**
FMemory::Memcpy(lData, lUVmap, theHeight*theWidth * 4 * sizeof(float));
}
lMip.BulkData.Unlock();
mTexture->UpdateResource();
UMaterialInstanceDynamic* lMaterialInstance = UMaterialInstanceDynamic::Create(mMaterialInterface, this);
lMaterialInstance ->SetTextureParameterValue(FName("Param_DistortionTexture"), mTexture);
}
Result: it does… nothing ! The texture does not seem to be updated. Every instruction seemed to be applied and does not return an error but I can see that the result is not changing.
I even tried to fill the UVmap buffer with 0.0 values just to see if it changes the result but it doesn’t.
I wonder if the default HDR texture (dds file) I have put actually overrides any texture change…
So what am I missing ?
Is this the best way to achieve the lens distortion ?
Any idea why the texture update does not work ?
Apart from that, is there somewhere a description of what is exactly expected as a UV set to be linked to the UV entry of SceneTexture:SceneColor ?
Best Regards,
Phoenix.