fix:代码提交

This commit is contained in:
zhangjiajia
2026-03-03 11:30:53 +08:00
parent adf60cc8df
commit 21ebd4c951
2520 changed files with 178964 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_ALPHA_INCLUDED
#define URPWATER_ALPHA_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
void ComputeAlpha(inout GlobalData data, Varyings IN)
{
#if _ALPHA_MASK_ON || _EDGEFADE_ON
float mask = 1;
#endif
#if _ALPHA_MASK_ON
mask *= IN.color.a;
#endif
#if _EDGEFADE_ON
float edgeMask = 1 - DistanceFade(data.sceneDepth, data.pixelDepth, 0, max(0, _EdgeSize));
mask *= edgeMask;
#endif
#if _ALPHA_MASK_ON || _EDGEFADE_ON
data.finalColor = lerp(data.clearColor, data.finalColor, mask);
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 9e31290dcd3774e4e954ea3edc734fb9
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,181 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_COMMON_INCLUDED
#define URPWATER_COMMON_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
Varyings vert(Attributes v)
{
Varyings OUT;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, OUT);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
half3 worldTangent = half3(1, 0, 0);
half3 worldNormal = half3(0, 1, 0);
half3 worldBinormal = half3(0, 0, 1);
//#ifndef _DISPLACEMENTMODE_GERSTNERPLUS
#ifndef _WORLD_UV
VertexNormalInputs vertexTBN = GetVertexNormalInputs(v.normal, v.tangent);
worldNormal = vertexTBN.normalWS;
worldBinormal = vertexTBN.bitangentWS;//TransformObjectToWorldDir(worldBinormal);
worldTangent = vertexTBN.tangentWS;//TransformObjectToWorldDir(worldTangent);
#endif
//#endif
// Gerstner Waves
ComputeWaves(v, worldNormal, worldBinormal, worldTangent);
// Compute Positions
VertexPositionInputs vInputs = GetVertexPositionInputs(v.vertex.xyz);
//TODO: Use for fog computation instead of redoing it in PS
//vInputs.positionVS
float3 worldPos = vInputs.positionWS;
// Pack Directions and world position
OUT.normal = float4(worldNormal, worldPos.x);
OUT.tangent = float4((worldTangent), worldPos.y);
OUT.binormal = float4((worldBinormal), worldPos.z);
#if _DYNAMIC_EFFECTS_ON
OUT.projectionUV = ProjectionUV(_DynamicEffectsParams, worldPos);
float4 dynamicTex = SAMPLE_TEXTURE2D_LOD(_DynamicEffectsTexture, URPWater_linear_clamp_sampler, OUT.projectionUV, 0);
float displacement = dynamicTex.a * _DynamicDisplacement;
v.vertex.y += displacement;
worldPos.y += displacement;
#else
float displacement = 0.0;
#endif
//TransformObjectToHClip(v.vertex.xyz);
OUT.pos = vInputs.positionCS - float4(0.0, displacement, 0.0, 0.0);
OUT.color = v.color;
OUT.screenCoord = vInputs.positionNDC; // Pixel Depth in alpha
//OUT.screenCoord.a = ComputePixelDepth(worldPos); // ComputeEyeDepth - Deprecated? Comment out if issues in VR
#if _WORLD_UV
v.texcoord.xy = worldPos.xz * 0.1;
#endif
//NormalMap UVa
#if _NORMALSMODE_FLOWMAP
OUT.texcoord.xy = v.texcoord.xy * _NormalMapATilings.xy;
OUT.texcoord.zw = v.texcoord.xy * _FlowTiling.xy + _FlowTiling.zw;
#else
OUT.texcoord = DualAnimatedUV(v.texcoord, _NormalMapATilings, _NormalMapASpeeds);
#endif
#if _NORMALSMODE_DUAL || _FOAM_ON
OUT.texcoord1 = float4(0, 0, 0, 0);
#endif
#if _NORMALSMODE_DUAL
OUT.texcoord1.xy = AnimatedUV(v.texcoord.xy, _NormalMapBTilings.xy, _NormalMapBSpeeds.xy).xy;
#endif
#if _NORMAL_FAR_ON
OUT.texcoord2 = DualAnimatedUV(v.texcoord, _NormalMapFarTilings, _NormalMapFarSpeeds);
#endif
#if _NORMAL_FAR_ON || _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
OUT.texcoord3 = float4(0, 0, 0, 0);
OUT.texcoord3.x = saturate(OUT.screenCoord.z / _NormalFarDistance);
#if _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
OUT.texcoord3.y = v.waveHeight;
#endif
#endif
// FoamUV
#if _FOAM_ON
OUT.texcoord1.zw = v.texcoord.xy * _FoamTiling.xy;
#endif
// Shadows
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
OUT.shadowCoord = GetShadowCoord(vInputs);
#endif
return OUT;
}
FRONT_FACE_TYPE vFace : FRONT_FACE_SEMANTIC;
#if _DOUBLE_SIDED_ON
float4 frag(Varyings IN, FRONT_FACE_TYPE vFace : FRONT_FACE_SEMANTIC) : SV_Target
#else
float4 frag(Varyings IN) : SV_Target
#endif
{
UNITY_SETUP_INSTANCE_ID(IN);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
GlobalData data;
InitializeGlobalData(data, IN);
#if _DOUBLE_SIDED_ON
data.vFace = IS_FRONT_VFACE(vFace, true, false);
#endif
ComputeDynamicData(data, IN);
ComputeNormals(data, IN);
ComputeRefractionData(data);
ComputeScattering(data, IN);
ComputeFoam(data, IN);
ComputeLighting(data, IN);
ComputeReflections(data, IN);
//return float4(IN.tangent.rgb, 1);
//return float4(IN.texcoord.rg,0, 1);
//half NdotL = saturate(dot(data.worldNormal, data.mainLight.direction));
//return float4(NdotL.xxx, 1);
//data.debug.rgb = data.worldViewDir;
//return float4(data.debug.rgb, 1);
Applyfog(data.finalColor, data.worldPosition);
// Enviro Support
//float linear01Depth = Linear01Depth(data.screenUV.z);
//data.finalColor.rgb = TransparentFog(float4(data.finalColor.rgb, 1), data.worldPosition, data.screenUV.xy, linear01Depth).rgb;
// End Enviro
ComputeAlpha(data, IN);
float4 output;
output.rgb = data.finalColor.rgb;
output.a = 1;
return output;
}
#endif

View File

@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: d448430caf48b0d4aa3d53ed2baf5797
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
preprocessorOverride: 0
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
#ifndef URPWATER_DYNAMIC_INCLUDED
#define URPWATER_DYNAMIC_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
void ComputeDynamicData(inout GlobalData data, Varyings IN)
{
#if _DYNAMIC_EFFECTS_ON
data.dynamicData = SAMPLE_TEXTURE2D(_DynamicEffectsTexture, URPWater_linear_clamp_sampler, IN.projectionUV);
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d8ce8384989b1b647b100ad9b06a6dbe
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_FOAM_INCLUDED
#define URPWATER_FOAM_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterHelpers.hlsl"
#include "URPWaterVariables.hlsl"
void ComputeFoam(inout GlobalData data, Varyings IN)
{
#if _FOAM_ON
float2 foamDistortion = data.worldNormal.xz * _FoamDistortion;
float edgeMask = DistanceFade(data.sceneDepth, data.pixelDepth, 0, max(0,_FoamSize));
float foamTex = SAMPLE_TEXTURE2D(_FoamTex, URPWater_trilinear_repeat_sampler, IN.texcoord1.zw + foamDistortion).r;
#if _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
#if _FOAM_WHITECAPS_ON
half capMask = saturate(IN.texcoord3.y * _FoamCapsIntensity);
capMask = smoothstep(_FoamCapsRangeMin, _FoamCapsRangeMax, capMask);
edgeMask += capMask;
#endif
#endif
#if _DYNAMIC_EFFECTS_ON
edgeMask += data.dynamicData.b * _DynamicFoam;
#endif
#if _ADD_FOAM_ON
edgeMask = max(edgeMask, IN.color.r);
#endif
#if _FOAM_RIPPLE_ON
float rippleMask = DistanceFade(data.sceneDepth, data.pixelDepth, 0, max(0, _FoamRippleDistance));
float rippleSpeed = 1 - _FoamRippleSpeed - 1;
float rippleSize = _FoamRippleSize + _FoamSize;
float ripples = rippleMask * saturate(sin((rippleMask - _Time.y * rippleSpeed + foamDistortion) * rippleSize * URP_WATER_PI) ).r;
data.foamMask = foamTex * max(edgeMask, ripples);
#else
data.foamMask = foamTex * edgeMask;
#endif
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: a6ac107a403887b47841054e6cdd963c
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,133 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_GERSTNERPLUS_INCLUDED
#define URPWATER_GERSTNERPLUS_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterHelpers.hlsl"
#include "URPWaterVariables.hlsl"
struct WavePlus
{
float Length;
float Steepness;
float Speed;
float Direction;
float DirectionOffset;
};
//#define PI 3.141592f
#define PI_2 PI * 2.f
#define WAVECOUNT 4.0
static float ClusterLengthMul[] = { 1.f, 0.8f, 0.5f, 0.3f };
static float ClusterSteepnessMul[] = { 1.f, 0.8f, 0.7f, 0.6f };
static float ClusterSpeedMul[] = { 1.f, 1.3f, 1.5f, 1.8f };
static float ClusterOffsetMul[] = { 0.0f, 0.1249f, 0.24845f, 0.52148f };
static float WaveLengthMul[] = { 1.f, 0.8f, 0.6f, 0.4f };
static float WaveSteepnessMul[] = { 1.f, 0.9f, 0.8f, 0.6f };
static float WaveSpeedMul[] = { 1.f, 1.3f, 1.5f, 1.8f };
static float WaveOffsetMul[] = { 0.0f, 0.1249f, 0.24845f, 0.52148f };
float3 GerstnerWave(WavePlus w, float3 p, inout float3 tangent, inout float3 binormal)
{
float steepness = w.Steepness;
float wavelength = w.Length;
float k = 2 * PI / wavelength;
float c = sqrt(9.8 / k);
float time = _Time.x;
float DirectionRads = (w.Direction + (w.DirectionOffset)) * PI_2;
float2 d = normalize(float2(cos(DirectionRads), sin(DirectionRads))); // Calculate this in C# ?
float f = k * (dot(d, p.xz) - c * time * w.Speed);
float a = steepness / k;
float sine = sin(f);
float cosine = cos(f);
// Tangent
tangent += float3(
-d.x * d.x * (steepness * sine),
d.x * (steepness * cosine),
-d.x * d.y * (steepness * sine)
);
// Binormal
binormal += float3(
-d.x * d.y * (steepness * sine),
d.y * (steepness * cosine),
-d.y * d.y * (steepness * sine)
);
// WPO
return float3(
d.x * (a * cosine),
a * sine,
d.y * (a * cosine)
);
}
void ComputeGerstnerWavesPlus(float3 worldPos, inout float3 offsets, inout half3 normal, inout half3 binormal, inout half3 tangent)
{
float3 WPO = 0.0;
half3 Normal = half3(0, 0, 0);
half3 Binormal = half3(0, 0, 1);
half3 Tangent = half3(1, 0, 0);
half ClustersCount = _ClustersCount;
half WaveLength[] = { _WaveParams1.x, _WaveParams2.x, _WaveParams3.x, _WaveParams4.x };
half WaveSteepness[] = { _WaveParams1.y, _WaveParams2.y, _WaveParams3.y, _WaveParams4.y };
half WaveSpeed[] = { _WaveParams1.z, _WaveParams2.z, _WaveParams3.z, _WaveParams4.z };
half WaveDirection[] = { _WaveParams1.w, _WaveParams2.w, _WaveParams3.w, _WaveParams4.w };
half WaveOffset[] = { _WaveOffsets.x, _WaveOffsets.y, _WaveOffsets.z, _WaveOffsets.w };
float maxWaves = min(_WavesPerCluster, WAVECOUNT);
UNITY_LOOP
for (int c = 0; c < ClustersCount; c++)
{
half clusterLength = WaveLength[c] * ClusterLengthMul[c];
half clusterSteepness = WaveSteepness[c] * ClusterSteepnessMul[c];
half clusterSpeed = WaveSpeed[c] * ClusterSpeedMul[c];
half clusterDirection = WaveDirection[c];
half clusterOffset = WaveOffset[c] + ClusterOffsetMul[c];
UNITY_LOOP
for (int x = 0; x < maxWaves; x++)
{
WavePlus w;
w.Length = clusterLength * WaveLengthMul[x];
w.Steepness = (clusterSteepness * WaveSteepnessMul[x]) / ((WAVECOUNT * ClustersCount * 0.25));
w.Speed = clusterSpeed * WaveSpeedMul[x];
w.Direction = clusterDirection;
w.DirectionOffset = clusterOffset * WaveOffsetMul[x];
WPO += GerstnerWave(w, worldPos, Tangent, Binormal);
}
}
offsets = WPO;
normal = normalize(cross(Binormal, Tangent));
binormal = Binormal;
tangent = Tangent;
#ifndef _WORLD_UV
binormal = -TransformObjectToWorldDir(half3(0, 0, 1));
tangent = -TransformObjectToWorldDir(half3(1, 0, 0));
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: aca67275c722b9645a9f15c23986d441
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,139 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_GERSTNERSIMPLE_INCLUDED
#define URPWATER_GERSTNERSIMPLE_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterHelpers.hlsl"
#include "URPWaterVariables.hlsl"
static uint DCount = 7;
static float2 Directions[] =
{
float2(0.53, 0.45),
float2(-0.209, 0.4),
float2(-0.125, 0.592),
float2(0.482, -0.876),
float2(-0.729, -0.694),
float2(-0.248, 0.968),
float2(0.844, -0.538)
};
static uint LCount = 6;
static float Lengths[] =
{
3.56,
2.85,
2.10,
1.30,
1.10,
1.2
};
static uint SCount = 5;
static float SteepnessRange[] =
{
1.0,
1.8,
1.6,
1.25,
0.5
};
static uint SpCount = 9;
static float Speeds[] =
{
0.62,
-0.8,
0.45,
-0.75,
0.88,
0.70,
-0.56,
0.35,
-0.71
};
struct Wave
{
float Length;
float Steepness;
float Speed;
float Amplitude;
float2 Direction;
};
//#define SOLVE_NORMAL_Z 1
#define SteepnessThreshold 1
#define GRAVITY 9.8
void SingleGerstnerWave(float3 WorldPos, Wave w, out float3 WPO, out half3 Normal)
{
float dispersion = 6.28318 / w.Length;
float c = sqrt(GRAVITY / dispersion) * w.Speed;
float2 d = w.Direction;
float Steepness = w.Steepness;
float Speed = w.Speed;
float f = dispersion * (dot(d, WorldPos.xz) - c * _Time.x);
float cf;
float sf;
sincos(f, sf, cf);
float a = Steepness / (dispersion * 1.5);
float wKA = a * dispersion;
WPO.xz = d.xy * (a * cf);
WPO.y = a * sf;
Normal.xz = -(cf * wKA * d);
Normal.y = 0;
/*
#if SOLVE_NORMAL_Z
Normal.y = sf * w.Steepness * saturate((a * SteepnessThreshold) / w.Length);
#else
Normal.y = 0;
#endif
*/
}
void ComputeGersterWaves(float3 worldPosition, inout float3 offsets, inout half3 normals)
{
normals = half3(0, 0, 0);
//for start
UNITY_LOOP
for (int i = 0; i < _WaveCount; i++)
{
float3 currentOffset;
float3 currentNormal;
float steepnessMul = lerp(1.0, 0.1, (1.0 / 32.0) * i);
Wave w;
w.Length = Lengths[i % LCount] * _WaveLength;
w.Steepness = SteepnessRange[i % SCount] * _WaveSteepness * steepnessMul;
w.Speed = Speeds[i % SpCount] * _WaveSpeed;
w.Direction = normalize(Directions[i % DCount]);
SingleGerstnerWave(worldPosition, w, currentOffset, currentNormal);
offsets += currentOffset;
normals += currentNormal;
}
//for end
normals = normalize(float3(normals.x, 1.0 - normals.y, normals.z));
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: e412c0d5d536df34b86f8fee40e91501
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,285 @@
#ifndef URPWATER_HELPERS_INCLUDED
#define URPWATER_HELPERS_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTexture.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
float4 DualAnimatedUV(float2 uv, float4 tilings, float4 speeds)
{
float4 coords;
coords.xy = uv * tilings.xy;
coords.zw = uv * tilings.zw;
#if _WORLD_UV
coords += speeds * _Time.x;
#else
coords += frac(speeds * _Time.x);
#endif
return coords;
}
float2 AnimatedUV(float2 uv, float2 tilings, float2 speeds)
{
float2 coords;
coords.xy = uv * tilings.xy;
#if _WORLD_UV
coords += speeds * _Time.xx;
#else
coords += frac(speeds * _Time.xx);
#endif
return coords;
}
float UVEdgeMask(float2 uv, float maskSize)
{
float2 edgeMaskUV = abs(uv * 2 - 1);
float edgeMask = 1 - max(edgeMaskUV.x, edgeMaskUV.y);
return smoothstep(0, maskSize, edgeMask);
}
float ComputePixelDepth(float3 worldPos)
{
return - TransformWorldToView(worldPos).z;
}
// From DeclareDepthTexture.hlsl
float SampleRawDepth(float2 uv)
{
/*
#if UNITY_REVERSED_Z
real depth = SampleSceneDepth(uv);
#else
// Adjust z to match NDC for OpenGL
real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, SampleSceneDepth(uv));
#endif
*/
//Manual mode in case Unity breaks this feature
//UNITY_SAMPLE_SCREENSPACE_TEXTURE(_CameraDepthTexture, sampler_ScreenTextures_linear_clamp, uv);
return SampleSceneDepth(uv);
}
float Linear01Depth(float z)
{
return 1.0 / (_ZBufferParams.x * z + _ZBufferParams.y);
}
float InvLerp(float a, float b, float v)
{
return (v - a) / (b - a);
}
float RawDepthToLinear(float rawDepth)
{
#if _ORTHO_ON
float persp = LinearEyeDepth(rawDepth, _ZBufferParams);
float orthoLinearDepth = _ProjectionParams.x > 0 ? rawDepth : 1 - rawDepth;
return lerp(_ProjectionParams.y, _ProjectionParams.z, orthoLinearDepth);
#else
return LinearEyeDepth(rawDepth, _ZBufferParams);
#endif
}
float SampleDepth(float2 uv)
{
return RawDepthToLinear(SampleRawDepth(uv));
}
float3 ProjectedWorldPos(GlobalData data, Varyings IN)
{
float3 rawWorldViewDir = _WorldSpaceCameraPos.xyz - data.worldPosition;
float4 rawScreenPos = ComputeScreenPos(IN.pos, _ProjectionParams.x);
float eyeDepth = data.refractionData.a;
float3 pos = rawWorldViewDir / rawScreenPos.a;
pos *= eyeDepth;
pos -= _WorldSpaceCameraPos.xyz;
return pos;
}
float3 ViewSpacePosAtPixelPosition(float2 pos, float2 offset)
{
float2 uv = pos * _CameraDepthTexture_TexelSize.xy + offset;
float3 viewSpaceRay = mul(unity_CameraInvProjection, float4(uv * 2.0 - 1.0, 1.0, 1.0) * _ProjectionParams.z).xyz;
float4 rawDepth = SampleSceneDepth(uv);//UNITY_SAMPLE_TEXTURE2D_LOD(_CameraDepthTexture, sampler_pointTextures_point_clamp, uv, 0.0);
return viewSpaceRay * Linear01Depth(rawDepth.r, _ZBufferParams);
}
float3 NormalFromDepthFast(float4 pos, float2 offset)
{
float3 vpc = ViewSpacePosAtPixelPosition(pos.xy, offset);
float3 vpl = ViewSpacePosAtPixelPosition(pos.xy + float2(-1, 0), offset);
float3 vpd = ViewSpacePosAtPixelPosition(pos.xy + float2(0, -1), offset);
// get view space normal from the cross product of the two smallest offsets
float3 viewNormal = normalize(-cross(vpc - vpd, vpc - vpl));
// transform normal from view space to world space
return mul((float3x3)unity_MatrixInvV, viewNormal);
// if needed, this will detect the sky
// float rawDepth = _CameraDepthTexture.Load(int3(i.pos.xy, 0)).r;
// if (rawDepth == 0.0)
// WorldNormal = float3(0,0,0);
}
half3 NormalFromDepth(float4 pos, float2 offset)
{
// get current pixel's view space position
half3 viewSpacePos_c = ViewSpacePosAtPixelPosition(pos.xy, offset);
// get view space position at 1 pixel offsets in each major direction
half3 viewSpacePos_l = ViewSpacePosAtPixelPosition(pos.xy + float2(-1.0, 0.0), offset);
half3 viewSpacePos_r = ViewSpacePosAtPixelPosition(pos.xy + float2(1.0, 0.0), offset);
half3 viewSpacePos_d = ViewSpacePosAtPixelPosition(pos.xy + float2(0.0, -1.0), offset);
half3 viewSpacePos_u = ViewSpacePosAtPixelPosition(pos.xy + float2(0.0, 1.0), offset);
// get the difference between the current and each offset position
half3 l = viewSpacePos_c - viewSpacePos_l;
half3 r = viewSpacePos_r - viewSpacePos_c;
half3 d = viewSpacePos_c - viewSpacePos_d;
half3 u = viewSpacePos_u - viewSpacePos_c;
// pick horizontal and vertical diff with the smallest z difference
half3 h = abs(l.z) < abs(r.z) ? l : r;
half3 v = abs(d.z) < abs(u.z) ? d : u;
// get view space normal from the cross product of the two smallest offsets
half3 viewNormal = normalize(cross(h, v));
// transform normal from view space to world space
return mul((float3x3)unity_MatrixInvV, viewNormal);
}
float3 WorldNormal(float3 t0, float3 t1, float3 t2, float3 bump)
{
return normalize(float3(dot(t0, bump), dot(t1, bump), dot(t2, bump)));
}
float3 HeightToNormal(float height, float3 normal, float3 pos)
{
float3 worldDirivativeX = ddx(pos);
float3 worldDirivativeY = ddy(pos);
float3 crossX = cross(normal, worldDirivativeX);
float3 crossY = cross(normal, worldDirivativeY);
float3 d = abs(dot(crossY, worldDirivativeX));
float3 inToNormal = ((((height + ddx(height)) - height) * crossY) + (((height + ddy(height)) - height) * crossX)) * sign(d);
inToNormal.y *= -1.0;
return normalize((d * normal) - inToNormal);
}
/*
float3 WorldToTangentNormalVector(Input IN, float3 normal) {
float3 t2w0 = WorldNormal(IN, float3(1, 0, 0));
float3 t2w1 = WorldNormal(IN, float3(0, 1, 0));
float3 t2w2 = WorldNormal(IN, float3(0, 0, 1));
float3x3 t2w = float3x3(t2w0, t2w1, t2w2);
return normalize(mul(t2w, normal));
}
*/
float DistanceFade(float depth, float pixelDepth, float start, float end)
{
float dist = ((depth - pixelDepth) - end) / (start - end);
return saturate(dist);
}
float2 OffsetUV(float2 uv, float2 offset)
{
#ifdef UNITY_Z_0_FAR_FROM_CLIPSPACE
uv.xy = offset + uv.xy;
//TODO: have to find use case
//uv.xy = offset * UNITY_Z_0_FAR_FROM_CLIPSPACE(IN.pos.z) + uv.xy;
#else
uv.xy = offset + uv.xy;
#endif
return uv.xy;
}
float2 OffsetDepth(float2 uv, float2 offset)
{
uv.xy = offset + uv.xy;
return uv.xy;
}
half2 DistortionUVs(half depth, float3 normalWS)
{
half3 viewNormal = mul((float3x3)GetWorldToHClipMatrix(), -normalWS).xyz;
return viewNormal.xz * saturate((depth) * 0.005);
}
float smootherstep(float x) {
x = saturate(x);
return saturate(x * x * x * (x * (6 * x - 15) + 10));
}
float InverseLerp(float A, float B, float T)
{
return (T - A) / (B - A);
}
float2 ProjectionUV(float4 captureParams, float3 worldPosition )
{
float3 simPos = worldPosition - captureParams.xyz;
float3 simScale = simPos / captureParams.w;
return simScale.xz + 0.5;
}
float2 CaptureUV(float2 worldPosition, float2 capturePosition, float2 captureSize)
{
float2 simPos = worldPosition.xy - capturePosition.xy;
float2 uv = simPos / captureSize.xy;
return uv.xy + 0.5;
}
float CaptureMask(float2 uv, float size)
{
float2 edgeMaskUV = abs(uv * 2 - 1);
float edgeMask = 1 - max(edgeMaskUV.x, edgeMaskUV.y);
float mask = smoothstep(0, size, edgeMask);
return saturate(mask);
}
void Applyfog(inout float3 color, float3 positionWS)
{
float3 inColor = color;
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
//float viewZ = -TransformWorldToView(TransformObjectToWorld(positionOS)).z;
float viewZ = -TransformWorldToView(positionWS).z;
float nearZ0ToFarZ = max(viewZ - _ProjectionParams.y, 0);
// ComputeFogFactorZ0ToFar returns the fog "occlusion" (0 for full fog and 1 for no fog) so this has to be inverted for density.
float density = 1.0f - ComputeFogIntensity(ComputeFogFactorZ0ToFar(nearZ0ToFarZ));
color = lerp(color, unity_FogColor.rgb, density);
#else
color = color;
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 6a5dbf27ffff7884881e93f714e77633
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,266 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_LIGHTING_INCLUDED
#define URPWATER_LIGHTING_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
// ShadowCoord: TransformWorldToShadowCoord(worldPosition);
// WorldNormal: TransformObjectToWorldNormal(v.normal);
// worldViewDir: SafeNormalize(_WorldSpaceCameraPos.xyz - worldPos)
// worldPos: TransformObjectToWorld(v.vertex.xyz);
// ShadowCoord: TransformWorldToShadowCoord(WorldPos);
// ShadowSample: MainLightRealtimeShadow(shadowCoord);
//From unity file:
//ShaderLibrary\Shadows.hlsl
half SampleShadows(Varyings IN, GlobalData data)
{
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord = IN.shadowCoord;
#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
float4 shadowCoord = TransformWorldToShadowCoord(data.worldPosition);
#else
float4 shadowCoord = float4(0, 0, 0, 0);
#endif
shadowCoord.xz += data.worldNormal.xz * 0.01;
return MainLightRealtimeShadow(shadowCoord);
}
void ComputeCaustics(out float3 causticColor, inout GlobalData data, Varyings IN, float3 Ambient)
{
#if UNITY_REVERSED_Z
real depth = data.rawDepthDst;
#else
// Adjust Z to match NDC for OpenGL ([-1, 1])
real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, data.rawDepthDst);
#endif
float3 worldUV = ComputeWorldSpacePosition(data.refractionUV, depth, UNITY_MATRIX_I_VP);
float causticFade = DistanceFade(data.refractionData.a, data.pixelDepth, _CausticsStart, _CausticsEnd);
#if _CAUSTICSMODE_3D
float3 speed = _CausticsSpeed3D.xyz * _Time.x;
float3 UV3D = worldUV * _CausticsTiling3D + speed;
float3 CausticMix = SAMPLE_TEXTURE3D(_CausticsTex3D, URPWater_linear_repeat_sampler, UV3D).rgb;
#else
float4 offsets = frac(_CausticsSpeed * _Time.x);
float3 CausticsA = SAMPLE_TEXTURE2D(_CausticsTex, URPWater_linear_repeat_sampler, worldUV.xz * _CausticsTiling.xy + offsets.xy).rgb;
float3 CausticsB = SAMPLE_TEXTURE2D(_CausticsTex, URPWater_linear_repeat_sampler, worldUV.xz * _CausticsTiling.zw + offsets.zw).rgb;
float3 CausticMix = min(CausticsA, CausticsB);
#endif
// Normals
#if _CAUSTICS_ANGLE_ON || _CAUSTICS_DIRECTION_ON
float3 angleMask = NormalFromDepthFast(IN.pos, data.refractionOffset);
#if _CAUSTICS_ANGLE_ON
causticFade *= saturate(angleMask.y * angleMask.y);
#endif
#if _CAUSTICS_DIRECTION_ON
float dotMask = dot(data.mainLight.direction, angleMask);
causticFade *= saturate(dotMask);
#endif
#endif
// Caustic projection for shadow
//Light mainLight = GetMainLight(TransformWorldToShadowCoord(worldUV));
//float shadow = mainLight.shadowAttenuation;
#if (defined(_MAIN_LIGHT_SHADOWS) || defined(_MAIN_LIGHT_SHADOWS_CASCADE) || defined(_MAIN_LIGHT_SHADOWS_SCREEN))
half shadow = SampleShadows(IN, data);
#else
half shadow = 1.0;
#endif
#if _FOAM_ON
causticFade *= 1 - data.foamMask * 2;
#endif
causticColor = CausticMix * max(0,_CausticsIntensity) * causticFade * (shadow + Ambient * 0.5);
}
// =================================================================
// Directional light computations
// =================================================================
float3 ComputeMainLightDiffuse(float3 direction, float3 worldNormal)
{
return saturate(dot(worldNormal, direction));
}
float3 ComputeMainLightSpecular(
Light mainLight,
float3 worldNormal,
float3 worldViewDir,
float3 specular,
float smoothness)
{
smoothness = exp2(10 * smoothness + 1);
/*
float reflectiveFactor = max(0.0, dot(-worldViewDir, reflect(mainLight.direction, worldNormal)));
float3 spec = pow(reflectiveFactor, smoothness);
return mainLight.color * specular * spec;
*/
// Unity spec
return LightingSpecular(mainLight.color, mainLight.direction, worldNormal, worldViewDir, float4(specular, 0), smoothness);
}
Light ComputeMainLight(float3 worldPos)
{
return GetMainLight(TransformWorldToShadowCoord(worldPos));
}
// =================================================================
// Additive lights computations
// =================================================================
float3 ComputeAdditiveLight(float3 worldPos, float3 worldNormal, float3 worldViewDir, float3 specColor, float smoothness)
{
smoothness = exp2(10 * smoothness + 1);
int pixelLightCount = GetAdditionalLightsCount();
float3 diffuse = float3(0, 0, 0);
float3 specularColor = float3(0, 0, 0);
for (int l = 0; l < pixelLightCount; ++l)
{
Light light = GetAdditionalLight(l, worldPos);
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
diffuse += LightingLambert(attenuatedLightColor, light.direction, worldNormal) * 0.25;
specularColor += LightingSpecular(attenuatedLightColor, light.direction, worldNormal, worldViewDir, half4(specColor, 0), smoothness);
}
return diffuse + specularColor;
}
void ComputeUnderWaterShading(inout GlobalData data, Varyings IN, float3 ambient)
{
float3 clearRefraction = data.refractionData.rgb;
#if _COLORMODE_COLORS
float3 shallowColor = _Color.rgb * data.refractionData.rgb;
float3 depthColor = _DepthColor.rgb * data.shadowColor.rgb;
data.refractionData.rgb = lerp(depthColor, shallowColor, data.depth);
#endif
#if _COLORMODE_GRADIENT
float3 ramp = SAMPLE_TEXTURE2D(_RefractionColor, URPWater_linear_clamp_sampler, float2(data.depth, 0.5)).rgb * data.shadowColor.rgb;
float3 shallowColor = ramp * data.refractionData.rgb;
data.refractionData.rgb = lerp(ramp, shallowColor, data.depth);
#endif
#if _DOUBLE_SIDED_ON
float3 underColor = lerp(clearRefraction.rgb * _UnderWaterColor.rgb, _UnderWaterColor.rgb, _UnderWaterColor.a);
data.refractionData.rgb = lerp(underColor, data.refractionData.rgb, data.vFace);
#endif
float invDepth = 1 - data.depth;
data.finalColor = lerp(data.refractionData.rgb, data.refractionData.rgb * saturate(data.mainLight.color) , invDepth);
}
// =================================================================
// Full lighting computations
// =================================================================
void ComputeLighting(inout GlobalData data, Varyings IN)
{
Light mainLight = data.mainLight;
float3 lightDir = mainLight.direction;
float3 lightColor = mainLight.color;
#if _DOUBLE_SIDED_ON
float3 flippedLight = mainLight.direction;
flippedLight.y = -flippedLight.y;
mainLight.direction = lerp(flippedLight, mainLight.direction, data.vFace);
#endif
//float3 mainDiffuse = ComputeMainLightDiffuse(lightDir.xyz, data.worldNormal) * lightColor * 0.5 + 0.5;
float3 mainSpecular = ComputeMainLightSpecular(mainLight, data.worldNormal, data.worldViewDir, _SpecColor.rgb, _Smoothness);
#if (defined(_MAIN_LIGHT_SHADOWS) || defined(_MAIN_LIGHT_SHADOWS_CASCADE) || defined(_MAIN_LIGHT_SHADOWS_SCREEN))
half shadow = SampleShadows(IN, data);
#else
half shadow = 1.0;
#endif
float shadowMask = shadow;//max(data.depth, shadow);
float3 ambient = SampleSH(data.worldNormal);
float3 AddLighting = ComputeAdditiveLight(data.worldPosition, data.worldNormal, data.worldViewDir, _SpecColor.rgb, _Smoothness);
// TODO: Try using ambient color again
//data.shadowColor = lerp(_AmbientColor.rgb, float3(1,1,1), saturate(shadow + data.depth));
// Shadow
data.shadowColor = saturate(shadowMask + ambient * 2.0);
// Underwater color
ComputeUnderWaterShading(data, IN, ambient);
//data.finalColor = lerp(data.refractionData.rgb, data.refractionData.rgb * saturate(lightColor) , invDepth);
#if _CAUSTICS_ON
float3 caustics = float3(1,1,1);
ComputeCaustics(caustics, data, IN, ambient);
#if _DOUBLE_SIDED_ON
caustics = lerp(0, caustics, data.vFace);
#endif
data.finalColor += data.finalColor * caustics * saturate(length(lightColor));
#endif
#if _SCATTERING_ON
data.finalColor.rgb += data.scattering * shadow;
#endif
#if _FOAM_ON
mainSpecular *= 1- saturate(data.foamMask.xxx);
half NdotL = dot(data.worldNormal, lightDir);
half3 foamColor = _FoamColor.rgb * saturate(NdotL) * lightColor * shadow + ambient;
data.finalColor = lerp(data.finalColor, foamColor, data.foamMask * _FoamColor.a);
#endif
// Specular and additive light after reflection
data.addLight = (AddLighting + mainSpecular) * data.shadowColor;
// Shadows
data.finalColor = data.finalColor * data.shadowColor;
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 9ad89349b47f7f94c98f3dd9939485c4
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,162 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_NORMALS_INCLUDED
#define URPWATER_NORMALS_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterHelpers.hlsl"
#include "URPWaterVariables.hlsl"
// Flow map
half4 SampleFlowMap(Texture2D tex, half2 texUV, Texture2D flowMap,half2 uv, half speed, half intensity)
{
half4 flowVal = (SAMPLE_TEXTURE2D(flowMap, URPWater_trilinear_repeat_sampler, uv) * 2 - 1) * intensity;
half dif1 = frac(_Time.x * speed + 0.5);
half dif2 = frac(_Time.x * speed);
half lerpVal = abs((0.5 - dif1) / 0.5);
half4 col1 = SAMPLE_TEXTURE2D(tex, URPWater_trilinear_repeat_sampler, texUV - flowVal.xy * dif1);
half4 col2 = SAMPLE_TEXTURE2D(tex, URPWater_trilinear_repeat_sampler, texUV - flowVal.xy * dif2);
return lerp(col1, col2, lerpVal);
}
// Global Normal Blend
half3 UnpackScaleNormal(half4 packednormal, half bumpScale) {
#if defined(UNITY_NO_DXT5nm)
return packednormal.xyz * 2 - 1;
#else
half3 normal;
normal.xy = (packednormal.wy * 2 - 1);
#if (SHADER_TARGET >= 30)
// SM2.0: instruction count limitation
// SM2.0: normal scaler is not supported
normal.xy *= bumpScale;
#endif
normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
return normal;
#endif
}
half3 BlendNormals(half3 n1, half3 n2)
{
return normalize(half3(n1.xy + n2.xy, n1.z * n2.z));
}
half3 NormalBlendReoriented(half3 A, half3 B)
{
half3 t = A.xyz + half3(0.0, 0.0, 1.0);
half3 u = B.xyz * half3(-1.0, -1.0, 1.0);
return (t / t.z) * dot(t, u) - u;
}
void ComputeNormals(inout GlobalData data, Varyings IN)
{
/*
half3 tangentNormal;
#if _NORMALSMODE_SINGLE
half4 nA = SAMPLE_TEXTURE2D(_NormalMapA, URPWater_trilinear_repeat_sampler, IN.texcoord.xy);
half3 normalA = UnpackNormal(nA);
normalA.xy *= -1;
tangentNormal = normalA;
#endif
half3 normalWS = TransformTangentToWorld(tangentNormal, data.tangentToWorld, true);
data.worldNormal = normalWS;
*/
#if _NORMALSMODE_FACET
half3 dpdx = ddx(data.worldPosition);
half3 dpdy = ddy(data.worldPosition);
data.worldNormal = normalize(cross(dpdy, dpdx));
#else
half3 tangentNormal;
#if _NORMALSMODE_SINGLE || _NORMALSMODE_DUAL
half4 nA = SAMPLE_TEXTURE2D(_NormalMapA, URPWater_trilinear_repeat_sampler, IN.texcoord.xy);
half4 nB = SAMPLE_TEXTURE2D(_NormalMapA, URPWater_trilinear_repeat_sampler, IN.texcoord.zw);
half3 normalA = UnpackScaleNormal(nA, _NormalMapAIntensity);
half3 normalB = UnpackScaleNormal(nB, _NormalMapAIntensity);
tangentNormal = NormalBlendReoriented(normalA, normalB);
#if _NORMALSMODE_DUAL
half3 normalC = UnpackScaleNormal(SAMPLE_TEXTURE2D(_NormalMapB, URPWater_trilinear_repeat_sampler, IN.texcoord1.xy), _NormalMapBIntensity);
tangentNormal = NormalBlendReoriented(tangentNormal, normalC);
#endif
#endif
#if _NORMALSMODE_FLOWMAP
half4 flowNormal = SampleFlowMap(_NormalMapA, IN.texcoord.xy, _FlowMap, IN.texcoord.zw, _FlowSpeed, _FlowIntensity);
tangentNormal = UnpackNormalScale(flowNormal, _NormalMapAIntensity);
#endif
// Far Normals
#if _NORMAL_FAR_ON
half4 nfA = SAMPLE_TEXTURE2D(_NormalMapFar, URPWater_trilinear_repeat_sampler, IN.texcoord2.xy);
half4 nfB = SAMPLE_TEXTURE2D(_NormalMapFar, URPWater_trilinear_repeat_sampler, IN.texcoord2.zw);
half3 normalFarA = UnpackScaleNormal(nfA, _NormalMapFarIntensity);
half3 normalFarB = UnpackScaleNormal(nfB, _NormalMapFarIntensity);
half3 farNormals = NormalBlendReoriented(normalFarA, normalFarB);
tangentNormal = lerp(tangentNormal, farNormals, IN.texcoord3.x);
#endif
// Dynamic Normal
#if _DYNAMIC_EFFECTS_ON
half4 normalDynamic = data.dynamicData;
normalDynamic = normalDynamic * 2 - 1;
normalDynamic.xy *= _DynamicNormal.xx * data.dynamicData.aa;
#if defined(UNITY_NO_DXT5nm)
normalDynamic.b = 1;
#else
normalDynamic.b = sqrt(1.0 - saturate(dot(normalDynamic.xy, normalDynamic.xy)));
#endif
tangentNormal = NormalBlendReoriented(tangentNormal, normalDynamic.rgb);
#endif
#if _DISPLACEMENTMODE_GERSTNERPLUS
//tangentNormal.xy *= -1;
#endif
// Combined tangent to world
half3 normalWS = TransformTangentToWorld(tangentNormal, data.tangentToWorld);
data.worldNormal = normalize(normalWS);
#endif
#if _DOUBLE_SIDED_ON
half3 flippedNormals = data.worldNormal;
flippedNormals.y = -flippedNormals.y;
data.worldNormal = lerp(flippedNormals, data.worldNormal, data.vFace);
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: ddc359a7f42bdac4d8cd8323946ab8cb
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,110 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_REFlECTIONS_INCLUDED
#define URPWATER_REFlECTIONS_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
float3 SampleReflections(float3 worldNormal, GlobalData data, float2 screenUV)
{
float3 reflection = 0;
float2 refOffset = 0;
float3 worldViewDir = data.worldViewDir;
#if _REFLECTIONMODE_CUBEMAP
float3 reflectVector = reflect(-worldViewDir, worldNormal);
reflection = SAMPLE_TEXTURECUBE(_CubemapTexture, sampler_CubemapTexture, reflectVector).rgb;
#elif _REFLECTIONMODE_PROBES
float3 reflectVector = reflect(-worldViewDir, worldNormal);
//reflection = GlossyEnvironmentReflection(reflectVector, data.worldPosition, _ReflectionRoughness, 1, screenUV);
//reflection = GlossyEnvironmentReflection(reflectVector, _ReflectionRoughness, 1);
#if USE_FORWARD_PLUS
reflection = SAMPLE_TEXTURECUBE_LOD(_GlossyEnvironmentCubeMap, sampler_GlossyEnvironmentCubeMap, reflectVector, _ReflectionRoughness).rgb;
#else
reflection = DecodeHDREnvironment(SAMPLE_TEXTURECUBE_LOD(unity_SpecCube0, samplerunity_SpecCube0, reflectVector, _ReflectionRoughness), unity_SpecCube0_HDR);
#endif
#elif _REFLECTIONMODE_REALTIME
/*
// get the perspective projection
float2 p11_22 = float2(unity_CameraInvProjection._11, unity_CameraInvProjection._22) * 10;
// conver the uvs into view space by "undoing" projection
float3 viewDir = -(float3((screenUV * 2 - 1) / p11_22, -1));
half3 viewNormal = mul(worldNormal, (float3x3)GetWorldToViewMatrix()).xyz;
half3 reflectVector = reflect(-viewDir, viewNormal);
*/
float3 reflectVector = reflect(-worldViewDir, worldNormal);
float2 reflectionUV = screenUV + worldNormal.zx * float2(0.02, 0.15);
reflection = SAMPLE_TEXTURE2D_LOD(_ReflectionTexture, sampler_ScreenTextures_linear_clamp, reflectionUV, 6 * _ReflectionRoughness).rgb;//planar reflection
#endif
return reflection;
}
void ComputeReflections(inout GlobalData data, Varyings IN)
{
#if _REFLECTIONMODE_CUBEMAP || _REFLECTIONMODE_PROBES || _REFLECTIONMODE_REALTIME
// Reflection Distortion
float3 reflectionNormal = data.worldNormal;
float3 n = IN.normal.xyz;
//n.xz *= _ReflectionDistortion;
reflectionNormal = lerp(n, reflectionNormal, _ReflectionDistortion);
float3 reflection = SampleReflections(reflectionNormal, data, data.screenUV.xy);
// Fresnel Distortion
#if _NORMAL_FADE_ON
float fresnelFade = IN.paramsA.x * _ReflectionFresnelNormal;
#else
float fresnelFade = _ReflectionFresnelNormal;
#endif
// Fresnel mask
float3 fresnelNormal = lerp(IN.normal.xyz, data.worldNormal, fresnelFade);
float fresnel = 1 - dot(fresnelNormal, data.worldViewDir);
fresnel = pow(saturate(fresnel), _ReflectionFresnel);
float reflectionMask = _ReflectionIntensity;
#if _FOAM_ON
reflectionMask *= 1 - data.foamMask * 2;
#endif
//fresnel = smoothstep(0, _ReflectionFresnel, saturate(fresnel));
float3 finalReflection = lerp(data.finalColor, saturate(reflection), fresnel * saturate(reflectionMask));
#if _DOUBLE_SIDED_ON
data.finalColor = lerp(data.finalColor, finalReflection, data.vFace);
#else
data.finalColor = finalReflection;
#endif
#endif
data.finalColor += data.addLight;
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 8d0fec0b0a6bdc2408756ab82d6d0355
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,85 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_REFRACTION_INCLUDED
#define URPWATER_REFRACTION_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
void ComputeOpaqueAndDepth(inout GlobalData data, out float4 clearData, out float4 refractionData, out float2 refractionOffset)
{
// ======================================================================
// Check if better way here:
//https://catlikecoding.com/unity/tutorials/flow/looking-through-water/
// Clean Data
//clearData.rgb = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture_linear_clamp, data.screenUV.xy).rgb; // Color
float2 screenUV = data.screenUV.xy;
//UNITY_SAMPLE_SCREENSPACE_TEXTURE(_CameraOpaqueTexture, sampler_CameraOpaqueTexture_linear_clamp, screenUV);
clearData.rgb = SampleSceneColor(screenUV); // Color
clearData.a = SampleDepth(screenUV); // Depth
// Distorted Data
float2 distortionAmount = _CameraOpaqueTexture_TexelSize.xy * _Distortion.xx;
// Far Distortion
float farDistance = saturate( 1 - InvLerp(0.0, 50.0, data.pixelDepth) );
distortionAmount = lerp(distortionAmount * 0.25, distortionAmount, farDistance);
#if _DOUBLE_SIDED_ON
distortionAmount = lerp(distortionAmount * 0.5, distortionAmount, data.vFace);
#endif
float2 offset = data.worldNormal.xz * distortionAmount;
float2 GrabUV = OffsetUV(data.screenUV.xy, offset);
float2 DepthUV = OffsetDepth(data.screenUV.xy, offset);
float4 distortedData;
distortedData.rgb = SampleSceneColor(GrabUV); // Color
float rawDistortedDepth = SampleRawDepth(DepthUV); // Raw to linear
distortedData.a = RawDepthToLinear(rawDistortedDepth); // Depth
refractionData = data.pixelDepth > distortedData.a ? clearData : distortedData;
refractionOffset = offset;
data.refractionUV = DepthUV;
data.rawDepthDst = rawDistortedDepth;
}
void ComputeRefractionData(inout GlobalData data)
{
float4 clearData;
float4 refractionData;
float2 refractionOffset;
float rawDepth;
float3 subNormal;
ComputeOpaqueAndDepth(data, clearData, refractionData, refractionOffset);
data.depth = DistanceFade(refractionData.a, data.pixelDepth, _DepthStart, _DepthEnd);
// Compositing in lighting
data.refractionData = refractionData;
data.clearColor.rgb = clearData.rgb;
data.sceneDepth = clearData.a;
data.refractionOffset = refractionOffset;
//data.debug.rgb = refractionData.xyz * float3(1,0,0);
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 80e55437c098b2346889e5a8cfc15757
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,90 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_SCATTERING_INCLUDED
#define URPWATER_SCATTERING_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterHelpers.hlsl"
void ComputeScattering(inout GlobalData data, Varyings IN)
{
#if _SCATTERING_ON
Light mainLight = data.mainLight;
half3 lightColor = mainLight.color;
// ==================================
// Scattering
// ==================================
half3 L = mainLight.direction;
half3 V = data.worldViewDir;
half3 N = lerp(half3(0,1,0), data.worldNormal, _CapsScatterNormals * 0.1);
#if _DOUBLE_SIDED_ON
L.y = lerp(-L.y, L.y, data.vFace);
#endif
half3 H = normalize(L + N);
half VdotH = max(0, dot(V, -H));
VdotH = smoothstep(_ScatteringRangeMin, _ScatteringRangeMax, VdotH);
half scatterMask = saturate(VdotH * _ScatteringIntensity);
// ===================================
// Caps Scattering
// ===================================
#if _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
#if _CAPS_SCATTERING_ON
half capsMask = smoothstep(_CapsScatteringRangeMin, _CapsScatteringRangeMax, saturate(IN.texcoord3.y));
half3 capsNormals = lerp(half3(0, 1, 0), data.worldNormal, _CapsScatterNormals);
//half NdotL = saturate(dot(IN.normal.xyz, -L) * 0.5 + 0.5);
half NdotV = saturate(dot(IN.normal.xyz, V));
half NdotV2 = saturate(dot(capsNormals, V));
half fresnel = max(0, pow(NdotV,4) * NdotV2 );
half topDownMask = saturate(dot(float3(0, 1, 0), -GetViewForwardDir()));
fresnel *= 1 - smoothstep(0.5, 1, topDownMask);
half finalCapsMask = saturate(fresnel) * capsMask * _CapsScatteringIntensity;
scatterMask += finalCapsMask;
//data.debug.rgb = scatterMask;
#endif
#endif
// ==================================
// Output
// ==================================
//data.debug.rgb = scatterMask;
half3 scatterColor = _ScatteringColor.rgb * saturate(scatterMask);
data.scattering = scatterColor * saturate(lightColor);
#if _DOUBLE_SIDED_ON
data.scattering = lerp(data.scattering * 0.25, data.scattering, data.vFace);
#endif
#endif
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 0a7a895ac4e263245a0ca47c6eff3eb4
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,306 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_VARIABLES_INCLUDED
#define URPWATER_VARIABLES_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
#define URP_WATER_PI 3.1415926535
TEXTURE2D(_CausticsTex);
TEXTURE3D(_CausticsTex3D);
TEXTURE2D(_FoamTex);
TEXTURE2D(_NormalMapA);
TEXTURE2D(_NormalMapB);
TEXTURE2D(_NormalMapFar);
TEXTURE2D(_FlowMap);
//UNITY_DECLARE_SCREENSPACE_TEXTURE_FLOAT(_CameraDepthTexture); // VR
//UNITY_DECLARE_SCREENSPACE_TEXTURE(_CameraOpaqueTexture); // VR
TEXTURE2D(_RefractionColor);
TEXTURE2D(_DynamicEffectsTexture);
SAMPLER(sampler_CameraOpaqueTexture_linear_clamp);
SAMPLER(sampler_ScreenTextures_linear_clamp);
SAMPLER(sampler_pointTextures_point_clamp);
TEXTURE2D(_ReflectionTexture);
TEXTURECUBE(_CubemapTexture);
SAMPLER(sampler_CubemapTexture);
SamplerState URPWater_trilinear_repeat_sampler;
SamplerState URPWater_linear_repeat_sampler;
SamplerState URPWater_linear_clamp_sampler;
#if UNITY_VERSION <= 202310 || UNITY_VERSION < 202320
float4 _CameraOpaqueTexture_TexelSize;
float4 _CameraDepthTexture_TexelSize;
#endif
float4 _DynamicEffectsTexture_TexelSize;
float4 _DynamicEffectsParams;
UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
float4 _NormalMapA_ST;
float4 _NormalMapASpeeds;
float4 _NormalMapATilings;
float4 _NormalMapB_ST;
float4 _NormalMapBSpeeds;
float4 _NormalMapBTilings;
float4 _NormalMapFarTilings;
float4 _NormalMapFarSpeeds;
float4 _FlowTiling;
float4 _CausticsTiling;
float4 _Color;
float4 _DepthColor;
float4 _UnderWaterColor;
//float4 _AmbientColor;
float4 _SpecColor;
float4 _CausticsSpeed;
float4 _CausticsSpeed3D;
float4 _ScatteringColor;
float4 _FoamColor;
float4 _FoamTiling;
float4 _OceanInfos;
float _CausticsStart;
float _CausticsEnd;
float _CausticsTiling3D;
float _CausticsDistortion;
float _Distortion;
float _Smoothness;
float _NormalMapAIntensity;
float _NormalMapBIntensity;
float _NormalMapFarIntensity;
float _NormalFarDistance;
float _FlowSpeed;
float _FlowIntensity;
float _DepthStart;
float _DepthEnd;
float _ReflectionDistortion;
float _ReflectionFresnelNormal;
float _ReflectionRoughness;
float _EdgeSize;
float _CausticsIntensity;
float _ReflectionFresnel;
float _ReflectionIntensity;
float _FoamSize;
float _FoamRippleDistance;
float _FoamRippleSize;
float _FoamRippleSpeed;
float _FoamDistortion;
float _FoamCapsRangeMin;
float _FoamCapsRangeMax;
float _FoamCapsIntensity;
float _ScatteringIntensity;
float _ScatteringRangeMin;
float _ScatteringRangeMax;
float _CapsScatteringIntensity;
float _CapsScatteringRangeMin;
float _CapsScatteringRangeMax;
float _CapsScatterNormals;
float _DynamicNormal;
float _DynamicFoam;
float _DynamicDisplacement;
float _WaveNormal;
float _WaveEffectsBoost;
float _WaveCount;
float _WaveLength;
float _WaveAmplitude;
float _WaveSteepness;
float _WaveSpeed;
float _WaveStartDistance;
float _WaveEndDistance;
float _ClustersCount;
float _WavesPerCluster;
float4 _WaveParams1;
float4 _WaveParams2;
float4 _WaveParams3;
float4 _WaveParams4;
float4 _WaveOffsets;
float _Tess;
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
struct Attributes
{
float4 vertex : POSITION;
float4 color : COLOR;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord : TEXCOORD0;
#if _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
float waveHeight : TEXCOORD1;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 pos : SV_POSITION;
half4 color : COLOR;
half4 normal : NORMAL;
half4 tangent : TANGENT;
half4 binormal : BINORMAL;
float4 texcoord : TEXCOORD0;
#if _NORMALSMODE_DUAL || _FOAM_ON
float4 texcoord1 : TEXCOORD1;
#endif
#if _NORMAL_FAR_ON
float4 texcoord2 : TEXCOORD2;
#endif
// X: Far Distance, Y: Waves Height Z: W:
#if _NORMAL_FAR_ON || _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
float4 texcoord3 : TEXCOORD3;
#endif
float4 screenCoord : TEXCOORD4;
#if _DYNAMIC_EFFECTS_ON
float2 projectionUV : TEXCOORD5;
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
float4 shadowCoord : TEXCOORD6;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
void InitializeVertexData(inout Varyings IN)
{
IN.pos = float4(0, 0, 0, 0);
IN.color = half4(0, 0, 0, 0);
IN.normal = half4(0, 1, 0, 0);
IN.tangent = half4(1, 0, 0, 0);
IN.binormal = half4(0, 0, 1, 0);
IN.texcoord = float4(0, 0, 0, 0);
IN.screenCoord = float4(0, 0, 0, 0);
#if _NORMALSMODE_DUAL || _FOAM_ON
IN.texcoord1 = float4(0, 0, 0, 0);
#endif
#if _NORMAL_FAR_ON
IN.texcoord2 = float4(0, 0, 0, 0);
#endif
#if _NORMAL_FAR_ON || _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
IN.texcoord3 = float4(0, 0, 0, 0);
#endif
#if _DYNAMIC_EFFECTS_ON
float2 projectionUV = float2(0, 0);
#endif
#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
IN.shadowCoord = float4(0, 0, 0, 0);
#endif
}
struct GlobalData
{
float depth; // Remapped Depth
float sceneDepth; // Linear Depth
float rawDepthDst; // Raw Depth Distorted
float pixelDepth;
float foamMask;
float2 refractionOffset;
float2 refractionUV;
float3 finalColor;
float4 refractionData; // RGB: Refraction Color A: Refraction Depth
float3 clearColor; // RGB: Clear Color
float3 shadowColor;
float3 worldPosition;
float3 worldNormal;
float3 worldViewDir;
float4 screenUV;
#if _DYNAMIC_EFFECTS_ON
float4 dynamicData;
#endif
#if _DOUBLE_SIDED_ON
float vFace;
#endif
#if _SCATTERING_ON
float3 scattering;
#endif
Light mainLight;
float3 addLight;
float3x3 tangentToWorld;
// Debug purpose only
float4 debug;
};
void InitializeGlobalData(inout GlobalData data, Varyings IN)
{
data.worldPosition = float3(IN.normal.w, IN.tangent.w, IN.binormal.w);
data.worldNormal = float3(0, 1, 0);
data.worldViewDir = GetWorldSpaceNormalizeViewDir(data.worldPosition);//SafeNormalize(_WorldSpaceCameraPos.xyz - data.worldPosition);
data.screenUV = float4(IN.screenCoord.xyz / IN.screenCoord.w, IN.pos.z); //ComputeScreenPos(TransformWorldToHClip(data.worldPosition), _ProjectionParams.x);
data.depth = 0;
data.sceneDepth = 0;
data.rawDepthDst = 0;
data.pixelDepth = -TransformWorldToView(data.worldPosition).z; //IN.screenCoord.a;
data.foamMask = 0;
data.refractionOffset = float2(0, 0);
data.refractionUV = float2(0, 0);
data.refractionData = float4(0, 0, 0 ,0);
data.clearColor = float3(1, 1, 1);
data.finalColor = float3(1, 1, 1);
data.shadowColor = float3(1, 1, 1);
#if _DYNAMIC_EFFECTS_ON
data.dynamicData = float4(0, 0, 0, 0);
#endif
#if _DOUBLE_SIDED_ON
data.vFace = 1;
#endif
#if _SCATTERING_ON
data.scattering = float3(0,0,0);
#endif
data.mainLight = GetMainLight(TransformWorldToShadowCoord(data.worldPosition));
data.addLight = float3(0, 0, 0);
data.tangentToWorld = float3x3(IN.tangent.xyz, IN.binormal.xyz, IN.normal.xyz);
data.debug = float4 (0, 0, 0, 1);
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 78a00af59a825aa4099121e1896f9a1b
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,94 @@
//-----------------------------------------------------------------------------
// Copyright(C) Yan Verde - All Rights Reserved
// Copyright protected under Unity Asset Store EULA
// Refer to https://unity3d.com/legal/as_terms for more informations
// -----------------------------------------------------------------------------
// URP Water
// Author : Yan Verde
// Date : April 10, 2021
// -----------------------------------------------------------------------------
#ifndef URPWATER_WAVE_INCLUDED
#define URPWATER_WAVE_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "URPWaterHelpers.hlsl"
#include "URPWaterVariables.hlsl"
#include "URPWaterGerstnerSimple.hlsl"
#include "URPWaterGerstnerPlus.hlsl"
void ComputeWaves(inout Attributes v, inout half3 outNormal, inout half3 outBinormal, inout half3 outTangent)
{
half3 WavesNormal = outNormal;
half3 worldBinormal = outBinormal;
half3 worldTangent = outTangent;
#if _DISPLACEMENTMODE_GERSTNER || _DISPLACEMENTMODE_GERSTNERPLUS
float3 WavesOffset;
float3 wPos = TransformObjectToWorld(v.vertex.xyz);
#if _DISPLACEMENTMODE_GERSTNER
ComputeGersterWaves(wPos, WavesOffset, WavesNormal);
#endif
#if _DISPLACEMENTMODE_GERSTNERPLUS
ComputeGerstnerWavesPlus(wPos, WavesOffset, WavesNormal, worldBinormal, worldTangent);
#endif
float amplitude = _WaveAmplitude;
// Distance Fade
float cameraDistance = length(_WorldSpaceCameraPos - wPos);
float distanceMask = 1.0 - saturate(InverseLerp(_WaveStartDistance, _WaveEndDistance, cameraDistance));
amplitude *= distanceMask;
#if _DISPLACEMENT_MASK_ON
amplitude *= v.color.b;
#endif
#if _OCEAN_MASK_ON
float2 pos = _OceanInfos.xy;
float2 size = _OceanInfos.zw;
float2 uv = CaptureUV(wPos.xz, pos, size);
amplitude *= CaptureMask(uv, 0.4);
#endif
// TODO: Calculate waveHeight only if caps scattering or white caps
/*
float waveHeight = max(0.01, WavesOffset.y - v.vertex.y);
float diff = (WavesOffset.y + _WaveEffectsBoost) - v.vertex.y;
float dist = distance(v.vertex.xyz, v.vertex.xyz + WavesOffset.xz);
v.waveHeight = (dist * diff) / waveHeight;
v.waveHeight = saturate(v.waveHeight);
v.waveHeight *= amplitude;
*/
//float wavesBound = v.vertex.y
float waveLength = (WavesOffset.y + _WaveEffectsBoost) - v.vertex.y;
float waveHeight = max(0.01, waveLength);
float l = length(WavesOffset.xyz);
v.waveHeight = saturate(waveHeight * amplitude);
v.vertex.xyz = TransformWorldToObject(wPos + WavesOffset * amplitude.xxx);
WavesNormal = lerp(v.normal, WavesNormal, amplitude * _WaveNormal);
#if _DISPLACEMENTMODE_GERSTNERPLUS
worldBinormal = lerp(outBinormal, worldBinormal, amplitude);
worldTangent = lerp(outTangent, worldTangent, amplitude);
#endif
#endif
outNormal = WavesNormal;
outBinormal = worldBinormal;
outTangent = worldTangent;
}
#endif

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2ae3669dea0e8cd4cbdfe165809ef157
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant: