To preface, I’m attempting to create a map of selectable provinces similar to how paradox interactive games tend to work (eu4, hoi4, etc). To accomplish this, I’m planning on creating a map of provinces with each province being a unique RGB value so that I can retrieve an RGB value when clicking on a material (the map) in order to select the corresponding province to that RGB value. This seems rather simple in blueprints in examples I’ve found, however I’m attempting to transfer the code into C++ and am running into an issue with actually getting the RGB value from the DrawMaterialToRenderTarget as the resulting RGB value is consistently 0,0,0.
I believe the root of the issue is occurring when calling DrawMaterialToRenderTarget. It appears that TextureRenderTarget2DPtr is likely not being updated within the function. However, I’m unsure if this is the case or if the issue is due to the material, how I retrieved the TextureRenderTarget2DPtr, or if it’s something else entirely.
I have verified that the MaterialInterface is being at least retrieved correctly and that the CollisionUV coordinates are correct.
UKismetRenderingLibrary::DrawMaterialToRenderTarget(
GetWorld(),
TextureRenderTarget2DPtr,
MaterialInterface);
As after this, when attempting to ReadRenderTargetRawUV, the LinearColor returned contains the values 0,0,0,0:
FLinearColor LinearColor = UKismetRenderingLibrary::ReadRenderTargetRawUV(
GetWorld(),
TextureRenderTarget2DPtr,
CollisionUV.X,
CollisionUV.Y
);
Here’s a pic of the material blueprint:
Entire Function
void ARTSPlayerController::SelectionPressed() {
HUDPtr->SetInitialPoint(HUDPtr->GetMousePos2D());
HUDPtr->bStartSelecting = true;
FVector WorldLocation;
FVector WorldDirection;
FHitResult HitResult(ForceInit);
this->DeprojectMousePositionToWorld(WorldLocation, WorldDirection);
FCollisionQueryParams TraceParams(TEXT("LineOfSight_Trace"), true, this);
TraceParams.bReturnFaceIndex = true;
GetWorld()->LineTraceSingleByChannel(HitResult, WorldLocation, WorldLocation + (WorldDirection * 10000), ECC_Visibility, TraceParams);
DrawDebugLine(GetWorld(), WorldLocation,WorldLocation + (WorldDirection * 10000), FColor::Red, false, 5, 0, 2.0);
DrawDebugBox(GetWorld(), HitResult.ImpactPoint, FVector(20, 20, 20), FColor::Blue, false, 5, ECC_WorldStatic, 5.0);
FVector2D CollisionUV;
if (UGameplayStatics::FindCollisionUV(HitResult, 0, CollisionUV)) {
UTextureRenderTarget2D* TextureRenderTarget2DPtr = UKismetRenderingLibrary::CreateRenderTarget2D(
GetWorld());
UMaterialInterface* MaterialInterface = Cast<UStaticMeshComponent>(HitResult.Component.Get())->GetMaterial(0);
UKismetRenderingLibrary::DrawMaterialToRenderTarget(
GetWorld(),
TextureRenderTarget2DPtr,
MaterialInterface);
FLinearColor LinearColor = UKismetRenderingLibrary::ReadRenderTargetRawUV(
GetWorld(),
TextureRenderTarget2DPtr,
CollisionUV.X,
CollisionUV.Y
);
GEngine->AddOnScreenDebugMessage(-1,5,FColor::Green,FString::Printf(TEXT("LinearColor: %f, %f, %f, %f"), LinearColor.R, LinearColor.G, LinearColor.B, LinearColor.A));
}
}
Example of blueprints I’m trying to codify into C++:
If anyone has any suggestions or thoughts on how else to implement this feature (provincial selection of provinces on a map) or has any clue as to what could be the root of the issue I’m facing here I’d really appreciate it.