Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FSlateRenderTarget : public FRenderTarget
- {
- public:
- /** FRenderTarget interface */
- virtual FIntPoint GetSizeXY() const override
- {
- return FIntPoint(ClippingRect.GetSize().X, ClippingRect.GetSize().Y);
- }
- /** Sets the texture that this target renders to */
- void SetRenderTargetTexture(FTexture2DRHIRef& InRHIRef)
- {
- RenderTargetTextureRHI = InRHIRef;
- }
- /** Clears the render target texture */
- void ClearRenderTargetTexture()
- {
- RenderTargetTextureRHI.SafeRelease();
- }
- const FSlateRect& GetClippingRect() const
- {
- return ClippingRect;
- }
- void SetClippingRect(const FSlateRect& InClippingRect)
- {
- ClippingRect = InClippingRect;
- }
- const FSlateRenderTransform& GetRenderTransform() const
- {
- return RenderTransform;
- }
- void SetRenderTransform(const FSlateRenderTransform& InRenderTransform)
- {
- RenderTransform = InRenderTransform;
- }
- private:
- FSlateRect ClippingRect;
- FSlateRenderTransform RenderTransform;
- };
- class FSlateElement : public ICustomSlateElement
- {
- public:
- FSlateElement(UTexture* InTexture)
- : Material(FindMaterialInstance())
- , Texture(InTexture)
- , RenderTarget(new FSlateRenderTarget)
- {
- Material->SetTextureParameterValue(TEXT("Texture"), InTexture);
- Size.X = Texture->GetSurfaceWidth();
- Size.Y = Texture->GetSurfaceHeight();
- }
- void DrawRenderThread(FRHICommandListImmediate& RHICmdList, const void* InWindowBackBuffer) override
- {
- if (!Texture || !Texture->Resource)
- return;
- auto RenderTransform = RenderTarget->GetRenderTransform();
- FVector2D Origin = RenderTransform.TransformPoint(FVector2D(0, 0));
- auto ClippingRect = RenderTarget->GetClippingRect().OffsetBy(Origin);
- RHICmdList.SetScissorRect(false, 0, 0, 0, 0);
- RenderTarget->SetRenderTargetTexture(*(FTexture2DRHIRef*)InWindowBackBuffer);
- {
- float CurrentTime = FApp::GetCurrentTime() - GStartTime;
- float DeltaTime = FApp::GetDeltaTime();
- FCanvas Canvas(RenderTarget.Get(), NULL, CurrentTime, CurrentTime, DeltaTime, GMaxRHIFeatureLevel);
- {
- FCanvasTileItem Tile(Origin,
- Material->GetRenderProxy(false),
- Size);
- Tile.BlendMode = SE_BLEND_AlphaComposite;
- Tile.UV0.Y = 0.0f;
- Tile.UV1.Y = 1.0f;
- Canvas.DrawItem(Tile);
- }
- Canvas.Flush_RenderThread(RHICmdList, true);
- }
- RenderTarget->ClearRenderTargetTexture();
- }
- TUniquePtr<FSlateRenderTarget> RenderTarget;
- private:
- FVector2D Size;
- TUniquePtr<UMaterialInstanceDynamic> Material;
- UTexture* Texture;
- };
- void SMyWidget::Construct(const FArguments& InArgs)
- {
- // Initialize Stuff, create the texture
- SlateElement = MakeShareable(new FSlateElement(Texture));
- }
- FVector2D SMyWidget::ComputeDesiredSize() const
- {
- return Size;
- }
- int32 SMyWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
- {
- SlateElement->RenderTarget->SetClippingRect(MyClippingRect);
- SlateElement->RenderTarget->SetRenderTransform(AllottedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform());
- FSlateDrawElement::MakeCustom(OutDrawElements, LayerId, SlateElement);
- return LayerId;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement