升级水插件

This commit is contained in:
2026-01-31 00:32:49 +08:00
parent a739d2fe3b
commit 4123e83573
293 changed files with 13449 additions and 2853 deletions

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestTransferWaves
#pragma multi_compile_local __ d_Texture d_TextureBlend
@@ -11,6 +13,7 @@
#include "HLSLSupport.cginc"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Filtering.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Constants.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
@@ -32,6 +35,8 @@ float _Crest_RespectShallowWaterAttenuation;
float _Crest_MaximumAttenuationDepth;
float _Crest_WaveResolutionMultiplier;
float _Crest_TransitionalWavelengthThreshold;
uint _Crest_Resolution;
bool _Crest_SeaLevelOnly;
// Texture
#if d_Texture
@@ -60,6 +65,8 @@ void TransferWaves(uint3 id)
const uint last = parameters.y;
const half transition = parameters.w;
const uint2 resolution = uint2(_Crest_Resolution, _Crest_Resolution);
#if !d_TextureBlend
// Additive only. All wavelengths filtered out for this LOD so nothing to do.
if (parameters.x < 0 || parameters.y < 0)
@@ -154,11 +161,12 @@ void TransferWaves(uint3 id)
axisZ1.x = -axisX1.y; axisZ1.y = axisX1.x;
}
#else
const float2 positionWaves = float2(dot(positionWS, _Crest_AxisX), dot(positionWS, float2(-_Crest_AxisX.y, _Crest_AxisX.x)));
#endif // d_Texture
const half depth = Cascade::MakeDepth(slice0).SampleSignedDepthFromSeaLevel(positionWS) +
Cascade::MakeLevel(slice0).SampleLevel(positionWS);
const half level = Cascade::MakeLevel(slice0).SampleLevel(positionWS);
const half depth = Cascade::MakeDepth(slice0).SampleSignedDepthFromSeaLevel(positionWS) + level;
half3 _displacement = 0.0;
@@ -217,8 +225,8 @@ void TransferWaves(uint3 id)
const float2 uv1 = float2(dot(positionScaledWS, axisX1), dot(positionScaledWS, axisZ1));
// Sample displacement, rotate into frame.
float3 displacement0 = _Crest_WaveBuffer.SampleLevel(sampler_Crest_linear_repeat, float3(uv0, waveBufferIndex), 0).xyz;
float3 displacement1 = _Crest_WaveBuffer.SampleLevel(sampler_Crest_linear_repeat, float3(uv1, waveBufferIndex), 0).xyz;
const float3 displacement0 = Utility::SampleBicubicRepeat(_Crest_WaveBuffer, uv0, resolution, waveBufferIndex).xyz;
const float3 displacement1 = Utility::SampleBicubicRepeat(_Crest_WaveBuffer, uv1, resolution, waveBufferIndex).xyz;
float3 displacement = lerp(displacement0, displacement1, t);
displacement.xz = displacement.x * axis + displacement.z * float2(-axis.y, axis.x);
@@ -227,7 +235,9 @@ void TransferWaves(uint3 id)
#else // !d_Texture
// Sample displacement, rotate into frame defined by global wind direction.
half3 displacement = _Crest_WaveBuffer.SampleLevel(sampler_Crest_linear_repeat, float3(positionWaves / waveBufferSize, waveBufferIndex), 0).xyz;
// Hardware bilinear produces small noise artifacts. Custom bilinear solves that,
// but we still get texel artifacts that show up in normals, so use bicubic.
float3 displacement = Utility::SampleBicubicRepeat(_Crest_WaveBuffer, positionWaves / waveBufferSize, resolution, waveBufferIndex).xyz;
displacement.xz = displacement.x * _Crest_AxisX + displacement.z * float2(-_Crest_AxisX.y, _Crest_AxisX.x);
_displacement += displacement * weight;
#endif // d_Texture
@@ -239,6 +249,13 @@ void TransferWaves(uint3 id)
_Crest_Target[id] *= 1.0 - saturate(alpha);
#endif
#if !d_Texture
if (_Crest_SeaLevelOnly)
{
_displacement *= saturate(1.0 - abs(level));
}
#endif
// Always write full alpha so textures show up in previews.
_Crest_Target[id] += float4(_displacement, 1.0);
}

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#pragma multi_compile_local d_Sphere d_Cube d_Rectangle

View File

@@ -3,6 +3,8 @@
// Adds clipping from a provided texture. Used by Painted and Texture input modes.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"

View File

@@ -6,6 +6,8 @@
// afterwards. It is converted to world-space in another shader before writing into
// the LOD data.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestCopy
#pragma kernel CrestFill
@@ -17,7 +19,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
Texture2D<float> _CamDepthBuffer;
RWTexture2D<float2> _Crest_Target;
RWTexture2D<m_Float2> _Crest_Target;
#if d_Crest_BackFaceInclusion
Texture2D<float> _Crest_CameraDepthBufferBackfaces;

View File

@@ -5,6 +5,8 @@
// from an ODC, then they are in object-space and are converted to world-space as
// the LOD data stores world-space height.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#pragma multi_compile_local __ d_CrestSDF
@@ -22,14 +24,14 @@
#if d_CrestSDF
#undef m_CrestType
#define m_CrestType float2
#define m_CrestType m_Float2
#endif
Texture2D<m_CrestType> _Crest_Texture;
RWTexture2DArray<m_CrestType> _Crest_Target;
CBUFFER_START(CrestInputTexture)
float2 _Crest_Multiplier;
m_CrestType _Crest_Multiplier;
float2 _Crest_TextureSize;
float2 _Crest_TexturePosition;
float2 _Crest_TextureRotation;

View File

@@ -3,6 +3,8 @@
// Adds flow from a provided texture. Used by Painted and Texture input modes.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"
@@ -15,7 +17,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Cascade.hlsl"
Texture2D _Crest_Texture;
RWTexture2DArray<float2> _Crest_Target;
RWTexture2DArray<m_Float2> _Crest_Target;
CBUFFER_START(CrestPerMaterial)
int _Crest_Blend;
@@ -62,7 +64,8 @@ void Execute(uint3 id)
}
const float2 target = _Crest_Target[id];
_Crest_Target[id] = Blend(_Crest_Blend, weight, 1.0, source, target);
const float2 result = Blend(_Crest_Blend, weight, 1.0, source, target);
_Crest_Target[id] = m_Float2FromFloat2(result);
}
m_CrestNameSpaceEnd

View File

@@ -3,6 +3,8 @@
// Adds foam from a provided texture. Used by Painted and Texture input modes.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"

View File

@@ -4,6 +4,8 @@
// An implementation of the Jump Flood algorithm by Rong and Tan
// Source: https://www.comp.nus.edu.sg/~tants/jfa.html
#pragma exclude_renderers glcore gles3
#pragma kernel CrestInitialize
#pragma kernel CrestExecute
#pragma kernel CrestApply
@@ -27,13 +29,13 @@ float _Crest_WaterLevel;
CBUFFER_END
// Holds scene depth for initialization.
Texture2D<float2> _Crest_Source;
RWTexture2D<float2> _Crest_Target;
Texture2D<m_Float2> _Crest_Source;
RWTexture2D<m_Float2> _Crest_Target;
// Setting this to zero means that geometry at exactly the origin won't be handled
// gracefully - but it would only affect a single-pixel in the worst-case and would
// doubtfully be noticable anyway. Use infinity instead.
#define m_CrestUninitializedPosition float2(m_Crest_PositiveInfinity, m_Crest_PositiveInfinity)
#define m_CrestUninitializedPosition m_Float2Constructor(m_Crest_PositiveInfinity, m_Crest_PositiveInfinity)
#if d_Crest_Inverted
#define m_DepthCheck depth > 0.0
@@ -66,7 +68,7 @@ void Initialize(const uint3 id)
depth += lerp(Cascade::MakeLevel(slice0).SampleLevel(position), Cascade::MakeLevel(slice1).SampleLevel(position), alpha);
#endif
_Crest_Target[id.xy] = m_DepthCheck ? position : m_CrestUninitializedPosition;
_Crest_Target[id.xy] = m_DepthCheck ? m_Float2FromFloat2(position) : m_CrestUninitializedPosition;
}
void Execute(const uint3 id)
@@ -111,7 +113,7 @@ void Execute(const uint3 id)
}
}
_Crest_Target[id.xy] = nearest;
_Crest_Target[id.xy] = m_Float2FromFloat2(nearest);
}
void Apply(const uint3 id)
@@ -144,7 +146,7 @@ void Apply(const uint3 id)
result.y = distance;
_Crest_Target[id.xy] = result;
_Crest_Target[id.xy] = m_Float2FromFloat2(result);
}
m_CrestNameSpaceEnd

View File

@@ -3,6 +3,8 @@
// Adds height from a provided texture. Used by Painted and Texture input modes.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#pragma multi_compile_local __ d_CatmullRom
@@ -27,7 +29,7 @@ float _Crest_Multiplier;
float2 _Crest_TextureSize;
float2 _Crest_TexturePosition;
float2 _Crest_TextureRotation;
float2 _Crest_Resolution;
float2 _Crest_TextureResolution;
CBUFFER_END
m_CrestNameSpace
@@ -49,7 +51,7 @@ void Execute(uint3 id)
}
#if d_CatmullRom
const float source = Utility::SampleTextureCatmullRom(_Crest_Texture, LODData_linear_clamp_sampler, uv, _Crest_Resolution)
const float source = Utility::SampleTextureCatmullRom(_Crest_Texture, LODData_linear_clamp_sampler, uv, _Crest_TextureResolution)
#else
const float source = _Crest_Texture.SampleLevel(LODData_linear_clamp_sampler, uv, 0.0)
#endif

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
#include "HLSLSupport.cginc"
@@ -12,7 +14,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/InputsDriven.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Cascade.hlsl"
RWTexture2DArray<float2> _Crest_Target;
RWTexture2DArray<m_Float2> _Crest_Target;
CBUFFER_START(CrestPerWaterInput)
float3 _Crest_Position;
@@ -116,7 +118,7 @@ void Execute(uint3 id)
// Helps interaction to work at different scales
acceleration /= minimumWavelength;
_Crest_Target[id] = float2(_Crest_Target[id].x, _Crest_Target[id].y + acceleration * _Crest_SimDeltaTime);
_Crest_Target[id] = m_Float2Constructor(_Crest_Target[id].x, _Crest_Target[id].y + acceleration * _Crest_SimDeltaTime);
}
m_CrestNameSpaceEnd

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestPackLevel
#include "HLSLSupport.cginc"

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestQueryDisplacement d_CrestDisplacement
#pragma kernel CrestQueryFlow d_CrestFlow
#pragma kernel CrestQueryDepth d_CrestDepth

View File

@@ -3,6 +3,8 @@
// Merged into Query.compute.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
[numthreads(1, 1, 1)]
void CrestExecute(uint3 id : SV_DispatchThreadID) { }

View File

@@ -3,6 +3,8 @@
// Merged into Query.compute.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestExecute
[numthreads(1, 1, 1)]
void CrestExecute(uint3 id : SV_DispatchThreadID) { }

View File

@@ -1,21 +1,19 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
// Compute shader to perform combine of displacements. Reads and writes to texture array which saves
// needing to do ping pong of render targets. Unfortunately reading/writing float4s is not supported
// on pre-DX11.3 hardware (aka typed UAV loads), so this path is not the default, for now..
#pragma exclude_renderers glcore gles3
#pragma kernel ShapeCombine
#pragma kernel ShapeCombine_DISABLE_COMBINE _DISABLE_COMBINE
#pragma kernel ShapeCombine_FLOW_ON _FLOW_ON
#pragma kernel ShapeCombine_FLOW_ON_DISABLE_COMBINE _FLOW_ON _DISABLE_COMBINE
#pragma kernel ShapeCombine_DYNAMIC_WAVE_SIM_ON _DYNAMIC_WAVE_SIM_ON
#pragma kernel ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE _DYNAMIC_WAVE_SIM_ON _DISABLE_COMBINE
#pragma kernel ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON _FLOW_ON _DYNAMIC_WAVE_SIM_ON
#pragma kernel ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE _FLOW_ON _DYNAMIC_WAVE_SIM_ON _DISABLE_COMBINE
#pragma kernel ShapeCombineDynamicWaves
#pragma kernel ShapeCombineDynamicWaves_DISABLE_COMBINE _DISABLE_COMBINE
#pragma kernel ShapeCombineCopyDynamicWaves
#pragma kernel CrestShapeCombineAnimatedWaves
#pragma kernel CrestShapeCopyAnimatedWaves
#pragma kernel CrestShapeCombineDynamicWaves d_CombineDynamicWaves
#pragma multi_compile_local _ d_Combine
#pragma multi_compile_local _ d_DynamicWaves
#pragma multi_compile_local _ d_Flow
#if d_CombineDynamicWaves
#define d_DynamicWaves 1
#endif
#include "HLSLSupport.cginc"
@@ -30,11 +28,10 @@ float _Crest_DisplaceClamp;
Texture2DArray _Crest_WaveBuffer;
RWTexture2DArray<float4> _Crest_Target;
RWTexture2DArray<float4> _Crest_DynamicWavesTarget;
RWTexture2DArray<float4> _Crest_AnimatedWavesTarget;
m_CrestNameSpace
#if d_Flow
void Flow(const float texel, out float2 offsets, out float2 weights)
{
const float period = max(3.0 * texel, 1.0);
@@ -44,7 +41,9 @@ void Flow(const float texel, out float2 offsets, out float2 weights)
if (weights.x > 1.0) weights.x = 2.0 - weights.x;
weights.y = 1.0 - weights.x;
}
#endif
#if d_Combine
void SampleDisplacementsCompute(
in RWTexture2DArray<float4> i_dispSampler,
in float i_resolution, in float3 i_uv_slice,
@@ -82,10 +81,11 @@ void SampleDisplacementsCompute(
io_worldPos += i_wt * dataLerped.xyz;
}
#endif
void ShapeCombineBase(uint3 id)
void ShapeCombine(uint3 id)
{
const uint slice0 = _Crest_LodIndex;
const uint slice0 = id.z;
const Cascade cascade = Cascade::MakeAnimatedWaves(slice0);
const float3 uv = cascade.IDToUV(id.xy);
@@ -93,23 +93,25 @@ void ShapeCombineBase(uint3 id)
float3 result = 0.0;
#if !d_CombineDynamicWaves
// Sample in waves for this cascade.
{
#if _FLOW_ON
#if d_Flow
const half2 flow = Cascade::MakeFlow(slice0).SampleFlow(positionWSXZ);
float2 offsets, weights;
Flow(cascade._Texel, offsets, weights);
result += cascade.Sample(_Crest_WaveBuffer, positionWSXZ - offsets[0] * flow).xyz * weights[0];
result += cascade.Sample(_Crest_WaveBuffer, positionWSXZ - offsets[1] * flow).xyz * weights[1];
result += cascade.SampleAnimatedWavesOverflow(_Crest_WaveBuffer, positionWSXZ - offsets.x * flow, 1.0).xyz * weights.x;
result += cascade.SampleAnimatedWavesOverflow(_Crest_WaveBuffer, positionWSXZ - offsets.y * flow, 1.0).xyz * weights.y;
#else
result += cascade.Sample(_Crest_WaveBuffer, uv).xyz;
#endif // _FLOW_ON
result += _Crest_WaveBuffer[id].xyz;
#endif
}
#endif
// Disabled for last LOD.
#if !_DISABLE_COMBINE
#if d_Combine
{
const Cascade cascade = Cascade::MakeAnimatedWaves(slice0 + 1);
// Sample the shape 1 texture at this world position.
@@ -119,61 +121,37 @@ void ShapeCombineBase(uint3 id)
}
#endif
#if _DYNAMIC_WAVE_SIM_ON
#if d_DynamicWaves
{
// Convert dynamic wave sim to displacements.
result += Cascade::MakeDynamicWaves(slice0)
.SampleDynamicWavesDisplacement(positionWSXZ, _Crest_HorizontalDisplace, _Crest_DisplaceClamp);
}
#endif // _DYNAMIC_WAVE_SIM_ON
#endif
_Crest_Target[uint3(id.xy, slice0)] = float4(result, 0.0);
_Crest_Target[id] = float4(result, 0.0);
}
void ShapeCombineAnimatedWaves(uint3 id)
{
id.z = _Crest_LodIndex;
ShapeCombine(id);
}
void ShapeCopyAnimatedWaves(uint3 id)
{
ShapeCombine(id);
}
void ShapeCombineDynamicWaves(uint3 id)
{
const uint slice0 = _Crest_LodIndex;
const Cascade cascade = Cascade::MakeAnimatedWaves(slice0);
const float3 uv = cascade.IDToUV(id.xy);
const float2 positionWSXZ = cascade.UVToWorld(uv);
float3 result = 0.0;
// Disabled for last LOD.
#if !_DISABLE_COMBINE
{
// We are sampling from the target which matches Animated Waves descriptor.
const Cascade cascade = Cascade::MakeAnimatedWaves(slice0 + 1);
const float3 uv = cascade.WorldToUV(positionWSXZ);
// Waves to combine down from the next lod up the chain.
SampleDisplacementsCompute(_Crest_DynamicWavesTarget, cascade._Resolution, uv, 1.0, result);
}
#endif
{
// We are sampling from Dynamic Waves.
const Cascade cascade = Cascade::MakeDynamicWaves(slice0);
const float3 uv = cascade.WorldToUV(positionWSXZ);
result += cascade.SampleDynamicWavesDisplacement(uv, _Crest_HorizontalDisplace, _Crest_DisplaceClamp);
}
_Crest_DynamicWavesTarget[uint3(id.xy, slice0)] = float4(result, 0.0);
}
void ShapeCombineCopyDynamicWaves(uint3 id)
{
_Crest_AnimatedWavesTarget[id] += _Crest_DynamicWavesTarget[id];
// We are combining from the target which matches the Animated Waves descriptor.
id.z = _Crest_LodIndex;
ShapeCombine(id);
}
m_CrestNameSpaceEnd
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_DISABLE_COMBINE(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_FLOW_ON(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_FLOW_ON_DISABLE_COMBINE(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_DYNAMIC_WAVE_SIM_ON(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombine_FLOW_ON_DYNAMIC_WAVE_SIM_ON_DISABLE_COMBINE(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineBase(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombineDynamicWaves(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineDynamicWaves(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombineDynamicWaves_DISABLE_COMBINE(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineDynamicWaves(id); }
[numthreads(THREAD_GROUP_SIZE_X, THREAD_GROUP_SIZE_Y, 1)] void ShapeCombineCopyDynamicWaves(uint3 id : SV_DispatchThreadID) { m_Crest::ShapeCombineCopyDynamicWaves(id); }
m_CrestKernelDefault(ShapeCombineAnimatedWaves)
m_CrestKernelDefault(ShapeCopyAnimatedWaves)
m_CrestKernelDefault(ShapeCombineDynamicWaves)

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestShorelineColor
#pragma multi_compile_local __ d_Crest_ShorelineColorSource_ShorelineDistance

View File

@@ -3,16 +3,19 @@
// Solves 2D wave equation
#pragma exclude_renderers glcore gles3
#pragma kernel CrestUpdateDynamicWaves
#include "HLSLSupport.cginc"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Constants.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/InputsDriven.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Cascade.hlsl"
RWTexture2DArray<float2> _Crest_Target;
RWTexture2DArray<m_Float2> _Crest_Target;
CBUFFER_START(CrestPerMaterial)
float _Crest_Damping;
@@ -45,7 +48,7 @@ void UpdateDynamicWaves(uint3 id)
if (sliceIndexSource < 0.0 || sliceIndexSource >= cascadeSource._Count)
{
// Always initialise with 0 values.
_Crest_Target[id] = (float2)0;
_Crest_Target[id] = 0.0;
return;
}
@@ -72,7 +75,7 @@ void UpdateDynamicWaves(uint3 id)
// Wave reflections off geometry.
if (waterDepth <= 0.0)
{
_Crest_Target[id] = float2(0.0, 0.0);
_Crest_Target[id] = 0.0;
return;
}
@@ -142,7 +145,7 @@ void UpdateDynamicWaves(uint3 id)
vtp = 0.0;
}
_Crest_Target[id] = float2(ftp, vtp);
_Crest_Target[id] = m_Float2Constructor(ftp, vtp);
}
m_CrestNameSpaceEnd

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestUpdateFoam
#include "HLSLSupport.cginc"
@@ -53,9 +55,16 @@ void UpdateFoam(uint3 id)
// accumulation of foam and causes overshoots when _Crest_WaveFoamStrength is less than 1.0.
float simDeltaTime = _Crest_NeedsPrewarming ? max(_Crest_SimDeltaTime, min(1.0, _Crest_WaveFoamStrength - 1.0) / _Crest_FoamFadeRate) : _Crest_SimDeltaTime;
// Limit wave contributions at low water scales only where filtering is needed.
uint filteredSlice = slice0;
if (g_Crest_WaterScale <= 8)
{
filteredSlice = max(_Crest_MinimumWavesSlice, filteredSlice);
}
// The determinant of the displacement Jacobian is a good measure for turbulence.
float det;
const half3 displacement = Cascade::MakeAnimatedWaves(max(_Crest_MinimumWavesSlice, slice0)).SampleDisplacement(worldPosXZ, det);
const half3 displacement = Cascade::MakeAnimatedWaves(filteredSlice).SampleDisplacement(worldPosXZ, det);
foam += 5.0 * simDeltaTime * _Crest_WaveFoamStrength * saturate( _Crest_WaveFoamCoverage - det );
// Prewarm shoreline foam. 1.0 / _Crest_FoamFadeRate perfectly matches a paused water in edit mode which is fine for

View File

@@ -3,6 +3,8 @@
// Soft shadow term is red, hard shadow term is green.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestUpdateShadowsBRP _BRP
#pragma kernel CrestUpdateShadowsHRP _HRP
#pragma kernel CrestUpdateShadowsURP _URP
@@ -33,7 +35,7 @@ bool _Crest_SampleColorMap;
float3 _Crest_Absorption;
float3 _Crest_Scattering;
RWTexture2DArray<float2> _Crest_Target;
RWTexture2DArray<m_Float2> _Crest_Target;
m_CrestNameSpace
@@ -146,7 +148,7 @@ void UpdateShadows(const uint3 id)
shadow = lerp(shadow, shadowThisFrame, _Crest_JitterDiameters_CurrentFrameWeights.zw * _Crest_SimDeltaTime * 60.0);
}
_Crest_Target[id] = shadow;
_Crest_Target[id] = m_Float2FromFloat2(shadow);
}
m_CrestNameSpaceEnd

View File

@@ -1,176 +1,4 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
// Soft shadow term is red, hard shadow term is green. In HDRP, hard shadows are not computed and y channel will be 0.
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Constants.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/InputsDriven.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Cascade.hlsl"
// Noise functions used for jitter.
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Noise/Noise.hlsl"
CBUFFER_START(CrestPerMaterial)
// Settings._jitterDiameterSoft, Settings._jitterDiameterHard, Settings._currentFrameWeightSoft, Settings._currentFrameWeightHard
float4 _Crest_JitterDiameters_CurrentFrameWeights;
float _Crest_SimDeltaTime;
bool _Crest_ClearShadows;
float3 _Crest_CenterPos;
float3 _Crest_Scale;
float4x4 _Crest_MainCameraProjectionMatrix;
bool _Crest_SampleColorMap;
float3 _Crest_Absorption;
float3 _Crest_Scattering;
CBUFFER_END
m_CrestNameSpace
struct Attributes
{
uint id : SV_VertexID;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings Vertex(Attributes input)
{
// This will work for all pipelines.
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.positionCS = GetFullScreenTriangleVertexPosition(input.id);
float2 uv = GetFullScreenTriangleTexCoord(input.id);
// World position from UV.
output.positionWS.xyz = float3(uv.x - 0.5, 0.0, uv.y - 0.5) * _Crest_Scale * 4.0 + _Crest_CenterPos;
output.positionWS.y = g_Crest_WaterCenter.y;
return output;
}
half SampleShadows(const float4 i_positionWS);
half ComputeShadowFade(const float4 i_positionWS);
// Compiler shows warning when using intermediate returns, disable this.
#pragma warning(push)
#pragma warning(disable: 4000)
half ComputeShadow(const float4 i_positionWS, const float i_jitterDiameter, const half i_terrainHeight)
{
float4 positionWS = i_positionWS;
if (i_jitterDiameter > 0.0)
{
// Add jitter.
positionWS.xz += i_jitterDiameter * (hash33(uint3(abs(positionWS.xz * 10.0), _Time.y * 120.0)) - 0.5).xy;
// Shadow Bleeding.
// If we are not within a terrain, then check for shadow bleeding.
if (i_positionWS.y > i_terrainHeight)
{
// WorldToSafeUV
half terrainHeight = Cascade::MakeDepth(_Crest_LodIndex).SampleSceneHeight(positionWS.xz);
// If our current position is below the jittered terrain height, then we have landed within a terrain and
// we do not want to sample those shadows.
if (i_positionWS.y < terrainHeight)
{
// Return no shadows.
return 1.0;
}
}
}
return SampleShadows(positionWS);
}
#pragma warning(pop)
half2 Fragment(Varyings input)
{
float4 positionWS = float4(input.positionWS.xyz, 1.0);
// Shadow from last frame. Manually implement black border.
const float sliceIndexSource = clamp((int)_Crest_LodIndex + g_Crest_LodChange, 0.0, g_Crest_LodCount - 1.0);
half2 shadow = Cascade::MakeShadowSource(sliceIndexSource).SampleShadowOverflow(positionWS.xz, 1.0);
// Add displacement so shorelines do not receive shadows incorrectly.
positionWS.xyz += Cascade::MakeAnimatedWaves(_Crest_LodIndex).SampleDisplacement(positionWS.xz);
// This was calculated in vertex but we have to sample sea level offset in fragment.
float4 mainCameraCoordinates = mul(_Crest_MainCameraProjectionMatrix, positionWS);
// Check if the current sample is visible in the main camera (and therefore the shadow map can be sampled). This is
// required as the shadow buffer is world aligned and surrounds viewer.
float3 projected = mainCameraCoordinates.xyz / mainCameraCoordinates.w;
if (projected.z < 1.0 && projected.z > 0.0 && abs(projected.x) < 1.0 && abs(projected.y) < 1.0)
{
half2 shadowThisFrame = 1.0;
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
positionWS.xyz -= _WorldSpaceCameraPos.xyz;
#endif
half terrainHeight = Cascade::MakeDepth(_Crest_LodIndex).SampleSceneHeight(positionWS.xz);
half softJitter = _Crest_JitterDiameters_CurrentFrameWeights[CREST_SHADOW_INDEX_SOFT];
if (_Crest_SampleColorMap)
{
half3 absorption = _Crest_Absorption;
half3 scattering = _Crest_Scattering;
if (g_Crest_SampleAbsorptionSimulation)
{
absorption = Cascade::MakeAbsorption(_Crest_LodIndex).SampleAbsorption(positionWS.xz);
}
if (g_Crest_SampleScatteringSimulation)
{
scattering = Cascade::MakeScattering(_Crest_LodIndex).SampleScattering(positionWS.xz);
}
half3 extinction = absorption + scattering;
half factor = saturate(min(min(extinction.x, extinction.y), extinction.z) * g_Crest_DynamicSoftShadowsFactor);
softJitter = (1.0 - factor) * k_Crest_MaximumShadowJitter;
}
// Add soft shadowing data.
shadowThisFrame[CREST_SHADOW_INDEX_SOFT] = ComputeShadow
(
positionWS,
softJitter,
terrainHeight
);
#ifdef CREST_SAMPLE_SHADOW_HARD
// Add hard shadowing data.
shadowThisFrame[CREST_SHADOW_INDEX_HARD] = ComputeShadow
(
positionWS,
_Crest_JitterDiameters_CurrentFrameWeights[CREST_SHADOW_INDEX_HARD],
terrainHeight
);
#endif
shadowThisFrame = (half2)1.0 - saturate(shadowThisFrame + ComputeShadowFade(positionWS));
shadow = lerp(shadow, shadowThisFrame, _Crest_JitterDiameters_CurrentFrameWeights.zw * _Crest_SimDeltaTime * 60.0);
}
return shadow;
}
m_CrestNameSpaceEnd
m_CrestVertex
m_CrestFragment(half2)
// Replaced with UpdateShadow.compute.

View File

@@ -39,6 +39,7 @@
result._Texture = g_Crest_Cascade##name##source; \
result._SamplingParameters = g_Crest_SamplingParametersCascade##name##source; \
result._Index = i_Index; \
result._IndexI = i_Index; \
result._PositionSnapped = perSlice.xy; \
result._Texel = perSlice.z; \
result._Resolution = perType.y; \
@@ -56,6 +57,7 @@
result._Texture = i_Cascade._Texture; \
result._SamplingParameters = i_Cascade._SamplingParameters; \
result._Index = i_Index; \
result._IndexI = i_Index; \
result._PositionSnapped = perSlice.xy; \
result._Texel = perSlice.z; \
result._Resolution = perType.y; \
@@ -70,6 +72,7 @@
const float4 perAll = g_Crest_CascadeData##source[i_Index]; \
Cascade result; \
result._Index = i_Index; \
result._IndexI = i_Index; \
result._Scale = perAll.x; \
result._Weight = perAll.y; \
result._MaximumWavelength = perAll.z; \
@@ -140,6 +143,51 @@
} \
} \
return result; \
} \
half4 Sample##name##Overflow(const Texture2DArray i_Texture, const float2 i_Position, const float i_Border) m_ConstantReturn \
{ \
half4 result = 0.0; \
const float3 uv = WorldToUV(i_Position); \
const half2 r = abs(uv.xy - 0.5); \
const half rMax = 0.5 - _OneOverResolution * i_Border; \
if (max(r.x, r.y) <= rMax) \
{ \
result = Sample(i_Texture, uv); \
} \
else if ((_Index + 1) < _Count) \
{ \
const Cascade cascade = Cascade::Make##name(_Index + 1, this); \
const float3 uv = cascade.WorldToUV(i_Position); \
const half2 r = abs(uv.xy - 0.5); \
const half rMax = 0.5 - cascade._OneOverResolution * i_Border; \
if (max(r.x, r.y) <= rMax) \
{ \
result = Sample(i_Texture, uv); \
} \
} \
return result; \
} \
half4 Sample##name##Overflow(const Texture2DArray i_Texture, const float3 i_UV, const float i_Border) m_ConstantReturn \
{ \
half4 result = 0.0; \
const half2 r = abs(i_UV.xy - 0.5); \
const half rMax = 0.5 - _OneOverResolution * i_Border; \
if (max(r.x, r.y) <= rMax) \
{ \
result = Sample(i_Texture, i_UV); \
} \
else if ((_Index + 1) < _Count) \
{ \
const Cascade cascade = Cascade::Make##name(_Index + 1, this); \
const float3 uv = cascade.WorldToUV(UVToWorld(i_UV)); \
const half2 r = abs(uv.xy - 0.5); \
const half rMax = 0.5 - cascade._OneOverResolution * i_Border; \
if (max(r.x, r.y) <= rMax) \
{ \
result = Sample(i_Texture, uv); \
} \
} \
return result; \
}
#define m_SampleWeighted(name, type) \
@@ -175,6 +223,8 @@ struct Cascade
// For copy constructor.
float4 _SamplingParameters[MAX_LOD_COUNT];
uint _IndexI;
m_MakeCascadeShared
m_MakeCascadeSharedPrevious
@@ -183,6 +233,7 @@ struct Cascade
const float4 perAll = i_Previous ? g_Crest_CascadeDataSource[i_Index] : g_Crest_CascadeData[i_Index];
Cascade result;
result._Index = i_Index;
result._IndexI = i_Index;
result._Scale = perAll.x;
result._Weight = perAll.y;
result._MaximumWavelength = perAll.z;
@@ -232,6 +283,12 @@ struct Cascade
return float3((i_Position - _PositionSnapped) / (_Texel * _Resolution) + 0.5, _Index);
}
uint3 WorldToID(const float2 i_Position) m_ConstantReturn
{
const float3 uv = WorldToUV(i_Position);
return uint3(uv.xy * _Resolution, _IndexI);
}
float2 IDToWorld(const uint2 i_ID) m_ConstantReturn
{
return UVToWorld(IDToUV(i_ID));
@@ -354,6 +411,7 @@ struct Cascade
float4 position = SampleAnimatedWaves(uv);
io_LevelOffset += position.w * i_Weight;
io_Position += position.xyz * i_Weight;
io_Position.y += position.w * i_Weight;
// Derivatives
{
@@ -456,6 +514,13 @@ struct Cascade
return value;
}
void SampleSignedDepthFromSeaLevelAndDistance(const float2 i_Position, const float i_Weight, inout half io_Depth, inout half io_Distance) m_ConstantReturn
{
const half2 value = SampleSignedDepthFromSeaLevelAndDistance(i_Position) * i_Weight;
io_Depth += value.x;
io_Distance += value.y;
}
void SampleSignedDepthFromSeaLevel(const float2 i_Position, const float i_Weight, inout half io_Depth) m_ConstantReturn
{
io_Depth += (g_Crest_WaterCenter.y - SampleSceneHeight(i_Position)) * i_Weight;

View File

@@ -9,7 +9,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings.Crest.hlsl"
SamplerState LODData_linear_clamp_sampler;
SamplerState LODData_point_clamp_sampler;
SamplerState sampler_Crest_point_clamp;
SamplerState sampler_Crest_linear_repeat;
SamplerState _Crest_linear_clamp_sampler;
@@ -25,6 +25,11 @@ float g_Crest_ClipByDefault;
float g_Crest_LodAlphaBlackPointFade;
float g_Crest_LodAlphaBlackPointWhitePointFade;
// Refraction
float g_Crest_WaterDepthAtViewer;
float g_Crest_MaximumVerticalDisplacement;
float2 g_Crest_HorizonNormal;
// Hack - due to SV_IsFrontFace occasionally coming through as true for
// backfaces, add a param here that forces water to be in undrwater state. I
// think the root cause here might be imprecision or numerical issues at water

View File

@@ -0,0 +1,13 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#ifndef d_WaveHarmonic_Crest_Keywords
#define d_WaveHarmonic_Crest_Keywords
#define d_Crest_AlbedoLod CREST_ALBEDO_SIMULATION
#define d_Crest_FlowLod defined(_CREST_FLOW_LOD)
#define d_Crest_ShadowLod CREST_SHADOW_SIMULATION
#define d_Crest_AbsorptionLod CREST_ABSORPTION_SIMULATION
#define d_Crest_ScatteringLod CREST_SCATTERING_SIMULATION
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 2e2c3338345cc4cc79fac0ec9b65a4f2
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -13,6 +13,22 @@
#define m_FloatMaximum 3.402823466e+38
//
// Fallback for WebGPU
//
#ifdef SHADER_API_WEBGPU
#define m_Float2 float4
#define m_Float2Constructor(x, y) float4(x, y, 0.0, 0.0)
#define m_Float2FromFloat2(p0) float4(p0.x, p0.y, 0, 0)
#else
#define m_Float2 float2
#define m_Float2Constructor(x, y) float2(x, y)
#define m_Float2FromFloat2(p0) p0
#endif
#if (CREST_FULL_PRECISION_DISPLACEMENT != 0)
#define m_DisplacementTexture(texture, components) texture<float##components>
#else

View File

@@ -1,19 +1,27 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#ifndef SETTINGS_CREST_HLSL
#define SETTINGS_CREST_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettings: static fields
//
#define CREST_PACKAGE_HDRP (1)
#define CREST_PACKAGE_URP (1)
#define CREST_PORTALS (0)
#define CREST_SHIFTING_ORIGIN (0)
#define CREST_FULL_PRECISION_DISPLACEMENT (1)
#define CREST_DISCARD_ATMOSPHERIC_SCATTERING (1)
#define CREST_LEGACY_UNDERWATER (0)
#ifndef d_WaveHarmonic_Crest_Settings
#define d_WaveHarmonic_Crest_Settings
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.hlsl"
#if CREST_PLATFORM_STANDALONE
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.Standalone.hlsl"
#elif CREST_PLATFORM_SERVER
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.Server.hlsl"
#elif CREST_PLATFORM_ANDROID
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.Android.hlsl"
#elif CREST_PLATFORM_IOS
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.iOS.hlsl"
#elif CREST_PLATFORM_WEB
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.Web.hlsl"
#elif CREST_PLATFORM_TVOS
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.tvOS.hlsl"
#elif CREST_PLATFORM_VISIONOS
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.visionOS.hlsl"
#else
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings/Settings.Crest.Default.hlsl"
#endif
#endif // d_WaveHarmonic_Crest_Settings

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bb30ad2b71bed4acca040c6ce2900f37
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_ANDROID_HLSL
#define SETTINGS_CREST_ANDROID_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsAndroid: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 307613c3b5e1c4f7da556862bfea5712
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_DEFAULT_HLSL
#define SETTINGS_CREST_DEFAULT_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsDefault: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 9b6abac1d09714922b174070c93c9054
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_SERVER_HLSL
#define SETTINGS_CREST_SERVER_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsServer: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 15a7369ec789c4ae08ac764eab29463e
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_STANDALONE_HLSL
#define SETTINGS_CREST_STANDALONE_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsStandalone: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d9f563484a71c4e04a3aefcbef8ce36a
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_WEB_HLSL
#define SETTINGS_CREST_WEB_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsWeb: static fields
//
#define CREST_ALBEDO_SIMULATION (0)
#define CREST_ABSORPTION_SIMULATION (0)
#define CREST_SCATTERING_SIMULATION (0)
#define CREST_SHADOW_SIMULATION (0)
#define CREST_CAUSTICS_FORCE_DISTORTION (0)
#define CREST_FOAM_BIOLUMINESCENCE (0)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (0)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 0eecd007d82c74bf88658c84586cda62
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_HLSL
#define SETTINGS_CREST_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettings: static fields
//
#define CREST_PACKAGE_HDRP (1)
#define CREST_PACKAGE_URP (1)
#define CREST_PORTALS (0)
#define CREST_SHIFTING_ORIGIN (0)
#define CREST_PLATFORM_STANDALONE (1)
#define CREST_PLATFORM_SERVER (0)
#define CREST_PLATFORM_ANDROID (0)
#define CREST_PLATFORM_IOS (0)
#define CREST_PLATFORM_WEB (0)
#define CREST_PLATFORM_TVOS (0)
#define CREST_PLATFORM_VISIONOS (0)
#define CREST_FULL_PRECISION_DISPLACEMENT (1)
#define CREST_DISCARD_ATMOSPHERIC_SCATTERING (1)
#define CREST_LEGACY_UNDERWATER (0)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: b25f99570a3c543dca0d91bc54a4e27e
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_IOS_HLSL
#define SETTINGS_CREST_IOS_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsIOS: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 9d8991669495c4da8b6d2d9c62dadcb1
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_TVOS_HLSL
#define SETTINGS_CREST_TVOS_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsTVOS: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: bff51ef931a4443a7b6c1a2fc2b88772
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
//
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
//
#ifndef SETTINGS_CREST_VISIONOS_HLSL
#define SETTINGS_CREST_VISIONOS_HLSL
//
// WaveHarmonic.Crest.Editor.ShaderSettingsVisionOS: static fields
//
#define CREST_ALBEDO_SIMULATION (1)
#define CREST_ABSORPTION_SIMULATION (1)
#define CREST_SCATTERING_SIMULATION (1)
#define CREST_SHADOW_SIMULATION (1)
#define CREST_CAUSTICS_FORCE_DISTORTION (1)
#define CREST_FOAM_BIOLUMINESCENCE (1)
#define CREST_NORMAL_MAPS (1)
#define CREST_SIMPLE_TRANSPARENCY (0)
#define CREST_PLANAR_REFLECTIONS (1)
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d0648a06882284ebc88f4b28f3c6f825
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -66,6 +66,67 @@ float4 SampleTextureCatmullRom(in Texture2D<float4> tex, in SamplerState linearS
return result;
}
float4 CubicWeights(const float f)
{
const float f2 = f * f;
const float f3 = f2 * f;
// CatmullRom (a = -0.5)
return float4
(
-0.5 * f3 + f2 - 0.5 * f,
1.5 * f3 - 2.5 * f2 + 1.0,
-1.5 * f3 + 2.0 * f2 + 0.5 * f,
0.5 * f3 - 0.5 * f2
);
}
float4 SampleBicubicRepeat
(
const Texture2DArray<float4> i_Texture,
const float2 i_UV,
const uint2 i_Size,
const uint i_Slice
)
{
// Convert to texel space (centered at pixel centers).
const float2 samplePosition = i_UV * (float2)i_Size - 0.5;
const int2 texelPosition = (int2)floor(samplePosition);
const float2 f = samplePosition - (float2)texelPosition;
// Precompute weights.
const float4 wx = CubicWeights(f.x);
const float4 wy = CubicWeights(f.y);
const uint2 size = i_Size - 1;
const uint4 x = uint4
(
(texelPosition.x - 1) & size.x,
(texelPosition.x + 0) & size.x,
(texelPosition.x + 1) & size.x,
(texelPosition.x + 2) & size.x
);
// Horizontal pass.
float4 row[4];
[unroll]
for (int j = -1; j <= 2; ++j)
{
const int y = (texelPosition.y + j) & size.y;
const float4 t0 = i_Texture[uint3(x.x, y, i_Slice)];
const float4 t1 = i_Texture[uint3(x.y, y, i_Slice)];
const float4 t2 = i_Texture[uint3(x.z, y, i_Slice)];
const float4 t3 = i_Texture[uint3(x.w, y, i_Slice)];
row[j + 1] = t0 * wx.x + t1 * wx.y + t2 * wx.z + t3 * wx.w;
}
// Vertical pass.
return row[0] * wy.x + row[1] * wy.y + row[2] * wy.z + row[3] * wy.w;
}
m_UtilityNameSpaceEnd
#endif // d_WaveHarmonic_Utility_Filtering

View File

@@ -43,6 +43,16 @@ float2 WorldNormalToScreenDirection(const float3 i_PositionWS, const float3 i_No
return direction;
}
float3 SafeComputeWorldSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invViewProjMatrix)
{
float4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth);
float4 hpositionWS = mul(invViewProjMatrix, positionCS);
// w is sometimes zero when using oblique projection.
// Zero is better than NaN.
return hpositionWS.w > 0.0 ? hpositionWS.xyz / hpositionWS.w : 0.0;
}
m_UtilityNameSpaceEnd
#endif // d_WaveHarmonic_Utility_Helpers

View File

@@ -86,6 +86,9 @@
// Transparent Objects Receives Shadows
//
// WebGPU does not like the binding.
#ifndef SHADER_API_WEBGPU
#if _SURFACE_TYPE_TRANSPARENT
#if _TRANSPARENT_RECEIVES_SHADOWS
#if SHADERPASS == SHADERPASS_FORWARD || SHADERPASS == SHADERPASS_FORWARD_ADD
@@ -108,4 +111,6 @@ float4 _ShadowMapTexture_TexelSize;
#endif
#endif
#endif // SHADER_API_WEBGPU
#endif // d_WaveHarmonic_Utility_ShaderGraphDefines

View File

@@ -21,24 +21,6 @@
#define CLUSTER_LIGHT_LOOP_SUBTRACTIVE_LIGHT_CHECK FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
#endif // FORWARD_PLUS_SUBTRACTIVE_LIGHT_CHECK
#if UNITY_VERSION >= 60000000
#if defined(STEREO_INSTANCING_ON) || defined(STEREO_MULTIVIEW_ON)
#if _ALPHATEST_ON
#if !USE_CLUSTER_LIGHT_LOOP
// If not clustered and additional light shadows and XR, the shading model
// completely breaks. It is like shadow attenuation is NaN or some obscure
// compiler issue. For 2022.3, it is broken for forward+ only, but cannot be fixed.
#define d_ShadowMaskBroken 1
#else
#if _RECEIVE_SHADOWS_OFF
// Right eye broken rendering similar to above.
#define d_AdditionalLightsBroken 1
#endif
#endif
#endif
#endif
#endif
#endif // CREST_URP
#if CREST_HDRP
@@ -78,7 +60,10 @@ void PrimaryLight
#elif CREST_BIRP
#ifndef USING_DIRECTIONAL_LIGHT
// Yes. This function wants the world position of the surface.
o_Direction = normalize(UnityWorldSpaceLightDir(i_PositionWS));
o_Direction = UnityWorldSpaceLightDir(i_PositionWS);
// Prevents divide by zero.
if (all(o_Direction == 0)) o_Direction = half3(0.0, 1.0, 0.0);
o_Direction = normalize(o_Direction);
#else
o_Direction = _WorldSpaceLightPos0.xyz;
// Prevents divide by zero.
@@ -153,14 +138,6 @@ LIGHT_LOOP_BEGIN(pixelLightCount)
// Includes shadows and cookies.
Light light = GetAdditionalLight(lightIndex, inputData, shadowMask, aoFactor);
#if d_ShadowMaskBroken
light.shadowAttenuation = 1.0;
#endif
#if d_AdditionalLightsBroken
light.color = 0.0;
#endif
#ifdef _LIGHT_LAYERS
if (IsMatchingLightLayer(light.layerMask, meshRenderingLayers))
#endif

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a381e15a67ba0474ea0e685cc27a6f8a
guid: 389b0f071dd5843c394f5255cd6ec73c
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: de25b8cb31cac9b4db1835b9632de8db
guid: ff5a7cc0943db46db9eac87f50e38097
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 7bf6aebdc1c0f3f47bad2841e2a11446
guid: 82c6020b68d9a4708a39b711f87490f8
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -11,8 +11,6 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Depth.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
float2 _Crest_HorizonNormal;
TEXTURE2D_X(_Crest_WaterMaskTexture);
m_CrestNameSpace
@@ -45,7 +43,7 @@ half4 Fragment(Varyings input)
const uint2 positionSS = input.positionCS.xy;
const float mask = LOAD_TEXTURE2D_X(_Crest_WaterMaskTexture, positionSS).x;
const float2 offset = -((float2)mask) * _Crest_HorizonNormal;
const float2 offset = -((float2)mask) * g_Crest_HorizonNormal;
float weight = 1.0;
// Sample three pixels along the normal. If the sample is different than the

View File

@@ -13,6 +13,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Depth.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Shim.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Keywords.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Lighting.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/VolumeLighting.hlsl"
@@ -40,6 +41,8 @@ int _Crest_DataSliceOffset;
half _Crest_Radius;
half _Crest_RefractionStrength;
bool _Crest_PortalInverted;
m_CrestNameSpace
struct Attributes
@@ -87,7 +90,7 @@ half4 Fragment(Varyings input)
#if d_Masked
// Prevent rendering inside of new portal modules.
if (LOAD_TEXTURE2D_X(_Crest_WaterMaskTexture, input.positionCS.xy).r == k_Crest_MaskInsidePortal)
if ((LOAD_TEXTURE2D_X(_Crest_WaterMaskTexture, input.positionCS.xy).r == k_Crest_MaskInsidePortal) == _Crest_PortalInverted)
{
discard;
}

View File

@@ -77,6 +77,7 @@ half3 Caustics
);
}
#if d_Crest_CausticsForceDistortion
if (i_Underwater)
{
float2 surfacePosXZ = i_ScenePositionWS.xz;
@@ -93,6 +94,7 @@ half3 Caustics
cuv1.xy += 1.30 * causticN;
cuv2.xy += 1.77 * causticN;
}
#endif
half causticsStrength = i_Strength;

View File

@@ -12,28 +12,43 @@ TEXTURE2D_FLOAT(_Crest_WaterLine);
float _Crest_WaterLineTexel;
float2 _Crest_WaterLineResolution;
float2 _Crest_WaterLineSnappedPosition;
bool _Crest_WaterLineFlatWater;
m_CrestNameSpace
float SampleWaterLineHeight(const float2 i_PositionWS)
{
const float2 uv = (i_PositionWS - _Crest_WaterLineSnappedPosition) / (_Crest_WaterLineTexel * _Crest_WaterLineResolution) + 0.5;
return _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv, 0).r + g_Crest_WaterCenter.y;
if (_Crest_WaterLineFlatWater)
{
return g_Crest_WaterCenter.y;
}
else
{
const float2 uv = (i_PositionWS - _Crest_WaterLineSnappedPosition) / (_Crest_WaterLineTexel * _Crest_WaterLineResolution) + 0.5;
return _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv, 0).r + g_Crest_WaterCenter.y;
}
}
half3 SampleWaterLineNormal(const float2 i_PositionWS, const float i_Height)
{
const float2 uv = (i_PositionWS - _Crest_WaterLineSnappedPosition) / (_Crest_WaterLineTexel * _Crest_WaterLineResolution) + 0.5;
const float3 dd = float3(1.0 / _Crest_WaterLineResolution.xy, 0.0);
const float xOffset = _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv + dd.xz, 0).r;
const float zOffset = _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv + dd.zy, 0).r;
if (_Crest_WaterLineFlatWater)
{
return half3(0, 1, 0);
}
else
{
const float2 uv = (i_PositionWS - _Crest_WaterLineSnappedPosition) / (_Crest_WaterLineTexel * _Crest_WaterLineResolution) + 0.5;
const float3 dd = float3(1.0 / _Crest_WaterLineResolution.xy, 0.0);
const float xOffset = _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv + dd.xz, 0).r;
const float zOffset = _Crest_WaterLine.SampleLevel(_Crest_linear_clamp_sampler, uv + dd.zy, 0).r;
return normalize(half3
(
(xOffset - i_Height) / _Crest_WaterLineTexel,
1.0,
(zOffset - i_Height) / _Crest_WaterLineTexel
));
return normalize(half3
(
(xOffset - i_Height) / _Crest_WaterLineTexel,
1.0,
(zOffset - i_Height) / _Crest_WaterLineTexel
));
}
}
m_CrestNameSpaceEnd

View File

@@ -0,0 +1,52 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#ifndef d_WaveHarmonic_Crest_Surface_Emission
#define d_WaveHarmonic_Crest_Surface_Emission
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
m_CrestNameSpace
half3 FoamBioluminescence
(
const half i_FoamData,
const half i_FoamMap,
const half3 i_BioluminescenceColor,
const half i_BioluminescenceIntensity,
const half i_BioluminescenceGlowCoverage,
const half i_BioluminescenceGlowIntensity,
const bool i_BioluminescenceSparklesEnabled,
const half i_BioluminescenceSparklesMap,
const half i_BioluminescenceSparklesCoverage,
const half i_BioluminescenceSparklesIntensity,
const half i_BioluminescenceMaximumDepth,
const half i_WaterDepth
)
{
half3 emission = 0.0;
const half weight = 1.0 - saturate(i_WaterDepth / i_BioluminescenceMaximumDepth);
if (weight <= 0.0)
{
return emission;
}
emission +=
(i_BioluminescenceColor * i_FoamMap * i_BioluminescenceIntensity) +
(i_BioluminescenceColor * saturate(i_FoamData - (1.0 - i_BioluminescenceGlowCoverage)) * i_BioluminescenceGlowIntensity);
if (i_BioluminescenceSparklesEnabled)
{
emission += (i_BioluminescenceColor * i_BioluminescenceSparklesMap * saturate(i_FoamData - (1.0 - i_BioluminescenceSparklesCoverage)) * i_BioluminescenceSparklesIntensity);
}
emission *= weight;
return emission;
}
m_CrestNameSpaceEnd
#endif // d_WaveHarmonic_Crest_Surface_Emission

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4707eb239a4b242d4ac074ac67b5544e
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -16,7 +16,7 @@
m_CrestNameSpace
half WhiteFoamTexture
half2 WhiteFoamTexture
(
const TiledTexture i_Texture,
const half i_Foam,
@@ -25,7 +25,8 @@ half WhiteFoamTexture
const float2 i_WorldXZ1,
const float2 i_TexelOffset,
const half i_LodAlpha,
const Cascade i_CascadeData0
const Cascade i_CascadeData0,
const bool i_Sample2ndChannel = false
)
{
const float2 uvOffset = i_TexelOffset + g_Crest_Time * i_Texture._speed / 32.0;
@@ -33,19 +34,32 @@ half WhiteFoamTexture
// material 'scale' slider into an intuitive range.
const float scale = i_Texture._scale * i_CascadeData0._Scale / 10.0;
half ft = lerp
(
i_Texture.Sample((i_WorldXZ0 + uvOffset) / scale).r,
i_Texture.Sample((i_WorldXZ1 + uvOffset) / (2.0 * scale)).r,
i_LodAlpha
);
const half2 f0 = i_Texture.Sample((i_WorldXZ0 + uvOffset) / scale).rg;
const half2 f1 = i_Texture.Sample((i_WorldXZ1 + uvOffset) / (2.0 * scale)).rg;
const half fr = lerp(f0.r, f1.r, i_LodAlpha);
// Black point fade.
half result = saturate(1.0 - i_Foam);
return smoothstep(result, result + i_Feather, ft);
const half result = saturate(1.0 - i_Foam);
#if d_Crest_FoamBioluminescence
if (i_Sample2ndChannel)
{
// Helps with exaggerating at LOD borders. Very specific to sparkles as they do not
// overlap leaving a dead zone in the middle.
const half fg = f0.g * min(0.5, 1.0 - i_LodAlpha) * 2.0 +
f1.g * min(0.5, i_LodAlpha) * 2.0;
return smoothstep(result, result + i_Feather, half2(fr, fg));
}
else
#endif
{
return half2(smoothstep(result, result + i_Feather, fr), 0.0);
}
}
half MultiScaleFoamAlbedo
half2 MultiScaleFoamAlbedo
(
const TiledTexture i_Texture,
const half i_Feather,
@@ -53,7 +67,8 @@ half MultiScaleFoamAlbedo
const Cascade i_CascadeData0,
const Cascade i_CascadeData1,
const half i_LodAlpha,
const float2 i_UndisplacedXZ
const float2 i_UndisplacedXZ,
const bool i_Sample2ndChannel
)
{
float2 worldXZ0 = i_UndisplacedXZ;
@@ -67,7 +82,7 @@ half MultiScaleFoamAlbedo
worldXZ1 -= ShiftingOriginOffset(i_Texture, i_CascadeData1);
#endif // CREST_SHIFTING_ORIGIN
return WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, (float2)0.0, i_LodAlpha, i_CascadeData0);
return WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, (float2)0.0, i_LodAlpha, i_CascadeData0, i_Sample2ndChannel);
}
half2 MultiScaleFoamNormal
@@ -97,8 +112,8 @@ half2 MultiScaleFoamNormal
// 0.25 is magic number found through tweaking.
const float2 dd = float2(0.25 * i_PixelZ * i_Texture._texel, 0.0);
const half whiteFoam_x = WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, dd.xy, i_LodAlpha, i_CascadeData0);
const half whiteFoam_z = WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, dd.yx, i_LodAlpha, i_CascadeData0);
const half whiteFoam_x = WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, dd.xy, i_LodAlpha, i_CascadeData0).r;
const half whiteFoam_z = WhiteFoamTexture(i_Texture, i_FoamData, i_Feather, worldXZ0, worldXZ1, dd.yx, i_LodAlpha, i_CascadeData0).r;
// Compute a foam normal - manually push in derivatives. If I used blend
// smooths all the normals towards straight up when there is no foam.
@@ -107,7 +122,7 @@ half2 MultiScaleFoamNormal
return magicStrengthFactor * i_NormalStrength * half2(whiteFoam_x - i_FoamAlbedo, whiteFoam_z - i_FoamAlbedo) / dd.x;
}
half MultiScaleFoamAlbedo
half2 MultiScaleFoamAlbedo
(
const Flow i_Flow,
const TiledTexture i_Texture,
@@ -116,7 +131,8 @@ half MultiScaleFoamAlbedo
const Cascade i_CascadeData0,
const Cascade i_CascadeData1,
const half i_LodAlpha,
const float2 i_UndisplacedXZ
const float2 i_UndisplacedXZ,
const bool i_Sample2ndChannel
)
{
return MultiScaleFoamAlbedo
@@ -127,7 +143,8 @@ half MultiScaleFoamAlbedo
i_CascadeData0,
i_CascadeData1,
i_LodAlpha,
i_UndisplacedXZ - i_Flow._Flow * i_Flow._Offset0
i_UndisplacedXZ - i_Flow._Flow * i_Flow._Offset0,
i_Sample2ndChannel
) * i_Flow._Weight0 + MultiScaleFoamAlbedo
(
i_Texture,
@@ -136,7 +153,8 @@ half MultiScaleFoamAlbedo
i_CascadeData0,
i_CascadeData1,
i_LodAlpha,
i_UndisplacedXZ - i_Flow._Flow * i_Flow._Offset1
i_UndisplacedXZ - i_Flow._Flow * i_Flow._Offset1,
i_Sample2ndChannel
) * i_Flow._Weight1;
}
@@ -214,8 +232,10 @@ void ApplyFoamToSurface
// as if it had transmission.
// There is still ugliness around the edges. There will either be black or
// incorrect reflections depending on the magic value.
io_NormalWS.y *= i_Foam > 0.15 ? -1.0 : 1.0;
io_NormalWS = i_Foam > 0.0 ? half3(0.0, 1.0, 0.0) : io_NormalWS;
#if _SPECULAR_SETUP
io_Specular = lerp(io_Specular, i_Specular, i_Foam);
#endif
}
}

View File

@@ -23,13 +23,31 @@ void SetUpFog(bool i_Underwater, float3 i_PositionWS, float i_Multiplier, float
{
s_IsUnderWater = i_Underwater;
// XR does not like early returns in URP.
#if !defined(STEREO_INSTANCING_ON) && !defined(STEREO_MULTIVIEW_ON)
if (!s_IsUnderWater)
{
return;
}
#endif
#if (CREST_LEGACY_UNDERWATER != 1)
s_PositionSS = i_PositionSS;
s_PositionWS = i_PositionWS;
s_ViewWS = i_ViewWS;
s_FogDistance = i_FogDistance;
s_DepthRaw = 0;
s_FogMultiplier = i_Multiplier;
ApplyUnderwaterEffect
(
0, // Not used (color)
0, // TIR only
0, // Caustics only
i_FogDistance,
i_ViewWS,
i_PositionSS,
i_PositionWS,
false, // No caustics
true, // TODO: implement
false, // Do not apply lighting
1.0, // TODO: implement
s_VolumeOpacity,
s_VolumeLighting
);
#endif
}

View File

@@ -6,7 +6,7 @@
#define m_Properties \
const float2 i_UndisplacedXZ, \
const float i_LodAlpha, \
float i_LodAlpha, \
const half i_WaterLevelOffset, \
const float2 i_WaterLevelDerivatives, \
const half2 i_Flow, \
@@ -19,13 +19,36 @@
const float3 i_PositionWS, \
const float3 i_PositionVS, \
const float2 i_StaticLightMapUV, \
out half3 o_Albedo, \
out half3 o_NormalWS, \
out half3 o_Specular, \
out half3 o_Emission, \
out half o_Smoothness, \
out half o_Occlusion, \
out half o_Alpha
inout half3 o_Albedo, \
inout half3 o_NormalWS, \
inout half3 o_Specular, \
inout half3 o_Emission, \
inout half o_Smoothness, \
inout half o_Occlusion, \
inout half o_Alpha
#define m_Parameters \
i_UndisplacedXZ, \
i_LodAlpha, \
i_WaterLevelOffset, \
i_WaterLevelDerivatives, \
i_Flow, \
i_ViewDirectionWS, \
i_Facing, \
i_SceneColor, \
i_SceneDepthRaw, \
i_ScreenPosition, \
i_ScreenPositionRaw, \
i_PositionWS, \
i_PositionVS, \
i_StaticLightMapUV, \
o_Albedo, \
o_NormalWS, \
o_Specular, \
o_Emission, \
o_Smoothness, \
o_Occlusion, \
o_Alpha
// Guard against Shader Graph preview.
#ifndef SHADERGRAPH_PREVIEW
@@ -33,6 +56,7 @@
#define d_Crest_WaterSurface 1
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Shim.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Keywords.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
@@ -54,6 +78,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Refraction.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Caustics.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/VolumeLighting.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Emission.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Fresnel.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Foam.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Alpha.hlsl"
@@ -79,17 +104,18 @@ static const TiledTexture _Crest_CausticsTiledTexture =
static const TiledTexture _Crest_CausticsDistortionTiledTexture =
TiledTexture::Make(_Crest_CausticsDistortionTexture, sampler_Crest_CausticsDistortionTexture, _Crest_CausticsDistortionTexture_TexelSize, _Crest_CausticsDistortionScale, 1.0);
void Fragment(m_Properties)
void Fragment
(
m_Properties,
const uint i_LodIndex0,
const uint i_LodIndex1,
const float2 i_PositionSS,
const bool i_Underwater,
const float i_SceneZRaw,
const float i_NegativeFog
)
{
o_Albedo = 0.0;
o_NormalWS = half3(0.0, 1.0, 0.0);
o_Specular = 0.0;
o_Emission = 0.0;
o_Smoothness = 0.7;
o_Occlusion = 1.0;
o_Alpha = 1.0;
const float2 positionSS = i_ScreenPosition.xy * _ScreenSize.xy;
float2 scenePositionSS = i_PositionSS;
// Editor only. There is no defined editor symbol.
if (_Crest_DrawBoundaryXZ)
@@ -105,47 +131,25 @@ void Fragment(m_Properties)
}
}
bool underwater = IsUnderWater(i_Facing, g_Crest_ForceUnderwater, positionSS);
const uint slice0 = i_LodIndex0;
const uint slice1 = i_LodIndex1;
// TODO: Should we use PosToSIs or check for overflow?
float slice0 = _Crest_LodIndex;
float slice1 = _Crest_LodIndex + 1;
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
const Flow flow = Flow::Make(i_Flow, g_Crest_Time);
#endif
const Cascade cascade0 = Cascade::Make(slice0);
const Cascade cascade1 = Cascade::Make(slice1);
float sceneRawZ = i_SceneDepthRaw;
float negativeFog = _ProjectionParams.y;
const float sceneZ = Utility::CrestLinearEyeDepth(i_SceneZRaw);
const float pixelZ = -i_PositionVS.z;
#if (CREST_PORTALS != 0)
#ifndef CREST_SHADOWPASS
#if _ALPHATEST_ON
if (m_CrestPortal)
{
const float pixelRawZ = i_ScreenPositionRaw.z / i_ScreenPositionRaw.w;
if (Portal::EvaluateSurface(i_ScreenPosition.xy, pixelRawZ, i_PositionWS, underwater, sceneRawZ, negativeFog))
{
o_Alpha = 0.0;
return;
}
}
#endif
#endif
#endif
float sceneZ = Utility::CrestLinearEyeDepth(sceneRawZ);
float pixelZ = -i_PositionVS.z;
const bool isLastLod = _Crest_LodIndex == (uint)g_Crest_LodCount - 1;
const bool isLastLod = slice0 == (uint)g_Crest_LodCount - 1;
const float weight0 = (1.0 - i_LodAlpha) * cascade0._Weight;
const float weight1 = (1.0 - weight0) * cascade1._Weight;
// Data that fades towards the edge.
half foam = 0.0; half _determinant = 0.0; half4 albedo = 0.0; half2 shadow = 0.0;
half foam = 0.0; half _determinant = 0.0; half4 albedo = 0.0; half2 shadow = 0.0; half waterDepth = i_WaterLevelOffset; half shorelineDistance = 0.0;
if (weight0 > m_CrestSampleLodThreshold)
{
Cascade::MakeAnimatedWaves(slice0).SampleNormals(i_UndisplacedXZ, weight0, o_NormalWS.xz, _determinant);
@@ -155,15 +159,28 @@ void Fragment(m_Properties)
Cascade::MakeFoam(slice0).SampleFoam(i_UndisplacedXZ, weight0, foam);
}
#if d_Crest_AlbedoLod
if (_Crest_AlbedoEnabled)
{
Cascade::MakeAlbedo(slice0).SampleAlbedo(i_UndisplacedXZ, weight0, albedo);
}
#endif
#if d_Crest_ShadowLod
if (_Crest_ShadowsEnabled)
{
Cascade::MakeShadow(slice0).SampleShadow(i_PositionWS.xz, weight0, shadow);
}
#endif
#if d_Crest_SimpleTransparency || d_Crest_FoamBioluminescence
#if !d_Crest_SimpleTransparency
if (_Crest_FoamEnabled && _Crest_FoamBioluminescenceEnabled)
#endif
{
Cascade::MakeDepth(slice0).SampleSignedDepthFromSeaLevelAndDistance(i_PositionWS.xz, weight0, waterDepth, shorelineDistance);
}
#endif
}
if (weight1 > m_CrestSampleLodThreshold)
@@ -175,15 +192,28 @@ void Fragment(m_Properties)
Cascade::MakeFoam(slice1).SampleFoam(i_UndisplacedXZ, weight1, foam);
}
#if d_Crest_AlbedoLod
if (_Crest_AlbedoEnabled)
{
Cascade::MakeAlbedo(slice1).SampleAlbedo(i_UndisplacedXZ, weight1, albedo);
}
#endif
#if d_Crest_ShadowLod
if (_Crest_ShadowsEnabled)
{
Cascade::MakeShadow(slice1).SampleShadow(i_PositionWS.xz, weight1, shadow);
}
#endif
#if d_Crest_SimpleTransparency || d_Crest_FoamBioluminescence
#if !d_Crest_SimpleTransparency
if (_Crest_FoamEnabled && _Crest_FoamBioluminescenceEnabled)
#endif
{
Cascade::MakeDepth(slice1).SampleSignedDepthFromSeaLevelAndDistance(i_PositionWS.xz, weight1, waterDepth, shorelineDistance);
}
#endif
}
// Invert so shadows are black as we normally multiply this by lighting.
@@ -191,99 +221,81 @@ void Fragment(m_Properties)
// Data that displays to the edge.
// The default simulation value has been written to the border of the last slice.
half3 absorption = 0.0; half3 scattering = 0.0;
half3 absorption = _Crest_Absorption.xyz; half3 scattering = _Crest_Scattering.xyz;
#if d_Crest_AbsorptionLod || d_Crest_ScatteringLod
{
const float weight0 = (1.0 - (isLastLod ? 0.0 : i_LodAlpha)) * cascade0._Weight;
const float weight1 = (1.0 - weight0) * cascade1._Weight;
#if d_Crest_ScatteringLod
if (g_Crest_SampleScatteringSimulation)
{
scattering = 0.0;
}
#endif
#if d_Crest_AbsorptionLod
if (g_Crest_SampleAbsorptionSimulation)
{
absorption = 0.0;
}
#endif
if (weight0 > m_CrestSampleLodThreshold)
{
#if d_Crest_ScatteringLod
if (g_Crest_SampleScatteringSimulation)
{
Cascade::MakeScattering(slice0).SampleScattering(i_UndisplacedXZ, weight0, scattering);
}
#endif
#if d_Crest_AbsorptionLod
if (g_Crest_SampleAbsorptionSimulation)
{
Cascade::MakeAbsorption(slice0).SampleAbsorption(i_UndisplacedXZ, weight0, absorption);
}
#endif
}
if (weight1 > m_CrestSampleLodThreshold)
{
#if d_Crest_ScatteringLod
if (g_Crest_SampleScatteringSimulation)
{
Cascade::MakeScattering(slice1).SampleScattering(i_UndisplacedXZ, weight1, scattering);
}
#endif
#if d_Crest_AbsorptionLod
if (g_Crest_SampleAbsorptionSimulation)
{
Cascade::MakeAbsorption(slice1).SampleAbsorption(i_UndisplacedXZ, weight1, absorption);
}
#endif
}
}
if (!g_Crest_SampleScatteringSimulation)
{
scattering = _Crest_Scattering.xyz;
}
if (!g_Crest_SampleAbsorptionSimulation)
{
absorption = _Crest_Absorption.xyz;
}
#endif
// Determinant needs to be one when no waves.
if (isLastLod)
{
_determinant += 1.0 - weight0;
waterDepth = 10000.0;
}
// Normal.
{
if (_Crest_NormalMapEnabled)
{
o_NormalWS.xz += SampleNormalMaps
(
#ifdef CREST_FLOW_ON
flow,
#if d_Transparent
// Feather at intersection. Cannot be used for shadows since depth is not available.
const float feather =
#if d_Crest_SimpleTransparency
saturate(waterDepth / 0.2);
#else
saturate((sceneZ - pixelZ) / 0.2);
#endif
#endif
_Crest_NormalMapTiledTexture,
_Crest_NormalMapStrength,
i_UndisplacedXZ,
i_LodAlpha,
cascade0
);
}
o_NormalWS = normalize(o_NormalWS);
WaterNormal
(
i_WaterLevelDerivatives,
i_ViewDirectionWS,
_Crest_MinimumReflectionDirectionY,
underwater,
o_NormalWS
);
o_NormalWS = normalize(o_NormalWS);
o_NormalWS.xz *= _Crest_NormalsStrengthOverall;
o_NormalWS.y = lerp(1.0, o_NormalWS.y, _Crest_NormalsStrengthOverall);
if (underwater)
{
// Flip when underwater.
o_NormalWS.xyz *= -1.0;
}
}
// Default for opaque render type.
float sceneDistance = 1000.0;
float3 scenePositionWS = 0.0;
const half3 ambientLight = AmbientLight();
const half3 extinction = VolumeExtinction(absorption, scattering);
float3 lightIntensity = 0.0;
half3 lightDirection = 0.0;
@@ -295,24 +307,123 @@ void Fragment(m_Properties)
lightDirection
);
half3 additionalLight = AdditionalLighting(i_PositionWS, i_ScreenPositionRaw, i_StaticLightMapUV);
const half3 ambientLight = AmbientLight();
const half3 additionalLight = AdditionalLighting(i_PositionWS, i_ScreenPositionRaw, i_StaticLightMapUV);
// Normal.
{
WaterNormal
(
i_WaterLevelDerivatives,
i_ViewDirectionWS,
_Crest_MinimumReflectionDirectionY,
i_Underwater,
o_NormalWS
);
#if d_Crest_NormalMap
if (_Crest_NormalMapEnabled)
{
const half2 normalMap = SampleNormalMaps
(
#if d_Crest_FlowLod
flow,
#endif
_Crest_NormalMapTiledTexture,
_Crest_NormalMapStrength,
i_UndisplacedXZ,
i_LodAlpha,
cascade0
);
half normalMapStrength = _Crest_NormalMapStrength;
if (_Crest_NormalMapTurbulenceEnabled)
{
normalMapStrength = NormalMapTurbulence
(
o_NormalWS,
normalMap,
normalMapStrength,
_Crest_NormalMapTurbulenceCoverage,
_Crest_NormalMapTurbulenceStrength,
i_ViewDirectionWS,
_determinant,
g_Crest_WaterCenter.y + i_WaterLevelOffset,
pixelZ,
lightDirection
);
}
o_NormalWS.xz += normalMap * normalMapStrength;
}
#endif // d_Crest_NormalMap
o_NormalWS = normalize(o_NormalWS);
WaterNormal
(
i_WaterLevelDerivatives,
i_ViewDirectionWS,
_Crest_MinimumReflectionDirectionY,
i_Underwater,
o_NormalWS
);
o_NormalWS = normalize(o_NormalWS);
o_NormalWS.xz *= _Crest_NormalsStrengthOverall;
o_NormalWS.y = lerp(1.0, o_NormalWS.y, _Crest_NormalsStrengthOverall);
if (i_Underwater)
{
// Flip when underwater.
o_NormalWS.xyz *= -1.0;
}
}
// Default for opaque render type.
float sceneDistance = 1000.0;
float3 scenePositionWS = i_PositionWS;
#if d_Crest_SimpleTransparency
sceneDistance = waterDepth;
// Increase ray for grazing angles.
sceneDistance += (1.0 - dot(i_ViewDirectionWS, o_NormalWS)) * waterDepth;
scenePositionWS.y = i_PositionWS.y - waterDepth;
// Cannot sample scene so go with average light.
o_Emission = i_Underwater ? 0.0 : (0.5 * (lightIntensity + additionalLight + ambientLight) * INV_PI);
#if CREST_HDRP
o_Emission /= GetCurrentExposureMultiplier();
#endif
#endif
bool caustics = !i_Underwater;
#if !d_Crest_SimpleTransparency
#if d_Transparent
#ifndef d_SkipRefraction
bool caustics;
RefractedScene
(
_Crest_RefractionStrength,
1.000293, // air
_Crest_RefractiveIndexOfWater,
o_NormalWS,
i_PositionWS,
i_ScreenPosition.xy,
i_ScreenPositionRaw,
pixelZ,
i_SceneColor,
i_ViewDirectionWS,
sceneZ,
sceneRawZ,
underwater,
i_SceneZRaw,
cascade0._Scale,
i_LodAlpha,
i_Underwater,
_Crest_TotalInternalReflectionIntensity,
o_Emission,
sceneDistance,
scenePositionWS,
scenePositionSS,
caustics
);
@@ -329,45 +440,46 @@ void Fragment(m_Properties)
#endif
#endif // d_SkipRefraction
#endif // d_Transparent
float refractedSeaLevel = g_Crest_WaterCenter.y;
float3 refractedSurfacePosition = float3(0, refractedSeaLevel, 0);
if (!underwater && slice1 < g_Crest_LodCount)
{
// Sample larger slice to avoid the first slice.
float4 displacement = Cascade::MakeAnimatedWaves(slice1).Sample(scenePositionWS.xz);
refractedSeaLevel = g_Crest_WaterCenter.y + displacement.w;
refractedSurfacePosition = displacement.xyz;
refractedSurfacePosition.y += refractedSeaLevel;
}
#endif // d_Crest_SimpleTransparency
// Out-scattering.
if (!underwater)
if (!i_Underwater)
{
// Account for average extinction of light as it travels down through volume. Assume flat water as anything else would be expensive.
half3 extinction = absorption.xyz + scattering.xyz;
o_Emission *= exp(-extinction * max(0.0, refractedSeaLevel - scenePositionWS.y));
// Account for extinction of light as it travels down through volume.
o_Emission *= exp(-extinction * max(0.0, i_PositionWS.y - scenePositionWS.y));
}
#if d_Transparent
#ifndef d_SkipRefraction
// Caustics
if (_Crest_CausticsEnabled && !underwater && caustics)
if (_Crest_CausticsEnabled && !i_Underwater && caustics)
{
half lightOcclusion = PrimaryLightShadows(scenePositionWS, positionSS);
half lightOcclusion = 1.0;
#if !d_Crest_SimpleTransparency
lightOcclusion = PrimaryLightShadows(scenePositionWS, scenePositionSS);
#endif
#if d_Crest_SimpleTransparency
if (_Crest_RefractionStrength > 0.0)
{
// Gives a parallax like effect.
const half3 ray = refract(-i_ViewDirectionWS, o_NormalWS, 1.0 / _Crest_RefractiveIndexOfWater) * _Crest_RefractionStrength;
scenePositionWS += ray * waterDepth * 2.0;
}
#endif
half blur = 0.0;
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
blur = _Crest_CausticsMotionBlur;
#endif
o_Emission *= Caustics
(
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
flow,
#endif
scenePositionWS,
refractedSurfacePosition.y,
i_PositionWS.y,
lightIntensity,
lightDirection,
lightOcclusion,
@@ -380,7 +492,7 @@ void Fragment(m_Properties)
_Crest_CausticsDistortionTiledTexture,
_Crest_CausticsDistortionStrength,
blur,
underwater
_Crest_CausticsForceDistortion
);
}
#endif // d_SkipRefraction
@@ -404,7 +516,6 @@ void Fragment(m_Properties)
}
// Volume Lighting
const half3 extinction = VolumeExtinction(absorption, scattering);
const half3 volumeOpacity = VolumeOpacity(extinction, sceneDistance);
const half3 volumeLight = VolumeLighting
(
@@ -431,7 +542,7 @@ void Fragment(m_Properties)
(
i_ViewDirectionWS,
o_NormalWS,
underwater,
i_Underwater,
1.0, // air
_Crest_RefractiveIndexOfWater,
_Crest_TotalInternalReflectionIntensity,
@@ -439,7 +550,7 @@ void Fragment(m_Properties)
reflected
);
if (underwater)
if (i_Underwater)
{
o_Emission *= transmitted;
o_Emission += volumeLight * reflected;
@@ -452,10 +563,12 @@ void Fragment(m_Properties)
}
}
#if _SPECULAR_SETUP
// Specular
{
o_Specular = _Crest_Specular * reflected * shadow.y;
}
#endif
// Smoothness
{
@@ -465,24 +578,27 @@ void Fragment(m_Properties)
// Occlusion
{
o_Occlusion = underwater ? _Crest_OcclusionUnderwater : _Crest_Occlusion;
o_Occlusion = i_Underwater ? _Crest_OcclusionUnderwater : _Crest_Occlusion;
}
// Planar Reflections
#if d_Crest_PlanarReflections
if (_Crest_PlanarReflectionsEnabled)
{
half4 reflection = PlanarReflection
(
_Crest_ReflectionTexture,
sampler_Crest_ReflectionTexture,
_Crest_ReflectionColorTexture,
sampler_Crest_ReflectionColorTexture,
_Crest_PlanarReflectionsIntensity,
o_Smoothness,
_Crest_PlanarReflectionsRoughness,
_Crest_MinimumReflectionDirectionY,
pixelZ,
o_NormalWS,
_Crest_PlanarReflectionsDistortion,
i_ViewDirectionWS,
i_ScreenPosition.xy,
underwater
i_Underwater
);
half alpha = reflection.a;
@@ -491,13 +607,14 @@ void Fragment(m_Properties)
// Results are darker than Unity's.
o_Occlusion *= 1.0 - alpha;
}
#endif // d_Crest_PlanarReflections
// Foam
if (_Crest_FoamEnabled)
{
half albedo = MultiScaleFoamAlbedo
half2 albedo = MultiScaleFoamAlbedo
(
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
flow,
#endif
_Crest_FoamTiledTexture,
@@ -506,19 +623,20 @@ void Fragment(m_Properties)
cascade0,
cascade1,
i_LodAlpha,
i_UndisplacedXZ
i_UndisplacedXZ,
_Crest_FoamBioluminescenceEnabled && _Crest_FoamBioluminescenceSparklesEnabled
);
half2 normal = MultiScaleFoamNormal
(
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
flow,
#endif
_Crest_FoamTiledTexture,
_Crest_FoamFeather,
_Crest_FoamNormalStrength,
foam,
albedo,
albedo.x,
cascade0,
cascade1,
i_LodAlpha,
@@ -530,13 +648,13 @@ void Fragment(m_Properties)
ApplyFoamToSurface
(
albedo,
albedo.x,
normal,
intensity,
_Crest_Occlusion,
_Crest_FoamSmoothness,
_Crest_Specular,
underwater,
i_Underwater,
o_Albedo,
o_NormalWS,
o_Emission,
@@ -546,36 +664,71 @@ void Fragment(m_Properties)
);
// We will use this for shadow casting.
foam = albedo;
const half foamData = foam;
foam = albedo.r;
#if d_Crest_FoamBioluminescence
if (_Crest_FoamBioluminescenceEnabled)
{
half3 emission = FoamBioluminescence
(
foamData,
albedo.r,
_Crest_FoamBioluminescenceColor.rgb,
_Crest_FoamBioluminescenceIntensity,
_Crest_FoamBioluminescenceGlowCoverage,
_Crest_FoamBioluminescenceGlowIntensity,
_Crest_FoamBioluminescenceSparklesEnabled,
albedo.y,
_Crest_FoamBioluminescenceSparklesCoverage,
_Crest_FoamBioluminescenceSparklesIntensity,
_Crest_FoamBioluminescenceMaximumDepth,
waterDepth
);
emission *= _Crest_FoamBioluminescenceSeaLevelOnly ? saturate(1.0 - abs(i_WaterLevelOffset)) : 1.0;
#if d_Transparent
// Apply feathering to avoid hardening the edge.
emission *= feather * feather * feather;
#endif
o_Emission += emission;
}
#endif // d_Crest_FoamBioluminescence
}
// Albedo
#if d_Crest_AlbedoLod
if (_Crest_AlbedoEnabled)
{
const float foamMask = _Crest_AlbedoIgnoreFoam ? (1.0 - saturate(foam)) : 1.0;
o_Albedo = lerp(o_Albedo, albedo.rgb, albedo.a * foamMask);
o_Emission *= 1.0 - albedo.a * foamMask;
}
#endif
#if d_Crest_SimpleTransparency
o_Alpha = i_Underwater
? 1.0 - transmitted
: max(max(length(volumeOpacity), _Crest_TransparencyMinimumAlpha), max(foam, albedo.a));
#endif
// Alpha
{
#ifndef CREST_SHADOWPASS
#if d_Transparent
// Feather at intersection. Cannot be used for shadows since depth is not available.
o_Alpha = saturate((sceneZ - pixelZ) / 0.2);
o_Alpha = min(o_Alpha, feather);
#endif
#endif
// This keyword works for all RPs despite BIRP having prefixes in serialised data.
#if _ALPHATEST_ON
#if d_Crest_AlphaTest
#if CREST_SHADOWPASS
o_Alpha = max(foam, albedo.a) - _Crest_ShadowCasterThreshold;
o_Alpha = min(o_Alpha, max(foam, albedo.a) - _Crest_ShadowCasterThreshold);
#endif
// Add 0.5 bias for LOD blending and texel resolution correction. This will help to
// tighten and smooth clipped edges.
o_Alpha -= ClipSurface(i_PositionWS.xz) > 0.5 ? 2.0 : 0.0;
#endif // _ALPHATEST_ON
#endif // d_Crest_AlphaTest
// Specular in HDRP is still affected outside the 0-1 alpha range.
o_Alpha = min(o_Alpha, 1.0);
@@ -583,22 +736,107 @@ void Fragment(m_Properties)
SetUpFog
(
underwater,
i_Underwater,
i_PositionWS,
1.0, // N/A: multiplier for fog nodes
sceneDistance - negativeFog,
pixelZ - i_NegativeFog,
i_ViewDirectionWS,
positionSS
i_PositionSS
);
}
// IMPORTANT!
// Do not branch on o_Alpha, as it is not robust. Rendering will break on stereo
// rendering in the form of losing additional lighting and/or broken reflection
// probe sampling. This is an issue with the shader compiler it seems.
void Fragment(m_Properties)
{
const float2 positionSS = i_ScreenPosition.xy * _ScreenSize.xy;
bool underwater = IsUnderWater(i_Facing, g_Crest_ForceUnderwater, positionSS);
float sceneRawZ = i_SceneDepthRaw;
float negativeFog = _ProjectionParams.y;
#if d_Crest_AlphaTest
#if !d_Crest_SimpleTransparency
#ifndef CREST_SHADOWPASS
#if (CREST_PORTALS != 0)
if (m_CrestPortal)
{
const float pixelRawZ = i_ScreenPositionRaw.z / i_ScreenPositionRaw.w;
o_Alpha = Portal::EvaluateSurface(i_ScreenPosition.xy, pixelRawZ, i_PositionWS, underwater, sceneRawZ, negativeFog) ? -1.0 : 1.0;
#ifndef SHADER_API_WEBGPU
clip(o_Alpha);
#endif
}
#endif
#endif
#endif
#endif
uint slice0;
uint slice1;
float alpha;
#if d_Crest_AlphaTest || d_Crest_CustomMesh
PosToSliceIndices(i_PositionWS.xz, 0.0, g_Crest_LodCount - 1.0, g_Crest_WaterScale, slice0, slice1, alpha);
#endif
#if d_Crest_AlphaTest
{
const Cascade cascade0 = Cascade::Make(slice0);
const Cascade cascade1 = Cascade::Make(slice1);
const float weight0 = (1.0 - alpha) * cascade0._Weight;
const float weight1 = (1.0 - weight0) * cascade1._Weight;
float clipSurface = 0.0;
if (weight0 > m_CrestSampleLodThreshold)
{
Cascade::MakeClip(slice0).SampleClip(i_PositionWS.xz, weight0, clipSurface);
}
if (weight1 > m_CrestSampleLodThreshold)
{
Cascade::MakeClip(slice1).SampleClip(i_PositionWS.xz, weight1, clipSurface);
}
// Add 0.5 bias for LOD blending and texel resolution correction. This will help to
// tighten and smooth clipped edges.
o_Alpha -= clipSurface > 0.5 ? 2.0 : 0.0;
#ifndef SHADER_API_WEBGPU
clip(o_Alpha);
#endif
}
#endif
{
#if !d_Crest_CustomMesh
slice0 = _Crest_LodIndex;
slice1 = _Crest_LodIndex + 1;
alpha = i_LodAlpha;
#endif
i_LodAlpha = alpha;
Fragment
(
m_Parameters,
slice0,
slice1,
positionSS,
underwater,
sceneRawZ,
negativeFog
);
}
}
m_CrestNameSpaceEnd
#endif // SHADERGRAPH_PREVIEW
void Fragment_float(m_Properties)
{
#if SHADERGRAPH_PREVIEW
o_Albedo = 0.0;
o_NormalWS = half3(0.0, 1.0, 0.0);
o_Specular = 0.0;
@@ -606,32 +844,10 @@ void Fragment_float(m_Properties)
o_Smoothness = 0.7;
o_Occlusion = 1.0;
o_Alpha = 1.0;
#else // SHADERGRAPH_PREVIEW
m_Crest::Fragment
(
i_UndisplacedXZ,
i_LodAlpha,
i_WaterLevelOffset,
i_WaterLevelDerivatives,
i_Flow,
i_ViewDirectionWS,
i_Facing,
i_SceneColor,
i_SceneDepthRaw,
i_ScreenPosition,
i_ScreenPositionRaw,
i_PositionWS,
i_PositionVS,
i_StaticLightMapUV,
o_Albedo,
o_NormalWS,
o_Specular,
o_Emission,
o_Smoothness,
o_Occlusion,
o_Alpha
);
#endif // SHADERGRAPH_PREVIEW
#if !SHADERGRAPH_PREVIEW
m_Crest::Fragment(m_Parameters);
#endif
}
#undef m_Properties

View File

@@ -11,9 +11,7 @@
// These are per cascade, set per chunk instance.
CBUFFER_START(CrestChunkGeometryData)
float _Crest_ChunkMeshScaleAlpha;
float _Crest_ChunkMeshScaleAlphaSource;
float _Crest_ChunkGeometryGridWidth;
float _Crest_ChunkGeometryGridWidthSource;
CBUFFER_END
m_CrestNameSpace

View File

@@ -0,0 +1,20 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#ifndef d_WaveHarmonic_Crest_SurfaceKeywords
#define d_WaveHarmonic_Crest_SurfaceKeywords
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Keywords.hlsl"
#define d_Crest_AlphaTest defined(_ALPHATEST_ON)
#define d_Crest_MotionVectors defined(_TRANSPARENT_WRITES_MOTION_VEC)
#define d_Crest_CustomMesh defined(_CREST_CUSTOM_MESH)
#define d_Crest_CausticsForceDistortion CREST_CAUSTICS_FORCE_DISTORTION
#define d_Crest_FoamBioluminescence CREST_FOAM_BIOLUMINESCENCE
#define d_Crest_NormalMap CREST_NORMAL_MAPS
#define d_Crest_SimpleTransparency CREST_SIMPLE_TRANSPARENCY
#define d_Crest_PlanarReflections CREST_PLANAR_REFLECTIONS
#endif

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 9d4fdd4ba0e96485c93d748132deef6a
ShaderIncludeImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -5,6 +5,7 @@
#define CREST_WATER_NORMAL_H
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings.Crest.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Constants.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Texture.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Flow.hlsl"
@@ -13,12 +14,43 @@
#include "Packages/com.waveharmonic.crest.shifting-origin/Runtime/Shaders/ShiftingOrigin.hlsl"
#endif
#if _CREST_CUSTOM_MESH
float4 _Crest_NormalMapParameters[MAX_LOD_COUNT];
#define _Crest_ChunkNormalMapParameters _Crest_NormalMapParameters[i_CascadeData._IndexI]
#else
// These are per cascade, set per chunk instance.
float _Crest_ChunkFarNormalsWeight;
float2 _Crest_ChunkNormalScrollSpeed;
#define _Crest_ChunkNormalMapParameters float3(_Crest_ChunkNormalScrollSpeed, _Crest_ChunkFarNormalsWeight)
#endif
m_CrestNameSpace
// Limit how close to horizontal reflection ray can get, useful to avoid unsightly below-horizon reflections.
half3 ApplyMinimumReflectionDirectionY
(
const half i_MinimumReflectionDirectionY,
const half3 i_ViewDirectionWS,
const half3 i_NormalWS
)
{
half3 normal = i_NormalWS;
float3 refl = reflect(-i_ViewDirectionWS, normal);
if (refl.y < i_MinimumReflectionDirectionY)
{
// Find the normal that keeps the reflection direction above the horizon. Compute
// the reflection dir that does work, normalize it, and then normal is half vector
// between this good reflection direction and view direction.
float3 FL = refl;
FL.y = i_MinimumReflectionDirectionY;
FL = normalize(FL);
normal = normalize(FL + i_ViewDirectionWS);
}
return normal;
}
half2 SampleNormalMaps
(
const TiledTexture i_NormalMap,
@@ -35,20 +67,24 @@ half2 SampleNormalMaps
worldXZUndisplaced -= ShiftingOriginOffset(i_NormalMap, i_CascadeData);
#endif
const float3 parameters = _Crest_ChunkNormalMapParameters.xyz;
const float2 speed = parameters.xy;
const float farWeight = parameters.z;
const float2 v0 = float2(0.94, 0.34), v1 = float2(-0.85, -0.53);
float scale = i_NormalMap._scale * i_CascadeData._Scale / 10.0;
const float spdmulL = _Crest_ChunkNormalScrollSpeed.x * i_NormalMap._speed;
const float spdmulL = speed.x * i_NormalMap._speed;
half2 norm =
UnpackNormal(i_NormalMap.Sample((worldXZUndisplaced + v0 * g_Crest_Time * spdmulL) / scale)).xy +
UnpackNormal(i_NormalMap.Sample((worldXZUndisplaced + v1 * g_Crest_Time * spdmulL) / scale)).xy;
// blend in next higher scale of normals to obtain continuity
const half nblend = i_LodAlpha * _Crest_ChunkFarNormalsWeight;
const half nblend = i_LodAlpha * farWeight;
if (nblend > 0.001)
{
// next lod level
scale *= 2.0;
const float spdmulH = _Crest_ChunkNormalScrollSpeed.y * i_NormalMap._speed;
const float spdmulH = speed.y * i_NormalMap._speed;
norm = lerp(norm,
UnpackNormal(i_NormalMap.Sample((worldXZUndisplaced + v0 * g_Crest_Time * spdmulH) / scale)).xy +
UnpackNormal(i_NormalMap.Sample((worldXZUndisplaced + v1 * g_Crest_Time * spdmulH) / scale)).xy,
@@ -56,7 +92,7 @@ half2 SampleNormalMaps
}
// approximate combine of normals. would be better if normals applied in local frame.
return i_Strength * norm;
return norm;
}
half2 SampleNormalMaps
@@ -86,6 +122,41 @@ half2 SampleNormalMaps
) * i_Flow._Weight1;
}
half NormalMapTurbulence
(
const half3 i_NormalWS,
const half2 i_NormalMap,
const half i_NormalMapStrength,
const half i_Coverage,
const half i_Strength,
const half3 i_ViewDirectionWS,
const half i_Determinant,
const half i_WaterLevel,
const float i_PixelZ,
const half3 i_PrimaryLightDirection
)
{
half strength = i_NormalMapStrength;
if (saturate(i_Coverage - i_Determinant) > 0)
{
// Add boosted normal map.
half3 normal = i_NormalWS;
normal.xz += i_NormalMap * i_Strength;
normal = normalize(normal);
// Increase normal map strength only if "sparkle".
if (dot(normal, normalize(i_ViewDirectionWS + i_PrimaryLightDirection)) >= 0.99)
{
// Height (100m) & distance (2m) cull. Looks odd up close and degrades up high.
const half cull = max(saturate(abs(_WorldSpaceCameraPos.y - i_WaterLevel) * 0.01), 1.0 - saturate(i_PixelZ * 0.5));
strength = lerp(i_Strength, strength, cull);
}
}
return strength;
}
void WaterNormal
(
const float2 i_WaterLevelDerivatives,
@@ -106,22 +177,11 @@ void WaterNormal
return;
}
// Limit how close to horizontal reflection ray can get, useful to avoid unsightly below-horizon reflections.
{
float3 refl = reflect(-i_ViewDirectionWS, io_NormalWS);
if (refl.y < i_MinimumReflectionDirectionY)
{
// Find the normal that keeps the reflection direction above the horizon. Compute
// the reflection dir that does work, normalize it, and then normal is half vector
// between this good reflection direction and view direction.
float3 FL = refl;
FL.y = i_MinimumReflectionDirectionY;
FL = normalize(FL);
io_NormalWS = normalize(FL + i_ViewDirectionWS);
}
}
io_NormalWS = ApplyMinimumReflectionDirectionY(i_MinimumReflectionDirectionY, i_ViewDirectionWS, io_NormalWS);
}
m_CrestNameSpaceEnd
#endif

View File

@@ -6,10 +6,16 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Utility.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Helpers.hlsl"
float _Crest_ReflectionOverscan;
float4 _Crest_ReflectionPositionNormal[2];
Texture2DArray _Crest_ReflectionTexture;
SamplerState sampler_Crest_ReflectionTexture;
float4x4 _Crest_ReflectionMatrixIVP[2];
float4x4 _Crest_ReflectionMatrixV[2];
Texture2DArray _Crest_ReflectionColorTexture;
SamplerState sampler_Crest_ReflectionColorTexture;
Texture2DArray _Crest_ReflectionDepthTexture;
SamplerState sampler_Crest_ReflectionDepthTexture;
m_CrestNameSpace
@@ -20,6 +26,8 @@ half4 PlanarReflection
const half i_Intensity,
const half i_Smoothness,
const half i_Roughness,
const half i_MinimumReflectionDirectionY,
const float i_SurfaceDepth,
const half3 i_NormalWS,
const half i_NormalStrength,
const half3 i_ViewDirectionWS,
@@ -27,10 +35,18 @@ half4 PlanarReflection
const bool i_Underwater
)
{
const uint slice = i_Underwater ? 1 : 0;
half3 planeNormal = half3(0.0, i_Underwater ? -1.0 : 1.0, 0.0);
half3 reflected = reflect(-i_ViewDirectionWS, lerp(planeNormal, i_NormalWS, i_NormalStrength));
reflected.y = -reflected.y;
// Limit how close to horizontal reflection ray can get, useful to avoid unsightly below-horizon reflections.
if (!i_Underwater)
{
reflected = ApplyMinimumReflectionDirectionY(i_MinimumReflectionDirectionY, i_ViewDirectionWS, reflected);
}
float4 positionCS = mul(UNITY_MATRIX_VP, half4(reflected, 0.0));
#if UNITY_UV_STARTS_AT_TOP
positionCS.y = -positionCS.y;
@@ -38,19 +54,39 @@ half4 PlanarReflection
float2 positionNDC = positionCS.xy * rcp(positionCS.w) * 0.5 + 0.5;
// Overscan.
positionNDC.xy -= 0.5;
positionNDC.xy *= _Crest_ReflectionOverscan;
positionNDC.xy += 0.5;
// Cancel out distortion if out of bounds. We could make this nicer by doing an edge fade but the improvement is
// barely noticeable. Edge fade requires recalculating the above a second time.
if (i_Underwater)
{
float4 positionAndNormal = _Crest_ReflectionPositionNormal[i_Underwater];
const float4 positionAndNormal = _Crest_ReflectionPositionNormal[slice];
if (dot(positionNDC - positionAndNormal.xy, positionAndNormal.zw) < 0.0)
{
positionNDC = lerp(i_PositionNDC, positionNDC, 0.25);
float2 ndc = i_PositionNDC;
ndc.xy -= 0.5;
ndc.xy *= _Crest_ReflectionOverscan;
ndc.xy += 0.5;
positionNDC = lerp(ndc, positionNDC, 0.25);
}
}
const half roughness = PerceptualSmoothnessToPerceptualRoughness(i_Smoothness);
const half level = PerceptualRoughnessToMipmapLevel(roughness, i_Roughness);
half4 reflection = i_ReflectionsTexture.SampleLevel(sampler_Crest_ReflectionTexture, float3(positionNDC, i_Underwater), level);
half4 reflection;
if (_Crest_PlanarReflectionsApplySmoothness)
{
const half roughness = PerceptualSmoothnessToPerceptualRoughness(i_Smoothness);
half level = PerceptualRoughnessToMipmapLevel(roughness, i_Roughness);
reflection = i_ReflectionsTexture.SampleLevel(sampler_Crest_ReflectionColorTexture, float3(positionNDC, i_Underwater), level);
}
else
{
reflection = i_ReflectionsTexture.SampleLevel(sampler_Crest_point_clamp, float3(positionNDC, i_Underwater), 0.0);
}
// If more than four layers are used on the terrain, they will appear black if HDR
// is enabled on the planar reflection camera. Alpha is probably a negative value.
@@ -58,6 +94,21 @@ half4 PlanarReflection
reflection.a *= i_Intensity;
// Mitigate leaks.
{
// TODO: calculate linear depth from device depth directly. First attempt failed.
// Most effective when surface is smooth due to mip-maps. Surprisingly effective
// even when rough.
const float rRawDepth = _Crest_ReflectionDepthTexture.SampleLevel(sampler_Crest_ReflectionDepthTexture, float3(positionNDC, i_Underwater), 0).r;
const float3 rPositionWS = Utility::SafeComputeWorldSpacePosition(positionNDC, rRawDepth, _Crest_ReflectionMatrixIVP[slice]);
const float rDepth = LinearEyeDepth(rPositionWS, _Crest_ReflectionMatrixV[slice]);
if (rRawDepth > 0.0 && rDepth <= i_SurfaceDepth)
{
reflection.a = 0.0;
}
}
return reflection;
}

View File

@@ -4,6 +4,8 @@
#ifndef CREST_WATER_REFRACTION_H
#define CREST_WATER_REFRACTION_H
#if !d_Crest_SimpleTransparency
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings.Crest.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Depth.hlsl"
@@ -18,91 +20,144 @@
#define FoveatedRemapLinearToNonUniform(uv) uv
#endif
#if (UNITY_VERSION < 60000000) || !defined(CREST_URP)
float4 _CameraDepthTexture_TexelSize;
#endif
m_CrestNameSpace
float2 GetRefractionCoordinates(const half3 i_View, const half3 i_Normal, const float3 i_Position, const half i_IOR, const half i_Strength)
{
float3 position = i_Position;
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
position -= _WorldSpaceCameraPos;
#endif
const half3 ray = refract(-i_View, i_Normal, i_IOR) * i_Strength;
float2 uv = ComputeNormalizedDeviceCoordinates(position + ray, UNITY_MATRIX_VP);
#if CREST_HDRP
// Prevent artifacts at edge. Maybe because depth is an atlas for HDRP.
uv = clamp(uv, _CameraDepthTexture_TexelSize.xy, 1.0 - _CameraDepthTexture_TexelSize.xy);
#endif
return FoveatedRemapLinearToNonUniform(uv);
}
// We take the unrefracted scene colour as input because having a Scene Colour node in the graph
// appears to be necessary to ensure the scene colours are bound?
void RefractedScene
(
const half i_RefractionStrength,
const half i_AirIOR,
const half i_WaterIOR,
const half3 i_NormalWS,
const float3 i_PositionWS,
const float2 i_PositionNDC,
const float4 i_ScreenPositionRaw,
const float i_PixelZ,
const half3 i_SceneColorUnrefracted,
const half3 i_View,
const float i_SceneZ,
const float i_SceneZRaw,
const float i_Scale,
const float i_LodAlpha,
const bool i_Underwater,
const half i_TotalInternalReflectionIntensity,
out half3 o_SceneColor,
out float o_SceneDistance,
out float3 o_ScenePositionWS,
out float2 o_PositionSS,
out bool o_Caustics
)
{
float2 positionNDC = i_PositionNDC;
float sceneDepthRaw = i_SceneZRaw;
o_Caustics = true;
// View ray intersects geometry surface either above or below water surface.
float2 refractOffset = i_RefractionStrength * i_NormalWS.xz;
if (!i_Underwater)
half strength = i_RefractionStrength;
const half _AirToWaterRatio = i_AirIOR / i_WaterIOR;
const half _WaterToAirRatio = i_WaterIOR / i_AirIOR;
// If no TIR, then use same IOR.
const bool isA2WR = !i_Underwater || i_TotalInternalReflectionIntensity < 1.0;
const half eta = isA2WR ? _AirToWaterRatio : _WaterToAirRatio;
half3 normal = i_NormalWS;
// Exchanges accuracy for less artifacts.
if (isA2WR)
{
// We're above the water, so behind interface is depth fog.
refractOffset *= min(1.0, 0.5 * (i_SceneZ - i_PixelZ)) / i_SceneZ;
}
else
{
// When looking up through water, full strength ends up being quite intense so reduce it a bunch.
refractOffset *= 0.3;
half multiplier = 0.0;
if (i_Underwater)
{
multiplier = 1.0;
// Max fade when water is 5m deep.
multiplier = saturate(g_Crest_WaterDepthAtViewer * 0.2);
// Max fade by displacement.
multiplier *= saturate(g_Crest_MaximumVerticalDisplacement - 1.0);
// Fade towards screen edge where off screen samples happen. + n is fade start.
multiplier *= saturate((dot(i_PositionNDC - 0.5, -g_Crest_HorizonNormal) + 0.5) * 2.0);
}
normal.y *= multiplier;
}
// Blend at the edge of the screen to avoid artifacts.
refractOffset *= 1.0 - EdgeBlendingFactor(positionNDC, i_PixelZ);
// Since we lose detail at a distance, boosting refraction helps visually.
strength *= lerp(i_Scale, i_Scale * 2.0, i_LodAlpha) * 0.25;
const float2 positionNDCRefracted = FoveatedRemapLinearToNonUniform(positionNDC + refractOffset);
float sceneDepthRawRefracted = SHADERGRAPH_SAMPLE_SCENE_DEPTH(positionNDCRefracted);
// Restrict to a reasonable maximum.
strength = min(strength, i_RefractionStrength * 4.0);
float2 uv = GetRefractionCoordinates(i_View, normal, i_PositionWS, eta, strength);
o_PositionSS = min(uv * _ScreenSize.xy, _ScreenSize.xy - 1.0);
#if CREST_BIRP
float deviceDepth = LoadSceneDepth(o_PositionSS);
#else
float deviceDepth = SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv);
#endif
#if (CREST_PORTALS != 0)
#if _ALPHATEST_ON
// Portals
Portal::EvaluateRefraction(positionNDCRefracted, i_SceneZRaw, i_Underwater, sceneDepthRawRefracted, o_Caustics);
Portal::EvaluateRefraction(uv, i_SceneZRaw, i_Underwater, deviceDepth, o_Caustics);
#endif
#endif
float linearDepth = Utility::CrestLinearEyeDepth(deviceDepth);
float depthDifference = linearDepth - i_PixelZ;
const float sceneZRefract = Utility::CrestLinearEyeDepth(sceneDepthRawRefracted);
normal *= saturate(depthDifference);
// Depth fog & caustics - only if view ray starts from above water.
// Compute depth fog alpha based on refracted position if it landed on an
// underwater surface, or on unrefracted depth otherwise.
if (sceneZRefract > i_PixelZ)
{
// Refracted.
o_SceneDistance = sceneZRefract - i_PixelZ;
o_SceneColor = SHADERGRAPH_SAMPLE_SCENE_COLOR(positionNDCRefracted);
uv = GetRefractionCoordinates(i_View, normal, i_PositionWS, eta, strength);
positionNDC = positionNDCRefracted;
sceneDepthRaw = sceneDepthRawRefracted;
}
else
{
// Unrefracted.
// It seems that when MSAA is enabled this can sometimes be negative.
o_SceneDistance = max(i_SceneZ - i_PixelZ, 0.0);
o_SceneColor = i_SceneColorUnrefracted;
o_PositionSS = min(uv * _ScreenSize.xy, _ScreenSize.xy - 1.0);
// NOTE: Causes refraction artifact with caustics. Cannot remember exactly why this was added.
// o_Caustics = false;
positionNDC = FoveatedRemapLinearToNonUniform(positionNDC);
}
#if CREST_BIRP
deviceDepth = LoadSceneDepth(o_PositionSS);
#else
deviceDepth = SHADERGRAPH_SAMPLE_SCENE_DEPTH(uv);
#endif
if (i_Underwater)
{
// Depth fog is handled by underwater shader.
o_SceneDistance = i_PixelZ;
}
linearDepth = Utility::CrestLinearEyeDepth(deviceDepth);
// It seems that when MSAA is enabled this can sometimes be negative.
depthDifference = max(linearDepth - i_PixelZ, 0.0);
o_ScenePositionWS = ComputeWorldSpacePosition(positionNDC, sceneDepthRaw, UNITY_MATRIX_I_VP);
#if CREST_BIRP
// Sampling artifacts which manifest as a fine outline around refractions. Always
// affects BIRP unless we use Load. Does not affect URP unless downsampling or MSAA
// is used, but Load exposes us to RT scaling. Best to use Sample with HDRP too.
o_SceneColor = LoadSceneColor(o_PositionSS).rgb;
#else
// Sampling artifacts if downsampling or MSAA used. Load does not help. And we get
// outlines around all objects irrespective of refraction.
o_SceneColor = SHADERGRAPH_SAMPLE_SCENE_COLOR(uv).rgb;
#endif
o_SceneDistance = depthDifference;
o_ScenePositionWS = ComputeWorldSpacePosition(uv, deviceDepth, UNITY_MATRIX_I_VP);
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
o_ScenePositionWS += _WorldSpaceCameraPos;
#endif
@@ -111,3 +166,4 @@ void RefractedScene
m_CrestNameSpaceEnd
#endif
#endif

View File

@@ -58,4 +58,8 @@
#endif
#endif
#if !defined(CREST_HDRP) && (SHADERPASS == SHADERPASS_MOTION_VECTORS)
#define _TRANSPARENT_WRITES_MOTION_VEC
#endif
#endif // CREST_SHADERGRAPH_CONSTANTS_H

View File

@@ -20,6 +20,7 @@
#ifndef SHADERGRAPH_PREVIEW
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Shim.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Keywords.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Globals.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/InputsDriven.hlsl"
@@ -29,13 +30,7 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Depth.hlsl"
#ifndef CREST_HDRP
#if (SHADERPASS == SHADERPASS_MOTION_VECTORS)
#define _TRANSPARENT_WRITES_MOTION_VEC 1
#endif
#endif
#if _TRANSPARENT_WRITES_MOTION_VEC
#if d_Crest_MotionVectors
#define m_Slice clamp((int)_Crest_LodIndex + (isMotionVectors ? g_Crest_LodChange : 0), 0, g_Crest_LodCount)
#define m_Make(slice) Make(slice, isMotionVectors)
#else
@@ -50,12 +45,14 @@ void Vertex(m_Properties)
// This will get called twice.
// With current and previous time respectively.
o_UndisplacedXZ = 0.0;
o_PositionWS = i_PositionWS;
o_UndisplacedXZ = i_PositionWS.xz;
o_LodAlpha = 0.0;
o_WaterLevelOffset = 0.0;
o_WaterLevelDerivatives = 0.0;
o_Flow = 0.0;
#if !d_Crest_CustomMesh
const bool isMotionVectors = i_Time < _Time.y;
const float slice0 = m_Slice;
@@ -63,8 +60,6 @@ void Vertex(m_Properties)
const Cascade cascade0 = Cascade::m_Make(slice0);
const Cascade cascade1 = Cascade::m_Make(slice1);
o_PositionWS = i_PositionWS;
// Vertex snapping and LOD transition.
SnapAndTransitionVertLayout
(
@@ -99,48 +94,48 @@ void Vertex(m_Properties)
// Data that needs to be sampled at the undisplaced position.
if (weight0 > m_CrestSampleLodThreshold)
{
#if _TRANSPARENT_WRITES_MOTION_VEC
#if d_Crest_MotionVectors
if (isMotionVectors)
{
Cascade::MakeAnimatedWavesSource(slice0).SampleDisplacement(o_UndisplacedXZ, weight0, o_PositionWS, o_WaterLevelDerivatives);
Cascade::MakeAnimatedWavesSource(slice0).SampleDisplacement(o_UndisplacedXZ, weight0, o_PositionWS, o_WaterLevelDerivatives, o_WaterLevelOffset);
}
else
#endif
{
Cascade::MakeAnimatedWaves(slice0).SampleDisplacement(o_UndisplacedXZ, weight0, o_PositionWS, o_WaterLevelDerivatives);
Cascade::MakeAnimatedWaves(slice0).SampleDisplacement(o_UndisplacedXZ, weight0, o_PositionWS, o_WaterLevelDerivatives, o_WaterLevelOffset);
}
}
if (weight1 > m_CrestSampleLodThreshold)
{
#if _TRANSPARENT_WRITES_MOTION_VEC
#if d_Crest_MotionVectors
if (isMotionVectors)
{
Cascade::MakeAnimatedWavesSource(slice1).SampleDisplacement(o_UndisplacedXZ, weight1, o_PositionWS, o_WaterLevelDerivatives);
Cascade::MakeAnimatedWavesSource(slice1).SampleDisplacement(o_UndisplacedXZ, weight1, o_PositionWS, o_WaterLevelDerivatives, o_WaterLevelOffset);
}
else
#endif
{
Cascade::MakeAnimatedWaves(slice1).SampleDisplacement(o_UndisplacedXZ, weight1, o_PositionWS, o_WaterLevelDerivatives);
Cascade::MakeAnimatedWaves(slice1).SampleDisplacement(o_UndisplacedXZ, weight1, o_PositionWS, o_WaterLevelDerivatives, o_WaterLevelOffset);
}
}
// Data that needs to be sampled at the displaced position.
if (weight0 > m_CrestSampleLodThreshold)
{
#if CREST_FLOW_ON
#if d_Crest_FlowLod
Cascade::MakeFlow(slice0).SampleFlow(o_UndisplacedXZ, weight0, o_Flow);
#endif
}
if (weight1 > m_CrestSampleLodThreshold)
{
#if CREST_FLOW_ON
#if d_Crest_FlowLod
Cascade::MakeFlow(slice1).SampleFlow(o_UndisplacedXZ, weight1, o_Flow);
#endif
}
#if _TRANSPARENT_WRITES_MOTION_VEC
#if d_Crest_MotionVectors
if (isMotionVectors)
{
o_PositionWS.xz -= g_Crest_WaterCenter.xz;
@@ -149,6 +144,7 @@ void Vertex(m_Properties)
o_PositionWS.xz += g_Crest_WaterCenterDelta;
}
#endif
#endif
}
m_CrestNameSpaceEnd

View File

@@ -179,11 +179,62 @@
},
{
"m_Id": "e4a54526c6204b5badd66e158dcf0b1d"
},
{
"m_Id": "eb89085b4d7c46c7bf82f63a3b43a0c6"
},
{
"m_Id": "a12a11f74d174f1d8a63d18ee8743b25"
},
{
"m_Id": "9f311e65704646e48b52019d389d3cd0"
},
{
"m_Id": "d209a2766f5546688fe4bc92416cd548"
},
{
"m_Id": "8ec616eaa4964d38b92b904775e34f7e"
},
{
"m_Id": "feb1793feb264a239de366d730977489"
},
{
"m_Id": "cc11871fc2f74ea2821e2c9f78d3d58e"
},
{
"m_Id": "a485464528a74269852b53d441f1f0a8"
},
{
"m_Id": "bc35f605d4fc49869c24479f827fbfa8"
},
{
"m_Id": "dd752976323845b0aad610adb0672e38"
},
{
"m_Id": "8c45cfde06fb4d5ca12db7bcfa8f6316"
},
{
"m_Id": "30ad385314c946f5a3cbd7adefe9c270"
},
{
"m_Id": "c06c8782d1b742d189925deba54c21bc"
},
{
"m_Id": "38e9cee298f8483e8ca80e75574c2175"
},
{
"m_Id": "4a21e5afa49f40d38aef98a49226fa6e"
},
{
"m_Id": "a6704108884e487799aa5f653566e56b"
}
],
"m_Keywords": [
{
"m_Id": "b91141f3e4cf4131ab732e6ae5828231"
},
{
"m_Id": "ada9e52f38cd4e66a4e97c1fd2a5bd65"
}
],
"m_Dropdowns": [],
@@ -203,6 +254,12 @@
{
"m_Id": "9475b0c108bc456eb09f35111542022b"
},
{
"m_Id": "72dfddd5562c42e4bf73f41a47cd4863"
},
{
"m_Id": "0c1d5d0d16af4922b19d366983ea194e"
},
{
"m_Id": "330cb63ecb704ddebac4935cb0b85ab7"
},
@@ -212,6 +269,9 @@
{
"m_Id": "76dfbf9d02da4e2b9a7cffa11a17b81d"
},
{
"m_Id": "475b6b25b79b44d19bf94d0af92bc9a1"
},
{
"m_Id": "76a4c02b522d4e1d84eca095e84b1f30"
},
@@ -220,6 +280,9 @@
},
{
"m_Id": "d65753c4eac547c5a73c35da6956d9e2"
},
{
"m_Id": "9125a3c8387e4d38987cc5707394b95b"
}
],
"m_Nodes": [
@@ -850,15 +913,15 @@
{
"m_Id": "b7891fe7e5a34f03adebd8a45505c5fc"
},
{
"m_Id": "8e8aee51883445f1b08bfbec696cb18d"
},
{
"m_Id": "e207931e020242bba83058a59f3fb07d"
},
{
"m_Id": "01717f91918a4191a16ca9eb0a394f85"
},
{
"m_Id": "8e8aee51883445f1b08bfbec696cb18d"
},
{
"m_Id": "641a42645cc2483bb33d553093357479"
},
@@ -1210,6 +1273,30 @@
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
"m_ObjectId": "0c1d5d0d16af4922b19d366983ea194e",
"m_Name": "Reflections (Planar)",
"m_ChildObjectList": [
{
"m_Id": "d6af4ca8280b4d56b6f2bf1c8b7a0bbd"
},
{
"m_Id": "2d31998b356d41179aa5291dcedc7850"
},
{
"m_Id": "eb89085b4d7c46c7bf82f63a3b43a0c6"
},
{
"m_Id": "0d81099c729342968bf4ee002d378dcf"
},
{
"m_Id": "e632a014145542d8a0f6bad7aac6cb63"
}
]
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlockNode",
@@ -1884,6 +1971,29 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "30ad385314c946f5a3cbd7adefe9c270",
"m_Guid": {
"m_GuidSerialized": "23c5d086-28d3-4f64-a7c7-b7bec078341f"
},
"m_Name": "Foam Bioluminescence Enabled",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Enabled",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Enabled",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceEnabled",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
@@ -1892,12 +2002,6 @@
"m_ChildObjectList": [
{
"m_Id": "14f5135a4bf8f5868cb55001b6472657"
},
{
"m_Id": "bbd2579dfd7f38879fab18706b09b5fb"
},
{
"m_Id": "d33c23a144f9400888ebc0b17741ceea"
}
]
}
@@ -1923,6 +2027,34 @@
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "38e9cee298f8483e8ca80e75574c2175",
"m_Guid": {
"m_GuidSerialized": "eb0230f0-4d3b-4348-b7af-d0c613693fbe"
},
"m_Name": "Foam Bioluminescence Intensity",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Intensity",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Intensity",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceIntensity",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 1.0,
"m_FloatType": 0,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@@ -1961,6 +2093,15 @@
},
{
"m_Id": "e608dd8f43794f5d8b3995446b21467f"
},
{
"m_Id": "a12a11f74d174f1d8a63d18ee8743b25"
},
{
"m_Id": "9f311e65704646e48b52019d389d3cd0"
},
{
"m_Id": "d209a2766f5546688fe4bc92416cd548"
}
]
}
@@ -2238,6 +2379,45 @@
"m_PositionSource": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
"m_ObjectId": "475b6b25b79b44d19bf94d0af92bc9a1",
"m_Name": "Bioluminescence",
"m_ChildObjectList": [
{
"m_Id": "30ad385314c946f5a3cbd7adefe9c270"
},
{
"m_Id": "feb1793feb264a239de366d730977489"
},
{
"m_Id": "38e9cee298f8483e8ca80e75574c2175"
},
{
"m_Id": "8c45cfde06fb4d5ca12db7bcfa8f6316"
},
{
"m_Id": "4a21e5afa49f40d38aef98a49226fa6e"
},
{
"m_Id": "cc11871fc2f74ea2821e2c9f78d3d58e"
},
{
"m_Id": "c06c8782d1b742d189925deba54c21bc"
},
{
"m_Id": "dd752976323845b0aad610adb0672e38"
},
{
"m_Id": "a485464528a74269852b53d441f1f0a8"
},
{
"m_Id": "bc35f605d4fc49869c24479f827fbfa8"
}
]
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot",
@@ -2284,6 +2464,29 @@
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "4a21e5afa49f40d38aef98a49226fa6e",
"m_Guid": {
"m_GuidSerialized": "b17934e1-f74e-4c4b-8313-3f5274067918"
},
"m_Name": "Foam Bioluminescence Sea Level Only",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Sea Level Only",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Sea_Level_Only",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceSeaLevelOnly",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": true
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
@@ -3368,7 +3571,7 @@
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.03,
"m_Value": 0.0,
"m_FloatType": 1,
"m_RangeValues": {
"x": -1.0,
@@ -3399,6 +3602,24 @@
"m_Value": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
"m_ObjectId": "72dfddd5562c42e4bf73f41a47cd4863",
"m_Name": "Reflections (Underwater)",
"m_ChildObjectList": [
{
"m_Id": "21db17a76f094b19ba523148b6f7aa41"
},
{
"m_Id": "bbd2579dfd7f38879fab18706b09b5fb"
},
{
"m_Id": "d33c23a144f9400888ebc0b17741ceea"
}
]
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@@ -3444,6 +3665,9 @@
{
"m_Id": "91e9a577d315a3848dfb1f48a6743f9c"
},
{
"m_Id": "a6704108884e487799aa5f653566e56b"
},
{
"m_Id": "4b09daebd022cf89bc77ad473c95586c"
},
@@ -3996,6 +4220,34 @@
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "8c45cfde06fb4d5ca12db7bcfa8f6316",
"m_Guid": {
"m_GuidSerialized": "c201c2da-d89e-4a7b-bc1f-358cac6ea74e"
},
"m_Name": "Foam Bioluminescence Maximum Depth",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Maximum Depth",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Maximum_Depth",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceMaximumDepth",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": Infinity,
"m_FloatType": 0,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector3MaterialSlot",
@@ -4053,6 +4305,34 @@
"m_SerializedDescriptor": "SurfaceDescription.Metallic"
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "8ec616eaa4964d38b92b904775e34f7e",
"m_Guid": {
"m_GuidSerialized": "025be3df-ddb1-4553-894c-4cbcafe64fda"
},
"m_Name": "Transparency Minimum Alpha",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Transparency Minimum Alpha",
"m_DefaultReferenceName": "_Transparency_Minimum_Alpha",
"m_OverrideReferenceName": "_Crest_TransparencyMinimumAlpha",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.95,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlockNode",
@@ -4108,6 +4388,18 @@
"m_Labels": []
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.CategoryData",
"m_ObjectId": "9125a3c8387e4d38987cc5707394b95b",
"m_Name": "Simple Transparency",
"m_ChildObjectList": [
{
"m_Id": "8ec616eaa4964d38b92b904775e34f7e"
}
]
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.ColorRGBMaterialSlot",
@@ -4243,9 +4535,6 @@
{
"m_Id": "c700bce5ef10cf8e9e5b69ba043d383b"
},
{
"m_Id": "21db17a76f094b19ba523148b6f7aa41"
},
{
"m_Id": "7b2f0f23d232ab88bcacd3e88392015a"
},
@@ -4260,18 +4549,6 @@
},
{
"m_Id": "71f6994de656018baa684da1128e623c"
},
{
"m_Id": "d6af4ca8280b4d56b6f2bf1c8b7a0bbd"
},
{
"m_Id": "2d31998b356d41179aa5291dcedc7850"
},
{
"m_Id": "0d81099c729342968bf4ee002d378dcf"
},
{
"m_Id": "e632a014145542d8a0f6bad7aac6cb63"
}
]
}
@@ -4447,6 +4724,34 @@
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "9f311e65704646e48b52019d389d3cd0",
"m_Guid": {
"m_GuidSerialized": "04aa688a-8387-4eda-bda1-edead599c4f0"
},
"m_Name": "Normal Map Turbulence Strength",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Normal Map Turbulence Strength",
"m_DefaultReferenceName": "_Normal_Map_Turbulence_Strength",
"m_OverrideReferenceName": "_Crest_NormalMapTurbulenceStrength",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.6,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.Texture2DShaderProperty",
@@ -4477,6 +4782,29 @@
"m_DefaultType": 0
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "a12a11f74d174f1d8a63d18ee8743b25",
"m_Guid": {
"m_GuidSerialized": "709317fe-af13-425b-90cf-63507779f13c"
},
"m_Name": "Normal Map Turbulence Enabled",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Normal Map Turbulence Enabled",
"m_DefaultReferenceName": "_Normal_Map_Turbulence_Enabled",
"m_OverrideReferenceName": "_Crest_NormalMapTurbulenceEnabled",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": true
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
@@ -4602,6 +4930,34 @@
"m_FunctionBody": "Enter function body here..."
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "a485464528a74269852b53d441f1f0a8",
"m_Guid": {
"m_GuidSerialized": "dbd6fa2d-e2ba-4ea9-b67c-e21877462ec6"
},
"m_Name": "Foam Bioluminescence Sparkles Intensity",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Sparkles Intensity",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Sparkles_Intensity",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceSparklesIntensity",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 500.0,
"m_FloatType": 0,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 2,
"m_Type": "UnityEditor.Rendering.Universal.ShaderGraph.UniversalLitSubTarget",
@@ -4670,6 +5026,29 @@
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "a6704108884e487799aa5f653566e56b",
"m_Guid": {
"m_GuidSerialized": "c4fdecae-cdbb-4529-8ad0-3ef2226477a5"
},
"m_Name": "Caustics Force Distortion",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Caustics Force Distortion",
"m_DefaultReferenceName": "_Caustics_Force_Distortion",
"m_OverrideReferenceName": "_Crest_CausticsForceDistortion",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": false
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.ViewDirectionNode",
@@ -4809,6 +5188,9 @@
},
{
"m_Id": "aa75e7087fa330809fdc753edcd8995b"
},
{
"m_Id": "ada9e52f38cd4e66a4e97c1fd2a5bd65"
}
]
}
@@ -4879,6 +5261,31 @@
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.ShaderKeyword",
"m_ObjectId": "ada9e52f38cd4e66a4e97c1fd2a5bd65",
"m_Guid": {
"m_GuidSerialized": "787edb3a-324f-493b-b3da-febad061005c"
},
"m_Name": "Custom Mesh",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Custom Mesh",
"m_DefaultReferenceName": "_CUSTOM_MESH",
"m_OverrideReferenceName": "_CREST_CUSTOM_MESH",
"m_GeneratePropertyBlock": false,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_KeywordType": 0,
"m_KeywordDefinition": 1,
"m_KeywordScope": 0,
"m_KeywordStages": 63,
"m_Entries": [],
"m_Value": 0,
"m_IsEditable": true
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector2MaterialSlot",
@@ -5043,7 +5450,7 @@
"m_DefaultRefNameVersion": 0,
"m_RefNameGeneratedByDisplayName": "",
"m_DefaultReferenceName": "BOOLEAN_547DED44_ON",
"m_OverrideReferenceName": "CREST_FLOW_ON",
"m_OverrideReferenceName": "_CREST_FLOW_LOD",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
@@ -5140,6 +5547,34 @@
}
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "bc35f605d4fc49869c24479f827fbfa8",
"m_Guid": {
"m_GuidSerialized": "be221cdb-d9a6-46a9-8e5c-899bb77b9ba5"
},
"m_Name": "Foam Bioluminescence Sparkles Coverage",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Sparkles Coverage",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Sparkles_Coverage",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceSparklesCoverage",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.5,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
@@ -5191,6 +5626,34 @@
}
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "c06c8782d1b742d189925deba54c21bc",
"m_Guid": {
"m_GuidSerialized": "ed5634bc-281c-4def-8a6e-9956331f4a20"
},
"m_Name": "Foam Bioluminescence Glow Coverage",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Glow Coverage",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Glow_Coverage",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceGlowCoverage",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.4,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector4MaterialSlot",
@@ -5344,6 +5807,34 @@
}
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "cc11871fc2f74ea2821e2c9f78d3d58e",
"m_Guid": {
"m_GuidSerialized": "db78df35-c8ae-45f7-b19b-6873133e270d"
},
"m_Name": "Foam Bioluminescence Glow Intensity",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Glow Intensity",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Glow_Intensity",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceGlowIntensity",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.7,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.ObjectNode",
@@ -5506,6 +5997,34 @@
"m_Labels": []
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",
"m_ObjectId": "d209a2766f5546688fe4bc92416cd548",
"m_Guid": {
"m_GuidSerialized": "187a910c-bd28-405b-b1e4-f495ca40e9f9"
},
"m_Name": "Normal Map Turbulence Coverage",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Normal Map Turbulence Coverage",
"m_DefaultReferenceName": "_Normal_Map_Turbulence_Coverage",
"m_OverrideReferenceName": "_Crest_NormalMapTurbulenceCoverage",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": 0.8999999761581421,
"m_FloatType": 1,
"m_RangeValues": {
"x": 0.0,
"y": 1.0
}
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.BlockNode",
@@ -5838,6 +6357,29 @@
"m_DefaultValue": true
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "dd752976323845b0aad610adb0672e38",
"m_Guid": {
"m_GuidSerialized": "4f72390b-2958-4755-9633-fdce875f1c0e"
},
"m_Name": "Foam Bioluminescence Sparkles Enabled",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Sparkles Enabled",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Sparkles_Enabled",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceSparklesEnabled",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": true
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Vector1MaterialSlot",
@@ -6033,6 +6575,29 @@
"m_Space": 2
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.Internal.BooleanShaderProperty",
"m_ObjectId": "eb89085b4d7c46c7bf82f63a3b43a0c6",
"m_Guid": {
"m_GuidSerialized": "bdc2d1fe-238b-4105-83e0-87f8ef392283"
},
"m_Name": "Planar Reflections Apply Smoothness",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Planar Reflections Apply Smoothness",
"m_DefaultReferenceName": "_Planar_Reflections_Apply_Smoothness",
"m_OverrideReferenceName": "_Crest_PlanarReflectionsApplySmoothness",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": false
}
{
"m_SGVersion": 0,
"m_Type": "UnityEditor.ShaderGraph.ColorRGBMaterialSlot",
@@ -6345,6 +6910,36 @@
"m_Labels": []
}
{
"m_SGVersion": 3,
"m_Type": "UnityEditor.ShaderGraph.Internal.ColorShaderProperty",
"m_ObjectId": "feb1793feb264a239de366d730977489",
"m_Guid": {
"m_GuidSerialized": "87224bff-8cf6-4adb-83d2-8ec6ed30c979"
},
"m_Name": "Foam Bioluminescence Color",
"m_DefaultRefNameVersion": 1,
"m_RefNameGeneratedByDisplayName": "Foam Bioluminescence Color",
"m_DefaultReferenceName": "_Foam_Bioluminescence_Color",
"m_OverrideReferenceName": "_Crest_FoamBioluminescenceColor",
"m_GeneratePropertyBlock": true,
"m_UseCustomSlotLabel": false,
"m_CustomSlotLabel": "",
"m_DismissedVersion": 0,
"m_Precision": 0,
"overrideHLSLDeclaration": false,
"hlslDeclarationOverride": 0,
"m_Hidden": false,
"m_Value": {
"r": 0.0,
"g": 1.55,
"b": 6.0,
"a": 1.0
},
"isMainColor": false,
"m_ColorMode": 1
}
{
"m_SGVersion": 1,
"m_Type": "UnityEditor.ShaderGraph.Internal.Vector1ShaderProperty",

View File

@@ -0,0 +1,36 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestAdd
#pragma multi_compile_local d_Float1 d_Float2 d_Float3 d_Float4
#if d_Float1
#define m_Type float
#elif d_Float2
#define m_Type float2
#elif d_Float3
#define m_Type float3
#elif d_Float4
#define m_Type float4
#endif
#include "HLSLSupport.cginc"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Macros.hlsl"
Texture2DArray<m_Type> _Crest_Source;
RWTexture2DArray<m_Type> _Crest_Target;
m_UtilityNameSpace
void Add(uint3 id)
{
_Crest_Target[id] += _Crest_Source[id];
}
m_UtilityNameSpaceEnd
m_UtilityKernelDefault(Add)

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: d18c1734c0d4d4e7b9a09a5da1ad1b2c
ComputeShaderImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel BlurHorizontal
#pragma kernel BlurVertical
#pragma multi_compile_local d_Float1 d_Float2 d_Float3 d_Float4
#if d_Float1
#define m_Type float
#elif d_Float2
#define m_Type float2
#elif d_Float3
#define m_Type float3
#elif d_Float4
#define m_Type float4
#endif
Texture2DArray<m_Type> _Crest_Source;
RWTexture2DArray<m_Type> _Crest_Target;
// (width, height, slices)
uint _Crest_Resolution;
// 5-tap Gaussian (sigma ≈ 1)
static const float w0 = 0.4026;
static const float w1 = 0.2442;
static const float w2 = 0.0545;
uint2 ClampXY(uint2 p)
{
return clamp(p, 0, _Crest_Resolution - 1);
}
[numthreads(8, 8, 1)]
void BlurHorizontal(uint3 id : SV_DispatchThreadID)
{
const int3 p = int3(id.x, id.y, id.z);
const int2 xy = p.xy;
_Crest_Target[p] =
_Crest_Source[id] * w0 +
_Crest_Source[uint3(ClampXY(xy + int2( 1, 0)), p.z)] * w1 +
_Crest_Source[uint3(ClampXY(xy + int2(-1, 0)), p.z)] * w1 +
_Crest_Source[uint3(ClampXY(xy + int2( 2, 0)), p.z)] * w2 +
_Crest_Source[uint3(ClampXY(xy + int2(-2, 0)), p.z)] * w2;
}
[numthreads(8, 8, 1)]
void BlurVertical(uint3 id : SV_DispatchThreadID)
{
const int3 p = int3(id.x, id.y, id.z);
const int2 xy = p.xy;
_Crest_Target[p] =
_Crest_Source[id] * w0 +
_Crest_Source[uint3(ClampXY(xy + int2(0, 1)), p.z)] * w1 +
_Crest_Source[uint3(ClampXY(xy + int2(0, -1)), p.z)] * w1 +
_Crest_Source[uint3(ClampXY(xy + int2(0, 2)), p.z)] * w2 +
_Crest_Source[uint3(ClampXY(xy + int2(0, -2)), p.z)] * w2;
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5a49c73b133c743caa1d82459943eabf
ComputeShaderImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -3,19 +3,33 @@
// Clear specific components using a mask.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestClearTarget
#pragma kernel CrestClearTargetBoundaryX d_BoundaryX
#pragma kernel CrestClearTargetBoundaryY d_BoundaryY
#pragma multi_compile_local d_Float1 d_Float2 d_Float3 d_Float4
#if d_Float1
#define m_Type float
#elif d_Float2
#define m_Type float2
#elif d_Float3
#define m_Type float3
#elif d_Float4
#define m_Type float4
#endif
#include "HLSLSupport.cginc"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Macros.hlsl"
RWTexture2DArray<float4> _Crest_Target;
RWTexture2DArray<m_Type> _Crest_Target;
CBUFFER_START(CrestPerMaterial)
float4 _Crest_ClearMask;
float4 _Crest_ClearColor;
m_Type _Crest_ClearMask;
m_Type _Crest_ClearColor;
uint _Crest_Resolution;
uint _Crest_TargetSlice;
CBUFFER_END

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 7c6a37fe33f609947b680abac9e850cc
guid: 5801035604cb84ff5a9557a2173630cd
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 557d66ce32c8ea44dba2cb2ff07dd49d
guid: 94b454ee025f04f81b984c298e5ff145
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -31,13 +31,8 @@
m_CrestNameSpace
static bool s_IsUnderWater;
static float s_FogDistance;
static half s_FogMultiplier;
static float2 s_PositionSS;
static float3 s_PositionWS;
static half3 s_ViewWS;
static float s_DepthRaw;
static half3 s_VolumeOpacity;
static half3 s_VolumeLighting;
float3 ApplyFog(float3 color)
{
@@ -46,22 +41,11 @@ float3 ApplyFog(float3 color)
{
return color;
}
else
#endif
return ApplyUnderwaterEffect
(
color,
s_DepthRaw,
0, // Caustics only
s_FogDistance,
s_ViewWS,
s_PositionSS,
s_PositionWS,
false, // No caustics
true, // TODO: implement
true, // TODO: implement
s_FogMultiplier
);
{
return lerp(color, s_VolumeLighting, s_VolumeOpacity * (s_IsUnderWater ? 1.0 : 0.0));
}
}
float3 NoFog(float3 color)
@@ -122,12 +106,23 @@ void SetUpFog(const float2 i_PositionNDC, const float3 i_PositionWS, const float
}
s_IsUnderWater = true;
s_PositionSS = positionSS;
s_PositionWS = i_PositionWS;
s_ViewWS = GetWorldSpaceNormalizeViewDir(i_PositionWS);
s_FogDistance = fogDistance;
s_DepthRaw = i_DepthRaw;
s_FogMultiplier = i_Multiplier;
ApplyUnderwaterEffect
(
0, // Color
0, // TIR only
0, // Caustics only
fogDistance,
GetWorldSpaceNormalizeViewDir(i_PositionWS),
positionSS,
i_PositionWS,
false, // No caustics
true, // TODO: implement
true, // TODO: implement
i_Multiplier,
s_VolumeOpacity,
s_VolumeLighting
);
}
m_CrestNameSpaceEnd
@@ -216,6 +211,11 @@ void CrestNodeIntegrateWaterVolume_float
o_Color = i_Color;
o_Emission = i_Emission;
if (i_Multiplier == 0)
{
return;
}
#ifndef SHADERGRAPH_PREVIEW
m_Crest::SetUpFog(i_PositionNDC, i_PositionWS, i_DepthRaw, i_Multiplier);
#endif

View File

@@ -1,6 +1,8 @@
// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
#pragma exclude_renderers glcore gles3
#pragma kernel CrestWaterLineBRP _BRP
#pragma kernel CrestWaterLineHRP _HRP
#pragma kernel CrestWaterLineURP _URP

View File

@@ -4,6 +4,8 @@
// Checks both orthogonal and diagonal pixels to fill artefacts in the mask. If checked pixels are all the same then it
// assumes that the current pixel should also be the same and fixes it.
#pragma exclude_renderers glcore gles3
#pragma kernel FillMaskArtefacts _BRP
// Built-in will not handle this for us unlike other RPs.

View File

@@ -5,6 +5,12 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Volume/UnderwaterShared.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Volume/Debug.hlsl"
#if (CREST_LEGACY_UNDERWATER == 1) || defined(CREST_URP)
#define d_Crest_SampleColor 1
#else
#define d_Crest_SampleColor 0
#endif
#ifndef SUPPORTS_FOVEATED_RENDERING_NON_UNIFORM_RASTER
#define FoveatedRemapLinearToNonUniform(uv) uv
#endif
@@ -19,7 +25,7 @@ TEXTURE2D_X(_Crest_CameraColorTexture);
m_CrestNameSpace
#if (CREST_LEGACY_UNDERWATER != 0)
#if (CREST_LEGACY_UNDERWATER != 0) || d_Crest_CustomColorTexture
float3 SampleSceneColor(float2 i_UV)
{
return LOAD_TEXTURE2D_X(_Crest_CameraColorTexture, i_UV * _ScreenSize.xy).rgb;
@@ -147,7 +153,8 @@ half4 Fragment(Varyings input)
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
positionWS += _WorldSpaceCameraPos;
#endif
sceneColour = ApplyUnderwaterEffect(sceneColour, rawDepth, sceneZ, fogDistance, view, positionSS, positionWS, hasCaustics, outScatterScene, applyLighting, 1.0);
half3 vo =0; half3 vl = 0;
sceneColour = ApplyUnderwaterEffect(sceneColour, rawDepth, sceneZ, fogDistance, view, positionSS, positionWS, hasCaustics, outScatterScene, applyLighting, 1.0, vo, vl);
}
return half4(sceneColour, 1.0);
@@ -167,18 +174,14 @@ half4 FragmentPlanarReflections(Varyings input)
return half4(_Crest_Scattering.xyz, 1.0);
}
#if d_Crest_SampleColor
half3 color = SampleSceneColor(positionNDC).rgb;
#else
half3 color = LoadSceneColor(positionSS).rgb;
#endif
// Calculate position and account for possible NaNs discovered during testing.
float3 positionWS;
{
float4 positionCS = ComputeClipSpacePosition(positionNDC, depth);
float4 hpositionWS = mul(UNITY_MATRIX_I_VP, positionCS);
// w is sometimes zero when using oblique projection.
// Zero is better than NaN.
positionWS = hpositionWS.w > 0.0 ? hpositionWS.xyz / hpositionWS.w : 0.0;
}
float3 positionWS = Utility::SafeComputeWorldSpacePosition(positionNDC, depth, UNITY_MATRIX_I_VP);
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
positionWS += _WorldSpaceCameraPos;
@@ -187,7 +190,8 @@ half4 FragmentPlanarReflections(Varyings input)
const half3 view = GetWorldSpaceNormalizeViewDir(positionWS);
const bool hasCaustics = depth > 0.0;
color = ApplyUnderwaterEffect(color, depth, 0.0, 0.0, view, positionSS, positionWS, hasCaustics, true, true, 1.0);
half3 vo = 0; half3 vl = 0;
color = ApplyUnderwaterEffect(color, depth, 0.0, 0.0, view, positionSS, positionWS, hasCaustics, true, true, 1.0, vo, vl);
return half4(color, 1.0);
}

View File

@@ -52,7 +52,7 @@ Shader "Crest/Underwater"
[PerRendererData] _Crest_CausticsDistortionStrength("Caustics Distortion Strength", Range(0, 0.25)) = 0.16
[PerRendererData] _Crest_CausticsDistortionScale("Caustics Distortion Scale", Range(0.01, 1000)) = 250
[PerRendererData] _Crest_CausticsMotionBlur("Caustics Motion Blur", Range(0, 10)) = 1
[PerRendererData] [Toggle] CREST_FLOW("Flow Enabled", Float) = 0
[PerRendererData] [Toggle(_CREST_FLOW_LOD)] _CREST_FLOW_LOD("Flow Enabled", Float) = 0
}
HLSLINCLUDE
@@ -61,7 +61,7 @@ Shader "Crest/Underwater"
// #pragma enable_d3d11_debug_symbols
// Also on the water shader.
#pragma multi_compile_local_fragment __ CREST_FLOW_ON
#pragma multi_compile_local_fragment __ _CREST_FLOW_LOD
#pragma shader_feature_local_fragment __ d_Dithering
@@ -231,8 +231,6 @@ Shader "Crest/Underwater"
HLSLPROGRAM
#include_with_pragmas "UnderwaterHDRP.hlsl"
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
@@ -286,8 +284,6 @@ Shader "Crest/Underwater"
HLSLPROGRAM
#include_with_pragmas "UnderwaterHDRP.hlsl"
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_PortalNegativeVolume 1
#define d_Crest_PortalWithBackFace 1
@@ -296,6 +292,35 @@ Shader "Crest/Underwater"
#pragma fragment Fragment
ENDHLSL
}
Pass
{
PackageRequirements
{
"com.waveharmonic.crest.portals"
}
Name "Fog After (Negative)"
Cull Front
ZTest LEqual
HLSLPROGRAM
#include_with_pragmas "UnderwaterHDRP.hlsl"
#pragma multi_compile_local _ d_Crest_ComputeMask
// Needs custom otherwise it will overwrite the fog before.
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Volume/Underwater.hlsl"
#pragma fragment Fragment
ENDHLSL
}
}
SubShader
@@ -467,8 +492,6 @@ Shader "Crest/Underwater"
#include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
@@ -526,8 +549,6 @@ Shader "Crest/Underwater"
#include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_PortalNegativeVolume 1
#define d_Crest_PortalWithBackFace 1
@@ -536,6 +557,37 @@ Shader "Crest/Underwater"
#pragma fragment Fragment
ENDHLSL
}
Pass
{
PackageRequirements
{
"com.waveharmonic.crest.portals"
}
Name "Fog After (Negative)"
Cull Front
ZTest LEqual
HLSLPROGRAM
#include_with_pragmas "UnderwaterURP.hlsl"
#include_with_pragmas "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRenderingKeywords.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/FoveatedRendering.hlsl"
#pragma multi_compile_local _ d_Crest_ComputeMask
// Needs custom otherwise it will overwrite the fog before.
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Volume/Underwater.hlsl"
#pragma fragment Fragment
ENDHLSL
}
}
SubShader
@@ -689,9 +741,6 @@ Shader "Crest/Underwater"
#pragma multi_compile_local _ d_Crest_ComputeMask
// For negative volumes.
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
@@ -745,8 +794,6 @@ Shader "Crest/Underwater"
HLSLPROGRAM
#include_with_pragmas "UnderwaterBIRP.hlsl"
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_PortalNegativeVolume 1
#define d_Crest_PortalWithBackFace 1
@@ -755,6 +802,35 @@ Shader "Crest/Underwater"
#pragma fragment Fragment
ENDHLSL
}
Pass
{
PackageRequirements
{
"com.waveharmonic.crest.portals"
}
Name "Fog After (Negative)"
Cull Front
ZTest LEqual
HLSLPROGRAM
#include_with_pragmas "UnderwaterBIRP.hlsl"
#pragma multi_compile_local _ d_Crest_ComputeMask
// Needs custom otherwise it will overwrite the fog before.
#define d_Crest_CustomColorTexture 1
#define d_Crest_Portal 1
#define d_Crest_Geometry 1
#define d_Crest_FogBefore 1
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Volume/Underwater.hlsl"
#pragma fragment Fragment
ENDHLSL
}
}
CustomEditor "WaveHarmonic.Crest.Editor.CustomShaderGUI"
}

View File

@@ -5,6 +5,7 @@
#define CREST_UNDERWATER_EFFECT_SHARED_INCLUDED
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Settings.Crest.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Surface/Keywords.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Constants.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/InputsDriven.hlsl"
@@ -13,6 +14,9 @@
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Depth.hlsl"
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Utility/Helpers.hlsl"
#undef d_Crest_CausticsForceDistortion
#define d_Crest_CausticsForceDistortion 1
#if d_Crest_Portal
#include "Packages/com.waveharmonic.crest.portals/Runtime/Shaders/Library/Portals.hlsl"
#endif
@@ -199,7 +203,9 @@ half3 ApplyUnderwaterEffect
const bool hasCaustics,
const bool i_OutScatterScene,
const bool i_ApplyLighting,
const half i_multiplier
const half i_multiplier,
out half3 volumeOpacity,
out half3 volumeLight
)
{
const bool isUnderwater = true;
@@ -208,8 +214,8 @@ half3 ApplyUnderwaterEffect
PrimaryLight(i_positionWS, lightColor, lightDirection);
// Uniform effect calculated from camera position.
half3 volumeLight = 0.0;
half3 volumeOpacity = 1.0;
volumeLight = 0.0;
volumeOpacity = 1.0;
{
half3 absorption = _Crest_Absorption.xyz;
half3 scattering = _Crest_Scattering.xyz;
@@ -224,19 +230,20 @@ half3 ApplyUnderwaterEffect
absorption *= _Crest_ExtinctionMultiplier;
scattering *= _Crest_ExtinctionMultiplier;
const float waterLevel = g_Crest_WaterCenter.y + Cascade::MakeAnimatedWaves(sliceIndex).Sample(_WorldSpaceCameraPos.xz).w;
const float4 displacement = Cascade::MakeAnimatedWaves(sliceIndex).Sample(_WorldSpaceCameraPos.xz);
const float waterLevel = g_Crest_WaterCenter.y + displacement.y + displacement.w;
half shadow = 1.0;
#if d_Crest_ShadowLod
{
// #if CREST_SHADOWS_ON
// Camera should be at center of LOD system so no need for blending (alpha, weights, etc). This might not be
// the case if there is large horizontal displacement, but the _Crest_DataSliceOffset should help by setting a
// large enough slice as minimum.
half2 shadowSoftHard = Cascade::MakeShadow(sliceIndex).SampleShadow(_WorldSpaceCameraPos.xz);
// Soft in red, hard in green. But hard not computed in HDRP.
shadow = 1.0 - shadowSoftHard.x;
// #endif
}
#endif
half3 ambientLighting = AmbientLight(_Crest_AmbientLighting);
@@ -289,7 +296,7 @@ half3 ApplyUnderwaterEffect
const uint slice0 = PositionToSliceIndex(i_positionWS.xz, 0, g_Crest_WaterScale);
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
half2 flowData = Cascade::MakeFlow(slice0).SampleFlow(i_positionWS.xz);
const Flow flow = Flow::Make(flowData, g_Crest_Time);
blur = _Crest_CausticsMotionBlur;
@@ -300,7 +307,7 @@ half3 ApplyUnderwaterEffect
sceneColour *= Caustics
(
#ifdef CREST_FLOW_ON
#if d_Crest_FlowLod
flow,
#endif
i_positionWS,

View File

@@ -3,6 +3,8 @@
// Inspired by https://github.com/speps/GX-EncinoWaves
#pragma exclude_renderers glcore gles3
// First SIZE constant must match FFT_KERNEL_0_RESOLUTION in FFTCompute.cs
#pragma kernel ComputeFFT SIZE=8 PASSES=3 CHANNEL=x TX=8 TY=1 FINAL=0
#pragma kernel ComputeFFT SIZE=8 PASSES=3 CHANNEL=y TX=1 TY=8 FINAL=1
@@ -22,11 +24,13 @@
// Must match CASCADE_COUNT in FFTCompute.cs
#define CASCADE_COUNT 16
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
Texture2D<float2> _Crest_InputButterfly;
#if !FINAL
RWTexture2DArray<float2> _Crest_Output1;
RWTexture2DArray<float2> _Crest_Output2;
RWTexture2DArray<float2> _Crest_Output3;
RWTexture2DArray<m_Float2> _Crest_Output1;
RWTexture2DArray<m_Float2> _Crest_Output2;
RWTexture2DArray<m_Float2> _Crest_Output3;
#else
Texture2DArray<float2> _Crest_InputH;
Texture2DArray<float2> _Crest_InputX;
@@ -140,9 +144,9 @@ void ComputeFFT(const uint3 id : SV_DispatchThreadID)
{
const uint coord = id.CHANNEL;
#if !FINAL
_Crest_IntermediatesH[coord] = conj(_Crest_Output1[id]);
_Crest_IntermediatesX[coord] = conj(_Crest_Output2[id]);
_Crest_IntermediatesZ[coord] = conj(_Crest_Output3[id]);
_Crest_IntermediatesH[coord] = conj(_Crest_Output1[id].xy);
_Crest_IntermediatesX[coord] = conj(_Crest_Output2[id].xy);
_Crest_IntermediatesZ[coord] = conj(_Crest_Output3[id].xy);
#else
_Crest_IntermediatesH[coord] = _Crest_InputH[id];
_Crest_IntermediatesX[coord] = _Crest_InputX[id];
@@ -164,9 +168,9 @@ void ComputeFFT(const uint3 id : SV_DispatchThreadID)
const float2 resultZ = pingpong ? _Crest_IntermediatesZ[coord] : _Crest_ScratchZ[coord];
#if !FINAL
_Crest_Output1[id] = resultH;
_Crest_Output2[id] = resultX;
_Crest_Output3[id] = resultZ;
_Crest_Output1[id] = m_Float2FromFloat2(resultH);
_Crest_Output2[id] = m_Float2FromFloat2(resultX);
_Crest_Output3[id] = m_Float2FromFloat2(resultZ);
#else
const float sign = ((id.x + id.y) % 2) == 1 ? -1.0 : 1.0;
const float3 res = float3(sign * resultX.x, sign * resultH.x, sign * resultZ.x);

View File

@@ -3,6 +3,8 @@
// Inspired by https://github.com/speps/GX-EncinoWaves
#pragma exclude_renderers glcore gles3
#pragma kernel SpectrumInitalize
#pragma kernel SpectrumUpdate
@@ -18,6 +20,8 @@
#define SPECTRUM_OCTAVE_COUNT 14.0
#define SPECTRUM_SMALLEST_WL_POW_2 -4.0
#include "Packages/com.waveharmonic.crest/Runtime/Shaders/Library/Macros.hlsl"
uint _Crest_Size;
float _Crest_WindSpeed;
float _Crest_Turbulence;
@@ -220,9 +224,9 @@ float _Crest_Time;
float _Crest_Chop;
Texture2DArray<float4> _Crest_Init0;
RWTexture2DArray<float2> _Crest_ResultHeight;
RWTexture2DArray<float2> _Crest_ResultDisplaceX;
RWTexture2DArray<float2> _Crest_ResultDisplaceZ;
RWTexture2DArray<m_Float2> _Crest_ResultHeight;
RWTexture2DArray<m_Float2> _Crest_ResultDisplaceX;
RWTexture2DArray<m_Float2> _Crest_ResultDisplaceZ;
float2 cmul(float2 lhs, float2 rhs)
{
@@ -257,7 +261,7 @@ void SpectrumUpdate(uint3 id : SV_DispatchThreadID)
const float4 h0 = _Crest_Init0[id];
const float2 h = cmul(h0.xy, fwd) + cmul(h0.zw, bkwd);
_Crest_ResultHeight[id] = h;
_Crest_ResultDisplaceX[id] = _Crest_Chop * float2(-h.y * k.x, h.x * k.x) / (kMag + 0.00001f);
_Crest_ResultDisplaceZ[id] = _Crest_Chop * float2(-h.y * k.y, h.x * k.y) / (kMag + 0.00001f);
_Crest_ResultHeight[id] = m_Float2FromFloat2(h);
_Crest_ResultDisplaceX[id] = m_Float2FromFloat2((_Crest_Chop * float2(-h.y * k.x, h.x * k.x) / (kMag + 0.00001f)));
_Crest_ResultDisplaceZ[id] = m_Float2FromFloat2((_Crest_Chop * float2(-h.y * k.y, h.x * k.y) / (kMag + 0.00001f)));
}

View File

@@ -3,6 +3,8 @@
// Computes a set of patches of waves, one for each scale.
#pragma exclude_renderers glcore gles3
#pragma kernel Gerstner
#include "HLSLSupport.cginc"