forked from MergHQ/CRYENGINE
/
Water.cfx
1229 lines (954 loc) · 38.6 KB
/
Water.cfx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2001-2019 Crytek GmbH / Crytek Group. All rights reserved.
#define VS_NO_SKINNING_DATA
// dx11 obfuscated this quite further, clean up for refactor phase
#include "Common.cfi"
#include "ModificatorVT.cfi"
#include "ShadeLib.cfi"
#include "PostEffectsLib.cfi"
#include "WaterCommon.cfi"
float Script : STANDARDSGLOBAL
<
string Script =
"Public;"
"NoPreview;"
"ShaderDrawType = General;"
"ShaderType = Water;"
"PreprType = ScanWater;"
#if %WATER_TESSELLATION_DX11
"HWTessellation;"
#endif
>;
//////////////////////////////////////////////////////////////////////////
// TODO: remove after old graphics pipeline is removed.
// Fog parameters
// For ocean comes from render element
float4 cOceanFogColorDensity = { PB_FromRE[8], PB_FromRE[9], PB_FromRE[10], PB_FromRE[11] };
//////////////////////////////////////////////////////////////////////////
// Tweakables /////////////////
float SoftIntersectionFactor
<
register = REG_PM_PARAM_0.x;
string UIName = "Soft intersection factor";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.1;
> = 1.0;
#if %FAKE_CAMERA_MOVEMENT
float FakeCameraSpeed
<
register = REG_PM_PARAM_0.y;
string UIName = "Fake camera speed";
string UIWidget = "slider";
float UIMin = -10.0;
float UIMax = 10.0;
float UIStep = 0.001;
> = 0.0;
#endif
float Tilling
<
register = REG_PM_PARAM_0.z;
string UIHelp = "Set bump tilling";
string UIName = "Tilling";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 32.0;
float UIStep = 0.001;
> = 10.0;
float DetailTilling
<
register = REG_PM_PARAM_0.w;
string UIHelp = "Set detail bump tilling";
string UIName = "Detail Tilling";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 32.0;
float UIStep = 0.001;;
> = 2.5;
float RainTilling
<
register = REG_PM_PARAM_1.x;
string UIName = "Rain ripples tilling";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.001;
> = 1.0;
half FresnelGloss
<
register = REG_PM_PARAM_1.y;
string UIName = "Fresnel gloss";
string UIWidget = "slider";
float UIMin = 0;
float UIMax = 1.0;
float UIStep = 0.001;
> = 0.9;
half ReflectionScale
<
register = REG_PM_PARAM_1.z;
string UIWidget = "slider";
string UIName = "Reflection scale";
float UIMin = 0.0;
float UIMax = 32.0;
float UIStep = 0.001;
> = 1.0;
half SubSurfaceScatteringScale
<
register = REG_PM_PARAM_1.w;
string UIName = "SSS scale";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.1;
> = 2.0;
half ReflectionBumpScale
<
register = REG_PM_PARAM_2.x;
string UIWidget = "slider";
string UIName = "Reflection bump scale";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.001;
> = 0.1;
half RefractionBumpScale
<
register = REG_PM_PARAM_2.y;
string UIWidget = "slider";
string UIName = "Refraction bump scale";
float UIMin = 0.0;
float UIMax = 1.0;
float UIStep = 0.001;
> = 0.1;
half DetailNormalsScale
<
register = REG_PM_PARAM_2.z;
string UIWidget = "slider";
string UIName = "Detail Normals scale";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.001;
> = 0.5;
half NormalsScale
<
register = REG_PM_PARAM_2.w;
string UIWidget = "slider";
string UIName = "Normals scale";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.001;
> = 1.25;
half GradientScale
<
register = REG_PM_PARAM_3.x;
string UIWidget = "slider";
string UIName = "Gradient scale";
float UIMin = 0.0;
float UIMax = 0.15;
float UIStep = 0.001;
> = 0.1;
half HeightScale
<
register = REG_PM_PARAM_3.y;
string UIWidget = "slider";
string UIName = "Height scale";
float UIMin = 0.0;
float UIMax = 0.25;
float UIStep = 0.001;
> = 0.2;
#if %FOAM
half FoamSoftIntersectionFactor
<
register = REG_PM_PARAM_3.z;
string UIName = "Foam soft intersection";
string UIWidget = "slider";
float UIMin = 0.35;
float UIMax = 10.0;
float UIStep = 0.1;
> = 0.75;
half FoamAmount
<
register = REG_PM_PARAM_3.w;
string UIName = "Foam Amount";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 8.0;
float UIStep = 0.001;
> = 1.0;
half FoamCrestAmount
<
register = REG_PM_PARAM_4.x;
string UIName = "Crest Foam Amount";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 10.0;
float UIStep = 0.001;
> = 1.0;
half FoamTilling
<
register = REG_PM_PARAM_4.y;
string UIName = "Foam tilling";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 32.0;
float UIStep = 0.001;
> = 12.0;
#endif
half RipplesNormalsScale
<
register = REG_PM_PARAM_4.z;
string UIName = "Ripples normals scale";
string UIWidget = "slider";
float UIMin = 0.0;
float UIMax = 4.0;
float UIStep = 0.001;
> = 1.0;
////////////////////////////////////////////////////////////////
TextureCube envCubeTex : register(t3) = TM_Env;
Texture2D ReflectionSampler : register(t0)
<
string Script =
"RenderOrder=PreProcess;"
"ProcessOrder=WaterReflection;"
"RenderCamera=WaterPlaneReflected;"
"RenderTarget_IDPool = _RT2D_WATER_ID;"
"RenderTarget_Width=$ScreenSize;"
"RenderTarget_Height=$ScreenSize;"
"RenderTarget_UpdateType=WaterReflect;"
"RenderDepthStencilTarget=DepthBuffer;"
"ClearSetColor=Black;"
"ClearSetDepth=1;"
"ClearTarget=Color;"
"ClearTarget=Depth;";
>;
//////////////////////////////////////////////////////////////////////////
// Per draw constant buffer for water ocean.
struct SWaterParams
{
float4x4 mReflProj;
// ocean related parameters
float4 OceanParams0;
float4 OceanParams1;
float4 cOceanFogColorDensity;
};
cbuffer CBWater : register(b0)
{
struct
{
float4x4 mReflProj;
// ocean related parameters
float4 OceanParams0;
float4 OceanParams1;
float4 cOceanFogColorDensity;
} cbWater;
};
SWaterParams GetWaterParams()
{
SWaterParams params;
params.mReflProj = cbWater.mReflProj;
params.OceanParams0 = cbWater.OceanParams0;
params.OceanParams1 = cbWater.OceanParams1;
params.cOceanFogColorDensity = cbWater.cOceanFogColorDensity;
return params;
}
/////////////////////////////
struct a2v
{
float4 Position : POSITION;
float2 baseTC : TEXCOORD;
float4 Color : COLOR;
};
struct v2f
{
#if !%WATER_TESSELLATION_DX11 || %_RT_NO_TESSELLATION
float4 Position : SV_POSITION;
#endif
half4 vView : TEXCOORDN;
float4 vPosWS : TEXCOORDN;
#if !%_RT_SAMPLE3
float4 baseTC : TEXCOORDN;
float4 screenProj : TEXCOORDN;
float4 envTC : TEXCOORDN;
half4 GlossinessK : TEXCOORDN;
half4 cSpecular : TEXCOORDN;
#if %_RT_SAMPLE4
float4 ripplesTC : TEXCOORDN;
#endif
half4 vVertexNormal : TEXCOORDN;
#endif
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
float4 vOceanParams : TEXCOORDN;
#endif
};
struct v2f_hs
{
float4 Position : SV_POSITION;
half4 vView : TEXCOORDN;
float4 vPosWS : TEXCOORDN;
#if !%_RT_SAMPLE3
float4 baseTC : TEXCOORDN;
float4 screenProj : TEXCOORDN;
float4 envTC : TEXCOORDN;
half4 GlossinessK : TEXCOORDN;
half4 cSpecular : TEXCOORDN;
#if %_RT_SAMPLE4
float4 ripplesTC : TEXCOORDN;
#endif
half4 vVertexNormal : TEXCOORDN;
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
float4 vOceanParams : TEXCOORDN;
#endif
#endif
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// DX11 specifics
struct HS_CONSTANT_DATA_OUTPUT
{
float Edges[3] : SV_TessFactor;
float Inside : SV_InsideTessFactor;
};
struct HS_CONTROL_POINT_OUTPUT
{
half4 vView : TEXCOORDN;
float4 vPosWS : TEXCOORDN;
#if !%_RT_SAMPLE3
float4 baseTC : TEXCOORDN;
float4 screenProj : TEXCOORDN;
float4 envTC : TEXCOORDN;
half4 GlossinessK : TEXCOORDN;
half4 cSpecular : TEXCOORDN;
#if %_RT_SAMPLE4
float4 ripplesTC : TEXCOORDN;
#endif
half4 vVertexNormal : TEXCOORDN;
#endif
float4 vOceanParams : TEXCOORDN;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Get vertex from screen space into world space
void GetGridVertexPos( inout float4 vPos )
{
SWaterParams params = GetWaterParams();
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
// lower than hi spec uses low tesselated screen space grid and no vertex displacement
float4 vPosOrig = vPos;
//vPos.xy = (vPos.xy + VS_WaterMeshParams.xy) /VS_WaterMeshParams.w;
vPos.xy = (vPos.xy * 2.0f - 1.0f) * ( (vPos.w > 0.98f) ? CV_NearFarClipDist.y * 2.0f : 500.0f);
// snap grid to avoid translation aliasing - this can be improved..
float2 cpos = g_VS_WorldViewPos.xy;
// ATI specific fix - skip using frac togheter with big values, breaks somehow. Alternatively better compute this on cpu
cpos.xy -= frac(cpos.xy / 100.0) * 100.0;
vPos.xy += cpos;//g_VS_WorldViewPos.xy;
// set ocean level and add offset to hide precision artifact at horizon
vPos.z = params.OceanParams1.w + (4.5f * (vPos.w > 0.98f));
return;
#endif
if( GetShaderQuality() >= QUALITY_HIGH )
{
// hi specs uses regular tessellated grid plus vertex displacement
float atten = ( vPos.z );
// simple lod - keep nearby detail until 200 meters after that project all vertices to horizon
float fLod0 = 100.0f * ( 1.0f / min(params.OceanParams0.w, 2.0f ));
// Set a 45 degree rotation to the grid
vPos.xy = 0.707f * vPos.xy + 0.707f * float2( - vPos.y, vPos.x);
float3x3 mCam = float3x3( CV_CameraRightVector.xyz, CV_CameraUpVector.xyz, CV_CameraFrontVector.xyz);
// select better projection axis if front angle too steep
const float fFrontThreshold = 0.75f;
float fFrontDotUpAxis = abs( CV_CameraFrontVector.z );
if( fFrontDotUpAxis > fFrontThreshold )
{
mCam[1] = CV_CameraFrontVector;
mCam[2] = -CV_CameraUpVector;
}
mCam = transpose( mCam );
// Orientate along camera direction - try keeping good amount of triangles on screen
float3 vPosRot = ( mul(( (const float3x3) mCam ), -fLod0 * float3(vPos.x, 0.0f, vPos.y - 0.3f)) );
// snap grid to avoid translation aliasing - this can be improved..
float2 cpos = g_VS_WorldViewPos.xy;
// ATI specific fix - skip using frac together with big values, breaks somehow. Alternatively better compute this on cpu
cpos.xy -= (atten < 0.98f) ? frac(cpos.xy / 10.0f) * 10.0f : 0.0f;
float fDistCamera = length(vPosRot.xy);
fDistCamera *= fDistCamera * 0.085f;
//ATI specific fix - stretch the grid according to the direction, mainly the nearer half so that the border cells are always outside the screen
float t = dot(normalize(CV_CameraFrontVector.xy), normalize(vPosRot.xy));
fDistCamera *= 1.0f + (t + 1.0f) * (t + 1.0f) * abs(g_VS_WorldViewPos.z - params.OceanParams1.w) / 16.0f; //stretch grid according to camera height so that the projected grid
// snap edges in front of camera to horizon - multiply FarDist by 2 to remove unnecessary gaps btwn ocean and sky.
fDistCamera = (sign(-t) * atten > 0.98f) ? CV_NearFarClipDist.y * 2.0f : fDistCamera;
// ATI specific fix - clamp values to reasonable range
fDistCamera = min(16384.0f, fDistCamera);
// add offset to ocean level to hide precision artifact at horizon
float fLevelOffset = (4.5f * (atten > 0.98f));
vPos = float4( cpos.xy + normalize(vPosRot.xy) * fDistCamera , params.OceanParams1.w + fLevelOffset, 1.0f);
}
else
{
// lower than hi spec uses low tessellated screen space grid and no vertex displacement
const float fGridQuadSize = 1.0 / (10);
vPos.xy = (vPos.xy) * 2 - 1 ;
vPos.xy *= 1.0 + fGridQuadSize * 0.5;
vPos.zw = float2(0, 1);
#if %_RT_REVERSE_DEPTH
vPos.z = vPos.w - vPos.z;
#endif
float4 vPosWS = mul( CV_InvViewProj, vPos );
vPosWS /= vPosWS.w;
// invert sign when bellow water
float fDirSign = sign( g_VS_WorldViewPos.z - params.OceanParams1.w);
// get z plane intersections
float2 z_isecs = fDirSign * float2( g_VS_WorldViewPos.zz - float2( vPosWS.z, params.OceanParams1.w ) );
//z_isecs.x = max( z_isecs, 0.05 / CV_NearFarClipDist.y);
z_isecs.x = max( z_isecs, CV_NearFarClipDist.w);
// clamp t just in case
//float t = min( z_isecs.y / z_isecs.x, CV_NearFarClipDist.y * 5.0 );
float t = z_isecs.y / z_isecs.x;
// project vertex along ray direction
float2 vRayDir = (vPosWS.xy - g_VS_WorldViewPos.xy);
vPos.xy = g_VS_WorldViewPos.xy + vRayDir.xy * t;
// Output ocean level and try avoid precision artefacts at horizon line by adding an offset
vPos.z = params.OceanParams1.w + (t / CV_NearFarClipDist.y);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Apply vertex dynamic displacement - additional ocean eye candy for hispecs
void ApplyVertexDisplacement( inout float4 vPos, inout v2f OUT)
{
SWaterParams params = GetWaterParams();
SPerPassWaterParams perPassParams = GetPerPassWaterParams();
#if !%_RT_QUALITY && %_RT_QUALITY1
// Apply FFT vertex displacement
float2 tcFFT = (vPos.xy) * 0.0125f * params.OceanParams0.w * 1.25f;
float4 vtxDispl = WaterDisplMapSampler.SampleLevel(ssMaterialTrilinear, tcFFT.xy, 0.0f);
float fCamDist = length(vPos.xyz - g_VS_WorldViewPos.xyz);
float fDisplaceAtten = saturate( fCamDist * 0.5 );
fDisplaceAtten *= fDisplaceAtten;
vPos.xyz += fDisplaceAtten * vtxDispl.xyz * 0.06f * params.OceanParams1.x * float3(1.5f, 1.5f, 1.0f);
#if !%_RT_SAMPLE3
// Compute normal on the fly (todo: prebake this in texture alpha..)
float4 h0 = vtxDispl;
float4 h10 = WaterDisplMapSampler.SampleLevel(ssMaterialTrilinear, tcFFT.xy + float2( 1.0f / 64.0f, 0.0f), 0.0f);
float4 h11 = WaterDisplMapSampler.SampleLevel(ssMaterialTrilinear, tcFFT.xy + float2( 0.0f, 1.0f / 64.0f), 0.0f);
half3 vWeights = half3(h0.z, h10.z, h11.z);
half3 vNormal = half3( (vWeights.x - vWeights.y), (vWeights.x - vWeights.z), 8);
vNormal = normalize(vNormal.xyz);
OUT.vVertexNormal.xyz = vNormal;
#endif
#endif
// Apply dynamic ripples displacement
#if %_RT_SAMPLE4
float4 ripplesTC = GetWaterRipplesUvs( vPos, perPassParams );
#if !%_RT_QUALITY && %_RT_QUALITY1
float fRipplesDisp = WaterDynRipplesTex.SampleLevel(ssMaterialTrilinear, ripplesTC.xy, 0.0f).z;
vPos.z -= fRipplesDisp * ripplesTC.w;
#endif
#if !%_RT_SAMPLE3
OUT.ripplesTC = ripplesTC;
#endif
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
v2f WaterVS(a2v IN)
{
v2f OUT = (v2f)1;
SWaterParams params = GetWaterParams();
float4 vPos = IN.Position;
vPos.w = vPos.z; // store edge information
// Prepare mesh position
GetGridVertexPos( vPos );
float4 vPosOrig = vPos;
#if !%WATER_TESSELLATION_DX11 || %_RT_NO_TESSELLATION
// Apply FFT and dynamic water interaction
ApplyVertexDisplacement( vPos, OUT );
OUT.Position = mul(CV_ViewProjMatr, float4(vPos.xyz, 1));
#endif
#if !%_RT_SAMPLE3
// Output projected reflection texture coordinates
float4 vProjTex = mul(params.mReflProj, float4(vPos.xyz, 1) );
OUT.envTC = vProjTex;
#endif
// Output eye/light vector
float3 vView = g_VS_WorldViewPos.xyz - vPos.xyz;
OUT.vView.xyz = ( vView.xyz );
OUT.vView.w = sign( OUT.vView.z );
// Output projected refraction texture coordinates
#if !%WATER_TESSELLATION_DX11 || %_RT_NO_TESSELLATION
// Avoid near clipping
if( dot(OUT.vView.xyz, OUT.vView.xyz) < 2.0 * 2.0 ) // this may cause surface tearing near camera.
{
#if %_RT_REVERSE_DEPTH
OUT.Position.z = ( abs(vView.z) > 0.2 ) ? OUT.Position.z : OUT.Position.w;
#else
OUT.Position.z *= saturate( abs(vView.z) > 0.2 );
#endif
}
#if !%_RT_SAMPLE3
OUT.screenProj.xyw = HPosToScreenTC( OUT.Position ).xyw;
#endif
#endif
#if !%_RT_SAMPLE3
// Output refraction depth scale
OUT.screenProj.z = saturate( 1.0f - 0.15f * sqrt( saturate(OUT.screenProj.w) ) );
#if !%_RT_QUALITY && %_RT_QUALITY1
float fNormalsAttenuation = saturate( 1.0f - 0.01f * ( OUT.screenProj.w ) );
OUT.vVertexNormal.xyz = lerp( float3(0.0f, 0.0f, 1.0f), OUT.vVertexNormal.xyz, fNormalsAttenuation);
#endif
// Output pre-computed phong normalization (saves 0.4ms on fragment shader)
OUT.GlossinessK.x = 4.0 * GlossToSpecExp255(MatSpecColor.w);
OUT.GlossinessK.y = 0.5 * GlossToSpecExp255(MatSpecColor.w);
OUT.GlossinessK.z = OUT.GlossinessK.x * ONE_OVER_TWO_PI + ONE_OVER_PI;
OUT.GlossinessK.w = OUT.GlossinessK.y * ONE_OVER_TWO_PI + ONE_OVER_PI;
OUT.cSpecular.xyz = MatSpecColor * CV_SunColor.xyz * CV_SunColor.w; // also take sun specular into account
// Output bump layers texture coordinates
float2 FlowDir = params.OceanParams1.yz;
float2 vTranslation= ( CV_AnimGenParams.z * params.OceanParams0.y * 0.0025)* FlowDir;
// dont touch this scales
float2 vTex = vPos.xy * 0.005;
#if %FAKE_CAMERA_MOVEMENT
vTex = (vPos.xy + FlowDir.xy * (CV_AnimGenParams.x * max(-0.35, min(0.35, FakeCameraSpeed * 0.5)))) * 0.005;
#endif
// Output texture coordinates for water - don't touch texture scales
OUT.baseTC.xywz = vTex.xyxy * float4(1, 1, 2, 2) * Tilling + vTranslation.xyxy * float4(1, 1, 2, 2);
float localDetailTiling = DetailTilling;
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
localDetailTiling = max(2, DetailTilling);
#endif
OUT.baseTC.wz *= localDetailTiling;
#endif
//////////////////////////////////////////////////////////////////////////////////////////////////
OUT.vPosWS = vPos;
OUT.vPosWS.w = IN.Position.z;
//////////////////////////////////////////////////////////////////////////////////////////////////
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
OUT.vOceanParams.x = params.OceanParams1.x;
OUT.vOceanParams.w = params.OceanParams0.w;
#endif
return OUT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// DX11 specifics
HS_CONSTANT_DATA_OUTPUT WaterConstantsHS(InputPatch<v2f, 3> p, uint PatchID : SV_PrimitiveID )
{
HS_CONSTANT_DATA_OUTPUT OUT = (HS_CONSTANT_DATA_OUTPUT)0;
// map inputs
float3 vWP0 = -p[0].vView.xyz + g_VS_WorldViewPos.xyz;
float3 vWP1 = -p[1].vView.xyz + g_VS_WorldViewPos.xyz;
float3 vWP2 = -p[2].vView.xyz + g_VS_WorldViewPos.xyz;
float3 vMiddle = (vWP0 + vWP1 + vWP2) / 3.0f;
float3 vDir = normalize(g_VS_WorldViewPos.xyz - vMiddle);
float3 vNormal = normalize(cross(vWP2 - vWP0, vWP1 - vWP0));
float fDot = dot(vNormal, vDir);
bool bBackFaceCull = false;//(fDot > 0.01);
bool bFrustumCulled = ViewFrustumCull(vWP0, vWP1, vWP2, g_VS_FrustumPlaneEquation, 5.0);
if (bFrustumCulled || bBackFaceCull)
{
// Set all tessellation factors to 0 if frustum cull test succeeds
OUT.Edges[0] = 0.0;
OUT.Edges[1] = 0.0;
OUT.Edges[2] = 0.0;
OUT.Inside = 0.0;
}
else
{
// Check edges ws distance - if too big mesh needs to be pre-diced. todo: verify if we can update levels for patches
float fEdgeDistThreshold = 100;
bool bNeedsPreDicing = distance(vWP0, vWP1) > fEdgeDistThreshold || distance(vWP1, vWP2) > fEdgeDistThreshold || distance(vWP0, vWP2) > fEdgeDistThreshold;
{
float4 vEdgeTessellationFactors;
// Min and max distance should be chosen according to scene quality requirements
vMiddle = (p[0].vView.xyz + p[1].vView.xyz + p[2].vView.xyz) / 3.0f;
float fMaxFactor = 16;
float TessellationFactorEdge = 32;
float TessellationFactorInside = 32;
const float fMinDistance = 0;
const float fMaxDistance = 300;
float fTessScale = 1;
float fInside = TessellationFactorInside * fTessScale;
float fEdge = TessellationFactorEdge * fTessScale;
// Tessellation level fixed by variable
vEdgeTessellationFactors = float4(fEdge, fEdge, fEdge, fInside);
vEdgeTessellationFactors.xyz = distance(vWP2, vWP1);
vEdgeTessellationFactors.x /= distance((vWP2 + vWP1) / 2, g_VS_WorldViewPos.xyz);
vEdgeTessellationFactors.y = distance(vWP2, vWP0);
vEdgeTessellationFactors.y /= distance((vWP2 + vWP0) / 2, g_VS_WorldViewPos.xyz);
vEdgeTessellationFactors.z = distance(vWP0, vWP1);
vEdgeTessellationFactors.z /= distance((vWP0 + vWP1) / 2, g_VS_WorldViewPos.xyz);
float rAvg = max( max(vEdgeTessellationFactors.x, vEdgeTessellationFactors.y), vEdgeTessellationFactors.z) ;
vEdgeTessellationFactors.w = saturate(rAvg);
vEdgeTessellationFactors.xyz *= fEdge;
vEdgeTessellationFactors.w *= fInside;
// Assign tessellation levels
OUT.Edges[0] = clamp(vEdgeTessellationFactors.x, 1, fMaxFactor);
OUT.Edges[1] = clamp(vEdgeTessellationFactors.y, 1, fMaxFactor);
OUT.Edges[2] = clamp(vEdgeTessellationFactors.z, 1, fMaxFactor);
OUT.Inside = clamp(vEdgeTessellationFactors.w, 1, fMaxFactor);
}
}
return OUT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
[domain("tri")]
[partitioning("fractional_odd")]
[outputtopology("triangle_cw")]
[outputcontrolpoints(3)]
[patchconstantfunc("WaterConstantsHS")]
[maxtessfactor(16)]
HS_CONTROL_POINT_OUTPUT WaterHS(InputPatch<v2f, 3> inputPatch, uint uCPID : SV_OutputControlPointID )
{
HS_CONTROL_POINT_OUTPUT OUT = (HS_CONTROL_POINT_OUTPUT)0;
// passthrough hull shader
OUT.vView = inputPatch[uCPID].vView;
OUT.vPosWS = inputPatch[uCPID].vPosWS;
OUT.vOceanParams = inputPatch[uCPID].vOceanParams;
#if !%_RT_SAMPLE3
OUT.baseTC = inputPatch[uCPID].baseTC;
OUT.screenProj = inputPatch[uCPID].screenProj;
OUT.envTC = inputPatch[uCPID].envTC;
OUT.GlossinessK = inputPatch[uCPID].GlossinessK;
OUT.cSpecular = inputPatch[uCPID].cSpecular;
#if %_RT_SAMPLE4
OUT.ripplesTC = inputPatch[uCPID].ripplesTC;
#endif
#endif
return OUT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
[domain("tri")]
v2f_hs WaterDS(HS_CONSTANT_DATA_OUTPUT IN,float3 BarycentricCoords : SV_DomainLocation, const OutputPatch<HS_CONTROL_POINT_OUTPUT, 3> TriPatch )
{
v2f_hs OUT = (v2f_hs)0;
// interpolate attributes
OUT.vView = BarycentricInterp(BarycentricCoords, TriPatch[0].vView, TriPatch[1].vView, TriPatch[2].vView);
#if !%_RT_SAMPLE3
OUT.baseTC = BarycentricInterp(BarycentricCoords, TriPatch[0].baseTC, TriPatch[1].baseTC, TriPatch[2].baseTC);
OUT.envTC = BarycentricInterp(BarycentricCoords, TriPatch[0].envTC, TriPatch[1].envTC, TriPatch[2].envTC);
OUT.GlossinessK = BarycentricInterp(BarycentricCoords, TriPatch[0].GlossinessK, TriPatch[1].GlossinessK, TriPatch[2].GlossinessK);
OUT.cSpecular = BarycentricInterp(BarycentricCoords, TriPatch[0].cSpecular, TriPatch[1].cSpecular, TriPatch[2].cSpecular);
#endif
OUT.vPosWS = BarycentricInterp(BarycentricCoords, TriPatch[0].vPosWS, TriPatch[1].vPosWS, TriPatch[2].vPosWS);
float4 vWorldPos = float4(-OUT.vView.xyz + g_VS_WorldViewPos.xyz, 1);
float fCamDist = length(vWorldPos.xyz - g_VS_WorldViewPos.xyz);
float fDisplaceAtten = saturate( fCamDist * 0.5 );
fDisplaceAtten *= fDisplaceAtten;
float4 vtxDispl = 0;
float2 tcFFT = (vWorldPos.xy) * 0.0125 * TriPatch[0].vOceanParams.w;
vtxDispl = GetTexture2DLod(WaterDisplMapSampler, ssMaterialBilinear, float4(tcFFT * 1, 0, 0));
vtxDispl += GetTexture2DLod(WaterDisplMapSampler, ssMaterialBilinear, float4(tcFFT * 2, 0, 0)) * float4(1.5, 1.5, 1, 1);
vtxDispl *= fDisplaceAtten;
vWorldPos.xyz += vtxDispl * 0.06 * TriPatch[0].vOceanParams.x;
float4 vtxDispl2;
vtxDispl2 = GetTexture2DLod(WaterDisplMapSampler, ssMaterialBilinear, float4(tcFFT * 1 + float2(1.0 / 64, 0), 0, 0));
vtxDispl2 += GetTexture2DLod(WaterDisplMapSampler, ssMaterialBilinear, float4(tcFFT * 2 + float2(1.0 / 64, 0), 0, 0)) * float4(1.5, 1.5, 1, 1);
vtxDispl2 *= fDisplaceAtten;
float4 edgeLen = (vtxDispl2 - vtxDispl);
vWorldPos.w = saturate( -(edgeLen.x + edgeLen.y) * 0.035 );
//vWorldPos.w = TriPatch[0].vOceanParams.w;//vtxDispl.z;
#if !%_RT_SAMPLE3
float3 vVtxN = GetTexture2DLod(WaterNormalTex, aniso16xWrapSState, float4( tcFFT, 0, 0)).xyz;
vVtxN += GetTexture2DLod(WaterNormalTex, aniso16xWrapSState, float4( tcFFT * 2, 0, 0)).xyz;
OUT.vVertexNormal.xyz = lerp( vVtxN, float3(0, 0, 1), saturate(OUT.vPosWS.w * 2) ) ;
#endif
vWorldPos.w = lerp(vWorldPos.w, 0, saturate(OUT.vPosWS.w * 1.5) ) ;
vtxDispl = 0;
#if %_RT_SAMPLE4
SPerPassWaterParams perPassParams = GetPerPassWaterParams();
float4 ripplesTC = GetWaterRipplesUvs( vWorldPos, perPassParams );
float fRipplesDisp = GetTexture2DLod(WaterDynRipplesTex, ssMaterialTrilinearClamp, float4(ripplesTC.xy, 0.0f, 0.0f)).z;
vtxDispl -= fRipplesDisp * ripplesTC.w;
#if !%_RT_SAMPLE3
OUT.ripplesTC = ripplesTC;
#endif
#endif
vWorldPos.z += vtxDispl.z;
OUT.vPosWS = vWorldPos;
// Transform world position with viewprojection matrix
float4 HPosition = mul(CV_ViewProjMatr, float4(vWorldPos.xyz, 1));
OUT.Position = HPosition;
// Avoid near clipping
// MK: since we have soft blending with the near plane it's not needed
// MK: was causing completely culled triangles -> C3:DT18198
// if( length(OUT.vView) < 2.0 )
// OUT.Position.z *=saturate( abs(OUT.vView.z) > 0.2 );
#if !%_RT_SAMPLE3
OUT.screenProj = HPosToScreenTC(HPosition);
// Output refraction depth scale
OUT.screenProj.z = saturate( 1 - 0.15 * sqrt( saturate(OUT.screenProj.w) ) );
#endif
return OUT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
half3 BumpGen( in v2f_hs IN, float2 vParalaxOffset )
{
#if %_RT_SAMPLE3
return half3(0.0f, 0.0f, 0.0f);
#else
half3 bumpNormal = 0;
// hi frequency
bumpNormal.xy = GetTexture2D(WaterNormalTex, aniso16xWrapSState, IN.baseTC.wz + vParalaxOffset.xy).xy * DetailNormalsScale;
// low frequency
float4 tcBase = IN.baseTC.xyxy * float4(0.25, 0.25, 1, 1) + vParalaxOffset.xyxy;
#if !%_RT_QUALITY && %_RT_QUALITY1 || (%WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION)
bumpNormal += GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBase.xy).xyz + GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBase.zw).xyz;
bumpNormal.xy *= NormalsScale * 0.5;
#else
bumpNormal += GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBase.xy).xyz + GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBase.zw).xyz;
bumpNormal.xy *= NormalsScale;
#endif
#if %_RT_SAMPLE4
// dynamic ripples
half2 cRipples = GetTexture2D(WaterDynRipplesTex, ssMaterialTrilinearClamp, IN.ripplesTC.xy).xy;
bumpNormal.xy += cRipples * IN.ripplesTC.w;
#endif
#if %WATER_TESSELLATION_DX11 && !%_RT_NO_TESSELLATION
bumpNormal.xyz = normalize(IN.vVertexNormal.xyz) * float3(2, 2, 1) + float3(bumpNormal.xy, 0);
#else
#if !%_RT_QUALITY && %_RT_QUALITY1
//// ref normal
//float3 faceEdgeA = ddx( IN.vPosWS );
//float3 faceEdgeB = ddy( IN.vPosWS );
//float3 vtxNormal = -normalize( cross(faceEdgeA, faceEdgeB));
bumpNormal.xyz += IN.vVertexNormal.xyz;
#endif
#endif
#if %_RT_OCEAN_PARTICLE
// Fetch rain sample - todo, remove funky 300/tilling after shipping...
bumpNormal.xy += GetXYNormalMap(RainRipplesTex, ssMaterialBilinear, IN.baseTC.xy * RainTilling * (300.0h / Tilling)) * 5.0h;
#endif
return normalize( bumpNormal );
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float2 GetParalaxOffset(in v2f_hs IN, half3 vView)
{
#if %_RT_SAMPLE3
return float2(0.0f, 0.0f);
#else
float4 tcBaseVectorized = IN.baseTC.xyxy * float4(0.25, 0.25, 1, 1) ; //float4(0.05, 0.1, 0.5, 0.25);
// adds 0.8 ms ....
//vView.xy /= vView.z;
const half lGradientScale = 0.5 * GradientScale;
const half lHeightScale = 0.5 * HeightScale;
// Compute the height at this location
half4 height = GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBaseVectorized.xy);
height.xy = lGradientScale * height.xy;
height.w = lHeightScale * height.w;
// Compute the offset
float2 vParalax = vView.xy * height.w + height.xy;
// Compute the height at this location
height = GetTexture2D(WaterNormalTex, aniso16xWrapSState, tcBaseVectorized.zw + vParalax);
height.xy = lGradientScale * height.xy;
height.w = lHeightScale * height.w;
// Compute the offset
vParalax += vView.xy * height.w+ height.xy;
// unfortunately just by adding this aditional layer costs 0.6 ms (!) - maybe revive later for hi specs
//// Compute the height at this location
//height = GetTexture2D(WaterNormalTex, aniso16xWrapSState, IN.baseTC.xy + vParalax);
//height.xy = lGradientScale * height.xy;
//height.w = lHeightScale * height.w;
//vParalax += vView.xy * height.w + height.xy;
#if %_RT_SAMPLE4
// Finally add water ripples to paralax offset
half fSimSize = WATER_RIPPLES_SIM_GRID_SIZE;
half4 cRipples = GetTexture2D(WaterDynRipplesTex, ssMaterialTrilinearClamp, IN.ripplesTC.xy + vParalax / fSimSize);
cRipples.xy = lGradientScale * cRipples.xy;
cRipples.z = lHeightScale * cRipples.z;
cRipples *= IN.ripplesTC.w;
vParalax += 4 * vView.xy * cRipples.z + cRipples.xy;
#endif
return vParalax;
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
void SunSpecular(in v2f_hs IN, half fNdotL, half3 vNormal, half3 vView, inout half3 cSpecularAcc)
{
#if !%_RT_SAMPLE3
half3 R = reflect(-vView, vNormal);
half LdotR = saturate(dot(CV_SunLightDir.xyz, R) );
// 2 spec lobes for ocean
half fSpec = dot(IN.GlossinessK.zw, half2( pow(LdotR , IN.GlossinessK.x), pow(LdotR , IN.GlossinessK.y)) );
//half3 cLight = g_PS_SunColor.xyz;;
half3 cSunSpecular = fNdotL * fSpec * IN.cSpecular; //IN.cSpecular = cLight * MatSpecColor * g_PS_SunColor.xyz * g_VS_SunLightDir.w;
//cSunSpecular *= PS_HDR_RANGE_ADAPT_MAX;
// Fake sun shadows: if sun oposing reflected surface normal, its darker - use this as hint for shadows (doens't work 100% but it's better than nothing)
// - note: this assumption works only for realtime shadows
// cSunSpecular *= saturate(dot(cSpecularAcc.xyz, 0.333)*dot(cSpecularAcc.xyz, 0.333)); // squashes sun specular result with low SunColor value
// Add sun specular term
cSpecularAcc.xyz += cSunSpecular.xyz;
#endif
}
////////////////////////////////////////////////////////////////////////////////////////////////////
float2 MapScreenTC(float2 tc, float2 maxTC)
{
return ClampScreenTC(MapViewportToRaster(tc), maxTC);
}
pixout WaterPS(v2f_hs IN)