Dynamically created USceneCaptureComponent2D issue. Black image

Hi there! I need to create avatars for chatacters in scene. I’ve found lot of tutorials to use USceneCaptureComponent2D. But all of them use only one player character. In may case I have lot of them (up to 10 for example). General way for one character is: create USceneCaptureComponent2D, create render target in editor, pass material to Widget. If I use this way all character have same avatar. As I understand it happens because all actors USceneCaptureComponent2D write in same RenderTarget. So I decided to create render target dynamically. My code is;

	CaptureComponentHead = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("CaptureComponentHead"));

void ATacticalCharacterPlayer::PostInitializeComponents()
	CaptureComponentHead->CaptureSource = SCS_FinalColorLDR;
	CaptureComponentHead->bCaptureEveryFrame = false;	
	FString asd = GetName();
	UTextureRenderTarget2D* renderTarget = NewObject<UTextureRenderTarget2D>(this, *asd);
	renderTarget->InitAutoFormat(512, 512);
	CaptureComponentHead->TextureTarget = renderTarget;

Afer that I pass reference to ATacticalCharacterPlayer in Widget and apply image

UTextureRenderTarget2D* t = OwnerAttr->CaptureComponentHead->TextureTarget;
        UTexture2D* texture2D = t->ConstructTexture2D(this, "Capture", EObjectFlags::RF_NoFlags, CTF_DeferCompression);			

But in that case texture2D is only black image. What have I missed? I saved UTextureRenderTarget2D to file and it’s black as well. So I guess issue somewhere in UTextureRenderTarget2D, but I can’t figure out where exactly.
BTW if I move initialization from PostInitializeComponents to constructor all characters have same UTextureRenderTarget2D reference, but I’m sure (checked it in debug) all constructors are called and separate UTextureRenderTarget2D are created and set. But in BeginPlay the UTextureRenderTarget2D is the same. I don’t undersatnd why it happens, but in that case I have correct image instead of black image at least, but again the image is the same for all characters.

Any help would be appreciated )

Thx a lot for your reply. At the end I did it in similar way through blueprints. But I’ve found several interesting places in your blueprint like destroying unnecessary objects) Unfortunatelly through pure c++ this flow doesn’t work and I can’t imagine why… But anyway it’s not a blocker anymore, just curious.

I had to do something similar for my project. This current implementation is in blueprint, with a C++ Scene Capture Component, but it works ok.

This is done on Begin Play, and then whenever needed, my portrait widget can access “Portrait Texture” and I use it to set a Texture parameter value on my portrait material.

There are probably better solutions but this worked for me as a starting point.

1 Like

This worked! I was doing something similar as OP with RenderTarget = NewObject<UTextureRenderTarget2D>()... and it worked in editor, but not in a packaged build. In the packaged build I got the default black ClearColor from my UI Material’s Render Texture param. I have a little different set up than you, but doing it in Blueprint works in editor and packaged build! You’re a gentleman and a scholar.