UE4 创建Viewport

UE4 创建Viewport

创建自定义的ViewportClient,并显示TextureRenderTarget2D对象。

ViewPort

创建TextureRenderTarget2D

设置蓝色背景。

UTextureRenderTarget2D*RenderTarget = NewObject(GetTransientPackage(), NAME_None, RF_Transient);
RenderTarget->RenderTargetFormat = RTF_RGBA8;
RenderTarget->InitAutoFormat(1024, 1024);
RenderTarget->ClearColor = FLinearColor::Blue;
RenderTarget->UpdateResource();

ViewPort绘制

ViewportClient::Draw(FViewport* Viewport,FCanvas* Canvas)中进行绘制。
绘制RenderTarget相当于绘制“栅格图像”。

//绘制RenderTarget
    FCanvasTileItem Tile(FVector2D::ZeroVector, RenderTarget->Resource, Viewport->GetSizeXY(), FLinearColor(1, 1, 1, 1));
    Canvas->DrawItem(Tile);

绘制一段线。

FCanvasLineItem Line(FVector2D(0, 0), FVector2D(Viewport->GetSizeXY().X, Viewport->GetSizeXY().Y));
    Line.SetColor(FLinearColor::Blue);
    Canvas->DrawItem(Line);

创建项目

创建Standalone Window插件来显示Viewport

Standalone Window

Plugins\TPViewport\Source\TPViewport\Public\RenderTestViewportClient.h

#include "CoreMinimal.h"
#include "UnrealClient.h"

class FRenderTestViewportClient : public FViewportClient
{
public:
    FRenderTestViewportClient();

    virtual void Draw(FViewport* Viewport, FCanvas* Canvas)override;

    class UTextureRenderTarget2D* RenderTarget;
};

Plugins\TPViewport\Source\TPViewport\Public\RenderTestViewportClient.cpp

#include "RenderTestViewportClient.h"
#include "Runtime/Engine/Public/CanvasTypes.h"
#include "Runtime/Engine/Public/CanvasItem.h"
#include "Engine/TextureRenderTarget2D.h"

#include "SimpleShader.h"

FRenderTestViewportClient::FRenderTestViewportClient()
{
    RenderTarget = NewObject(GetTransientPackage(), NAME_None, RF_Transient);
    RenderTarget->RenderTargetFormat = RTF_RGBA8;
    RenderTarget->InitAutoFormat(1024, 1024);
    RenderTarget->ClearColor = FLinearColor::Blue;
    RenderTarget->UpdateResource();
}

void FRenderTestViewportClient::Draw(FViewport* Viewport, FCanvas* Canvas)
{
    //绘制RenderTarget
    FCanvasTileItem Tile(FVector2D::ZeroVector, RenderTarget->Resource, Viewport->GetSizeXY(), FLinearColor(1, 1, 1, 1));
    Canvas->DrawItem(Tile);
}

Plugins\TPViewport\Source\TPViewport\Public\TPViewport.h
三者之间的关系
ViewportWidget--->FSceneViewport---->FViewportClient

///
    ///......
    ///
    TSharedPtr Viewport;
    TSharedPtr ViewportWidget;
    TSharedPtr ViewportClient;

Plugins\TPViewport\Source\TPViewport\Public\TPViewport.cpp
创建SDockTab

TSharedRef FTPViewportModule::OnSpawnPluginTab(const FSpawnTabArgs& SpawnTabArgs)
{
    TSharedRef Tab = SNew(SDockTab)
        .TabRole(ETabRole::NomadTab)
        [
            SNew(SBox)
            .HAlign(HAlign_Fill)
            .VAlign(VAlign_Fill)
            [
                SAssignNew(ViewportWidget, SViewport)
            ]
        ];

    ViewportClient = MakeShareable(new FRenderTestViewportClient());
    Viewport = MakeShareable(new FSceneViewport(ViewportClient.Get(), ViewportWidget));
    ViewportWidget->SetViewportInterface(Viewport.ToSharedRef());

    return Tab;
}
void FTPViewportModule::StartupModule()
{
    // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
    
     ///
    ///......
    ///
    
    FGlobalTabmanager::Get()->RegisterNomadTabSpawner(TPViewportTabName, FOnSpawnTab::CreateRaw(this, &FTPViewportModule::OnSpawnPluginTab))
        .SetDisplayName(LOCTEXT("FTPViewportTabTitle", "TPViewport"))
        .SetMenuType(ETabSpawnerMenuType::Hidden);
}

参考链接

UE4 创建Viewport

你可能感兴趣的:(UE4 创建Viewport)