Download

Access raw data in a custom material expression : Special settings needed for Texture2D?

I have a material that is using a Custom Expression where I wrote some HLSL code.
I require loops and so I needed to use a custom expression.

My data is dynamic and needs to be updated every ticks meaning that I put my data in a Texture2D and trying to use it as a simple data buffer.

How I get the data :
float amplitude = WavesData.mips[0][pos][jTex];

How I create the Texture2D :

UTexture2D* UWavesBPFunctions::AddWavesToTextureObjects(const TArray& waves)
{
// ----- Don’t copy the previous state of a wave stored in the wave ----------
const SIZE_T copySize = sizeof(FWaveStruct) - sizeof(FWaveStruct::LastWave);
UTexture2D* waveTex = UTexture2D::CreateTransient(FMath::RoundToInt(copySizewaves.Num()/16.0f), 1, PF_A32B32G32R32F, FName(“WaveDataTex”));
FTexture2DMipMap& mipPart1 = waveTex->PlatformData->Mips[0];
uint8
dataPart1 = static_cast<uint8*>(mipPart1.BulkData.Lock(LOCK_READ_WRITE));
for (TArray::TConstIterator wavesIter(waves); wavesIter; ++wavesIter)
{
const uint8* srcPtr = static_cast<const uint8*>(static_cast<const void*>(&(*(wavesIter))));
FMemory::Memcpy(dataPart1, srcPtr+sizeof(FWaveStruct::LastWave), copySize);

  dataPart1+=copySize;

}
mipPart1.BulkData.Unlock();
waveTex->UpdateResource();

return waveTex;
}

My struct :

USTRUCT(BlueprintType)
struct FWaveStruct
{
GENERATED_BODY()

struct LastWave
{
FVector2D direction;
float amplitude;
float length;
float speed;
float steepness;
} old;

UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
FVector2D direction;
UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
float amplitude;
UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
float length;
UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
float speed;
UPROPERTY(NotReplicated, BlueprintReadWrite, EditAnywhere)
float steepness;
};

When I check the buffer of the texture, everything is setup fine exactly how I want.
Now my question is the following : Are my data modified in any way when being passed to the shader ?
Is there any option I can set to the Texture2D so that my data are not compressed or modified one way or another ?

Thanks for the help :slight_smile: