Okay I have a Blueprint which contains a Billboard component. Now I’m trying to draw a text onto the the Billboard material:
TextMesh = PCIP.CreateDefaultSubobject<UBillboardComponent>(this, TEXT("Text Mesh"));
TextMesh->AttachParent = TouchSphere;
TextMesh->RelativeLocation = FVector(0.f, 0.f, TouchSphere->GetScaledSphereRadius()*2);
if (!TextMesh) return;
UMaterialInterface* BillboardMaterial = TextMesh->GetMaterial(0);
if (!BillboardMaterial) return;
// Create a Render Texture and initialise it.
UTextureRenderTarget2D* RenderTexture = (UTextureRenderTarget2D*)StaticConstructObject(UTextureRenderTarget2D::StaticClass());
RenderTexture->ClearColor = FLinearColor::Black;
RenderTexture->InitAutoFormat(400,200);
RenderTexture->UpdateResourceImmediate();
// Create a Canvas and assign the render texture resource to it
FTextureRenderTarget2DResource* TextureResource = (FTextureRenderTarget2DResource*)RenderTexture->Resource;
FCanvas* Canvas = new FCanvas(TextureResource, NULL, 0, 0, 0);
Canvas->Clear(FLinearColor::Black);
//Your drawing code goes here
Canvas->DrawShadowedText(0, 0, DisplayName, HintFont, FLinearColor(1,0,0,1), FLinearColor(0,0,0,0));
//Assign render texture to material
UMaterialInstanceDynamic* MText = UMaterialInstanceDynamic::Create(BillboardMaterial, this);
MText->SetTextureParameterValue(TEXT("Target"), RenderTexture);
TextMesh->SetMaterial(0, MText);
I got the render-to-texture code from google, but it does not work. Also, it seems to me that this code draws the Billboard material onto the canvas, but I want to draw the canvas onto the billboard material.
Edit: I just saw that the TextMesh->GetMaterial(0); always returns NULL. When will the material for this be loaded? How can I run my code after that?
Edit2: Okay I changed the code to the following:
TextMesh = PCIP.CreateDefaultSubobject<UBillboardComponent>(this, TEXT("Text Mesh"));
TextMesh->AttachParent = TouchSphere;
TextMesh->RelativeLocation = FVector(0.f, 0.f, TouchSphere->GetScaledSphereRadius()*2);
if (!TextMesh) return;
// Create a Render Texture and initialise it.
UTextureRenderTarget2D* RenderTexture = (UTextureRenderTarget2D*)StaticConstructObject(UTextureRenderTarget2D::StaticClass());
RenderTexture->ClearColor = FLinearColor::Black;
RenderTexture->InitAutoFormat(512,512);
RenderTexture->UpdateResourceImmediate();
// Create a Canvas and assign the render texture resource to it
FTextureRenderTarget2DResource* TextureResource = (FTextureRenderTarget2DResource*)RenderTexture->Resource;
FCanvas* Canvas = new FCanvas(TextureResource, NULL, 0, 0, 0);
Canvas->Clear(FLinearColor::Black);
//Your drawing code goes here
Canvas->DrawShadowedText(0, 0, DisplayName, HintFont, FLinearColor(1,0,0,1), FLinearColor(0,0,0,0));
Canvas->Flush();
//Assign render texture to material
UTexture2D* tex = RenderTexture->ConstructTexture2D(this, TEXT("SprTex"), RF_NoFlags);
TextMesh->Sprite = tex;
The tex variable is not null, but the sprite does not change in appearance. ****.