ue4.24调通了shader文档

本文详细介绍了在UE4中实现镜头畸变UV生成的过程,包括如何利用顶点和像素着色器直接计算畸变和非畸变UV位移,并将其存储到纹理的红绿通道中。文章还涉及了蓝图的熟悉运用、渲染到纹理的机制、虚拟目录的使用以及源码和插件的参考,特别强调了与镜头畸变模型相关的结构体和函数的定义与调用。

先上图,

 

有几个感慨:

1,ue4文档不与时俱进,所以还要看源码或者例子。这个就是按照源码插件依葫芦画瓢进行的

2,shader加载时机,按照build.cs进行,要不会崩溃,启动编辑器之后才加载

3.蓝图要熟,因为要给蓝图用,所以,还要学学。

4,渲染到纹理是每帧进行,一开始忘了,所以没出来结果,event tick()

5,需要虚拟目录了,

 

6,虚拟目录后,vs和Ps找的shader文件,也不是真实的目录,而是逻辑层次(这点存疑,还需要看看)


IMPLEMENT_SHADER_TYPE(, FLensDistortionUVGenerationVS, TEXT("/Plugin/Foo/Private/UVGeneration.usf"), TEXT("MainVS"), SF_Vertex)
IMPLEMENT_SHADER_TYPE(, FLensDistortionUVGenerationPS, TEXT("/Plugin/Foo/Private/UVGeneration.usf"), TEXT("MainPS"), SF_Pixel)

先看看存放目录

 

磨蹭了很长时间,发现蓝图不熟,以后还得慢慢学,

暴露给蓝图使用,类要用category标识,比如

        UFUNCTION(BlueprintPure, Category = "findMyFoo")
        static void GetUndistortOverscanFactor(
            const FFooCameraModel& CameraModel,
            float DistortedHorizontalFOV,
            float DistortedAspectRatio,
            float& UndistortOverscanFactor);

这个方法的Catetory是"findMyFoo",在蓝图中,输入"find"及以后的就可以智能弹出,能够找到。

 

但是结构体很诡异,我一开始用FooCameraModel,结果显示的ooCameraModel,用FFooCameraModel,才显示FooCameraModel,第一个F被吃了。
struct FOO_API FFooCameraModel
{

}..

 

generate.h生成时,要用.uproject文件

另外,为了显示是否正确生成,在ps上直接赋值红色。

蓝图设置上,

最后,直接上源码了

 

Foo.h


#pragma once

#include "CoreMinimal.h"
#include "Modules/ModuleInterface.h"
#include "Modules/ModuleManager.h"


/**
* The public interface to this module
*/
class ILensDistortion : public IModuleInterface
{

public:

    /**
    * Singleton-like access to this module's interface.  This is just for convenience!
    * Beware of calling this during the shutdown phase, though.  Your module might have been unloaded already.
    *
    * @return Returns singleton instance, loading the module on demand if needed
    */
    static inline ILensDistortion& Get()
    {
        return FModuleManager::LoadModuleChecked< ILensDistortion >("Foo");
    }

    /**
    * Checks to see if this module is loaded and ready.  It is only valid to call Get() if IsAvailable() returns true.
    *
    * @return True if the module is loaded and ready to use
    */
    static inline bool IsAvailable()
    {
        return FModuleManager::Get().IsModuleLoaded("Foo");
    }
};

Foo.cpp

// Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.

#include "Foo.h"
#include "CoreMinimal.h"
#include "Modules/ModuleManager.h"
#include "Interfaces/IPluginManager.h"
#include "Misc/Paths.h"
#include "ShaderCore.h"


class FLensDistortion : public ILensDistortion
{
    /** IModuleInterface implementation */
    virtual void StartupModule() override;
    virtual void ShutdownModule() override;
};

IMPLEMENT_MODULE(FLensDistortion, Foo)


void FLensDistortion::StartupModule()
{
    FString PluginShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("Foo"))->GetBaseDir(), TEXT("Shaders"));
    AddShaderSourceDirectoryMapping(TEXT("/Plugin/Foo"), PluginShaderDir);
}


void FLensDistortion::ShutdownModule()
{
    // This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
    // we call this function before unloading the module.
}


FooBlueprintFunctionLibrary.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/ObjectMacros.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "FooCameraModule.h"
#include "FooBlueprintFunctionLibrary.generated.h"

/**
 * 
 */

UCLASS(MinimalAPI, meta = (ScriptName = "FooFunctionLibrary"))
class UFooBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_UCLASS_BODY()


        /** Returns the overscan factor required for the undistort rendering to avoid unrendered distorted pixels. */
        UFUNCTION(BlueprintPure, Category = "findMyFoo")
        static void GetUndistortOverscanFactor(
            const FFooCameraModel& CameraModel,
            float DistortedHorizontalFOV,
            float DistortedAspectRatio,
            float& UndistortOverscanFactor);

    /** Draws UV displacement map within the output render target.
    * - Red & green channels hold the distortion displacement;
    * - Blue & alpha channels hold the undistortion displacement.
    * @param DistortedHorizontalFOV The desired horizontal FOV in the distorted render.
    * @param DistortedAspectRatio The desired aspect ratio of the distorted render.
    * @param UndistortOverscanFactor The factor of the overscan for the undistorted render.
    * @param OutputRenderTarget The render target to draw to. Don't necessarily need to have same resolution or aspect ratio as distorted render.
    * @param OutputMultiply The multiplication factor applied on the displacement.
    * @param OutputAdd Value added to the multiplied displacement before storing into the output render target.
    */
    UFUNCTION(BlueprintCallable, Category = "findMyFoo", meta = (WorldContext = "WorldContextObject"))
        static void DrawUVDisplacementToRenderTarget(
            const UObject* WorldContextObject,
            const FFooCameraModel& CameraModel,
            float DistortedHorizontalFOV,
            float DistortedAspectRatio,
            float UndistortOverscanFactor,
            class UTextureRenderTarget2D* OutputRenderTarget,
            float OutputMultiply = 0.5,
            float OutputAdd = 0.5
        );

    /* Returns true if A is equal to B (A == B) */
    UFUNCTION(BlueprintPure, meta = (DisplayName = "Equal (FFooCameraModel)", CompactNodeTitle = "==", Keywords = "== equal"), Category = "findMyFoo")
        static bool EqualEqual_CompareFFooCameraModels(
            const FFooCameraModel& A,
            const FFooCameraModel& B)
    {
        return A == B;
    }

    /* Returns true if A is not equal to B (A != B) */
    UFUNCTION(BlueprintPure, meta = (DisplayName = "NotEqual (FFooCameraModel)", CompactNodeTitle = "!=", Keywords = "!= not equal"), Category = "findMyFoo")
        static bool NotEqual_CompareFFooCameraModels(
            const FFooCameraModel& A,
            const FFooCameraModel& B)
    {
        return A != B;
    }
};

FooBlueprintFunctionLibrary.cpp

 

// Fill out your copyright notice in the Description page of Project Settings.


#include "FooBlueprintFunctionLibrary.h"


UFooBlueprintFunctionLibrary::UFooBlueprintFunctionLibrary(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{ }


// static
void UFooBlueprintFunctionLibrary::GetUndistortOverscanFactor(
    const FFooCameraModel& CameraModel,
    float DistortedHorizontalFOV,
    float DistortedAspectRatio,
    float& UndistortOverscanFactor)
{
    

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值