-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathBGWater.Shader
More file actions
393 lines (289 loc) · 14.6 KB
/
BGWater.Shader
File metadata and controls
393 lines (289 loc) · 14.6 KB
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
Shader "BadDog/URP/BGWater"
{
Properties
{
[Header(Three Direction Wave)]
_MainWave ("Main Wave", 2D) = "white" { }
_MainWaveBumpScale ("Main Wave Bump Scale", Range(0, 2)) = 1
_SecondWave ("Second Wave", 2D) = "white" { }
_SecondWaveBumpScale ("Second Normal Bump Scale", Range(0, 2)) = 1
_MainWaveTilingOffset ("Main Wave Tiling Offset", Vector) = (1, 1, 1, 1)
_SecondWaveTilingOffset ("Second Wave Tiling Offset", Vector) = (1, 1, -1, 1)
_ThirdWaveTilingOffset ("Third Wave Tiling Offset", Vector) = (1, 1, 1, -1)
[Header(Water)]
_WaterBaseColor ("Water Base Color", Color) = (1.0, 1.0, 1.0, 1)
[Header(Muddy)]
_WaterMuddyColor ("Water Muddy Color", Color) = (1.0, 1.0, 1.0, 1)
_WaterMuddyScale ("Water Muddy Scale", Range(0, 2)) = 1
_WaterDepthOffset ("Water Depth Offset", Range(0, 1)) = 1
[Header(Specular)]
_SpecularIntensity ("Specular Intensity", Range(0, 8)) = 1
[Header(Refraction)]
_WaterDistortScale ("Distort Scale", Range(0, 10)) = 1
[Header(Enviroment Reflection)]
_EnviromentIntensity ("Enviroment Intensity", Range(0, 10)) = 1
[Header(Screen Space Reflection)]
[Toggle] _BGWATER_SSR ("Screen Space Reflection", Float) = 1.0
_SSRMaxSampleCount ("SSR Max Sample Count", Range(0, 64)) = 12
_SSRSampleStep ("SSR Sample Step", Range(4, 32)) = 16
_SSRIntensity ("SSR Intensity", Range(0, 2)) = 0.5
[Toggle] _BGWATER_ORTHO ("Orthographic Camera", Float) = 0
}
SubShader
{
Tags
{
"IgnoreProjector" = "True" "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline"
}
Pass
{
Name "ForwardLit"
Tags
{
"LightMode" = "UniversalForward"
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Back
HLSLPROGRAM
#if !defined(UNITY_PASS_FORWARDBASE)
#define UNITY_PASS_FORWARDBASE
#endif
#pragma vertex VertexForward
#pragma fragment FragForward
#pragma target 3.0
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
#pragma multi_compile_instancing
#pragma multi_compile _ _BGWATER_SSR_ON
#pragma multi_compile _ _BGWATER_ORTHO_ON
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#ifndef BADDOG_WATER_BASE
#define BADDOG_WATER_BASE
sampler2D _MainWave;
float4 _MainWave_ST;
sampler2D _SecondWave;
float4 _SecondWave_ST;
half _MainWaveBumpScale;
half _SecondWaveBumpScale;
half4 _MainWaveTilingOffset;
half4 _SecondWaveTilingOffset;
half4 _ThirdWaveTilingOffset;
half _WaterDepthOffset;
half _WaterMuddyScale;
half _WaterDistortScale;
half4 _WaterBaseColor;
half4 _WaterMuddyColor;
half _SpecularIntensity;
half _EnviromentIntensity;
#include "./BGWaterOrtho.hlsl"
#ifndef BADDOG_WATER_STRUCT
#define BADDOG_WATER_STRUCT
struct BGWaterVertexInput
{
float4 vertex : POSITION;
float4 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct BGWaterVertexOutput
{
float4 pos : SV_POSITION;
float3 mainWaveUV : TEXCOORD0;
float4 secondWaveUV : TEXCOORD1;
float4 worldNormalDir : TEXCOORD2;
float4 worldTangentDir : TEXCOORD3;
float4 worldBitangentDir : TEXCOORD4;
float4 screenPos : TEXCOORD5;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
struct BGLightingData
{
float3 worldPos;
float3 worldNormal;
float3 worldLightDir;
float3 worldViewDir;
half NoL;
half NoV;
half NoH;
half LoH;
half3 R;
half3 diffuseColor;
half3 specularColor;
half3 lightColor;
float2 screenUV;
};
#endif
#include "WaterSSR.cginc"
#ifndef BADDOG_WATER_LIGHTING
#define BADDOG_WATER_LIGHTING
float3 WaveNormal(BGWaterVertexOutput vertexOutput)
{
half3 waterNormal1 = tex2D(_MainWave, vertexOutput.mainWaveUV.xy).xyz;
half3 waterNormal2 = tex2D(_MainWave, vertexOutput.secondWaveUV.xy).xyz;
half3 waterNormal3 = tex2D(_SecondWave, vertexOutput.secondWaveUV.zw).xyz;
half3 waterNormal = ((waterNormal1 + waterNormal2) * 0.6667 - 0.6667) * half3(
_SecondWaveBumpScale, _SecondWaveBumpScale, 1);
waterNormal3 = waterNormal3 * 2 - 1;
waterNormal += (waterNormal3 * half3(_MainWaveBumpScale, _MainWaveBumpScale, 1));
return normalize(TransformTangentToWorld(waterNormal,
float3x3(vertexOutput.worldTangentDir.xyz,
vertexOutput.worldBitangentDir.xyz,
vertexOutput.worldNormalDir.xyz)));
}
BGLightingData PrepareLighting(BGWaterVertexOutput vertexOutput)
{
BGLightingData lightingData = (BGLightingData)0;
lightingData.worldPos = float3(vertexOutput.worldNormalDir.w, vertexOutput.worldTangentDir.w,
vertexOutput.worldBitangentDir.w);
lightingData.worldNormal = WaveNormal(vertexOutput);
lightingData.worldNormal = half3(0, 1, 0);
lightingData.worldLightDir = normalize(_MainLightPosition.xyz);
lightingData.worldViewDir = normalize(_WorldSpaceCameraPos.xyz - lightingData.worldPos);
half3 H = normalize(lightingData.worldLightDir + lightingData.worldViewDir);
lightingData.NoL = saturate(dot(lightingData.worldLightDir, lightingData.worldNormal));
lightingData.NoV = saturate(dot(lightingData.worldNormal, lightingData.worldViewDir));
lightingData.NoH = saturate(dot(lightingData.worldNormal, H));
lightingData.LoH = saturate(dot(lightingData.worldLightDir, H));
lightingData.R = normalize(reflect(-lightingData.worldViewDir, lightingData.worldNormal));
lightingData.diffuseColor = _WaterBaseColor;
lightingData.specularColor = half3(0.04, 0.04, 0.04);
lightingData.lightColor = _MainLightColor.rgb;
lightingData.screenUV = vertexOutput.screenPos.xy / vertexOutput.screenPos.w;
#if defined(UNITY_SINGLE_PASS_STEREO)
lightingData.screenUV.xy = UnityStereoTransformScreenSpaceTex(lightingData.screenUV.xy);
#endif
return lightingData;
}
half3 IndirectDiffuse(BGLightingData lightingData)
{
return SampleSH(lightingData.worldNormal);
}
half3 Diffuse(BGLightingData lightingData)
{
return lightingData.lightColor * lightingData.NoL;
}
half3 Specular(BGLightingData lightingData)
{
float D = (-0.004) / (lightingData.NoH * lightingData.NoH - 1.005);
D *= D;
half x = 1 - lightingData.LoH;
half x2 = x * x;
half x5 = x2 * x2 * x;
float F = lightingData.specularColor + (1 - lightingData.specularColor) * x5;
return lightingData.lightColor * D * F * PI * _SpecularIntensity;
}
half3 IndirectSpecular(BGLightingData lightingData)
{
half3 probe = GlossyEnvironmentReflection(lightingData.R, 0, 1);
half fresnelTerm = 1.0 - saturate(dot(lightingData.worldNormal, lightingData.worldViewDir));
fresnelTerm *= fresnelTerm;
fresnelTerm *= fresnelTerm;
return probe.rgb * lerp(lightingData.specularColor, 1, fresnelTerm);
}
half3 GetReflectionWithSSR(BGWaterVertexOutput vertexOutput, BGLightingData lightingData)
{
half3 indirectDiffuse = IndirectDiffuse(lightingData);
half3 diffuse = Diffuse(lightingData);
half3 specular = Specular(lightingData);
half3 indirectSpecular = IndirectSpecular(lightingData);
#if defined(_BGWATER_SSR_ON)
half4 ssrLighting = GetWaterSSR(lightingData.screenUV,lightingData.NoV,lightingData.worldPos,lightingData.R);
indirectSpecular = lerp(lerp(indirectSpecular, ssrLighting.rgb, ssrLighting.a), ssrLighting, ssrLighting.a > 0.99);
#endif
indirectSpecular *= _EnviromentIntensity;
return (indirectDiffuse + diffuse) * lightingData.diffuseColor + specular + indirectSpecular;
}
half4 GetRefraction(BGWaterVertexOutput vertexOutput, BGLightingData lightingData)
{
float2 screenUV = lightingData.screenUV;
float2 grabUV = screenUV;
half3 worldViewDir = normalize(lightingData.worldViewDir);
half worldViewDirY = abs(worldViewDir.y);
#if defined(_BGWATER_ORTHO_ON)
float depth = GetOrthoEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV));
#else
float depth = LinearEyeDepth(
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV), _ZBufferParams);
#endif
depth = depth - vertexOutput.screenPos.z;
half2 deltaUV = lightingData.worldNormal.xz * _WaterDistortScale * saturate(depth) * worldViewDirY /
vertexOutput.screenPos.z;
#if defined(_BGWATER_ORTHO_ON)
float newDepth = GetOrthoEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV + deltaUV));
#else
float newDepth = LinearEyeDepth(
SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, screenUV + deltaUV),
_ZBufferParams);
#endif
newDepth = newDepth - vertexOutput.screenPos.z;
half signDepth = saturate(newDepth * 10);
grabUV = grabUV + deltaUV * signDepth;
depth = lerp(depth, newDepth, signDepth);
half viewMultiplier = (worldViewDirY + _WaterMuddyScale) * _WaterDepthOffset * _WaterDepthOffset;
depth *= viewMultiplier;
half alpha = saturate(1 - depth);
alpha = saturate(1.02 - pow(alpha, (dot(lightingData.worldNormal.xyz, worldViewDir) * 5 + 6)));
half4 refraction = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, grabUV);
refraction.rgb = lerp(refraction.rgb, refraction.rgb * _WaterMuddyColor * _WaterMuddyScale, alpha);
refraction.a = alpha;
return refraction;
}
#endif
BGWaterVertexOutput VertexCommon(BGWaterVertexInput v)
{
BGWaterVertexOutput o = (BGWaterVertexOutput)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = TransformObjectToHClip(v.vertex);
o.mainWaveUV.xy = v.texcoord * _MainWaveTilingOffset.xy + _Time.r * _MainWaveTilingOffset.zw * 0.1;
o.secondWaveUV.xy = v.texcoord * _SecondWaveTilingOffset.xy + _Time.r * _SecondWaveTilingOffset.zw *
0.1;
o.secondWaveUV.zw = v.texcoord * _ThirdWaveTilingOffset.xy + _Time.r * _ThirdWaveTilingOffset.zw * 0.1;
float3 worldPos = TransformObjectToWorld(v.vertex);
VertexNormalInputs normalInput = GetVertexNormalInputs(v.normal, v.tangent);
o.worldNormalDir = float4(normalInput.normalWS, worldPos.x);
o.worldTangentDir = float4(normalInput.tangentWS, worldPos.y);
o.worldBitangentDir = float4(normalInput.bitangentWS, worldPos.z);
o.mainWaveUV.z = ComputeFogFactor(o.pos.z);
o.screenPos = ComputeScreenPos(o.pos);
o.screenPos.z = -TransformWorldToView(worldPos).z;
return o;
}
BGWaterVertexOutput VertexForward(BGWaterVertexInput vertexInput)
{
BGWaterVertexOutput vertexOutput = VertexCommon(vertexInput);
return vertexOutput;
}
half4 FragForward(BGWaterVertexOutput vertexOutput) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(vertexOutput);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(vertexOutput);
BGLightingData lightingData = PrepareLighting(vertexOutput);
// return vertexOutput.screenPos;
// reflection + fog
// half3 reflection = GetReflectionWithSSR(vertexOutput, lightingData);
// reflection = MixFog(reflection, vertexOutput.mainWaveUV.z);
#if defined(_BGWATER_SSR_ON)
lightingData.worldNormal.xyz = half3(0, 1, 0);
half4 ssrLighting = GetWaterSSR(lightingData.screenUV,lightingData.NoV,lightingData.worldPos,lightingData.R);
ssrLighting.rgb *= ssrLighting.a;
return half4(ssrLighting.rgb, 1);
#endif
return half4(0, 0, 0, 1);
// // refraction
// half4 refraction = GetRefraction(vertexOutput, lightingData);
// // final
// half3 finalColor = lerp(refraction.rgb, reflection, refraction.a);
// return half4(finalColor, 1);
}
#endif
ENDHLSL
}
}
FallBack "VertexLit"
}