void ABuilding::BakeSprite(
UObject* WorldContextObject,
UPaperSprite* BottomSprite,
UPaperSprite* ContainSprite,
UPaperSprite* TopSprite,
TArray<FVector2D> ContainPostion)
{
if (!TextureRenderTarget)
{
UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
if (SizeX > 0 && SizeY > 0 && World)
{
TextureRenderTarget = NewObject<UTextureRenderTarget2D>(WorldContextObject);
check(TextureRenderTarget);
TextureRenderTarget->MipGenSettings = TMGS_NoMipmaps;
TextureRenderTarget->LODGroup = TEXTUREGROUP_Pixels2D;
TextureRenderTarget->RenderTargetFormat = RTF_RGBA16f;
TextureRenderTarget->ClearColor = FLinearColor::Transparent;
TextureRenderTarget->bAutoGenerateMips = false;
TextureRenderTarget->bCanCreateUAV = false;
TextureRenderTarget->InitAutoFormat(SizeX * 16, SizeX * 16);
TextureRenderTarget->UpdateResourceImmediate(true);
}
else
{
return;
}
}
else
UKismetRenderingLibrary::ClearRenderTarget2D(WorldContextObject, TextureRenderTarget, FLinearColor::Transparent);
UCanvas* Canvas;
FVector2D Size;
FDrawToRenderTargetContext Context;
UKismetRenderingLibrary::BeginDrawCanvasToRenderTarget(WorldContextObject, TextureRenderTarget, Canvas, Size, Context);
if (BottomSprite)
{
DrawTextureFromSprite(Canvas, FVector2D::ZeroVector, BottomSprite);
}
if (ContainSprite)
{
for (auto a : ContainPostion)
{
FVector2D ScreenPosition;
ScreenPosition.X = a.X - ContainSprite->GetSourceSize().X / 2;
ScreenPosition.Y = a.Y - ContainSprite->GetSourceSize().Y / 2;
DrawTextureFromSprite(Canvas, ScreenPosition, ContainSprite);
}
}
if (TopSprite)
{
DrawTextureFromSprite(Canvas, FVector2D::ZeroVector, TopSprite);
}
UKismetRenderingLibrary::EndDrawCanvasToRenderTarget(WorldContextObject, Context);
if (!SourceTexture)
{
SourceTexture = UTexture2D::CreateTransient(SizeX * 16, SizeX * 16);
SourceTexture->CompressionSettings = TextureCompressionSettings::TC_EditorIcon;
SourceTexture->LODGroup = TextureGroup::TEXTUREGROUP_Pixels2D;
SourceTexture->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
}
UKismetRenderingLibrary::ConvertRenderTargetToTexture2DEditorOnly(WorldContextObject, TextureRenderTarget, SourceTexture);
if (!BakedSprite)
{
BakedSprite = NewObject<UPaperSprite>();
PaperSprite->SetSprite(BakedSprite);
FSpriteAssetInitParameters SpriteInitParams;
SpriteInitParams.SetTextureAndFill(SourceTexture);
const UPaperImporterSettings* ImporterSettings = GetDefault<UPaperImporterSettings>();
ImporterSettings->ApplySettingsForSpriteInit(SpriteInitParams, ESpriteInitMaterialLightingMode::Automatic);
BakedSprite->InitializeSprite(SpriteInitParams);
}
BakedSprite->PostEditChange();
BakedSprite->RebuildData();
return;
}
The above is the new code i have add to my program, i think the reason why the breakpoint occured is because of it.
void ABuilding::DrawTextureFromSprite(UCanvas* Canvas, FVector2D ScreenPosition, UPaperSprite* InSprite)
{
UTexture2D* RenderTexture;
RenderTexture = InSprite->GetSourceTexture();
FVector2D InScreenPosition = ScreenPosition;
FVector2D InScreenSize;
InScreenSize.X = InSprite->GetSourceSize().X;
InScreenSize.Y = InSprite->GetSourceSize().Y;
FVector2D InCoordinatePosition;
InCoordinatePosition.X = InSprite->GetSourceUV().X / RenderTexture->GetPlatformData()->SizeX;
InCoordinatePosition.Y = InSprite->GetSourceUV().Y / RenderTexture->GetPlatformData()->SizeY;
FVector2D InCoordinateSize;
InCoordinateSize.X = InSprite->GetSourceSize().X / RenderTexture->GetPlatformData()->SizeX;
InCoordinateSize.Y = InSprite->GetSourceSize().Y / RenderTexture->GetPlatformData()->SizeY;
Canvas->K2_DrawTexture(RenderTexture, InScreenPosition, InScreenSize, InCoordinatePosition, InCoordinateSize, FLinearColor::White, EBlendMode::BLEND_AlphaComposite);
}
The breakpoint occured when I start a saved game and the building need to bake sprite.
I sincerely hope that someone could help me.Thank you.