/ V6 R+ z' [: g4 a 我的专栏目录: 1 z& P1 \3 b: n" M7 _4 K6 F( |
7 r7 K# f) ^: o% o+ f* l' s2 c
简介:
: W; X2 u7 G" m7 \ V 海洋这个要素在游戏里占比越来越多,开放世界的游戏要求角色能上天入地下水。目前游戏里做海洋的方法就那几种。(1)预烘焙法(2)Gerstner wave(3)FFT海洋。预烘焙法可以是实现烘焙好DisplacementMap或者是FFT的运算结果。Gerstner wave可以在GPU或者CPU上算。FFT的话就是拿海洋频率模型算出Displacement。
! |3 ?' H) _/ S% `: o: b. F 【1】基础环境搭建* [4 I) s7 y# _- I8 o
在开始研究之前我们需要先搭建起我们的环境。我选择在ComputeShader种完成各种计算,然后在顶点着色器种直接Sample前面的ComputeShader的波形计算结果。不要把波形的计算塞到VertexShader里。把波形计算独立出来还有个好处就是我能把波形的结果储存起来拿给其它效果使用,比如制作浮力部分的时候我们就需要知道海面波形的信息,如果塞VertexShader里就拿不到这些信息了。 , t7 `2 ]7 m* k- Z$ g2 h
搭建ComputeShader的方法前面我的文章有提到,这里我就直接贴代码了。使用的引擎版本是4.21.0,如果引擎更新了新版本可能代码有一点区别。FFT部分我会给出4.22的代码。 7 M9 w" Z. w5 [& f
如果不是在Unreal中实现或者不想做这么复杂,可以直接跳过这部分。
! K8 H# m! @! U3 A. t% i & |( ]8 @1 F- s9 s4 c
( [6 e; _% s/ L! M
SDHOcean.build.cs ! e; I- R/ @0 i, y$ F( d* o3 h/ j
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.3 S6 H0 m/ v: d) s
4 S. H/ l+ ]9 `* k6 Y& t using UnrealBuildTool;7 u) i! m8 O" r8 J$ p& y
* o6 {( Y% }+ q- L! y: H' x
public class SDHOcean : ModuleRules1 ~. U5 Q: v' Y- y# U& W
{7 _& i1 @' w- ]5 u5 M3 x6 W
public SDHOcean(ReadOnlyTargetRules Target) : base(Target)
, z6 Z ~4 M4 o6 g/ c/ F& e: I {' U- h- M2 g* e6 Q* v, v9 P" ^9 \0 B
PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs;1 I* d& Q& J2 n2 I9 X
0 k. Z2 A' o- | PublicIncludePaths.AddRange(
7 {6 c/ S8 t+ Z* { new string[] {* s1 S: i. p5 m* v& u9 m- X
// ... add public include paths required here ...
% I0 K8 x3 M. b$ g8 t }
9 F! p+ Z0 V5 z( f );
2 _$ b$ Z. Z* P G2 B1 C
, i2 [$ q& }- h9 \4 J0 `% }$ p) E, \
- n8 \0 m+ b) z( Z4 C1 U# X PrivateIncludePaths.AddRange(3 {7 C: w. e: k/ g$ S' g
new string[] {9 g/ ]7 k% W% Q" @ ^. l5 X. Q
// ... add other private include paths required here ...
2 V7 Y; h) V" M9 m4 m9 ?/ ` }) w' P6 ^3 d' }) y/ I; r: h
);
# ]: N3 v- @; \3 N, W) |( r4 A. ]4 Z( t! T0 \. ?" S
5 ^7 P, o7 r, z" T3 V PublicDependencyModuleNames.AddRange(
0 ?% h- ^/ F$ I2 ~# V; \/ T' Y new string[]
; Y6 K3 O% s; M {
$ M6 D G3 `1 l5 D& ?% U "Core",
8 @ E' @: l' h2 r3 B "CoreUObject",
! F3 \# a2 w( C# V; x+ _ "Engine",. \" p [- I5 f. }1 A, M0 s
"RHI",9 F0 n! [2 m8 z6 ~
"Engine",
6 }7 ?( _! L, C+ W8 D "RenderCore",; w( @7 I! o9 j3 H7 e' T
"ShaderCore",
3 I) @2 k, e2 g* l0 I, u // ... add other public dependencies that you statically link with here ...( l+ f- D: L. n
}
, U- E: V. s# L* ? );
1 E& n1 \! \% d; u& j( T( q. t8 Y2 W/ {+ P, f
6 @) c0 t+ e& M, j PrivateDependencyModuleNames.AddRange(- B* z4 J/ j) u* L+ U. G
new string[]7 ~6 b( _7 y5 I8 p9 R
{0 j/ G& V/ Y. \% ^ {5 k
"CoreUObject",) r# d/ U( G" h+ Q/ \& G7 R! h
"Engine",2 p' i, A* f) y+ i, ?9 B( N6 ]# a
"Slate",
7 ?8 E9 a; Q6 F; ~- y "SlateCore",
0 b' W0 C) E, C9 C5 ?$ v% t+ ?1 l "UnrealEd",) {( e! o* L: S2 n2 U; i# a
"Projects",1 h* K3 j0 h, I$ |4 e
// ... add private dependencies that you statically link with here ..." H+ @; g" i7 t: _, K1 e
}1 L; \: R9 H& N, \# k) @0 @
); K. M z; m: X8 h* ^- z: g- h
0 a0 U. I1 r4 ~& }6 a; x9 R( O! @7 A7 O( I- u$ n5 i
DynamicallyLoadedModuleNames.AddRange(2 B! m5 j$ f/ x( T
new string[]! R7 t; f) ~# X9 ~4 S0 U
{
0 R. W9 h. b- k" U% Q // ... add any modules that your module loads dynamically here ...& I5 d/ j/ e, p# r+ V
}
7 K# O( V$ Y4 g9 o6 a; ?' u );
( |: S7 l# d0 Y }
% [; R. }3 g% o% `3 S1 ?% F }
5 G9 _! W3 V2 D' ~; J SDHOcean.h [% t$ `$ U3 \% t# H
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.6 B4 n7 s/ }" k' C. l4 Z# \
0 v9 ?# |! d1 @. ?- o0 b
#pragma once/ j4 a) [9 r7 m/ d" I: l4 n' a
, x0 @# {0 b: H
#include "CoreMinimal.h"
6 K7 G( p- I* R4 E #include "Modules/ModuleManager.h"; e7 x9 n: N. {# B+ s
#include "Interfaces/IPluginManager.h"
6 G" ]+ w5 r* {! j) U3 d #include "Misc/Paths.h"6 t& l; l9 E+ l+ [# `
#include "Modules/ModuleManager.h"5 w( ?) X7 s7 S" |! r: E
, b6 s8 x9 @0 B( E; _1 x5 e7 W! t class FSDHOceanModule : public IModuleInterface
0 F" l" J. S7 X, {4 p1 S {
, \. w1 J. k' @4 d public:% P3 o: _6 J6 |6 t& ?1 r# L8 G
4 O0 l5 B4 J# F /** IModuleInterface implementation */+ |* E. c g* ^, y- i ?2 Q
virtual void StartupModule() override;6 j, E* `7 p1 I; U% A3 x- Z9 f
virtual void ShutdownModule() override;' A( p+ R$ v. d3 X
}; : g8 _8 b: R1 u, S% n! K- H! W
SDHOcean.cpp
' f Z3 q8 o2 W/ M5 n6 k // Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.# p/ g' t3 p, H; R# o
* b: B5 Q3 F% P" f8 ]
#include "SDHOcean.h"
/ m* u; q8 t' W: u: Z7 l4 \9 P* l; J7 u# P/ Q1 J9 m2 s
#define LOCTEXT_NAMESPACE "FSDHOceanModule"/ Z% F" P$ P2 `$ m i0 U) g8 s
; p& I: J7 e# B" O8 J' z" \# ]
void FSDHOceanModule::StartupModule()
" H: H2 M' }& I {
0 ?& L( R; Y" U2 ?) t // This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module
0 ^; u: X. o$ M# M1 ^1 C. \ FString PluginShaderDir = FPaths::Combine(IPluginManager::Get().FindPlugin(TEXT("SDHOcean"))->GetBaseDir(), TEXT("Shaders"));! F! T$ L. M) X. J
AddShaderSourceDirectoryMapping(TEXT("/Plugin/SDHOcean"), PluginShaderDir);
' f5 J+ k X' g }
' k4 l1 a5 ` F6 Z7 O
# V g$ y5 J' M' x8 ^( s& |' b void FSDHOceanModule::ShutdownModule()
6 ?8 S+ P8 L. w9 b* \" Z! R3 z {
\4 b, i8 g' g- v // This function may be called during shutdown to clean up your module. For modules that support dynamic reloading,
% S; w* D6 t: K; M3 f' a3 v // we call this function before unloading the module.
/ E$ a) U1 }" \7 `, `, g" s }, O6 u; v T# g( l
$ [8 U0 _# W1 w3 l
#undef LOCTEXT_NAMESPACE
' ], s, v! k$ F5 K' {
9 R$ g4 B# Q# B7 |. u3 {, C/ y$ n IMPLEMENT_MODULE(FSDHOceanModule, SDHOcean) $ T$ H0 r$ o8 O. U- \5 k
Ocean.h
" |% e& @/ J9 c. \ Z #pragma once3 r5 G" @4 n1 r
6 g9 w' o! E$ _4 F9 b #include "CoreMinimal.h"8 B# ]' v( s: V7 \
#include "UObject/ObjectMacros.h"& c" U4 u& J9 Q
#include "Runtime/Engine/Classes/Components/ActorComponent.h"
/ @) I& w5 a# t9 f: t* E7 W #include "Engine/Classes/Engine/TextureRenderTarget2D.h"# U$ l3 |6 F, F# G, C+ {
#include "Ocean.generated.h"% i' `& X D; D4 D) x! g
8 U9 H, u6 \/ R% M5 A
typedef TRefCountPtr<class FRHITexture2D> FTexture2DRHIRef;
1 L1 F0 ?- N, d$ A( S$ O8 \ typedef TRefCountPtr<class FRHIUnorderedAccessView> FUnorderedAccessViewRHIRef;
2 [4 r) T8 t& `7 D) W7 F8 v+ i8 M typedef TRefCountPtr<class FRHIStructuredBuffer> FStructuredBufferRHIRef;
& }3 {7 ^% d' G m: D( ` class FRHITexture;
& M: U# k2 ^! x class FRHIUnorderedAccessView;, H1 d/ O% D* K5 Z, z1 `4 D
class FRHICommandListImmediate; u% B7 Z9 k& E$ ~' j+ X
! J) n7 E7 b% l1 q USTRUCT(BlueprintType)
- Z# x) u" e2 S2 H3 } struct FOceanBasicStructData_GameThread7 R7 \' I! r1 g% b% B" V
{
1 f8 G7 T6 }& r) m4 ~! v GENERATED_USTRUCT_BODY()6 x. v' i @: H: A/ ?
+ N% ]0 A' I' U+ o4 A FOceanBasicStructData_GameThread(){}
1 O% ~# c8 P* o2 u
5 r% ^4 N' N6 o8 o L% C( E% K UPROPERTY(BlueprintReadWrite, EditAnywhere) s& e% J- s+ v: }4 ]& S) y
FVector4 OceanTime_GameThread;
, G+ T2 z. D1 q4 m9 J };! F3 Y1 \/ }& R/ U+ l, `: o
0 p A4 K, v& G H2 z3 ~" P UCLASS(hidecategories = (Object, LOD, Physics, Collision), editinlinenew, meta = (BlueprintSpawnableComponent), ClassGroup = Rendering, DisplayName = "OceanRenderComp")
9 H4 R. ^9 c- o9 V class SDHOCEAN_API UOceanRenderComponent : public UActorComponent
9 w* p+ J6 m/ X& ~ {
! s) U1 l, i' a8 r3 c6 t8 ^+ p* q7 u GENERATED_BODY()
$ h4 n; k/ s$ k: R3 P
+ \' }/ ?; ]- A1 r public:
% g X3 `/ F; I7 x9 G+ \) s, F$ J. M3 B& U
UOceanRenderComponent(const FObjectInitializer& ObjectInitializer);1 G- |* i- r3 r% j; E
//~ Begin UActorComponent Interface.' B* U4 U8 d* s
virtual void OnRegister() override;8 b4 T. Z7 D: q! E. d
virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
" p$ k# W5 p$ j' ]% r9 P2 I h( x) L9 e U
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent") f' ]) x1 S# m( A; `- ~$ f0 B
UTextureRenderTarget2D* OutputRenderTarget2D;5 n4 Q. p8 h" U5 a" ]% l( B0 y
% _- U8 v( w* O( M3 i" i
//UniformData for Ocean render
7 T' S2 h9 e2 a( G, W UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "OceanComponent")
7 E! v% N0 r: R3 V: p FOceanBasicStructData_GameThread OceanUniformDataBuffer;
5 Z# O. c! w- T" y( n" J9 F
/ s+ P! T; a3 G, V! Y; u0 m int32 TargetSize;4 K. H; \) Z+ z2 x, S/ d5 g9 Q
ETextureRenderTargetFormat RenderTargetFormat;
2 D8 E! B) j) T8 _# I) M! {& S
" z5 U/ U6 p: N3 H1 |+ l0 [2 P* I$ e private:
6 w1 T# f% @) y4 u- c Y
3 g/ {% e8 L( p$ \ //Render ocean render thread6 [# k2 N4 s: G, v2 d3 A( ~
void OceanCalculating_GameThread();4 K& H0 r1 s+ I/ `& R
void OceanCalculating_RenderThread
8 {3 ]0 B1 L t! r" ^1 d (
6 D' c1 s! O. A FRHICommandListImmediate& RHICmdList,
7 X% Y, n. m& {; o! T ERHIFeatureLevel::Type FeatureLevel,0 h4 M/ G4 K" Z, R/ ^) P) F
FRHITexture* OutputRenderTarget,
. Z0 r7 p( Z" k5 P9 V! o/ {. ^& m int32 SurfaceSize,9 g- f: O# G+ m9 {1 x9 ~
const FOceanBasicStructData_GameThread& OceanUniformData1 ?9 p" h# B S- @, u0 Q9 M: H
);
S6 B& z* V. c9 W) r6 e3 m
) l4 |, f$ p, t* C7 y" d; S3 z+ @ FTexture2DRHIRef OutputTexture;
6 J2 n l$ r9 N6 N: N u0 y FUnorderedAccessViewRHIRef OutputTextureUAV;# y4 r+ y+ B0 F) R3 J
};
3 O# e) s9 N7 c0 O6 K) \2 X ' l' N) z; E7 ~( x0 T
Ocean.cpp
$ d9 {4 }' o1 |' _0 f$ t #include "SDHOcean/Public/Ocean.h"( Y$ G G2 t1 S0 k
& i2 k) x# t8 ?
#include "ShaderCore/Public/GlobalShader.h": B/ Z4 n* V" k I& d
7 s: X/ H5 c5 Y! H0 y; m) h1 ~* t
#include "Classes/Engine/World.h"
' [7 Y: c! f P* c& r) w! T #include "Public/GlobalShader.h"0 t* ^% K! {% g' Z, P# M
#include "Public/PipelineStateCache.h"# K: j2 V4 j" C9 M4 ?5 h6 O
#include "Public/RHIStaticStates.h"
# H7 T' ^# L0 Y- c #include "Public/SceneUtils.h"
0 m8 l! a' b e) V) g6 ~ O! H #include "Public/SceneInterface.h"
! P* T; g( A9 G# [9 t #include "Public/ShaderParameterUtils.h"3 B$ B- }$ N; G; `- P3 v T
#include "Public/Logging/MessageLog.h"# E! {0 H# f5 m
#include "Public/Internationalization/Internationalization.h"9 C/ x! ?! R# T+ _$ }
#include "Public/StaticBoundShaderState.h"
( N, f' {* s( C' y# d #include "RHI/Public/RHICommandList.h"& t5 e; x1 B8 R) |) O, s Q) v
#include "RHI/Public/RHIResources.h"
R$ d7 A8 p+ E #include "Engine/Classes/Kismet/KismetRenderingLibrary.h"2 Q) B8 ^5 C" ?8 u8 I: Q6 C: P
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
! n' ^, w# B4 a6 V8 y2 v/ _7 M, A
5 f( U/ `! x ] #define LOCTEXT_NAMESPACE "SDHOcean"
8 A$ J* {* H) F5 b( B' }! t
8 Z7 ~6 R! q$ u$ z* i5 N7 _ BEGIN_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, )3 G. ~6 ~6 a2 X0 [! B9 f# n( q
UNIFORM_MEMBER(FVector4, OceanTime)( t, I C3 K- ]$ e/ ~/ y- C, h
END_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData)6 F6 Q7 Z K8 Z, R! x/ \& P% p
: F9 ?9 f/ R. ]6 F IMPLEMENT_UNIFORM_BUFFER_STRUCT(FOceanBasicStructData, TEXT("OceanBasicStructData"))
9 Y- i! X6 e7 s n! ~2 {, ?; _6 L4 a% n0 P
class FOceeanCSShader : public FGlobalShader P$ L$ L* B% K% z
{) _: H% Y# v/ D- C( N* \
" `, W$ g& `! |7 W1 j6 ~: V) Q* y DECLARE_SHADER_TYPE(FOceeanCSShader, Global)
5 [( R# i% j: S
* |( c7 C) e: q/ X' L& U$ }$ O: _ public:
, b3 j9 q J: w* P0 g5 X. T4 X+ [4 U' `
FOceeanCSShader() {}
6 w' d6 N& a6 Q( o7 [2 g3 @ FOceeanCSShader(const ShaderMetaType::CompiledShaderInitializerType& Initializer)
% d2 O3 H5 l6 y/ y% l0 s% t+ q0 h : FGlobalShader(Initializer)
! H8 W$ {* ^9 |3 R. D2 u# X5 E. `& G {, q: M2 s v1 M6 D+ p" `$ [/ f
//TODO Bind pramerter here% X& ~6 L" @8 ^# P: ^
OutputBufferSurface.Bind(Initializer.ParameterMap, TEXT("OutputBufferSurface"));8 ?9 }4 o2 Q) d5 A4 F
SurfaceClearColor.Bind(Initializer.ParameterMap, TEXT("SurfaceClearColor"));; _. l& [( l0 Y0 n/ _! |* y, J
}
4 n; }/ f+ d) h0 G" ~ //----------------------------------------------------//
3 A( o! D$ p4 c0 i& V8 x static bool ShouldCache(EShaderPlatform PlateForm)5 ?9 {* a' I8 x
{+ B" |. [" }/ l, u b- j
return IsFeatureLevelSupported(PlateForm, ERHIFeatureLevel::SM5);
0 T6 m0 ~& ]( ?0 }" s/ G }
7 `6 c4 q# @# {4 G# ~) f. w //----------------------------------------------------//5 q r0 w9 ]5 x! q& Y
static bool ShouldCompilePermutation(const FGlobalShaderPermutationParameters& Parameters)
. u) j- ]9 n$ t g& n {9 M0 d0 }7 e2 D/ H9 ?! \( n Q
return IsFeatureLevelSupported(Parameters.Platform, ERHIFeatureLevel::SM5);2 Q' S# x8 @- }" U) R: t
}/ i$ ^$ n3 @- x& A1 r
//----------------------------------------------------//
$ g- r) p* }2 \4 G6 n static void ModifyCompilationEnvironment(const FGlobalShaderPermutationParameters& Parameters, FShaderCompilerEnvironment& OutEnvironment)9 ]% ]/ v" ]0 E' ^% `2 a9 x
{ |2 L! n/ i* `9 X* E
FGlobalShader::ModifyCompilationEnvironment(Parameters, OutEnvironment);
# Q, y) b0 ~$ M4 T //Define micro here
" _) e1 i* p6 G) Y! f* d$ C* H+ {6 w$ p //OutEnvironment.SetDefine(TEXT("TEST_MICRO"), 1);
- d7 e- x: f5 ^( ? }% n S3 C9 U0 `( c M6 N7 _
//----------------------------------------------------//
/ L7 ]; d8 T+ l9 ^) e% i* q) l$ o9 B) F/ t
void SetSurface(FRHICommandList& RHICmdList,
- J% {5 _6 e6 z) z4 q! V FUnorderedAccessViewRHIRef& OutputUAV,3 {1 u6 J8 L# ~- g | g
const FLinearColor ClearColor
" ~0 {3 @' N! g; f0 Q7 @( f; v0 x )
; P& ~) p/ H+ v {& s* l7 e& S6 ]. F; H' Z7 ^8 G$ \
//set the UAV
& b# D) m8 u7 R/ C FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader();
F% F; H# K7 H5 b# S if (OutputBufferSurface.IsBound())5 [* W; j' ~7 X9 u9 s
RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), OutputUAV);
; d; w- f- N, h1 w( A if (SurfaceClearColor.IsBound())# T9 E2 u2 t$ g* l
//RHICmdList.SetShaderParameter(GetComputeShader(), SurfaceClearColor.GetBufferIndex(), SurfaceClearColor.GetBaseIndex(), SurfaceClearColor.GetNumBytes(), ClearColor);
' Y# Y9 Q& B5 \% S3 |' G SetShaderValue(RHICmdList, GetComputeShader(), SurfaceClearColor, ClearColor);" C/ ]2 V! `6 Z
}! Z, L) z/ A R- ?) }! U) h
$ a; i' ?: C; N4 i8 @
void SetOceanUniformBuffer(FRHICommandList& RHICmdList, const FOceanBasicStructData_GameThread& OceanStructData)
1 l( E0 F" }; h4 _6 H. e {
7 ?' B. [" _6 h# o8 b" ^ FOceanBasicStructData UniformData;! J" N6 X+ M7 s
UniformData.OceanTime = OceanStructData.OceanTime_GameThread;7 ?7 ~+ i1 O# P$ A* _
SetUniformBufferParameterImmediate(RHICmdList, GetComputeShader(), GetUniformBufferParameter<FOceanBasicStructData>(), UniformData);
4 m4 r1 B; `$ B. U' |! V, W8 M) Z }7 v. C8 Y: v* \+ O7 W8 G
8 n. n0 q1 ]3 x7 L: T* R/ S2 X% u* | void UnBindBuffers(FRHICommandList& RHICmdList)" B# e# I% G. N: F2 ^
{
, R0 [7 x9 Z2 M [# _& X' i, \" y FComputeShaderRHIParamRef ComputeShaderRHI = GetComputeShader();
6 q; _7 y8 n' }1 l
/ {5 ]2 G- D' R9 Y1 \6 m, U. D: X if (OutputBufferSurface.IsBound()). Z- `) t! L+ n- A0 Z# r- \5 p
RHICmdList.SetUAVParameter(ComputeShaderRHI, OutputBufferSurface.GetBaseIndex(), FUnorderedAccessViewRHIRef());
3 v. ]! s1 Y0 z ~5 h }2 U) ]$ ^+ T) G* C3 p5 V: U
! K% P/ W/ W! Q" I1 ?# E virtual bool Serialize(FArchive& Ar) override
2 r9 r! d. A3 [4 H7 ^2 c {
B( T$ P+ e* S* o$ Q$ r6 F bool bShaderHasOutdatedParameters = FGlobalShader::Serialize(Ar);: d8 D5 F9 \/ h
//Serrilize something here
1 o% W: a/ ?! M) k Ar << OutputBufferSurface << SurfaceClearColor;
" i$ p9 Q5 v; x+ z% V' R' U' T return bShaderHasOutdatedParameters;
' s( N3 j/ C; c! f, ^ }) n* Q0 b0 G+ O3 y
' E8 m$ I2 {! |* C1 P
private:
8 D2 L3 b- V$ C9 Z; I; X4 _' i8 Y
) B5 m$ ]. s1 z1 t# R: w, S) A4 u FShaderResourceParameter OutputBufferSurface;3 C5 o1 x- d" b& [; l
FShaderParameter SurfaceClearColor;
3 g" m) H8 A( |& R, j };
6 }- W# }: S T/ {2 p0 I" U) K+ |2 j
IMPLEMENT_SHADER_TYPE(, FOceeanCSShader, TEXT("/Plugin/SDHOcean/Ocean.usf"), TEXT("OceanMainCS"), SF_Compute). C5 t8 g/ z% K3 S5 Y1 K
8 a3 m' P4 u' R! P' m% A+ Q: R; H void UOceanRenderComponent::OceanCalculating_RenderThread
" C4 y2 v7 A- G4 M1 X (
2 N, U; s. i9 \9 X. P* I4 D FRHICommandListImmediate& RHICmdList,
* L7 ^3 Z6 x8 q3 ^ X2 g1 p ERHIFeatureLevel::Type FeatureLevel,
, O* k+ F: w+ U9 f, u FRHITexture* OutputRenderTarget,
( v* L* h- O: p: c3 H4 C3 r int32 SurfaceSize,
. t/ j N3 Z; V& t" k const FOceanBasicStructData_GameThread& OceanUniformData
2 z0 R) X$ q- d7 v- t )
3 N6 c) h3 d& s8 Z1 [ {
" a$ f d- ^6 B6 ^. {5 }9 T4 C check(IsInRenderingThread());/ b& s- {. |' |2 e1 [2 P" p# Y
check(OutputRenderTarget);
' I2 Z! s* h1 m* k. k* r, k0 t4 G* a. W+ K! m8 q( p
TShaderMapRef<FOceeanCSShader>OceanComputeShader(GetGlobalShaderMap(FeatureLevel));5 \9 w |$ G6 E) i7 ]' P; _4 Q
RHICmdList.SetComputeShader(OceanComputeShader->GetComputeShader());
$ x* d. E4 ]2 x- w/ d" O& R1 \. C
G: l; g9 {: M$ g* y if (OutputTexture.IsValid() == false)
$ F7 q3 @/ [! T& d( x, [9 Y9 S2 | {
4 ]. [$ ^; N/ b if (OutputTexture.IsValid())
+ [, T/ L, F- F OutputTexture->Release();
$ f" l( V5 f1 t5 F4 l5 p) b if (OutputTextureUAV.IsValid())
4 _4 Y! q' l5 F OutputTextureUAV->Release();( Z9 Q2 F/ j( C M; T
" J. E2 `/ E3 Q8 X
FRHIResourceCreateInfo CreateInfo;
: d; b) M/ ^% @4 H: ?/ f' d OutputTexture = RHICreateTexture2D(SurfaceSize, SurfaceSize, PF_FloatRGBA, 1, 1, TexCreate_ShaderResource | TexCreate_UAV, CreateInfo);( C# n/ L7 g; z. Q9 ]
OutputTextureUAV = RHICreateUnorderedAccessView(OutputTexture);7 `. e4 Z: y4 Q# G) C
/ d9 r2 k; Y7 j9 a! j
}4 c4 @7 H: I, \% `! X" y6 C
+ K! M+ ^9 z8 i! G
OceanComputeShader->SetSurface(RHICmdList, OutputTextureUAV, FLinearColor(1,1,1,1));( g/ d9 x W" V1 p
OceanComputeShader->SetOceanUniformBuffer(RHICmdList ,OceanUniformData);; w2 h& k# u c" f. q- G
1 b' m. Z/ `% Y2 ?7 L9 z" ]
DispatchComputeShader(RHICmdList, *OceanComputeShader, SurfaceSize / 32, SurfaceSize / 32, 1);
* }) a, L7 s; ^0 C* r P' L OceanComputeShader->UnBindBuffers(RHICmdList);' x8 L0 ?* \+ \: _, J7 ~5 z" I {
+ V8 K: @6 ?, I& D7 S$ G h/ j# D/ L RHICmdList.CopyToResolveTarget(OutputTexture, OutputRenderTarget, FResolveParams());
+ B4 f6 f' T3 i: p) V0 f! M% ~4 Z: N* J //FRHICopyTextureInfo copyinfo(SurfaceSize, SurfaceSize);; v) t- K/ `5 v4 A$ `& l+ c1 n8 N5 p. q
//RHICmdList.CopyTexture(OutputTexture, OutputRenderTarget, copyinfo);: i }/ W' |9 y( l
}# v0 f( r( J* V" J* {+ b( |
) k# {9 [9 ?; D
UOceanRenderComponent::UOceanRenderComponent(const FObjectInitializer& ObjectInitializer)
) y( j+ B* P: L( ~6 b* a" h$ H :Super(ObjectInitializer)9 s$ f1 z- ?4 ~6 ^9 N
{5 d/ E6 T. H6 c9 I1 d
PrimaryComponentTick.bCanEverTick = true;
* u* @* y3 M' L% F9 T* G Y bTickInEditor = true;
# t2 H/ l% u6 K7 I! {) A bAutoActivate = true;3 T) T' w$ Y. M: F8 h1 g! f4 K
2 o2 ~* E" e' D# ?& [
RenderTargetFormat = RTF_RGBA32f;
) l' ]6 Z, M" `$ q3 ^5 p }
' e# Y7 A% \. ~- I6 W6 Y' ?* n7 V) S& @' {
void UOceanRenderComponent::OnRegister()1 o) a/ y% h' b1 E
{( }- V9 w: I& z1 J1 \
Super::OnRegister();1 T# W5 n' E0 E+ y& U) \& i: P
}
. B# t. P7 j$ n& i: M1 l" H" V% k% o) b, f; U& G! c
void UOceanRenderComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction); d0 O) G, |9 K' W8 f
{* j' C7 j- M: \3 y
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
/ w' U {2 a- S9 r _! t9 O* w) S; A5 @
//Tick render the ocean Y" {( P/ I2 {- Y
OceanCalculating_GameThread();5 W1 N V% L" e# {# z" }% D) w* Q
}
. |3 V d$ G# E
4 V: d6 p+ |# L void UOceanRenderComponent::OceanCalculating_GameThread()
% D, }8 B$ i9 l {
, M0 h; O M( _5 J0 t UWorld* world = GetWorld();
2 t4 g6 m' W o/ f9 R$ k ERHIFeatureLevel::Type FeatureLevel = world->Scene->GetFeatureLevel();6 D4 {, w5 S* {6 d
checkf(FeatureLevel == ERHIFeatureLevel::SM5, TEXT("Only surpport SM5"));4 ?; S2 s+ a7 Q+ B' J# L4 ?
1 \& S: e$ K0 N) @' u
if (OutputRenderTarget2D == nullptr) return;, T' L. X3 K4 h% M
3 L( E+ _" @) Y8 U5 H* d- `7 r3 }
//Using front RT to render,back buffer store last frame imformation
7 f2 X6 P9 [" z C UKismetRenderingLibrary::ClearRenderTarget2D(world, OutputRenderTarget2D);$ x) C2 m' V/ w( Q5 Q, [! h+ q
) Q8 L& k) U5 K2 L/ q FTextureReferenceRHIRef OutputRenderTargetTextureRHI = OutputRenderTarget2D->TextureReference.TextureReferenceRHI;" l& E; j1 g/ Y4 L0 |. _! u, W) u8 p
! Q' W& Y# L% b
checkf(OutputRenderTargetTextureRHI != nullptr, TEXT("Cant get render target %d texture"));
1 ~- f9 i; ~6 X/ g& t3 V( I- V" |2 P! W/ Z4 Q4 B' E" Y T
FRHITexture* RenderTargetTextureRef = OutputRenderTargetTextureRHI->GetTextureReference()->GetReferencedTexture();
; ^8 x( f5 X4 V" i' P5 @ TargetSize = OutputRenderTarget2D->SizeX;+ o& I" C) H6 | C
2 V5 z' i* _3 k8 B/ [, s: R //Update the uniform buffer+ I( p5 E: d* M* b( K; S. k
OceanUniformDataBuffer.OceanTime_GameThread.X = UGameplayStatics::GetRealTimeSeconds(GetWorld());* G& E% }- r1 U# n w; N$ {
4 {1 n9 W6 k, r& Q1 A ENQUEUE_RENDER_COMMAND(OceanRenderCommand)( q. E$ r+ M# s7 `. G
(
# B5 ~3 E- E; a- K# q c [FeatureLevel, RenderTargetTextureRef, this](FRHICommandListImmediate& RHICmdList): |* n/ x5 I! g+ y1 u7 o3 X* A
{
# @$ c) ?6 I) t; X2 ^ OceanCalculating_RenderThread
6 c1 H! m( d- U+ h: ` (
+ V/ [0 q6 q+ W" u% D, G RHICmdList,3 l/ M- U- ^9 I3 S
FeatureLevel,' g% c9 A) m! _$ z5 u S. e4 f+ ?
RenderTargetTextureRef,
# _* B1 F, F2 _* u% h6 Y9 W this->TargetSize,3 |* j1 _! H! J J' Q6 s
this->OceanUniformDataBuffer
# w& s" Q* R3 U );
: v R7 L" S3 Z. j! E7 X) u' R }; f/ S; R. X; X' _( ?
);$ q4 m: f' R5 J
}
5 K" a; A! }. S6 L: O2 r. a! v: B3 Z3 T* V& c& h; A! W
#undef LOCTEXT_NAMESPACE
3 I5 M% [" k7 f8 J! }' k, h S4 b5 _ Ocean.usf & l) W0 h% k E) G" X
#include "/Engine/Private/Common.ush"! d+ u% U) p* n+ l
1 l7 D0 r* x# c: k
RWTexture2D<float4> OutputBufferSurface;
) r9 S: P$ j: B: K1 w2 u0 @ float4 SurfaceClearColor;2 a% i5 t1 R0 W/ J
# ~: ~. I8 I8 v- F8 X) |( Y) m" U struct OceanUniform. M0 S8 F W9 F, F8 P
{
7 z( B0 |, M) n, b float Time;$ Q {1 Z6 N W8 L& ]
};$ X. q7 @1 _: |, ]' \
void InitOceanUniform(out OceanUniform uniformval)6 J" s+ N) d6 v/ V0 Q8 C8 E; j
{
' G& i! D! V; I' Q uniformval.Time = OceanBasicStructData.OceanTime.x;: s1 {' Z; |* v# G
}
2 m" d$ _2 g! B8 V6 j* T' \/ {/ S0 ]4 |) C; `) e$ v& h& `: J
[numthreads(32, 32, 1)]
7 B$ \! t7 k6 r void OceanMainCS(uint3 ThreadId : SV_DispatchThreadID)
' e- r) b( [1 i3 M# l1 |1 ^- V {
. l5 s9 y, M% x; P //Set up some variables we are going to need( [1 K" S) g- h0 y. `! y% g& f
//The size of outputsurface and input surface is same
6 C% }6 M1 A5 X. B0 y0 [ float sizeX, sizeY;4 z& n4 B! C' ]. L \; F% Z7 F2 q
OutputBufferSurface.GetDimensions(sizeX, sizeY);
' Y0 b1 G2 r. F& v
2 X6 y H8 y' i OceanUniform OceanUniformVal;
! a8 o6 k7 T2 k* E; S- F0 X InitOceanUniform(OceanUniformVal);' ~" N3 k8 m* w7 S F' F
( o! v* i' `' U2 V/ R float2 iResolution = float2(sizeX, sizeY);3 t S; Y3 m& p. n ]/ r" C
float2 UV = (ThreadId.xy / iResolution.xy) * 50.0f;
Z. R: B7 m% O5 L, J9 M1 r7 B
}6 P3 S( a9 D" A! V4 O float4 Output = float4(1.0f, 1.0f, 1.0f, 1.0f);
, ]" ?) v: P% A0 m% Q6 {
+ U' u' b9 Y! X5 r. @4 r7 w% k Output.xyz = float3(sin(UV.x + OceanUniformVal.Time), 0, 0);1 k& g/ K- W$ {+ F1 e" C. J
0 n( W* m/ k n- Q/ y* L; A( C
OutputBufferSurface[ThreadId.xy] = Output;
0 d! j; J* {( u& Q3 }* u4 V } 2 k" ]9 T1 R7 h% y& R
我这里做了个UniformBuffer然后拿到系统的时间,把系统的时间变量塞到我的Compute shader中。 ! l* r1 [& u$ H
2 H4 z2 `; [( |' I& O# u' p 直接把值采出来连到VertexPositionOffset上就可以把我们的ComputeShader的结果传到顶点着色器了。 4 `, ~) n' }# ^4 O" S6 L; v
【2】Gerstner Wave; e$ d7 M% Z, N! J% g! {
在Gerstner Waves之前,先使用正玄波变形的方法模拟。对正玄波进行变形属于经验性的方法,把正弦波变形让它的形状更接近水浪。 $ x6 Y: d$ a' n. q% _! @9 b3 W
1 e+ Q; u9 e3 s* O F F(x)=2(\frac{sin(x) + 1}{2})^{k}
( W. K9 j. h I1 k4 q 下面先来制作正玄波水面
; P4 ? @! O5 H5 c
8 X; Z- J% l! ]/ |1 U& m6 Z 于是乎我们可以得到如下效果 8 q* U$ b2 l1 Z8 U* _# v
6 m, @0 r3 V" h/ m! u4 Y4 u& G: Q7 \
3 {) ^* Q3 H' j* b
\2 \ L1 t6 X8 G. L
有了基础的波形后,剩下就是让海面变化更丰富。想让海面变丰富那就是多叠几层波
7 J$ S/ T, m4 N! Q- |9 ^# b7 y& t
2 I# C) O- U, Y1 P+ s s! p$ Q2 [
, j, \! M) E. n1 s$ }$ ^
8 b" _# v- G. k Output.z = 0.5 * pow((sin(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);
* b/ x1 |# S# r3 q( q- q Output.z += 0.2 * pow((sin(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f);8 _) J( H+ W0 S! @0 }$ k1 x/ {
Output.z += 0.15 * sin(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);
$ [3 z1 E3 I8 ?# C" r. J Output.z += 0.1 * sin(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);
) H% [1 S0 N5 I Output.z += 0.1 * sin(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);! d9 c, A5 s! U8 a& j& q' L& {
9 u4 R0 b+ S& B3 |* z0 ]
Output.y = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);* t o6 p6 q+ ]1 y4 o4 ]
Output.y += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f);
$ h& }# k# ]% N3 O" b Output.y += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);
0 ]/ W, t- l, d5 l2 F, P; W Output.y += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);( m, r/ h2 `6 i" `" |
Output.y += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);
& p* G3 {! u- K4 T( ~* x6 B( x& I- V: a0 u( x
Output.z = 0.5 * pow((cos(WaveLength * Speed + dot(direction, UV * 0.8) * WaveLength) + 1) / 2, 2.5f);
0 N6 Q- L& D& j Output.z += 0.2 * pow((cos(WaveLength * Speed * 0.8f + dot(float2(0.8, 0.1), UV * 0.9) * WaveLength) + 1) / 2, 2.5f); \+ n. [: k& G# ^7 ], G- S
Output.z += 0.15 * cos(WaveLength * Speed * 1.2f + dot(float2(-0.8, -0.1), UV) * WaveLength * 1.3f);9 E! Z6 y6 n1 h: ^( D9 e
Output.z += 0.1 * cos(WaveLength * Speed * 1.2f + dot(float2(0.6, -0.5), UV) * WaveLength * 1.5f);
3 Z2 d' z! u0 ~* g/ {: f. _- p8 C" q Output.z += 0.1 * cos(WaveLength * Speed * 0.5f + dot(float2(0.5, -0.1), UV) * WaveLength * 1.5f);
9 o$ Q; S5 H1 ^4 q8 R
2 u z8 `( o! o* {# x# Q) h+ O8 T OutputBufferSurface[ThreadId.xy] = Output;
, l+ u/ I. c: q, [2 W. J 可以看到正玄波水面波浪比较平,无法模拟出水波的波峰陡峭的特点。因此我们需要使用新的模拟模型:Gerstner Wave。
4 }9 |2 D$ _7 I J3 D% I 6 a4 J7 c9 o9 o! K' ?$ M
* c B, m: H7 ~' @/ b
1 a4 I9 T: B5 R. ^ ]1 _ PositionOffset: : y3 Q8 I* b* y9 n7 c
4 T5 O6 {& J+ ?5 N* |% m
Normal: 8 t' w" Y7 I, S, s9 f
I8 Q* V' M* c! K' ^
Gerstner Wave是周期重力波欧拉方程的解, 其描述的是拥有无限深度且不可压缩的流体表面的波形 。
) p; U. I( ^: P
" `* H, m' X2 H k( c( M
7 T6 ^/ M$ z$ I7 |: r \ # i% k* P) V; K+ [7 i& `
代码如下: % f0 A* }, c+ f/ S/ s
% [8 z3 Z7 K' T$ ]9 u$ d 想要更好的效果可以优化下参数和多叠几层波,反正Computeshader里算这种东西很快的啦。 1 p5 H6 F2 a1 \" ^3 G
【3】FFT海面理论推导
' j9 s0 W1 P2 o FFT海面的核心思路是我们通过一系列测量得到真实海面的波的频率然后把这些频率通过FFT变换到时域然后计算出置换贴图。下面来公式推导
% B2 }2 _+ W; t' y2 E0 Q 设波的高度为水平方向的位置和时间的关系 h(X,t) 。在水平方向上 X= (x,z) 根据Tessendorf J.2001的论文我们可以得到如下公式
5 ]5 A4 ~; w( m h(X,t)=\sum_{K}^{}{x}\tilde{h}(K,t)e^{iK\cdot X}
# v/ j5 l: n; _( F+ |5 R$ D8 J4 h 其中 K 代表波正在运动的二维水平方向, K = (k_{x}, K_{y}) 。 & d7 a; Y+ j! |: o) V, O4 j
k_{x} = 2\pi/L_{x} , k_{y} = 2\pi m/L_{y} 。 & D3 Q1 h, c- J' t
n 是水平方向的Domain Resolution。 m 是竖直方向的Domain Resolution 5 \4 p3 |0 y6 l* l8 ?
所以 K 的范围是: 9 ?6 ?9 E- A5 q! Y
K=(2\pi/L_{x},2\pi m/L_{y}) # g: [( t# s9 U4 r3 O
The fft process generates the height field at discrete points . `4 c- N: ?& `
X = (nL_{x}/N, mL_{z}/N) 0 g3 _; [$ m! h/ r( K2 i$ w
- p8 a* Y7 i8 h+ j- k
按照上面的公式可以渲染得到下面一张图: ) Z0 y% _2 L* I# z1 |
+ \. F, [$ F3 z9 q6 t1 i3 k (2)然后需要用高斯分布生成一个Phillips spectrum $ Q3 |6 A8 W. Y! m. B( ~" v0 C8 [. j
, Y6 R& v5 D2 Z' c& F$ Q# Z
0 x; T; O8 \7 {, G& J. |! A
会得到如下效果 ) J; _( Z8 A3 s$ J
% F9 E6 }! y9 q' C, d
把它和gauss分布结合后得到如下效果: 7 a) ?( K1 y; j! ?( f
: j$ Z2 q! q2 ]" W* L
然后现在我们有了海面的频谱图。下一步需要进行IFFT变换,但是在做变换前我们需要一个OmegaTexture做蝶形变换
' V6 w4 C" w4 [% y # W1 |8 ]; f9 J+ v6 y
. m3 I. U' q: W1 q, g/ o% U* H& X
(3)然后做IFFT变换生成Displacement,然后生成高度图和normal : v+ H' p' g, q$ c: z' m; R
% x/ L, z4 T* a& Z) b. a. V; {7 `7 x
最后把这些生成的图弄到渲染管线种作为渲染资源渲染海面即可。下面就根据这上面的步骤来生成我们的海面。 # G5 @& D2 `3 s" J4 O9 c0 h
下一卷我将给出具体FFT实现。
% j# w8 B5 Z8 Z' U& z Enjoy it。 + a0 B4 Y. O% ~- z! R% }
Next:2 N4 E ?3 s6 }" u/ E, w8 B' J. M
【参考资料】 $ P6 x. W2 o+ v7 Y& k8 K+ I' ~
【1】Ocean Shader with Gerstner Waves 4 L, N$ O8 B6 h# S0 d& \
【2】音速键盘猫:Shader相册第6期 --- 实时水面模拟与渲染(一)
7 k' ?. X" u2 o1 y 【3】https://labs.karmaninteractive.com/ocean-simulation-pt-1-introduction-df134a47150
& o: v& p. l( T3 I: B 【4】Ocean simulation part one: using the discrete Fourier transform
% w w! S }) q3 i$ u 【5】Ocean simulation part two: using the fast Fourier transform
# U5 h9 m4 F. ^( n* A; a8 | 【6】https://www.slideshare.net/Codemotion/an-introduction-to-realistic-ocean-rendering-through-fft-fabio-suriano-codemotion-rome-2017
- x2 M8 k( h$ m# M: S: G9 h( R 【7】海洋模擬 FFT算法實現--基於GPU的基2快速傅里葉變換 2維FFT算法實現--基於GPU的基2快速二維傅里葉變換 【pbrt】使用openFrameworks調用pbrt
/ ^* N+ n4 _3 D" ?: w/ r 【8】白霂凡:一小时学会快速傅里叶变换(Fast Fourier Transform) + K+ v' S1 }% G, }2 [) l
【9】海面模拟以及渲染(计算着色器、FFT、Reflection Matrix) 6 i" k8 O& N' ^4 H7 U% U
【10】wubugui/Jerry-Tessendorf-2004 + ]% ^- B9 F- e2 B. l& {) Q
【11】http://evasion.imag.fr/~Fabrice.Neyret/images/fluids-nuages/waves/Jonathan/articlesCG/waterslides2001.pdf 8 T% E6 ~5 M" X) F: m6 O1 m
【12】https://dsqiu.iteye.com/blog/1636299 ( [; M4 F) F0 N& O, t9 M7 p: R5 G
【13】https://www.bilibili.com/video/av19141078?t=1053
O0 \" l3 e0 w 傅里叶变换基础教程(如果对傅里叶变换完全不清楚的建议按顺序看完下面的链接) - v& i* a c; o* U
【14】Heinrich:傅里叶分析之掐死教程(完整版)更新于2014.06.06
4 L, g! H% c9 N: |! `8 x# Z- S* R 【15】https://www.bilibili.com/video/av34364399/?spm_id_from=333.788.videocard.0
9 d# X2 L* \; _* b; v4 L 【16】https://www.bilibili.com/video/av34556069/?spm_id_from=333.788.videocard.0 5 m( e! N5 m) }0 V& O7 e4 x I' y: A
【17】https://www.bilibili.com/video/av34845617/?spm_id_from=333.788.videocard.0 ! |# _. _* f+ q# q
【18】https://www.bilibili.com/video/av35047004/?spm_id_from=333.788.videocard.0 $ ^ x3 V. S. d; R$ o" ^
【19】https://www.bilibili.com/video/av35810587/?spm_id_from=333.788.videocard.0
8 c. X6 h+ D" [* N( ^$ D' z# V 【20】https://www.bilibili.com/video/av36343956/?spm_id_from=333.788.videocard.0
$ o9 T% u$ \( c& `2 r' `3 R- |4 Y; N# ^' D7 o1 _1 ?7 N
! V* T; e( g9 ~" g( @& \; N
' h" s5 X, R0 m. u: E* ~
- T: @# I. V/ s8 e0 r |