SHARE
TWEET

Rendering Material in Slate

NikolaDimitroff Feb 23rd, 2015 453 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FSlateRenderTarget : public FRenderTarget
  2. {
  3. public:
  4.     /** FRenderTarget interface */
  5.     virtual FIntPoint GetSizeXY() const override
  6.     {
  7.         return FIntPoint(ClippingRect.GetSize().X, ClippingRect.GetSize().Y);
  8.     }
  9.  
  10.     /** Sets the texture that this target renders to */
  11.     void SetRenderTargetTexture(FTexture2DRHIRef& InRHIRef)
  12.     {
  13.         RenderTargetTextureRHI = InRHIRef;
  14.     }
  15.  
  16.     /** Clears the render target texture */
  17.     void ClearRenderTargetTexture()
  18.     {
  19.         RenderTargetTextureRHI.SafeRelease();
  20.     }
  21.    
  22.     const FSlateRect& GetClippingRect() const
  23.     {
  24.         return ClippingRect;
  25.     }
  26.  
  27.     void SetClippingRect(const FSlateRect& InClippingRect)
  28.     {
  29.         ClippingRect = InClippingRect;
  30.     }
  31.  
  32.     const FSlateRenderTransform& GetRenderTransform() const
  33.     {
  34.         return RenderTransform;
  35.     }
  36.  
  37.     void SetRenderTransform(const FSlateRenderTransform& InRenderTransform)
  38.     {
  39.         RenderTransform = InRenderTransform;
  40.     }
  41. private:
  42.     FSlateRect ClippingRect;
  43.     FSlateRenderTransform RenderTransform;
  44. };
  45.  
  46. class FSlateElement : public ICustomSlateElement
  47. {
  48. public:
  49.     FSlateElement(UTexture* InTexture)
  50.         : Material(FindMaterialInstance())
  51.         , Texture(InTexture)
  52.         , RenderTarget(new FSlateRenderTarget)
  53.     {
  54.         Material->SetTextureParameterValue(TEXT("Texture"), InTexture);
  55.         Size.X = Texture->GetSurfaceWidth();
  56.         Size.Y = Texture->GetSurfaceHeight();
  57.     }
  58.  
  59.     void DrawRenderThread(FRHICommandListImmediate& RHICmdList, const void* InWindowBackBuffer) override
  60.     {
  61.         if (!Texture || !Texture->Resource)
  62.             return;
  63.  
  64.         auto RenderTransform = RenderTarget->GetRenderTransform();
  65.         FVector2D Origin = RenderTransform.TransformPoint(FVector2D(0, 0));
  66.         auto ClippingRect = RenderTarget->GetClippingRect().OffsetBy(Origin);
  67.  
  68.         RHICmdList.SetScissorRect(false, 0, 0, 0, 0);
  69.         RenderTarget->SetRenderTargetTexture(*(FTexture2DRHIRef*)InWindowBackBuffer);
  70.         {
  71.             float CurrentTime = FApp::GetCurrentTime() - GStartTime;
  72.             float DeltaTime = FApp::GetDeltaTime();
  73.             FCanvas Canvas(RenderTarget.Get(), NULL, CurrentTime, CurrentTime, DeltaTime, GMaxRHIFeatureLevel);
  74.             {
  75.                 FCanvasTileItem Tile(Origin,
  76.                     Material->GetRenderProxy(false),
  77.                     Size);
  78.  
  79.                 Tile.BlendMode = SE_BLEND_AlphaComposite;
  80.                 Tile.UV0.Y = 0.0f;
  81.                 Tile.UV1.Y = 1.0f;
  82.                 Canvas.DrawItem(Tile);
  83.             }
  84.             Canvas.Flush_RenderThread(RHICmdList, true);
  85.         }
  86.         RenderTarget->ClearRenderTargetTexture();
  87.     }
  88.  
  89.     TUniquePtr<FSlateRenderTarget> RenderTarget;
  90. private:
  91.     FVector2D Size;
  92.     TUniquePtr<UMaterialInstanceDynamic> Material;
  93.     UTexture* Texture;
  94. };
  95.  
  96. void SMyWidget::Construct(const FArguments& InArgs)
  97. {
  98.     // Initialize Stuff, create the texture
  99.     SlateElement = MakeShareable(new FSlateElement(Texture));
  100. }
  101.  
  102. FVector2D SMyWidget::ComputeDesiredSize() const
  103. {
  104.     return Size;
  105. }
  106.  
  107. int32 SMyWidget::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyClippingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const
  108. {
  109.     SlateElement->RenderTarget->SetClippingRect(MyClippingRect);
  110.     SlateElement->RenderTarget->SetRenderTransform(AllottedGeometry.ToPaintGeometry().GetAccumulatedRenderTransform());
  111.     FSlateDrawElement::MakeCustom(OutDrawElements, LayerId, SlateElement);
  112.     return LayerId;
  113. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top