void USlateBrushThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget* RenderTarget, FCanvas* Canvas) { USlateBrushAsset* SlateBrushAsset = Cast<USlateBrushAsset>(Object); if (SlateBrushAsset) { FSlateBrush Brush = SlateBrushAsset->Brush; UTexture2D* Texture = Cast<UTexture2D>( Brush.GetResourceObject() ); // Draw the background checkboard pattern const int32 CheckerDensity = 8; auto* Checker = UThumbnailManager::Get().CheckerboardTexture; Canvas->DrawTile( 0.0f, 0.0f, Width, Height, // Dimensions 0.0f, 0.0f, CheckerDensity, CheckerDensity, // UVs FLinearColor::White, Checker->Resource); // Tint & Texture if (Texture) { switch(Brush.DrawAs) { case ESlateBrushDrawType::Image: { FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } break; case ESlateBrushDrawType::Border: { FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } break; case ESlateBrushDrawType::Box: { float NaturalWidth = Texture->GetSurfaceWidth(); float NaturalHeight = Texture->GetSurfaceHeight(); float TopPx = FMath::Clamp<float>(NaturalHeight * Brush.Margin.Top, 0, Height); float BottomPx = FMath::Clamp<float>(NaturalHeight * Brush.Margin.Bottom, 0, Height); float VerticalCenterPx = FMath::Clamp<float>(Height - TopPx - BottomPx, 0, Height); float LeftPx = FMath::Clamp<float>(NaturalWidth * Brush.Margin.Left, 0, Width); float RightPx = FMath::Clamp<float>(NaturalWidth * Brush.Margin.Right, 0, Width); float HorizontalCenterPx = FMath::Clamp<float>(Width - LeftPx - RightPx, 0, Width); // Top-Left FVector2D TopLeftSize( LeftPx, TopPx ); { FVector2D UV0( 0, 0 ); FVector2D UV1( Brush.Margin.Left, Brush.Margin.Top ); FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, TopLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } // Bottom-Left FVector2D BottomLeftSize( LeftPx, BottomPx ); { FVector2D UV0( 0, 1 - Brush.Margin.Bottom ); FVector2D UV1( Brush.Margin.Left, 1 ); FCanvasTileItem CanvasTile( FVector2D( X, Y + Height - BottomPx ), Texture->Resource, BottomLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } // Top-Right FVector2D TopRightSize( RightPx, TopPx ); { FVector2D UV0( 1 - Brush.Margin.Right, 0 ); FVector2D UV1( 1, Brush.Margin.Top ); FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y ), Texture->Resource, TopRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } // Bottom-Right FVector2D BottomRightSize( RightPx, BottomPx ); { FVector2D UV0( 1 - Brush.Margin.Right, 1 - Brush.Margin.Bottom ); FVector2D UV1( 1, 1 ); FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y + Height - BottomPx ), Texture->Resource, BottomRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } //----------------------------------------------------------------------- // Center-Vertical-Left FVector2D CenterVerticalLeftSize( LeftPx, VerticalCenterPx ); { FVector2D UV0( 0, Brush.Margin.Top ); FVector2D UV1( Brush.Margin.Left, 1 - Brush.Margin.Bottom ); FCanvasTileItem CanvasTile( FVector2D( X, Y + TopPx), Texture->Resource, CenterVerticalLeftSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } // Center-Vertical-Right FVector2D CenterVerticalRightSize( RightPx, VerticalCenterPx ); { FVector2D UV0( 1 - Brush.Margin.Right, Brush.Margin.Top ); FVector2D UV1( 1, 1 - Brush.Margin.Bottom ); FCanvasTileItem CanvasTile( FVector2D( X + Width - RightPx, Y + TopPx), Texture->Resource, CenterVerticalRightSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } //----------------------------------------------------------------------- // Center-Horizontal-Top FVector2D CenterHorizontalTopSize( HorizontalCenterPx, TopPx ); { FVector2D UV0( Brush.Margin.Left, 0 ); FVector2D UV1( 1 - Brush.Margin.Right, Brush.Margin.Top ); FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y), Texture->Resource, CenterHorizontalTopSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } // Center-Horizontal-Bottom FVector2D CenterHorizontalBottomSize( HorizontalCenterPx, BottomPx ); { FVector2D UV0( Brush.Margin.Left, 1 - Brush.Margin.Bottom ); FVector2D UV1( 1 - Brush.Margin.Right, 1 ); FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y + Height - BottomPx ), Texture->Resource, CenterHorizontalBottomSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } //----------------------------------------------------------------------- // Center FVector2D CenterSize( HorizontalCenterPx, VerticalCenterPx ); { FVector2D UV0( Brush.Margin.Left, Brush.Margin.Top ); FVector2D UV1( 1 - Brush.Margin.Right, 1 - Brush.Margin.Bottom ); FCanvasTileItem CanvasTile( FVector2D( X + LeftPx, Y + TopPx), Texture->Resource, CenterSize, UV0, UV1, Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } } break; case ESlateBrushDrawType::NoDrawType: { FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), Brush.TintColor.GetSpecifiedColor() ); CanvasTile.BlendMode = SE_BLEND_Translucent; CanvasTile.Draw( Canvas ); } break; default: check(false); } } } }
void UTextureThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget*, FCanvas* Canvas) { UTexture* Texture = Cast<UTexture>(Object); if (Texture != nullptr && Texture->Resource != nullptr) { UTexture2D* Texture2D = Cast<UTexture2D>(Texture); // Take the alpha channel into account for textures that have one. // This provides a much better preview than just showing RGB, // Because the RGB content in areas with an alpha of 0 is often garbage that will not be seen in normal conditions. // Non-UI textures often have uncorrelated data in the alpha channel (like a skin mask, specular power, etc) so we only preview UI textures this way. const bool bUseTranslucentBlend = Texture2D && Texture2D->HasAlphaChannel() && Texture2D->LODGroup == TEXTUREGROUP_UI; UTextureCube* TextureCube = Cast<UTextureCube>(Texture); UTextureRenderTargetCube* RTTextureCube = Cast<UTextureRenderTargetCube>(Texture); UTextureLightProfile* TextureLightProfile = Cast<UTextureLightProfile>(Texture); TRefCountPtr<FBatchedElementParameters> BatchedElementParameters; if(TextureCube || RTTextureCube) { // is released by the render thread when it was rendered BatchedElementParameters = new FMipLevelBatchedElementParameters((float)0); // If the thumbnail is square then make it 2:1 for cubes. if(Width == Height) { Height = Width / 2; Y += Height / 2; } } else if (TextureLightProfile) { BatchedElementParameters = new FIESLightProfileBatchedElementParameters(TextureLightProfile->Brightness); } else if (Texture2D && Texture2D->IsNormalMap()) { BatchedElementParameters = new FNormalMapBatchedElementParameters(); } if (bUseTranslucentBlend) { // If using alpha, draw a checkerboard underneath first. const int32 CheckerDensity = 8; auto* Checker = UThumbnailManager::Get().CheckerboardTexture; Canvas->DrawTile( 0.0f, 0.0f, Width, Height, // Dimensions 0.0f, 0.0f, CheckerDensity, CheckerDensity, // UVs FLinearColor::White, Checker->Resource); // Tint & Texture } // Use A canvas tile item to draw FCanvasTileItem CanvasTile( FVector2D( X, Y ), Texture->Resource, FVector2D( Width,Height ), FLinearColor::White ); CanvasTile.BlendMode = bUseTranslucentBlend ? SE_BLEND_Translucent : SE_BLEND_Opaque; CanvasTile.BatchedElementParameters = BatchedElementParameters; CanvasTile.Draw( Canvas ); if (TextureLightProfile) { float Brightness = TextureLightProfile->Brightness; // Brightness in Lumens FText BrightnessText = FText::AsNumber( Brightness ); FCanvasTextItem TextItem( FVector2D( 5.0f, 5.0f ), BrightnessText, GEngine->GetLargeFont(), FLinearColor::White ); TextItem.EnableShadow(FLinearColor::Black); TextItem.Scale = FVector2D(Width / 128.0f, Height / 128.0f); TextItem.Draw(Canvas); } } }