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