Hi,
Currently, I’m using the unrealcv plugin in my project as a Machine Learning dataset generator. Each Actor in my scene is attached by a UAnnotationComponent derived from UPrimitiveComponent with MID for annotation rendering.
The scene should be like the left image, and UAnnotationComponent view should be the right one (Captured with ShowOnlyList Config).
It’s good when the number of actors in the scene is small (e.g., less than 150), But when there are more actors, the reflection material reflects UAnnotationComponents, not lit.
Could anyone give me some hints, please?
Thank you very much!
Here’s the brief code:
UCLASS(meta = (BlueprintSpawnableComponent))
class UNREALCV_API UAnnotationComponent : public UPrimitiveComponent
{
GENERATED_BODY()
public:
//bla bla bla
void SetAnnotationColor(FColor AnnotationColor);
virtual void OnRegister() override {
Super::OnRegister();
// Note: This can not be placed in the constructor, MID means material instance dynamic
AnnotationMID = UMaterialInstanceDynamic::Create(AnnotationMaterial, this, TEXT("AnnotationMaterialMID"));
//bla bla bla
const float OneOver255 = 1.0f / 255.0f;
FLinearColor LinearAnnotationColor = FLinearColor(
this->AnnotationColor.R * OneOver255,
this->AnnotationColor.G * OneOver255,
this->AnnotationColor.B * OneOver255,
1.0);
AnnotationMID->SetVectorParameterValue("AnnotationColor", LinearAnnotationColor);
};
private:
//blablabla
UPROPERTY()
UMaterial* AnnotationMaterial;
UPROPERTY()
UMaterialInstanceDynamic* AnnotationMID;
};
// in function of setting Actor annotation
{
// blablabla
TArray<UActorComponent*> MeshComponents = Actor->GetComponentsByClass(UMeshComponent::StaticClass());
for (UActorComponent* Component : MeshComponents)
{
UMeshComponent* MeshComponent = Cast<UMeshComponent>(Component);
UAnnotationComponent* AnnotationComponent = NewObject<UAnnotationComponent>(MeshComponent);
AnnotationComponent->SetupAttachment(MeshComponent);
AnnotationComponent->RegisterComponent();
// Set annotation color after the component is registered
AnnotationComponent->SetAnnotationColor(AnnotationColor);
AnnotationComponent->MarkRenderStateDirty();
}
}