I have this material created in editor, and intended to load and draw it to a render target in c++ code. However when running the UKismetRenderingLibrary::DrawMaterialToRenderTarget() function in component’s tick function, it is highly like that FMaterialInstanceResource* Resources[3] array within the material instance contains null pointers or 0x0000FFFF alike pointer values, which, I doubt, coule be garbage collected somewhere or sometime in the process.
Here’s code exempts.
// component constructor
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.TickGroup = TG_PostPhysics;
static ConstructorHelpers::FObjectFinder<UMaterial> MaterialFinder(TEXT("Material'/MyProject/Materials/RelatedMat.RelatedMat'"));
if (MaterialFinder.Succeeded())
{
RelatedMat = MaterialFinder.Object;
}
RelatedMatIns = UMaterialInstanceDynamic::Create(RelatedMat, this, TEXT("RelatedMatIns"));
// component initialization
ResultRenderTarget = UKismetRenderingLibrary::CreateRenderTarget2D(this, Resolution.X, Resolution.Y, ETextureRenderTargetFormat::RTF_RGBA8);
ResultRenderTarget->TargetGamma = 2.2f;
RelatedMatIns->SetScalarParameterValue("TexSize", Resolution.X);
RelatedMatIns->SetTextureParameterValue("Tex0", RenderTargets[0]);
RelatedMatIns->SetTextureParameterValue("Tex1", RenderTargets[1]);
RelatedMatIns->SetTextureParameterValue("Tex2", RenderTargets[2]);
RelatedMatIns->SetTextureParameterValue("Tex3", RenderTargets[3]);
RelatedMatIns->SetTextureParameterValue("Tex4", RenderTargets[4]);
// component tick function
UKismetRenderingLibrary::DrawMaterialToRenderTarget(GetWorld(), ResultRenderTarget, RelatedMatIns); // this is where things go wrong
FTextureRenderTargetResource* RTResource = FisheyeRenderTarget->GameThread_GetRenderTargetResource();
FReadSurfaceDataFlags ReadPixelFlags(RCM_MinMax);
TArray<FColor> pixelBuffer;
if (FisheyeRTResource->ReadPixels(pixelBuffer, ReadPixelFlags))
.................
So my question is why material instance’s rendering resources go missing, and how do I prevent this?
Update:
In UCanvas::K2_DrawMaterial(), a FCanvasTitleItem will be created, and during its creation, a check will be executed on InMaterialRenderProxy. When RelatedMatIns contains a illegible resources and send in such value, the program crashes.