海洋环境模型 -海洋环境模型图片

[复制链接]
& v$ p& z& E, R* ]# P |2 f

我的专栏目录:

- _' c! R6 F. N( N6 B. q! I 5 E5 x9 U4 B( Q7 b( E0 ]/ Y

简介:

" d6 k' ~& I' T) |

海洋这个要素在游戏里占比越来越多,开放世界的游戏要求角色能上天入地下水。目前游戏里做海洋的方法就那几种。(1)预烘焙法(2)Gerstner wave(3)FFT海洋。预烘焙法可以是实现烘焙好DisplacementMap或者是FFT的运算结果。Gerstner wave可以在GPU或者CPU上算。FFT的话就是拿海洋频率模型算出Displacement。

) }* b5 ~5 [1 o

【1】基础环境搭建

O% o, k5 H* R0 C0 ~& J/ l" C: ^! ~

在开始研究之前我们需要先搭建起我们的环境。我选择在ComputeShader种完成各种计算,然后在顶点着色器种直接Sample前面的ComputeShader的波形计算结果。不要把波形的计算塞到VertexShader里。把波形计算独立出来还有个好处就是我能把波形的结果储存起来拿给其它效果使用,比如制作浮力部分的时候我们就需要知道海面波形的信息,如果塞VertexShader里就拿不到这些信息了。

) ]& [- F$ `& ^

搭建ComputeShader的方法前面我的文章有提到,这里我就直接贴代码了。使用的引擎版本是4.21.0,如果引擎更新了新版本可能代码有一点区别。FFT部分我会给出4.22的代码。

' c( q8 P6 J0 U9 U/ G8 `

如果不是在Unreal中实现或者不想做这么复杂,可以直接跳过这部分。

4 h1 @# M$ }9 U9 U1 I* U
) ~$ l! k9 p9 P
! ^( C+ [/ ^; l

SDHOcean.build.cs

Z! A( Q6 z# {* J% S8 v7 W) Z5 q
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. - `( l5 V+ x. Q9 } & y1 S1 C, A( a' E using UnrealBuildTool; 9 F. H% |7 _8 C9 K J & b ~" E1 Z9 i' }6 z8 I public class SDHOcean : ModuleRules h5 ] r) D" Q+ k$ O6 {6 n { 7 p9 V/ g" T2 F, {, y; Q public SDHOcean(ReadOnlyTargetRules Target) : base(Target) 2 Z$ R: L4 Z# R9 L c3 _! t8 H { / B' `8 B% p6 l1 R PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; - z/ ]$ I$ _4 A2 v ( x$ H& `: s: f% v. U3 E; x, @ PublicIncludePaths.AddRange(! g1 i, f) p! F# p2 R0 z" r7 [ new string[] {1 o+ H# _: @" U: r // ... add public include paths required here ... 2 w" U2 c. c/ x. d1 L( b' i } ) C3 b+ [' L- o' j* M );) U0 S" m1 i0 W ; W0 h5 u8 g$ j' N& _% z; H7 _6 b/ p) j, p! P PrivateIncludePaths.AddRange(' W% X; C8 B" w6 I) ^ new string[] {! `4 u3 N! Q8 j2 u2 D4 f // ... add other private include paths required here ...1 a- W0 L n/ g) ?: { } : \. T7 p* M& K) S* s );+ e8 p9 z# a6 L! | 2 V& E6 T/ r1 R& }" t' E 4 y# E! l$ Y# c2 A5 D3 N PublicDependencyModuleNames.AddRange(: E: f# R4 ^' p new string[]; n/ n4 Q# B+ U6 Z$ ~# v { , s+ z# h4 {, M+ n "Core", / F4 q; k0 Z0 z. j "CoreUObject", , E& L+ F' c# h0 r S2 o8 T6 l" E; j "Engine", " K2 W4 I$ @3 v @" K) j2 F6 G& t5 | "RHI", # l+ S! m2 T! v& B0 G! {) S "Engine", : ?/ I+ U, e) z. O# \1 p$ T; [ "RenderCore",2 L8 m2 D. ]6 H) P- {6 C" \ "ShaderCore",6 ~8 Z- c3 E. J // ... add other public dependencies that you statically link with here ...9 A( q/ J2 G6 b+ O* |- |" U } ! w; n J8 |, l; r9 E! D- h );7 A: \" O3 t j$ h1 N 3 z. x8 D0 Z# @% K. K; N3 c& M9 q6 J! D% L8 R2 S PrivateDependencyModuleNames.AddRange(* }/ j/ s3 A$ ?- P4 T+ D new string[] 1 o1 X; @$ u2 U { 3 ?4 Q1 {7 X1 |# l "CoreUObject"," _+ b) O. A) h- K "Engine", $ M/ o0 v3 X2 @ "Slate",3 q, T- i7 @4 h; V9 U6 h* J4 H "SlateCore",3 C! ~5 f, c U5 b "UnrealEd",- q: P$ H3 E S8 h0 V "Projects",6 b8 V' j+ ]3 A' ? // ... add private dependencies that you statically link with here ... 2 M" z( C- W# d }1 o; p; i } 8 S' V! ]+ B: j& R: k% a4 { );# |7 B, o7 E+ X 4 i2 O- l% o, A: ?2 q / l& j$ }. r. _: ^4 G3 Z1 v DynamicallyLoadedModuleNames.AddRange(8 B6 Z2 V4 C) u, M new string[] 4 N* v+ z$ J, [: y4 U) t' F {) G! C' z# k- l$ L# f X2 l& V' c // ... add any modules that your module loads dynamically here ... + y2 J5 G5 h1 }' n! K4 ] }7 l+ R$ ]- Z7 L1 S g3 K );: l1 D. K# l$ @' [ }. l5 x$ B$ x! D$ r9 F }
+ F1 O9 R( x& ]+ M' T. Y3 `7 r

SDHOcean.h

% J5 `3 t; a: S \7 `4 t. y
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. ; Z% z; E. n2 z- W4 t& R) C! C) Q" L' M! [( V5 M #pragma once 2 v, p* ~# n$ Y6 A7 D, P $ V/ y7 p7 n1 n7 f4 r( p1 y #include "CoreMinimal.h" 5 b% c/ P* B( H/ R #include "Modules/ModuleManager.h"2 Y) ?* H( M6 R #include "Interfaces/IPluginManager.h" " U1 }$ i8 v, ?' x, d$ V0 {+ v #include "Misc/Paths.h"% {$ C2 i2 x# o, j1 ?" n #include "Modules/ModuleManager.h"# @2 {$ Q- \& N0 C 7 s$ _/ s7 d E2 Q class FSDHOceanModule : public IModuleInterface ( W6 I; ~6 P1 z3 M$ W# ? { ) l, A) g# w+ h% k public:, \+ R$ C0 ? o; }% q 6 e, A7 ]' E9 z! a. E! H+ f1 p1 h: w /** IModuleInterface implementation */& v+ Z9 w" @; R* E! L0 B virtual void StartupModule() override; & J3 b" F% Y. Z T virtual void ShutdownModule() override;, G' H, K7 h% X" G+ G };
! Z& `: W9 t, g* ?

SDHOcean.cpp

; v% U) Z: M* a2 O: ]
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. , [- A2 o0 x) f5 w3 o+ G' W$ C" f% G9 q W- S0 G4 P. ~3 u #include "SDHOcean.h" : s6 A& C) G$ h; E% Z; V$ ~/ x5 }! e1 O #define LOCTEXT_NAMESPACE "FSDHOceanModule"! r7 u, x! |& B5 ^1 ?' B 9 W! E; B7 M0 J! h l. `" P void FSDHOceanModule::StartupModule(), s. H/ u6 w, P, |0 g+ [/ ^ {, c! v% S6 K4 x1 _' [ // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module 9 x7 K, x3 F& x, O8 V3 n/ y FString PluginShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("SDHOcean"))->GetBaseDir(), TEXT("Shaders"));; J1 f9 B8 ~. _6 C" M, b9 O6 A AddShaderSourceDirectoryMapping(TEXT("/Plugin/SDHOcean"), PluginShaderDir); & b/ S( k' p, D9 I# U } ( O8 G# Q6 c, C8 D/ Z, d2 O7 e# x- r$ c void FSDHOceanModule::ShutdownModule() 4 X, o- W* k# @6 W { ; Y9 W/ o( h. \ // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading, & L0 L4 {- D8 ^- N; A // we call this function before unloading the module.! P( O7 v) d, N1 h9 o } 6 l; \; m O, T( U9 L& a0 @1 z) J 3 A# w0 b& n, T! o/ f. R0 a #undef LOCTEXT_NAMESPACE 2 i( W8 V0 V- [% N/ \5 H% H& ]0 {3 B' n" _ IMPLEMENT_MODULE(FSDHOceanModule, SDHOcean)
4 q4 l3 W4 N- l8 r4 H

Ocean.h

3 z# A( V4 q4 P3 ]! H
#pragma once6 A- A% U. ~: q) ~. }. z : ?3 r. k* R' w #include "CoreMinimal.h" ) D- ?. G g, e+ U; T( f #include "UObject/ObjectMacros.h" ! L J4 D8 ~. ]( c# o _ #include "Runtime/Engine/Classes/Components/ActorComponent.h"1 n0 V `% q8 _5 A+ r #include "Engine/Classes/Engine/TextureRenderTarget2D.h"0 e2 R v+ {1 q) j' b9 | #include "Ocean.generated.h" , g; O0 I3 l3 \$ c: V1 A( v : X" D5 ] @; z8 f- T' G4 T typedef TRefCountPtr<class FRHITexture2D> FTexture2DRHIRef; 5 t) r8 k5 x4 q6 D typedef TRefCountPtr<class FRHIUnorderedAccessView> FUnorderedAccessViewRHIRef;- T* V0 d+ Q3 N. n: B5 _3 f typedef TRefCountPtr<class FRHIStructuredBuffer> FStructuredBufferRHIRef; " `' m) H6 M0 P( O( h$ Y class FRHITexture; + s2 E6 G- T2 d9 z! ~! M: u class FRHIUnorderedAccessView; 9 p: \4 D1 B9 R& ^- e# H3 R* {( O! s class FRHICommandListImmediate;5 v c } X# w, ? 2 e' [, C# V7 l$ A, H( P) G2 c USTRUCT(BlueprintType)# v7 A Z$ u& L: E. x# X struct FOceanBasicStructData_GameThread : O: \2 W! a" { { ' c% \0 E1 `' M, T GENERATED_USTRUCT_BODY() 1 U0 l0 o/ M. d, Y+ y# h4 ]& C6 d . K3 \3 N& U9 f" J9 \: w4 W FOceanBasicStructData_GameThread(){}3 r. n+ M: T8 A( B( Z, S& p' U * x, g4 ^0 t8 P" N0 K. b" e- q UPROPERTY(BlueprintReadWrite, EditAnywhere) $ }) H3 V2 X6 l& ]/ h5 J FVector4 OceanTime_GameThread; , }0 R$ A i; i; z d% Y };8 k; d. `5 H) Q- l8 u 9 b7 Z* f; c2 ?3 Q! Q" M UCLASS(hidecategories = (Object, LOD, Physics, Collision), editinlinenew, meta = (BlueprintSpawnableComponent), ClassGroup = Rendering, DisplayName = "OceanRenderComp") - r; e1 P5 C t, C5 t- l class SDHOCEAN_API UOceanRenderComponent : public UActorComponent( t; R4 z' Q% a { 4 ?" K8 p$ f+ ]/ T: g) ~8 [, L5 i. c GENERATED_BODY() ' h6 z! v, U8 K! ]5 d0 E9 {1 z7 Q1 H& p9 | public: * I/ L3 p* D% C+ R6 l; @( ] 1 g8 V: f: T" T& s9 M/ C UOceanRenderComponent(const FObjectInitializer& ObjectInitializer);2 o. q& ]( ~8 F6 U' H //~ Begin UActorComponent Interface. [2 V2 f5 l& n! G2 p- N0 ~ virtual void OnRegister() override; ) Q* F7 l; h6 _ virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override; 8 r* _, A' a! g' a+ [ $ w% T# k- n( j% {( C UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent") , K$ N( n" ^/ y UTextureRenderTarget2D* OutputRenderTarget2D; $ Y9 T: r1 }& U2 A, y 8 O* R4 I3 p# \7 J" `. z //UniformData for Ocean render: X! }# J# _ l UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent") 0 f4 x. C3 m3 `2 u( c2 G FOceanBasicStructData_GameThread OceanUniformDataBuffer; 3 O% K# d$ K6 O, U) { ( j+ i+ h6 n2 c- v int32 TargetSize;2 | z3 F# j5 @6 C# k ETextureRenderTargetFormat RenderTargetFormat; , c$ x S( A3 ]8 ?( C1 i # s, L' c. S; E3 A. K private: 8 S/ K! ?7 \* e9 E + d* t( K7 F9 ` //Render ocean render thread& W) \% M, t+ M6 {. e void OceanCalculating_GameThread();0 u7 L" c* p G0 a; } void OceanCalculating_RenderThread 4 z5 X3 r: X1 |8 g/ H8 E$ _8 p ( 4 G! a8 |' ]8 Q1 e8 H+ x9 q FRHICommandListImmediate& RHICmdList, 9 h" B4 W. I4 w* ?$ o/ L5 f+ X ERHIFeatureLevel::Type FeatureLevel,0 q" i( I9 Q% I. F* _ FRHITexture* OutputRenderTarget, t! m* |5 N, B/ Z, A int32 SurfaceSize, + x; q) \6 T K! _ const FOceanBasicStructData_GameThread& OceanUniformData) _& O6 U' r+ \, U! l0 U8 T ); 1 X/ P8 R& h! ]" F, F " L7 ?' u& R7 k FTexture2DRHIRef OutputTexture;6 }1 m8 B W3 g5 r FUnorderedAccessViewRHIRef OutputTextureUAV;+ E. W2 K, b* P- B, S }; 6 `# b( s8 Z5 G0 ?$ M7 j7 R5 D) d/ n
: N1 c* a1 L' `6 V. c5 B- P3 T5 _; O

Ocean.cpp

: l9 Y) v7 I5 o! t
#include "SDHOcean/Public/Ocean.h" + v+ ]4 E1 T" o b5 J F4 C1 I# `3 a #include "ShaderCore/Public/GlobalShader.h" `8 y& y2 h G6 s" p' | 0 ]8 r; u7 \8 k" C8 H #include "Classes/Engine/World.h" 5 V1 N! O( o$ n #include "Public/GlobalShader.h"' N) v8 d3 L: ] #include "Public/PipelineStateCache.h" ) t \8 O% R. q5 G- U7 z2 B #include "Public/RHIStaticStates.h" 4 w4 S8 f5 |% e3 L4 A# } #include "Public/SceneUtils.h"' P# m& ~# G, B. i3 f) l #include "Public/SceneInterface.h"& d. \' q( x8 m; D# a; B' t #include "Public/ShaderParameterUtils.h", _4 N; W7 u5 ` #include "Public/Logging/MessageLog.h"0 w& ]$ p, u7 W; a0 ~) N #include "Public/Internationalization/Internationalization.h" ; s. R' t# J' L K9 q6 F1 i #include "Public/StaticBoundShaderState.h"0 H9 T2 ]+ v% M #include "RHI/Public/RHICommandList.h" 4 D) h2 O2 X9 D T #include "RHI/Public/RHIResources.h" ' Y/ F/ ]' _0 U #include "Engine/Classes/Kismet/KismetRenderingLibrary.h"3 L: N* Q9 y/ B+ `- N- F #include "Runtime/Engine/Classes/Kismet/GameplayStatics.h" . q, m: ?7 a6 b1 e+ `5 O7 P; h& h1 D4 L4 S% d; _/ L5 ^3 j #define LOCTEXT_NAMESPACE "SDHOcean" , f# C4 R: ?6 x$ o' `" K9 s4 \' J/ i8 U& X2 D BEGIN_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, )+ B: O# X4 L+ |% | UNIFORM_MEMBER(FVector4, OceanTime) 2 [5 K# W3 A+ ]3 N9 m END_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData) 5 \6 o3 F) |8 R( |# a1 [4 G1 e& p9 B5 ?0 `. I1 ] IMPLEMENT_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, TEXT("OceanBasicStructData"))( X' Q/ P) k& e; ~( R/ j# @ i* x , ?& q* O8 L6 P" c( v/ M class FOceeanCSShader : public FGlobalShader 2 @* D3 ^' H5 O1 @) o" S* X7 ^ { r; N! E% C& U3 r+ h6 [ # g" g! ^' G% p DECLARE_SHADER_TYPE(FOceeanCSShader, Global)% a! s" t* L$ @6 ?; a- v7 M( U% e $ A7 H; y* V( K public:8 P" @1 m+ ^3 G - k% U5 n1 d% G FOceeanCSShader() {}% J6 q0 Q' V7 h& T6 T6 X/ [5 m FOceeanCSShader(const ShaderMetaType::CompiledShaderInitializerType& Initializer) / n( G, e" Q$ }" W5 ?2 ]$ i- y : FGlobalShader(Initializer) ; N) x$ t/ h7 n( c! w" C7 C {) A9 t8 y3 w# q. {5 @( i! W //TODO Bind pramerter here* z2 m; I! D, M# I% I7 O1 M OutputBufferSurface.Bind(Initializer.ParameterMap, TEXT("OutputBufferSurface")); , m" U% y" g( i) ?/ q5 o3 A SurfaceClearColor.Bind(Initializer.ParameterMap, TEXT("SurfaceClearColor"));* `3 g( P: T' m) z# ~ }. B* a3 {. F3 i& W0 ]/ x* r" B //----------------------------------------------------//; O9 {) t0 w+ t* t8 A1 z static bool ShouldCache(EShaderPlatform PlateForm) " b. c9 w- f( K- ^ {) d! F# ~* C7 V* m( p return IsFeatureLevelSupported(PlateForm, ERHIFeatureLevel::SM5);: T0 N$ B8 B- m1 Z }8 A$ u9 v( [$ r' w //----------------------------------------------------//; U$ ~* B3 H9 Z8 p static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)5 o- j2 s$ ~9 I+ K! r* y* W0 @8 N: q {4 \" G1 Z {- T2 ?4 f! S return IsFeatureLevelSupported(Parameters.Platform, ERHIFeatureLevel::SM5);: r n& v( E& u' P7 I } ( Q5 f: @% f3 e. }# x( v2 f% k* T //----------------------------------------------------// ! }0 r" O8 m7 v9 l# `9 a" T( E6 [# P static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment) # P& m; M9 s! m/ n( | { k+ I/ R- S; {( S' K8 i9 h' M: b FGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment); 3 r% h3 V$ U# ?1 q# y //Define micro here+ t4 g, |( j, g) T) t( ~" d9 i //OutEnvironment.SetDefine(TEXT("TEST_MICRO"), 1); & X' K! k- S, z, |2 Z) s }4 U& _- F) _7 L8 m8 @ //----------------------------------------------------//% w! W) F+ j) p+ u. X - s" r" k- r* Q4 q- c/ m void SetSurface(FRHICommandList& RHICmdList, & c# T! i& B9 o* E FUnorderedAccessViewRHIRef& OutputUAV, 6 V0 ~# d' `2 w) J1 w const FLinearColor ClearColor( G Y- \! B" _# Y )8 M p" Z& u5 |: ^ { 6 ?# m# J+ |2 F6 G% p" m5 F2 D //set the UAV & D o1 f% G# T0 m& R9 M FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader(); + p U( I" z5 z1 g$ f0 O/ _ if (OutputBufferSurface.IsBound()); u9 [$ o! ]9 S2 r# z7 ? RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), OutputUAV);- X- W# ~' l6 d! H5 s; Y& X. Z if (SurfaceClearColor.IsBound()) ) A7 d" ^" f q) ] //RHICmdList.SetShaderParameter(GetComputeShader(), SurfaceClearColor.GetBufferIndex(), SurfaceClearColor.GetBaseIndex(), SurfaceClearColor.GetNumBytes(), ClearColor); * W/ _4 v! Z+ N4 T4 x% `% O! D* \ SetShaderValue(RHICmdList, GetComputeShader(), SurfaceClearColor, ClearColor);2 y/ H( `6 t0 U& n; A } / P2 f0 |( ]; R) }* [4 k+ m8 c. R' ~/ _- u4 ` void SetOceanUniformBuffer(FRHICommandList& RHICmdList, const FOceanBasicStructData_GameThread& OceanStructData)6 s- T9 u7 T8 N/ j ]7 t: o { ) u& L/ ?- U$ [8 Y FOceanBasicStructData UniformData;) k9 \5 O" [- X UniformData.OceanTime = OceanStructData.OceanTime_GameThread;. k1 K8 w6 k; L% @ SetUniformBufferParameterImmediate(RHICmdList, GetComputeShader(), GetUniformBufferParameter<FOceanBasicStructData>(), UniformData); 8 T( `6 X7 j$ e+ d/ _8 `: X } . U% [$ ~. t0 G/ M $ ^7 | j( e! u4 y5 k, F# P: y void UnBindBuffers(FRHICommandList& RHICmdList) 1 b" a/ b: Q. h& ?" c% X- P { 2 V/ C/ ~0 R" R0 S: J, r4 @7 e6 k FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader(); 6 ?1 n! L2 l* X ^1 @# c+ i7 x+ P' K9 Z- Z: L/ S0 Y2 b e9 D if (OutputBufferSurface.IsBound())" v/ T5 ^$ n! z. ]0 T+ ^ RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), FUnorderedAccessViewRHIRef()); 7 k, R5 {' J2 q' J2 ~9 y }0 ~) i4 E8 n M' r. w( ` ( l1 \8 K& x2 { virtual bool Serialize(FArchive& Ar) override 2 y1 f" `# ^7 ]# @ { ~* C( _. v$ M. v0 U" ]& x bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);; H3 R2 j" ]% T/ l3 `& u. v //Serrilize something here/ s, z. U" l! t0 N* t Ar << OutputBufferSurface << SurfaceClearColor; 1 G, j( {/ u; o. V% k return bShaderHasOutdatedParameters;: E# e9 W) Q; Y6 Z5 D } % R! ?/ U* z0 f, P' {7 q7 D4 x# _9 D' C2 }$ ` private: ; M* g& A1 p, F+ P' s 9 w, }; r2 T; [2 q FShaderResourceParameter OutputBufferSurface;& \! p( p- C# P2 f' o6 n) k FShaderParameter SurfaceClearColor;! u; j0 L S7 W3 j* U& Q }; + s+ S$ ]4 R5 S G! m8 S5 @9 W. @9 w5 O Z IMPLEMENT_SHADER_TYPE(, FOceeanCSShader, TEXT("/Plugin/SDHOcean/Ocean.usf"), TEXT("OceanMainCS"), SF_Compute) 5 ^% k" X+ v t8 r 6 _3 L2 n+ U9 C1 ~- L void UOceanRenderComponent::OceanCalculating_RenderThread 6 A+ U3 F# w1 ?( X ( 8 B2 M/ k$ F! M% H: \ FRHICommandListImmediate& RHICmdList,. Q1 C+ d, N3 g1 s ERHIFeatureLevel::Type FeatureLevel, 9 W5 a O; L x) Y. g FRHITexture* OutputRenderTarget, * F% U3 r8 j' ~ I5 [5 `2 _ int32 SurfaceSize,7 Q1 ^4 I- U% }1 H$ L* s5 W3 M const FOceanBasicStructData_GameThread& OceanUniformData0 u; t: g6 A# N* D ) / }8 p' ?, z* C F! k6 K {) ]& C' T3 p' j. M check(IsInRenderingThread());- v: @! z- Y% u2 Y- L check(OutputRenderTarget);( v! b8 z' f/ s: q+ `7 o. B# Q4 Z * s, i, x" n6 H1 Q TShaderMapRef<FOceeanCSShader>OceanComputeShader(GetGlobalShaderMap(FeatureLevel));7 n7 L3 M1 B+ J" G; e) b, n; D( k+ ~" D RHICmdList.SetComputeShader(OceanComputeShader->GetComputeShader()); 7 B% ~3 E9 a" p' R 7 V* c( H: _/ H. L if (OutputTexture.IsValid() == false), ]& S( A: @8 F0 @ { , q* S5 x' l; w1 ^* O1 o if (OutputTexture.IsValid()) ) T5 `& |2 R; Y0 V: B9 H0 p OutputTexture->Release();9 G" e1 q7 `7 N" ^! @- H6 i if (OutputTextureUAV.IsValid())& K% Y- x) g6 ~( G' _- Q0 C! q OutputTextureUAV->Release();: Z1 s- E$ c; i- d " {/ C0 [+ y" } m) ^# z FRHIResourceCreateInfo CreateInfo;4 j, G2 P) h1 A! I' E1 H OutputTexture = RHICreateTexture2D(SurfaceSize, SurfaceSize, PF_FloatRGBA, 1, 1, TexCreate_ShaderResource | TexCreate_UAV, CreateInfo);$ K3 d; c. M3 z+ E/ C ]7 ?9 z OutputTextureUAV = RHICreateUnorderedAccessView(OutputTexture); / w3 ^3 N, l# g8 X- O* l: V' T% Q9 e) U4 {$ z a3 `$ l }! E- I( \/ @7 F" ^# E. y: B( ` B: r + w( S4 ` N; ~ j6 o( Q OceanComputeShader->SetSurface(RHICmdList, OutputTextureUAV, FLinearColor(1,1,1,1));1 \) k1 ^" y6 V* L2 G' W0 p OceanComputeShader->SetOceanUniformBuffer(RHICmdList ,OceanUniformData); / o( g6 O/ y' o' M2 r3 C / z# H* { _! e/ X6 h DispatchComputeShader(RHICmdList, *OceanComputeShader, SurfaceSize / 32, SurfaceSize / 32, 1); ~5 t) Z/ s. }$ j9 z8 u! A* n OceanComputeShader->UnBindBuffers(RHICmdList);3 k. o! p9 R2 `/ C9 Y & y8 d& m% ^' t, q$ k% f RHICmdList.CopyToResolveTarget(OutputTexture, OutputRenderTarget, FResolveParams()); 2 F7 o! ~) V6 v //FRHICopyTextureInfo copyinfo(SurfaceSize, SurfaceSize); 4 h% q N- _1 U //RHICmdList.CopyTexture(OutputTexture, OutputRenderTarget, copyinfo); % J" U3 t4 F3 |% {' p } + M0 I/ d$ l; p1 }" R% I . X2 L' ]3 }9 J4 ]! p' Z UOceanRenderComponent::UOceanRenderComponent(const FObjectInitializer& ObjectInitializer) 3 g) T0 Y* V4 {5 I :Super(ObjectInitializer)# Y3 U& x, f# R# {$ e- P$ x; W {/ |+ r: ^0 t Z& o6 [7 k* B6 I* c9 w PrimaryComponentTick.bCanEverTick = true; : G5 U3 v; M* h' n bTickInEditor = true; 1 ?( Y$ y5 n) f9 e bAutoActivate = true; S0 ~; A1 O5 Z3 ^4 l; m8 g; m6 l + m9 M% N F. d5 t, q RenderTargetFormat = RTF_RGBA32f;, A5 c3 K* h$ s( i. } }3 [; A8 L) f0 v" F3 r( s0 q + h. ~; ?6 B' }6 U+ I( F) N H void UOceanRenderComponent::OnRegister()$ @: v6 t# D. {. _- d1 h+ ^% F {/ o( g7 r* L* S+ [. ]2 h( V: c( X9 v Super::OnRegister();; }7 p7 Q* _ r }" ~1 ?/ {# S6 o / m8 w3 |0 G u0 w void UOceanRenderComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction). X W+ v) P6 n3 y" Y0 c { + _- k9 f, t! o- O Super::TickComponent(DeltaTime, TickType, ThisTickFunction);; K2 w9 x) t5 k- x) R$ X% `* Z3 a / ^# M/ I& r6 n //Tick render the ocean3 d* Z; I. ]* I) b6 z1 W OceanCalculating_GameThread(); / R1 Q( a1 K2 X- w }3 \# H8 J; u! f3 m0 h * d6 J* Q" {! z/ Q3 P1 t+ q void UOceanRenderComponent::OceanCalculating_GameThread()& m P( D1 i$ \! Z9 Z5 F( G { % O, \- H! U/ @ A2 [) V+ C, Q UWorld* world = GetWorld();& `+ l1 H2 W& c- F0 s% m9 N3 r9 f9 h ERHIFeatureLevel::Type FeatureLevel = world->Scene->GetFeatureLevel();+ m" T% \; e! j* U2 A, ]# ?/ }8 Z checkf(FeatureLevel == ERHIFeatureLevel::SM5, TEXT("Only surpport SM5")); ) j& T' b1 n5 N3 z9 b6 z' i) ~2 c if (OutputRenderTarget2D == nullptr) return;& Z0 o3 k1 ]; q1 a" e6 g" ]. ^ : {+ A3 w/ _6 e //Using front RT to render,back buffer store last frame imformation4 @9 D; h; h3 `9 ^. d5 r% V UKismetRenderingLibrary::ClearRenderTarget2D(world, OutputRenderTarget2D);- I2 X7 p; V# l9 u3 l( L ( |; K+ U/ K& K0 y. E" B" O0 w9 G: f FTextureReferenceRHIRef OutputRenderTargetTextureRHI = OutputRenderTarget2D->TextureReference.TextureReferenceRHI;; x5 w" ~' g; ?! e* T2 a2 Y ; g8 o3 Y1 a, r# L* h& J( U2 j checkf(OutputRenderTargetTextureRHI != nullptr, TEXT("Cant get render target %d texture")); 6 v& k' U! {, [2 B" \$ R) c# f# N) e! L" [9 z0 U2 X- w' B FRHITexture* RenderTargetTextureRef = OutputRenderTargetTextureRHI->GetTextureReference()->GetReferencedTexture(); $ b) A! O( x, ~: `9 _) L; l7 y TargetSize = OutputRenderTarget2D->SizeX;5 D9 L* \" v4 R" }8 ]0 x4 X6 } - S/ }1 q# x c9 H# K. e8 R) j //Update the uniform buffer' A6 \8 C ?$ A OceanUniformDataBuffer.OceanTime_GameThread.X = UGameplayStatics::GetRealTimeSeconds(GetWorld());5 w2 q- ~. ^9 L9 W4 z( |# R) ^ 2 R1 h- M" Y- n' f5 ^' h+ W ENQUEUE_RENDER_COMMAND(OceanRenderCommand)& T8 c1 Q' w4 X D2 u0 o (9 s8 O3 e6 |, M6 D8 n1 x [FeatureLevel, RenderTargetTextureRef, this](FRHICommandListImmediate& RHICmdList) . C' c7 B0 L! V# o { . D7 @* K6 t) X- H r; f OceanCalculating_RenderThread { s/ g: Y- h ( 2 P1 n7 h. k% ]' r G RHICmdList,% O: L( { _- V/ w' S FeatureLevel, 4 {$ r+ V& {: G7 T, Z w$ T RenderTargetTextureRef,) d) M6 H; q! H& l- e this->TargetSize, 3 o9 x1 D2 k3 E, Z% B0 @8 | this->OceanUniformDataBuffer # u% Z" A5 Y( e# }' @/ a/ [4 E3 ^ ); # Z; ]2 x G8 ~/ D6 n D }( c" t8 }, T+ o; J+ X4 Q );: ~7 s1 L5 B" B) p+ E9 ` }6 k3 `9 d, U0 w 7 R5 R/ b. p$ |0 e8 C #undef LOCTEXT_NAMESPACE
, z9 [, j/ C) F& x5 Y. K- m2 i

Ocean.usf

. N; i b! z3 |! M" \" J
#include "/Engine/Private/Common.ush") x$ n: O( Y+ F* W- ` ; I5 g$ |1 C5 d, M, c RWTexture2D<float4> OutputBufferSurface;& r! D' E- n1 e; [8 m float4 SurfaceClearColor; 1 X! T7 ]! @. ~! ]; s: c8 @: b 3 Q) j; R2 |3 I- I# T- F7 d6 V( |$ \ struct OceanUniform 2 M7 V5 H% n8 n {' m5 {+ g9 |4 ~& J: e G" Y% E float Time;( B8 j) D- {- V- E5 B };8 F. d8 d7 }1 R5 _ \% C void InitOceanUniform(out OceanUniform uniformval) 3 z. | s# t, X, C { " W- J! k6 K2 p, A1 b% F2 D! D, u uniformval.Time = OceanBasicStructData.OceanTime.x;9 r( j6 r! T) R: d } 5 ]; s( _9 r4 t Z4 l3 S* U1 Z 8 W& i/ k- z Z [numthreads(32, 32, 1)] 2 T, x" b. L1 Y6 P# I void OceanMainCS(uint3 ThreadId : SV_DispatchThreadID) 1 z2 n+ n0 N* E4 _/ @8 M { 0 k5 H, g Y4 E& I //Set up some variables we are going to need ; B! v5 b) W; w' y3 M0 ^ //The size of outputsurface and input surface is same" N: N7 P5 d D3 g5 S' X( _ float sizeX, sizeY; 4 Y* ]% E5 P6 R( K OutputBufferSurface.GetDimensions(sizeX, sizeY); ! P: f8 z9 _& X' I, ?9 x( _& i3 k6 ^' K$ ]+ p6 P OceanUniform OceanUniformVal; " q8 ~& {0 N! o2 N5 o3 P: A, Y# X InitOceanUniform(OceanUniformVal);) O) Q/ P& t$ \' Z4 K " ^& X1 O0 @) A) }: n, ? float2 iResolution = float2(sizeX, sizeY); 4 m3 m6 U$ B1 V float2 UV = (ThreadId.xy / iResolution.xy) * 50.0f; ?$ a! x2 c- O" Z5 g# {0 I9 f! h$ Z- H9 b float4 Output = float4(1.0f, 1.0f, 1.0f, 1.0f); . C% q5 s1 [" k( K" D' h- O: |8 v5 { Output.xyz = float3(sin(UV.x + OceanUniformVal.Time), 0, 0); ) j; f9 v5 R/ f( g! l$ ~( f1 \" J# N 7 A' M+ P1 _% C$ C, o& p7 \ OutputBufferSurface[ThreadId.xy] = Output; X* b' I1 T0 O$ }5 p* V$ e }
8 N; m P. a9 J# f

我这里做了个UniformBuffer然后拿到系统的时间,把系统的时间变量塞到我的Compute shader中。

. d/ N3 e9 g9 T! _# R( i
( K8 a, s2 f$ d9 M0 D% q9 t

直接把值采出来连到VertexPositionOffset上就可以把我们的ComputeShader的结果传到顶点着色器了。

! `# H, J2 u9 c8 p* ~' V

【2】Gerstner Wave

( B# P3 p {) z2 O$ J6 c/ ~

在Gerstner Waves之前,先使用正玄波变形的方法模拟。对正玄波进行变形属于经验性的方法,把正弦波变形让它的形状更接近水浪。

% g4 S g n7 Q' g: q9 V% T
% o" `* q4 x+ X# j+ A0 d+ i7 t

F(x)=2(\frac{sin(x) + 1}{2})^{k}

; i- S% c% i0 g# g2 Q

下面先来制作正玄波水面

9 V1 B; t# T0 f! q$ K4 i- {' r0 z( a
$ ~" G: a# v0 B! G

于是乎我们可以得到如下效果

- J# R- X( o; u! M1 s& b
# v# {7 b( V# `; |; m# j
3 o( w. u+ H' t/ K2 ?
0 g. ~+ @4 f, T: V6 @! g- t5 R8 c- t

有了基础的波形后,剩下就是让海面变化更丰富。想让海面变丰富那就是多叠几层波

4 q: x- u/ ?& H4 o9 Y) p
+ b- F' M, [! a+ `( O' z
2 R8 W8 P: \3 G' t0 B9 X
$ G9 O3 C- y; K( @& S
Output.z = 0.5 * pow((sin(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f); 7 K1 k$ U1 @ h Output.z += 0.2 * pow((sin(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f);0 @" ] b* X8 U4 Y4 t Output.z += 0.15 * sin(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);# K% b7 E4 k# B: K- y' s Output.z += 0.1 * sin(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);. M' J+ o' U1 d- Z M* j Output.z += 0.1 * sin(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f); C& `) D# o7 T" o; O5 \! N* V5 ^ + o2 V- k; a, t7 `( {* @/ I& Z Output.y = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);, s( O6 j6 s% s3 O+ v Output.y += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f); * R2 W. x9 M$ @ y0 \ Output.y += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);: j$ A& [6 U6 Z2 [# B Output.y += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);7 C- ~6 }1 |) ^( V1 @ Output.y += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);3 I; m; D' R- F* `! f, t$ ?1 Y( W 4 u$ j6 c( q5 i% f+ u4 W. k, v6 ~ Output.z = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);: h3 G1 E+ U: E$ F' E Output.z += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f);0 A1 C5 O0 C/ {1 V# S% U% F) v Output.z += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);8 I* z0 i' `/ V5 f1 N" [8 E Output.z += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);' f6 @7 t" r+ \, A' W0 v. ^! y, ~ Output.z += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);7 A9 P: _1 t% Y8 k0 m 9 R$ ]$ z4 P: n( n4 g) w* y OutputBufferSurface[ThreadId.xy] = Output;
* p9 y# N8 C+ i a5 J8 X

可以看到正玄波水面波浪比较平,无法模拟出水波的波峰陡峭的特点。因此我们需要使用新的模拟模型:Gerstner Wave。

( a. {, I4 C) q* W/ L1 A5 E- W
3 P% [3 n# z) J$ R! c7 }, M
: J7 s P, f2 s: K K
& w9 |) l; B K O! t/ Z, x V1 m

PositionOffset:

7 i9 \* ]5 h/ y6 k& a2 W
; A& H C) b+ r( \

Normal:

3 ~- m. [) a4 @1 T
$ L u- s- D$ Y# S; M3 [8 u- J

Gerstner Wave是周期重力波欧拉方程的解, 其描述的是拥有无限深度且不可压缩的流体表面的波形 。

* r( s. ~* Y/ s. M" d# e
1 }6 v1 L6 Q# t! D6 l' q8 _
. z% s4 h6 l- v2 L* r
+ H- p& F% ^- b7 F* e

代码如下:

( Y0 v5 E |7 E$ O* C X
5 k, b% a# k3 q! _$ C/ t! q

想要更好的效果可以优化下参数和多叠几层波,反正Computeshader里算这种东西很快的啦。

; u+ z& J. G) y1 D& S% p

【3】FFT海面理论推导

) |- t2 R9 C6 ~2 i3 z U6 ?

FFT海面的核心思路是我们通过一系列测量得到真实海面的波的频率然后把这些频率通过FFT变换到时域然后计算出置换贴图。下面来公式推导

, ^0 n# J+ B( b# S" R

设波的高度为水平方向的位置和时间的关系 h(X,t) 。在水平方向上 X= (x,z) 根据Tessendorf J.2001的论文我们可以得到如下公式

; K3 [! @" |- n6 K- B4 o

h(X,t)=\sum_{K}^{}{x}\tilde{h}(K,t)e^{iK\cdot X}

# g! |6 M* }% d5 A$ q3 J7 g7 O) s

其中 K 代表波正在运动的二维水平方向, K = (k_{x}, K_{y})

: V6 g8 P% S' ^. C

k_{x} = 2\pi/L_{x} , k_{y} = 2\pi m/L_{y}

6 s. W) u5 X( V; _# {/ _

n 是水平方向的Domain Resolution。 m 是竖直方向的Domain Resolution

9 M3 m7 X2 w8 L ~

所以 K 的范围是:

3 n$ I5 n+ |9 ~1 o4 P8 r$ x

K=(2\pi/L_{x},2\pi m/L_{y})

) K6 _0 o8 U/ ?5 D& c5 J1 T

The fft process generates the height field at discrete points

8 g5 C+ }7 l3 w5 F. A

X = (nL_{x}/N, mL_{z}/N)

]; f5 D0 w# U
w$ a: }/ K6 h. @0 L; m" ?( V5 c

按照上面的公式可以渲染得到下面一张图:

P8 j" e8 H' d" F* x1 ]
3 Q; E' R) q2 e h% Q

(2)然后需要用高斯分布生成一个Phillips spectrum

3 o" Y* U" d4 R3 l; m; {0 M& u
6 u; ?" f3 \- D: F2 R
- X4 S. s2 d1 }7 A- p" [/ \

会得到如下效果

+ d% _; ~. b7 p$ Z, m" x- E( a
- c$ g+ `3 \/ { x2 Q9 O

把它和gauss分布结合后得到如下效果:

' ^) p& p+ r) C$ J% @) L
4 s1 l. i0 o! A: g" \* s& r: b) x

然后现在我们有了海面的频谱图。下一步需要进行IFFT变换,但是在做变换前我们需要一个OmegaTexture做蝶形变换

0 \ J8 L# ^9 d* Q
$ W& m4 u0 R9 Q3 Y9 {
8 c5 c$ `/ x7 J5 H1 {3 c$ A+ d8 G

(3)然后做IFFT变换生成Displacement,然后生成高度图和normal

H1 k, n; W0 h; q$ r) M
& t# T/ M+ f% K

最后把这些生成的图弄到渲染管线种作为渲染资源渲染海面即可。下面就根据这上面的步骤来生成我们的海面。

( l) {, p d6 o* w" q/ T Q

下一卷我将给出具体FFT实现。

; k. J J- s/ C* U

Enjoy it。

* @) `. P# ^: Y8 \

Next:

9 Q: [. n, Y& n, J; _' A

【参考资料】

5 }8 M( r# p$ c0 ]8 `

【1】Ocean Shader with Gerstner Waves

5 \' N% j% F" h8 b. H z

【2】音速键盘猫:Shader相册第6期 --- 实时水面模拟与渲染(一)

0 B! k) P# e8 c& ]* D

【3】https://labs.karmaninteractive.com/ocean-simulation-pt-1-introduction-df134a47150

# h1 i) V9 ]+ B3 X3 Z

【4】Ocean simulation part one: using the discrete Fourier transform

2 ?/ E; G. D* B& ^

【5】Ocean simulation part two: using the fast Fourier transform

1 [! c: h3 K( k4 { S. U" F

【6】https://www.slideshare.net/Codemotion/an-introduction-to-realistic-ocean-rendering-through-fft-fabio-suriano-codemotion-rome-2017

: N, t: Q3 i3 N2 V/ E# O1 ~, U

【7】海洋模擬 FFT算法實現--基於GPU的基2快速傅里葉變換 2維FFT算法實現--基於GPU的基2快速二維傅里葉變換 【pbrt】使用openFrameworks調用pbrt

2 C; z! C7 F! ?& V$ o1 C) X

【8】白霂凡:一小时学会快速傅里叶变换(Fast Fourier Transform)

$ e Z t& i6 s/ @

【9】海面模拟以及渲染(计算着色器、FFT、Reflection Matrix)

! Z8 y" w3 D; M& e, B; K

【10】wubugui/Jerry-Tessendorf-2004

: B0 A" M" s- f) w3 b; U

【11】http://evasion.imag.fr/~Fabrice.Neyret/images/fluids-nuages/waves/Jonathan/articlesCG/waterslides2001.pdf

$ u5 z) @9 P& f2 z1 z

【12】https://dsqiu.iteye.com/blog/1636299

" a( W. m5 }! ~

【13】https://www.bilibili.com/video/av19141078?t=1053

3 A- g% Z0 y T; f3 m- ?( B

傅里叶变换基础教程(如果对傅里叶变换完全不清楚的建议按顺序看完下面的链接)

/ Z) t Q% P" L/ F+ u$ a! F

【14】Heinrich:傅里叶分析之掐死教程(完整版)更新于2014.06.06

( Z- g |% H K7 A7 V q

【15】https://www.bilibili.com/video/av34364399/?spm_id_from=333.788.videocard.0

( J+ x, i7 Z- D+ R& k

【16】https://www.bilibili.com/video/av34556069/?spm_id_from=333.788.videocard.0

' ~- j( e7 W% {8 ]/ ?

【17】https://www.bilibili.com/video/av34845617/?spm_id_from=333.788.videocard.0

; e4 w1 k1 x7 M D+ R/ v$ U/ y' g

【18】https://www.bilibili.com/video/av35047004/?spm_id_from=333.788.videocard.0

$ {4 l# a& X2 r2 W2 v) s

【19】https://www.bilibili.com/video/av35810587/?spm_id_from=333.788.videocard.0

8 j1 l/ ]% y$ U$ @! L0 V7 @

【20】https://www.bilibili.com/video/av36343956/?spm_id_from=333.788.videocard.0

( J, J6 i- ?$ S1 |( q; S % ]' U" x" E% v' F H3 m% M2 [ 3 s9 t a" k6 V( e3 } - G1 K6 a- m+ H * A, X9 {' s7 Z: t) l
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
深宫鲍
活跃在2024-12-1
快速回复 返回顶部 返回列表