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