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

[复制链接]
; _9 \6 p2 f4 l+ _1 }" V2 a+ b# m

我的专栏目录:

: C g' [1 n7 [' l$ F7 @0 H 8 M v0 Y' \- p3 ]9 w$ S6 I

简介:

& s; n& G, W: I2 A. y1 ]

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

' E: b! e3 h% s7 W& t; |3 u" x/ k; s8 Y

【1】基础环境搭建

7 W. a8 o, J/ M. t. m6 c# a2 F

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

0 [ D' k& i+ n3 I- D# s5 A0 ?

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

$ b9 y' _. P- g8 z! M

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

, y7 ]1 o Q4 a! B0 Y, w8 B
& _4 T: C' W" }' D
( d7 }1 N+ |( N+ U

SDHOcean.build.cs

P' C7 j/ {- P
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved. 1 q8 A2 U1 {; L: N2 E; x5 F, f2 ?5 f4 g. Z2 H2 ]4 F using UnrealBuildTool;- e# ~/ `: P2 Y% ?/ N 4 F9 [! H" d3 ?, h0 D, j public class SDHOcean : ModuleRules" u) h" b9 V' W% l9 v$ s6 \7 ^ { + t3 k/ @' H+ }! I$ X | public SDHOcean(ReadOnlyTargetRules Target) : base(Target)" f3 K/ B3 C0 M1 a5 x { `7 g- R4 Z4 y* A! [ PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; 3 x- N" v5 s. U5 q - Y* ]0 v+ D5 i! u PublicIncludePaths.AddRange(' H: H+ J: w ?% M3 X8 O new string[] {% B' C* q/ ~2 t8 O' B a // ... add public include paths required here ... 3 D2 q+ m* I; M+ C8 o9 g } . Y1 i- D ?/ d: C d B ); ' c5 r% P* a/ `6 a ^( ^+ z1 J3 ~- g$ g - c. E3 ]- [1 M 2 n; m, i3 V: r1 { PrivateIncludePaths.AddRange(: u8 g( t, b- t. f+ h new string[] { 2 u3 E; J+ N9 S3 Q // ... add other private include paths required here ...* G6 X# B$ Y0 L) y( M) z. K }5 E9 v) U. x0 T( L+ M ); . Q$ H# q8 C6 A0 l. i; m0 @5 r9 P$ Q( w , i, l; o& I+ V8 g PublicDependencyModuleNames.AddRange( + n/ ?6 G: _9 _& \ new string[]0 O7 F# M8 g3 t5 t# `7 z/ B { * U+ _. B. n) E3 `. z "Core", 4 K `0 L$ S% e$ V "CoreUObject",( e: P }5 I7 D) ^1 x+ N# M; } "Engine",+ j2 e L" C p/ Y2 K% m" A "RHI",. N! g' M- R* _" T4 p) v Q "Engine", - x, B+ T/ D2 [: _+ k/ } "RenderCore",0 a V% R, s6 h* X" Z "ShaderCore",2 g# G# L. ~2 k Z // ... add other public dependencies that you statically link with here ...9 w: h8 e- K' k4 A, p- \ } ; a# o& ], F. V& a# q: V );& O7 S% A( r1 d I! V) ~ ; i. n& P6 X n6 P1 T$ n. E4 u* r% H% n- x$ {+ _& G PrivateDependencyModuleNames.AddRange( + O( \. o3 C5 u" v new string[] 4 |: i$ d j3 c- N3 ` {9 S' ]% |0 c/ V# e$ H "CoreUObject",$ Q* t. @. q/ j. p2 F1 a "Engine", 5 H3 f, B/ T$ V "Slate", 3 C4 U/ K8 ^& n "SlateCore",6 Y0 M" e: D& D1 V8 w* k# h- h "UnrealEd", 4 b. F( I: O1 f "Projects",& ?% ~8 R& D& W // ... add private dependencies that you statically link with here ...' n; R9 O! f1 F' b k } ) n y3 C( R) ?% w! M );# V. H* P6 Z ]3 E 5 N$ b4 C0 |$ t; | - T: @& E$ _* m) x DynamicallyLoadedModuleNames.AddRange( 5 J6 A7 m/ s+ e" b/ i$ Q7 Y8 M5 O new string[]* U% f: i; l; Z {( \% C) `% x' x; x9 O# P1 T // ... add any modules that your module loads dynamically here ...2 U1 g; `* J" h/ H; [( h } ! h2 I6 C6 ?; U ); , k/ a9 O1 q* \7 r# g* _* U } : ~5 B. B+ g) a2 _5 _ }
1 V0 a" j, g4 l/ K2 G" y) a1 T5 Q

SDHOcean.h

) l1 x1 l( t0 V' G! C$ m3 b1 b; V
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.2 V3 [; Y* J, {! A " W+ Z* r% A2 @3 D$ h$ E8 _ #pragma once6 Q+ o2 g- H( Q% F" c! n ' n, z$ N/ Y1 q8 e; |: } #include "CoreMinimal.h" 7 U K2 I, W( p* F+ K9 | #include "Modules/ModuleManager.h" " H5 n/ C. j. C. Z. X- W2 b6 i #include "Interfaces/IPluginManager.h" - w5 B( _/ F. ^& M" l- [) d #include "Misc/Paths.h"2 r( w8 e! {' @ #include "Modules/ModuleManager.h" , z. q. y, N& O/ @" [8 U- O- R- @. ]) n! l7 h; h. m, E) \ class FSDHOceanModule : public IModuleInterface ' w' D! `8 j7 M) c/ Q4 ~ V3 m* M { 4 A4 \/ @0 D* A K: L public:( g" \) H" l7 v( o+ E& M. O- } ; w: f8 i+ Z# ]8 w2 y$ B& m+ n( X /** IModuleInterface implementation */ 0 T* R1 ~/ t) Y7 j4 l virtual void StartupModule() override; & t" P' m2 ] ^ d virtual void ShutdownModule() override; 4 w8 r0 Z* M! c5 q! R: e };
& v2 T6 V# E9 e) L0 s) Z

SDHOcean.cpp

& V; d5 }2 P3 N' E' M7 ~
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.' |- T; L% }% {4 F8 Y: F, | 2 w6 Q' V* _0 e n* i! i #include "SDHOcean.h" / Y6 o2 W0 M& }0 A: V2 i) w/ o8 ] }) f, `, `3 ?: J+ ~% }- T4 X" n; \( n #define LOCTEXT_NAMESPACE "FSDHOceanModule" 0 A& t* Q; e' C& c1 a u: r 7 U5 g4 \+ b1 }$ E( @ void FSDHOceanModule::StartupModule()$ l2 z- L7 l% y. o {# D6 r, d8 V4 |" V$ m0 S // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module I. W6 g& n( T% i) ^4 V$ L FString PluginShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("SDHOcean"))->GetBaseDir(), TEXT("Shaders"));' w b' Y; J' G AddShaderSourceDirectoryMapping(TEXT("/Plugin/SDHOcean"), PluginShaderDir); 3 Q# \# R% e0 b, X9 s) ? } R% ?3 }5 B( d, ]% h& W2 H; ]$ V$ @3 B" p2 H8 Z void FSDHOceanModule::ShutdownModule() $ `& l" G T" Z { ! Q* m4 ~, _0 `' Q2 v // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading, 7 h* \/ \3 s% B) Q( H // we call this function before unloading the module.+ r+ N- P* @$ |7 W3 D9 \8 Z+ J. y }% c" p# J( \3 P/ R( q2 b4 } ' W6 s8 ~" o. m b; D0 X #undef LOCTEXT_NAMESPACE * u. J* n5 A) @$ ~" P/ X8 n3 t/ w" B" D0 w* [& S/ Q( C IMPLEMENT_MODULE(FSDHOceanModule, SDHOcean)
_/ l( s$ y( y# y# c9 W! I

Ocean.h

) R) W0 w& T9 z, e( I
#pragma once " m0 z5 e- N/ x & h$ g/ T1 o# C# R9 q" | #include "CoreMinimal.h"4 f; q# z |2 a1 I1 |! R( u #include "UObject/ObjectMacros.h" , b; x! h' B: s0 Y, u #include "Runtime/Engine/Classes/Components/ActorComponent.h"1 n* M# p8 E( s, `9 L! ?4 H #include "Engine/Classes/Engine/TextureRenderTarget2D.h" 5 @7 p4 J) C$ S; R& k$ O #include "Ocean.generated.h" + W z2 |; l9 C+ Y+ ^% W7 T/ A6 H% r" h R$ a/ Q typedef TRefCountPtr<class FRHITexture2D> FTexture2DRHIRef; 8 m3 @. i8 M, F2 b4 y typedef TRefCountPtr<class FRHIUnorderedAccessView> FUnorderedAccessViewRHIRef;7 K+ Y4 x) [: e. X; r% w* O1 Z) H typedef TRefCountPtr<class FRHIStructuredBuffer> FStructuredBufferRHIRef; 5 q/ g* B& P0 y5 c! e; l class FRHITexture; 0 v7 v: \5 a+ h. v. F, P class FRHIUnorderedAccessView;2 }3 b& r2 e# t/ ^ class FRHICommandListImmediate;/ e3 p0 q. i) ~/ ~! N# A " Q$ `" [. V0 A0 @# y USTRUCT(BlueprintType) ( H! E' z% z- v" Q struct FOceanBasicStructData_GameThread M, [0 {, h& F |+ v! ]# w {/ j. ^& l* t, ^; \ GENERATED_USTRUCT_BODY() " h- O% J" g( C: ~ C" j/ k: F) g) u: D0 E' E6 f FOceanBasicStructData_GameThread(){} . d, v: J7 Y4 p U# t' _. Q/ T& P; O/ ^( H UPROPERTY(BlueprintReadWrite, EditAnywhere)9 X# R h) D1 R* }; V FVector4 OceanTime_GameThread; , w: A6 \) ]. D };7 E" u+ X( @' V0 a2 E A2 \1 M, O# ] ; _8 {: r$ g6 L: i4 g UCLASS(hidecategories = (Object, LOD, Physics, Collision), editinlinenew, meta = (BlueprintSpawnableComponent), ClassGroup = Rendering, DisplayName = "OceanRenderComp") , O; C% C2 I/ z7 l1 g+ \2 a class SDHOCEAN_API UOceanRenderComponent : public UActorComponent6 L! v; y; s2 L { ' G/ ?$ G5 h1 \ GENERATED_BODY() % W2 U- V' I& l( v' e1 p$ r1 F2 T: G0 f public:3 z9 q0 B8 r5 H3 j / U4 g% W H+ m/ T) X UOceanRenderComponent(const FObjectInitializer& ObjectInitializer);- L0 |& v4 _# G- @: U" R //~ Begin UActorComponent Interface. # O- Q I( D) R7 P. \2 B5 D) x virtual void OnRegister() override;/ Y1 W/ j( ^# r: G* v- r virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override; 4 X8 Q3 ^" j4 n) P; q$ O/ X3 q5 g1 [! n; H UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent")+ l; I8 E$ x7 M UTextureRenderTarget2D* OutputRenderTarget2D;- E. i( e; J! k( S! b 1 [4 [" J4 H+ Z6 b) ~" [$ f //UniformData for Ocean render 1 r; x, q5 I' |, V UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent") - I [+ h5 L- M6 @ FOceanBasicStructData_GameThread OceanUniformDataBuffer; & N! S# Z2 a' `% o7 `6 p( F8 S8 T0 H) v0 m, c$ N. g2 g5 { int32 TargetSize; 4 R2 r4 L6 k0 u3 P" f7 y; a ETextureRenderTargetFormat RenderTargetFormat;1 h! {( H! j: L$ ~8 V" [ 2 _( D; L3 n1 ~" n9 k private:* a) M- n% j' a1 \& H! L' W, u( K ) l, Z9 R2 V4 [2 b4 e* O5 \ //Render ocean render thread3 o' h! W/ w9 a8 s( f& d. D void OceanCalculating_GameThread(); % Y) m7 ~' N- S* z9 F+ H void OceanCalculating_RenderThread 5 I+ q+ `) F2 m4 F# ?5 I b ( : D2 | U/ V7 I% k' m# M, ]& k2 _0 D0 x( Y9 v FRHICommandListImmediate& RHICmdList, * n- }' o' _7 O( | ERHIFeatureLevel::Type FeatureLevel," k$ u& k9 t* L) p! O# F) e. E FRHITexture* OutputRenderTarget, [9 m H t; n" a% U. J/ e int32 SurfaceSize, : U; W7 d/ k8 K4 n& b const FOceanBasicStructData_GameThread& OceanUniformData* C; `" u$ E1 s3 p) F7 F w );2 P5 a. {; z0 Z: |, D# J4 A \ 6 l( B: V5 u3 q1 D& f" U7 c FTexture2DRHIRef OutputTexture;; J8 z8 f( P: E3 S FUnorderedAccessViewRHIRef OutputTextureUAV;% C- q" w$ `) W4 k; N1 a. t };! P% p- @, ?/ T; i# ^
) A8 E+ e- ]* W) [

Ocean.cpp

+ \1 g% z$ |/ c# c
#include "SDHOcean/Public/Ocean.h"! D7 |$ F& J( {/ }* ^. q9 y! I : H# c/ X ?9 W #include "ShaderCore/Public/GlobalShader.h"7 A+ Q# F8 ] p, f1 M ^ 3 |3 N' E4 H D7 J: G #include "Classes/Engine/World.h"& F' D0 j: I' P" m1 B2 @* g #include "Public/GlobalShader.h" 7 W# ?$ I$ S) j #include "Public/PipelineStateCache.h"$ p+ l3 Y7 g% K3 Q" u' D: i #include "Public/RHIStaticStates.h" D" Y5 i# v* k9 [, r) o #include "Public/SceneUtils.h" & y1 ?; b H/ [, b9 J! T, s o #include "Public/SceneInterface.h" # ~6 R2 F' e. u7 ` #include "Public/ShaderParameterUtils.h"6 D* U% L9 Z/ {; V: F$ T' F5 v3 _( ]7 F #include "Public/Logging/MessageLog.h" 6 c( s1 ?% Y8 f0 v" } #include "Public/Internationalization/Internationalization.h" 4 @: e# P# h8 i# `+ e #include "Public/StaticBoundShaderState.h"6 w4 w4 J% S2 H( [ #include "RHI/Public/RHICommandList.h" , c! ]$ X U1 m7 z2 b #include "RHI/Public/RHIResources.h" & G% E5 z. i8 \4 e* M' j #include "Engine/Classes/Kismet/KismetRenderingLibrary.h" * M2 @! `1 }" G& m #include "Runtime/Engine/Classes/Kismet/GameplayStatics.h" b9 {$ }8 X7 U # U- g( I) W+ X& }" t2 m #define LOCTEXT_NAMESPACE "SDHOcean" & N! z, m& i$ |* {. q! j: o+ G. @) d% P BEGIN_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, ) % C8 _6 G: |5 N4 T! P: C$ z- o UNIFORM_MEMBER(FVector4, OceanTime) 1 J/ ?3 |: `3 f. c( l END_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData)6 ^+ P' c g# R0 e9 s8 T: j $ l: G {( o3 X8 w) f+ x IMPLEMENT_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, TEXT("OceanBasicStructData"))) q6 L3 I; o' ]+ }/ \7 P# J: [$ N / ^5 }6 \! o1 l1 @ \ class FOceeanCSShader : public FGlobalShader7 h U/ N4 x! K0 T& r { . g2 g: x [+ \8 M" m9 {% W% t1 T+ M- h$ f DECLARE_SHADER_TYPE(FOceeanCSShader, Global) ! f$ Z& v! Y) } & H& O& u* P! g, K: K) W public:$ W; [# B/ i" j: I& @/ S. ^' Q / {$ B! ]/ l, g6 P FOceeanCSShader() {} % Y1 T; v. D* u3 D! `+ N( w% t FOceeanCSShader(const ShaderMetaType::CompiledShaderInitializerType& Initializer)/ \5 v+ P* [* j4 ^% W% k : FGlobalShader(Initializer) 0 n2 L4 [# a% K1 @2 R; }) ~$ K { ) r- @0 m( j5 G5 _* P4 r5 X //TODO Bind pramerter here / Y5 T, j5 v0 L5 B" C OutputBufferSurface.Bind(Initializer.ParameterMap, TEXT("OutputBufferSurface"));6 J1 g) B8 x# l7 g& g- x SurfaceClearColor.Bind(Initializer.ParameterMap, TEXT("SurfaceClearColor")); ' j) o4 y! D" ~2 l/ v. ^0 ~ } 6 D8 K1 o9 U2 W //----------------------------------------------------// % f' U( r, f: K% Y4 w. p |3 v8 H static bool ShouldCache(EShaderPlatform PlateForm) 3 `0 ^2 u; G* t4 I- I$ H. m {! J4 |% c: @7 i return IsFeatureLevelSupported(PlateForm, ERHIFeatureLevel::SM5);6 _( d% R7 C+ W/ i: ^. d, X } ' u2 L! e7 }) P0 Q% x2 w l7 C //----------------------------------------------------// 1 Z8 w2 E" D- E4 P static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)& w' ]3 b' X+ E- I {7 e8 T* r' j) d$ S return IsFeatureLevelSupported(Parameters.Platform, ERHIFeatureLevel::SM5); $ Z4 a# z1 r1 J } * E7 F9 g$ B% A9 a2 A //----------------------------------------------------// 7 \) p) C9 v2 H static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment)* d9 ?1 r2 s& P/ n) x { ; M3 \( g% b6 | FGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment); ! B- q, `% f7 K5 I- L0 ?0 { S //Define micro here- _+ o% H" B/ T2 U/ G, t //OutEnvironment.SetDefine(TEXT("TEST_MICRO"), 1);" l( c( r; l& U1 s9 g2 h: V0 ^ } ; n% Z& j! y$ s/ P1 G/ U //----------------------------------------------------// $ W# H/ O% L; ~' o$ V6 K/ b# ^! n4 d , w9 X" @: X2 B2 o h void SetSurface(FRHICommandList& RHICmdList, ' ^4 P. p0 ^5 n0 C* _1 ^. @. u; ^, o FUnorderedAccessViewRHIRef& OutputUAV, w+ `, I) y/ T d$ v4 q8 n" t# S const FLinearColor ClearColor % I, b0 Y) L3 [& r- w! }6 E6 _ )) U% S1 W7 F/ x, B/ D { ) m0 J/ R+ z9 m/ i) Y$ z //set the UAV) g+ A( C1 c2 ^5 @ FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader();+ |5 o. w8 m2 d6 G* _ if (OutputBufferSurface.IsBound()): l7 t! ^" e2 w: q& | RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), OutputUAV);9 c% `. q7 H0 y" r) N1 { if (SurfaceClearColor.IsBound())8 H' r8 q9 }7 G# K5 v. Y, E, h //RHICmdList.SetShaderParameter(GetComputeShader(), SurfaceClearColor.GetBufferIndex(), SurfaceClearColor.GetBaseIndex(), SurfaceClearColor.GetNumBytes(), ClearColor); # g$ T8 i/ k* S0 H, ^. B3 c SetShaderValue(RHICmdList, GetComputeShader(), SurfaceClearColor, ClearColor); - R' K; f5 c( W, t }% m% ` l, }# N$ l + `5 D* s, J1 a- g2 a void SetOceanUniformBuffer(FRHICommandList& RHICmdList, const FOceanBasicStructData_GameThread& OceanStructData) + \# E1 M# y) D" z% P {! Z( E' S' X+ D FOceanBasicStructData UniformData; ) Z. C, l9 Y! F# w) w( v' h) R UniformData.OceanTime = OceanStructData.OceanTime_GameThread;" h- Q1 S4 M, \% T* e# I& D; {3 O SetUniformBufferParameterImmediate(RHICmdList, GetComputeShader(), GetUniformBufferParameter<FOceanBasicStructData>(), UniformData); - S- }) }2 `) `8 k. y! N } # W4 b3 J' l$ f4 C7 m u: i) f1 x/ o2 @1 b( ~( W1 \* K void UnBindBuffers(FRHICommandList& RHICmdList)& `8 R! ] t' l5 F3 }3 W, W {* y: Z& i9 y- W6 b4 k/ w FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader();% I* {( i5 N) _5 [* d1 ^6 p # ?' K# @: B( |6 V8 m% S if (OutputBufferSurface.IsBound())3 N9 U5 r7 T8 J% N. P; W% G RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), FUnorderedAccessViewRHIRef()); 0 b% G3 P3 @, ~/ c( K } 8 |1 V+ p8 L$ f7 K2 h( | ~ s7 a5 a% x5 q! h( r9 Y& Z virtual bool Serialize(FArchive& Ar) override: X8 Q& N! u% Y9 S { g% B1 `0 t, k4 ] bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);9 S5 C1 h4 j+ s ]- F+ Z. s" p //Serrilize something here" t y R2 B8 Q: |3 G9 `6 K4 I2 ^ Ar << OutputBufferSurface << SurfaceClearColor; ( d) v% Z" c9 X, | return bShaderHasOutdatedParameters;. I7 b/ N, D8 R3 L9 G, X5 ?. @ } / Y6 G8 z# ^: m; G, k N# o. ^ ' n0 l4 j4 r& D8 H private: & ]# l2 d9 D/ c- U; R* y" D! D8 t, Z% i FShaderResourceParameter OutputBufferSurface;% k; r. R8 s7 U W# Y FShaderParameter SurfaceClearColor;* E+ r7 j/ r1 Z1 W& P$ C };7 t* D0 o" ]3 ] ! {7 B' c% R8 M$ a IMPLEMENT_SHADER_TYPE(, FOceeanCSShader, TEXT("/Plugin/SDHOcean/Ocean.usf"), TEXT("OceanMainCS"), SF_Compute) & K5 k. ~+ m8 W1 p* Y1 _7 `; Y3 i7 \4 o: c/ l0 {9 K void UOceanRenderComponent::OceanCalculating_RenderThread , H% H0 n9 p, i8 C8 @ ( , V, c0 Z0 E4 L( A FRHICommandListImmediate& RHICmdList,# R9 \4 e5 t* P) i. m; _+ M% J$ @ ERHIFeatureLevel::Type FeatureLevel,$ t; K5 D! `! }$ N FRHITexture* OutputRenderTarget, # S5 v, w! u& w3 g% s- c int32 SurfaceSize,+ M: j4 C( z/ i; _/ X6 K3 S6 q' c const FOceanBasicStructData_GameThread& OceanUniformData * `/ ?9 i$ I0 W3 @& h- w! E# m+ ^ ) J2 V G9 X' u {. v' T9 t8 i4 r3 `, X! Z$ w check(IsInRenderingThread()); 2 T6 @) G) [$ l check(OutputRenderTarget);( Y: K9 j7 O# V0 q ~2 C $ ~4 y; C: P% T( i/ \ TShaderMapRef<FOceeanCSShader>OceanComputeShader(GetGlobalShaderMap(FeatureLevel)); " s' e7 C, k6 u RHICmdList.SetComputeShader(OceanComputeShader->GetComputeShader()); 7 J/ G; E0 e' A% h 7 p' T9 K8 H) F9 r; ]9 u% F if (OutputTexture.IsValid() == false)$ s. S3 G* Z4 f& Z( M9 g# P { & U& p' \1 M$ ^2 B. b if (OutputTexture.IsValid()) , w/ A K2 h. N7 {" ~9 H7 F6 P OutputTexture->Release();* W" R- c9 [4 u$ C1 \/ V if (OutputTextureUAV.IsValid())+ V, a H0 m) T6 l# X& C OutputTextureUAV->Release();. y' \4 m4 T( \* {8 @3 y" h" W: y 0 T' E7 ?1 E y! ^3 N. `& }3 C$ O% k FRHIResourceCreateInfo CreateInfo;0 @+ n6 N7 b$ m3 e/ s+ M" H# A( Q* ?5 { OutputTexture = RHICreateTexture2D(SurfaceSize, SurfaceSize, PF_FloatRGBA, 1, 1, TexCreate_ShaderResource | TexCreate_UAV, CreateInfo); 9 C3 a, q- O4 t5 Y4 ~4 c OutputTextureUAV = RHICreateUnorderedAccessView(OutputTexture); & n4 D( q- ]. ~* h; \$ |( h4 {2 w/ x$ o7 B } - Y/ X" [6 }8 y5 ]/ i! W # o3 ]0 i! j: d0 v OceanComputeShader->SetSurface(RHICmdList, OutputTextureUAV, FLinearColor(1,1,1,1)); % n* k6 Z0 v- x/ Y' c) ^0 T/ g OceanComputeShader->SetOceanUniformBuffer(RHICmdList ,OceanUniformData); 0 ^0 T! c* V, _) Z/ y0 ^8 b$ a3 \/ v5 f) ^5 N% h% r DispatchComputeShader(RHICmdList, *OceanComputeShader, SurfaceSize / 32, SurfaceSize / 32, 1); ) d B9 h6 \5 v& d OceanComputeShader->UnBindBuffers(RHICmdList);; U, i+ F. J) |2 M9 X7 | 9 N m9 u2 K" q. ]* }! F7 o RHICmdList.CopyToResolveTarget(OutputTexture, OutputRenderTarget, FResolveParams()); F0 `2 v& U3 m //FRHICopyTextureInfo copyinfo(SurfaceSize, SurfaceSize);$ H) z' `: Y- y //RHICmdList.CopyTexture(OutputTexture, OutputRenderTarget, copyinfo); ' ?5 X8 P! N! R. h5 ? } 8 F- o( N; y: c- H7 j: e6 E% H3 C _+ e UOceanRenderComponent::UOceanRenderComponent(const FObjectInitializer& ObjectInitializer)5 N/ K$ ^* ~. `' \. m$ q' {5 _ :Super(ObjectInitializer) 4 p$ D2 K$ w8 b! d" ]9 q {3 ~8 Y t/ {6 H: @* D I3 H! I PrimaryComponentTick.bCanEverTick = true; 7 ]0 X4 K. ?9 u7 x$ { bTickInEditor = true; " N( s) @3 U D N5 Q, a bAutoActivate = true;2 o. {3 S1 q$ N H" n |9 Q3 y! X RenderTargetFormat = RTF_RGBA32f; ' H7 W9 ?# C" Y j }% e n# @2 ~5 C0 k1 { 4 V9 m" g- z; t void UOceanRenderComponent::OnRegister()5 V6 e" S, ~6 M$ J' O {" A" y I3 s: N' w/ d/ r Super::OnRegister(); * H2 t7 B! w$ v& c3 ^' r2 Q } / G- {8 j4 P7 ~9 p: i% ] 3 S, x* n- R4 k void UOceanRenderComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)8 j9 q8 k2 M; j1 ~- z {) G8 T; D0 G( l) V8 B% g Super::TickComponent(DeltaTime, TickType, ThisTickFunction); - V+ c8 D5 Y7 l+ J# v. n- Q3 v ! P0 o. g3 O) L& u //Tick render the ocean 8 U& J1 r/ r! c. \! O3 ? OceanCalculating_GameThread(); ' J2 l/ l7 {: r# L, c' W3 P% q } d' o0 h. ^6 x: X& f7 U }0 B9 i* l1 i( e/ i: E- B2 s! D8 A' c8 W void UOceanRenderComponent::OceanCalculating_GameThread() # Y* ?9 X6 U8 }. _% ` {, s% L' H$ E& Z$ q( C UWorld* world = GetWorld();: W0 r& J% H8 x" y* J/ y0 w2 K ERHIFeatureLevel::Type FeatureLevel = world->Scene->GetFeatureLevel();$ ~; `3 M1 b$ z) O checkf(FeatureLevel == ERHIFeatureLevel::SM5, TEXT("Only surpport SM5"));( \! r- R1 W+ X# L! k E2 V2 J ) E6 t; m a& Z) _3 ^1 c9 I3 q% l if (OutputRenderTarget2D == nullptr) return;1 [$ h" R( v' l3 ` [" R ! S1 Y3 q; x1 \. u- x! x //Using front RT to render,back buffer store last frame imformation+ B [; ]/ U' c) R UKismetRenderingLibrary::ClearRenderTarget2D(world, OutputRenderTarget2D); 4 I! l5 N/ h' }3 d0 ]5 C, O0 K5 _+ {" q8 m% F FTextureReferenceRHIRef OutputRenderTargetTextureRHI = OutputRenderTarget2D->TextureReference.TextureReferenceRHI; 9 t/ v. W9 b1 `. U 2 z) |: I2 w( R6 X8 T0 {" A/ F checkf(OutputRenderTargetTextureRHI != nullptr, TEXT("Cant get render target %d texture")); ! V6 Q: C( Q5 K6 [" F1 w1 V 3 `& b; ?' Q3 V- b) m5 D& _ FRHITexture* RenderTargetTextureRef = OutputRenderTargetTextureRHI->GetTextureReference()->GetReferencedTexture();9 K# y: v5 O, w" S0 J! o TargetSize = OutputRenderTarget2D->SizeX;; R8 P+ B; X3 e) \/ _( n% f! v - f0 o, E" [( [, Z/ _ //Update the uniform buffer $ D! L k p1 S$ Q1 f( s( l5 j( m OceanUniformDataBuffer.OceanTime_GameThread.X = UGameplayStatics::GetRealTimeSeconds(GetWorld());- w H, r0 M( z& ]% a) ~5 E* I / F' I- x/ ~& [9 I! u' i6 W ENQUEUE_RENDER_COMMAND(OceanRenderCommand)" r, m( O3 c- w- }) O- d ( 4 [& D) u7 c" h [FeatureLevel, RenderTargetTextureRef, this](FRHICommandListImmediate& RHICmdList), p+ G% U( H+ s0 i/ R- R {! g) f7 v2 {7 t2 `4 T3 |! ^ OceanCalculating_RenderThread & z) }2 {! }5 L$ N (3 f% v9 n/ ~/ l( z& B# E+ J RHICmdList,* w: F8 ^! t) Z3 V J FeatureLevel," g, ?) p e1 j RenderTargetTextureRef, : c8 r' P6 K. P this->TargetSize,7 p2 E( ~( X2 N2 H# S7 E this->OceanUniformDataBuffer- |3 H/ f2 O6 i: N) i! o4 K3 E' W ); / }2 _) j5 ^. k( |7 }7 x9 Q/ x" O& M } 0 }) |0 |# y) m0 K% {3 P. u9 e );0 ^4 ~, a" _0 B* l$ T( A9 w1 X/ D } * |6 b" R, G4 H" Q5 `% N9 r5 A$ d: I! h+ K6 B8 V: z- v6 M #undef LOCTEXT_NAMESPACE
3 {6 S9 I/ L/ o2 S; G

Ocean.usf

) F0 b3 \! e# R& W w
#include "/Engine/Private/Common.ush" 0 C0 K" u6 r8 s4 f , u- E+ J$ \/ D6 r' m* ~1 j" l RWTexture2D<float4> OutputBufferSurface; 5 h; k" K: c: H$ b. A# f float4 SurfaceClearColor; % K0 n3 Z; o8 ]2 m3 j/ r 7 T3 t. @1 D1 N1 ~: h& B struct OceanUniform( i" B% ]9 y# k$ l3 d9 q {3 d& M7 m! h$ B4 y5 y5 k/ l float Time; . {1 G: b @" H8 j, ?! @ }; & A8 e, _+ B- g* C' K1 \ void InitOceanUniform(out OceanUniform uniformval). A* m8 N- B% `- g% j6 v8 k. n { ( e* I; v6 S/ z: G- T) Q5 I uniformval.Time = OceanBasicStructData.OceanTime.x;* I2 }( J: ]9 i; d } : |7 P$ n3 r6 \9 v1 M4 h . T+ m+ R" k2 l; I8 f9 [5 l! r6 ] [numthreads(32, 32, 1)]* S9 |/ C+ F' P0 d- @. } void OceanMainCS(uint3 ThreadId : SV_DispatchThreadID)8 j% q- c) f: `- ^ { 6 r, F2 S& e2 w( ~ y9 g; a //Set up some variables we are going to need " X. r$ H' Q5 B' C1 A% C //The size of outputsurface and input surface is same0 u) S0 K0 u4 Q Y. r9 O float sizeX, sizeY; : s# [- r' Z: j OutputBufferSurface.GetDimensions(sizeX, sizeY); ^: K; q* c' ]1 L! I( K! h5 Y0 x/ t # }1 @' s0 s) i8 V! t; L( X OceanUniform OceanUniformVal; + Y; ^0 t2 s6 N3 {3 r* o: G InitOceanUniform(OceanUniformVal);6 Z$ y- Z% [* [# y ) L" F6 k$ Q7 Z4 c9 [7 I N7 F float2 iResolution = float2(sizeX, sizeY);" `' _0 y: I7 I& `1 ?$ P float2 UV = (ThreadId.xy / iResolution.xy) * 50.0f; 7 N" Q4 Y8 v. J. [0 q* ?6 \+ l* v Y; r* z( ^5 k5 u float4 Output = float4(1.0f, 1.0f, 1.0f, 1.0f); 0 K1 P0 V" a" C! {' x# z a; w6 x; z, ]3 O$ C3 V! U! F9 G Output.xyz = float3(sin(UV.x + OceanUniformVal.Time), 0, 0); 3 j s/ g/ u5 y2 l. | 1 n# k% O4 H! i U OutputBufferSurface[ThreadId.xy] = Output; $ R! W6 v/ k% I }
- }; {) g) O! ^( I2 o0 R" F" e0 H* b

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

6 h" X. `- p2 i* {
, @. ] f5 R8 e$ F

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

! t0 K6 c; }' `# @ P( Y3 n

【2】Gerstner Wave

) T- y' S' W; _9 S$ ^/ `8 L J

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

( G$ v% V3 \" q3 U% {, p1 v
3 v- B% `% ~2 ]- P, Y) r

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

# P+ A' e! D0 ?9 b m& P% B% `

下面先来制作正玄波水面

+ \5 Z3 {2 E5 }2 t
6 }3 G8 d' v6 ~" n. [, f

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

& ?9 J+ M$ `( S% r2 L
7 y% K, ?6 h& r. e* c% m
7 P6 `9 O, F6 p1 D" J: g5 e1 }/ @
* R4 v/ b' h! E- m5 s8 o) o* T. {

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

: m5 P; C. X1 d9 {+ ~
1 J; C( q' Y2 e, A' w" a* T
' ^, `+ T$ A% E7 R' V+ l
y( ^- L8 n; m( c& z. K& C6 M
Output.z = 0.5 * pow((sin(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f); / F2 z% M4 k, K M$ Y Output.z += 0.2 * pow((sin(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f); ! B$ t) ^( V/ R& V# E Output.z += 0.15 * sin(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);: q! s( u8 r: k/ U- L. Z4 ] Output.z += 0.1 * sin(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f); 6 }' {$ Q$ T. h& s Output.z += 0.1 * sin(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);; Z9 e1 `& M) K) a 1 X, g0 P1 R7 q# K7 T% e7 k Output.y = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);9 _3 T6 G' k6 L. l% R' a( h# y" M Output.y += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f); * o( ?, H( ~$ m$ x( q0 | Output.y += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);+ J# r* z7 z' T1 m) c/ h, v+ x0 E Output.y += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);* J2 r" l, I' C Output.y += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);: _; F/ T8 r( O 0 S3 n; a: r9 u Output.z = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f); ' H8 n8 L7 q) e/ }1 d$ F4 x- Z4 C Output.z += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f); - u# j8 B+ e; i( Y8 M Output.z += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f); - x2 C% F6 ~) g3 l3 i Output.z += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f); " f6 Y0 U" ~8 S0 D# T! G Output.z += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);* g, v; B$ Z; W8 F0 ]& o # @4 s2 ?! |& T/ r. T- K OutputBufferSurface[ThreadId.xy] = Output;
* H- k$ R* g1 T) J! ?. F

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

3 u( N3 @+ O3 A: g8 w# C5 x H
2 z, N' u8 s- s
% g$ p4 q) i! H J: i+ ^) k K
& Z- _% Z; U, [( [* @

PositionOffset:

$ L- l5 k* E% B/ M
/ H1 W& [0 T: V0 ]5 L/ V1 {

Normal:

* v* ^3 }% w( b4 l
8 S, Y: h: \% [: o+ K

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

, z5 l; ?) {) p b
* }2 l5 m! [4 @7 V4 s* G6 x
8 z4 H; _( M8 j5 V" K3 U
L) k- L3 O, v! m

代码如下:

. X( `7 S/ q) b1 c
( t4 r- S5 ~4 B; B% e

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

9 L" ] f% `/ g" V1 d

【3】FFT海面理论推导

) \4 [& N4 |7 r7 b0 ~8 x" o% p

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

0 X# L* _# r9 J) h% j" R

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

/ p9 d0 d- |+ e9 Q% d1 @

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

2 i9 x- X; K: A4 X& |" ]

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

0 H! a V& L5 P, ~ u0 h

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

/ L' D: L) G4 j8 g

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

' n6 _/ J/ _/ |

所以 K 的范围是:

) P1 q& A$ H1 L9 A6 y) }9 t( V; b

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

; `' X$ p1 I- [$ U

The fft process generates the height field at discrete points

" @& T5 z. M4 w, {

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

( c' Q$ c0 B! Z5 j+ {
S2 e; ~: R$ |# s

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

6 x3 f& }. _' |8 n. i
\3 Y7 Q! Q' e7 s/ E

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

3 a; ]2 d) I8 t3 S3 Q
Y: z5 n2 D) A7 k. m
$ g6 j; g P2 K) I: b& A; W" |

会得到如下效果

- I: K% N; B& C7 l8 H
; ^ f" W; U- R: f

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

: K. F6 c8 s3 _- L. p
2 u2 W5 \' m* [ S2 R _

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

$ f, Z6 g1 a1 P
?) d- M, X) ?) y. E1 V/ t
% r" S) b7 @- H1 W. Z8 d7 W% l* X

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

. I6 _: ]2 j* p
1 z; W2 I6 ~/ F) B ?5 R# {1 g

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

) r0 j+ e8 I) Y6 C9 [6 F4 N

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

" _& U+ H$ Y5 m3 e" c

Enjoy it。

+ \& w9 i6 |* t3 L2 n7 U4 H @0 j

Next:

: k* ^ R) J/ E3 r' o$ C3 m

【参考资料】

# v4 B' V+ n0 f8 i7 j1 R5 n9 [

【1】Ocean Shader with Gerstner Waves

+ E- m/ Q3 `3 r* i. O

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

! g$ L: d2 W' z) @

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

; K) A* I' \! m4 U {

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

; q6 |! E3 z! j; d" w. T

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

% }+ f. v: P( Y/ w8 C- H% S, i. q1 {

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

$ i! ^- L. R9 S Z- Q% z7 {& x1 s

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

! k; N/ b5 |1 F

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

2 I/ M& F/ Z: I3 ^

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

% L* ^4 C' ^$ d9 p+ o/ _: E

【10】wubugui/Jerry-Tessendorf-2004

, V4 Q7 a. n. _& I2 p

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

+ X/ Q! R5 U7 m2 f/ c, b8 ?

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

" `& v `: N# v# |) h1 J0 T

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

; V5 _* F1 W8 y9 m

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

& Y/ v, A9 Z1 a+ p7 G+ ~

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

' R) W0 N7 Q1 h5 ?' \! G, e/ g" G

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

- E( F* [: y" n, Y" t, e4 q' i

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

. ^- B ^) n" l

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

7 h9 u" |& f: D8 P/ A8 y; @; q) }

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

8 }9 e, A2 C( a3 j2 L# _0 ~

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

5 {8 F3 P; z4 ]

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

% W: t( j! E" H+ q2 _; r ( D4 G# R0 ]# ^+ e 9 V! H' D5 M# |# z& R 0 u3 O2 V6 K3 d' x0 Y/ |0 S9 r2 A0 b3 k! o" C6 L
回复

举报 使用道具

相关帖子

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