Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace ExtendedMaterials
return float4(AdjustDisplacementNormalized(displacement.x, params), AdjustDisplacementNormalized(displacement.y, params), AdjustDisplacementNormalized(displacement.z, params), AdjustDisplacementNormalized(displacement.w, params));
}

float GetMipLevel(float2 coords, Texture2D<float4> tex)
float GetMipLevel(float2 coords, Texture2D<float4> tex, float screenNoise)
{
// Compute the current gradients:
float2 textureDims;
Expand Down Expand Up @@ -72,6 +72,9 @@ namespace ExtendedMaterials
mipLevel++;
#endif

// Stochastic mip selection: use screen noise to select between adjacent mip levels
mipLevel = floor(mipLevel) + (screenNoise < frac(mipLevel) ? 1.0 : 0.0);

return mipLevel;
}

Expand Down Expand Up @@ -349,21 +352,19 @@ namespace ExtendedMaterials

#if defined(LANDSCAPE)
if (nearBlendToFar < 1.0) {
uint numSteps = uint((max(6, scale * 8) * (1.0 - nearBlendToFar)) + 0.5);
numSteps = clamp((numSteps + 3) & ~0x03, 4, max(8, scale * 8));
#else
# if defined(TRUE_PBR)
if ((PBRFlags & PBR::Flags::InterlayerParallax) != 0 || nearBlendToFar < 1.0)
# else
if (nearBlendToFar < 1.0)
# endif
{
#endif
float maxSteps = SharedData::InInterior ? 8 : 16;
uint numSteps = uint((maxSteps * (1.0 - nearBlendToFar)) + 0.5);
numSteps = clamp((numSteps + 3) & ~0x03, 4, max(6, scale * maxSteps));
#endif
numSteps = clamp(numSteps, 1, max(6, scale * maxSteps));

float stepSize = rcp(numSteps);
stepSize += (noise * 2.0 - 1.0) * stepSize * stepSize;

float2 offsetPerStep = viewDirTS.xy * float2(maxHeight, maxHeight) * stepSize.xx;
float2 prevOffset = viewDirTS.xy * float2(minHeight, minHeight) + coords.xy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[Info]
Version = 1-1-0
Version = 1-1-1
13 changes: 9 additions & 4 deletions features/Water Effects/Shaders/WaterEffects/WaterParallax.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace WaterEffects
// http://www.diva-portal.org/smash/get/diva2:831762/FULLTEXT01.pdf
// https://bartwronski.files.wordpress.com/2014/03/ac4_gdc.pdf

float GetMipLevel(float2 coords, Texture2D<float4> tex)
float GetMipLevel(float2 coords, Texture2D<float4> tex, float screenNoise)
{
// Compute the current gradients:
float2 textureDims;
Expand All @@ -33,6 +33,9 @@ namespace WaterEffects
// Compute the current mip level (* 0.5 is effectively computing a square root before )
float mipLevel = max(0.5 * log2(minTexCoordDelta), 0);

// Stochastic mip selection: use screen noise to select between adjacent mip levels
mipLevel = floor(mipLevel) + (screenNoise < frac(mipLevel) ? 1.0 : 0.0);

return mipLevel;
}

Expand All @@ -54,10 +57,12 @@ namespace WaterEffects
// Parallax scale is also multiplied by normalScalesRcp
parallaxOffsetTS *= 20.0;

float screenNoise = Random::InterleavedGradientNoise(input.HPosition.xy, SharedData::FrameCount);

float3 mipLevels;
mipLevels.x = GetMipLevel(input.TexCoord1.xy, Normals01Tex);
mipLevels.y = GetMipLevel(input.TexCoord1.zw, Normals02Tex);
mipLevels.z = GetMipLevel(input.TexCoord2.xy, Normals03Tex);
mipLevels.x = GetMipLevel(input.TexCoord1.xy, Normals01Tex, screenNoise);
mipLevels.y = GetMipLevel(input.TexCoord1.zw, Normals02Tex, screenNoise);
mipLevels.z = GetMipLevel(input.TexCoord2.xy, Normals03Tex, screenNoise);

#if defined(VR)
mipLevels = mipLevels + 4;
Expand Down
30 changes: 15 additions & 15 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
# if defined(EMAT)
# if defined(PARALLAX)
if (SharedData::extendedMaterialSettings.EnableParallax) {
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexParallaxSampler);
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexParallaxSampler, screenNoise);
uv = ExtendedMaterials::GetParallaxCoords(viewPosition.z, uv, mipLevel, viewDirection, tbnTr, screenNoise, TexParallaxSampler, SampParallaxSampler, 0, displacementParams, pixelOffset);
if (SharedData::extendedMaterialSettings.EnableShadows && (parallaxShadowQuality > 0.0f || SharedData::extendedMaterialSettings.ExtendShadows))
sh0 = TexParallaxSampler.SampleLevel(SampParallaxSampler, uv, mipLevel).x;
Expand All @@ -1092,7 +1092,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
if (complexMaterial) {
if (envMaskTest > (4.0 / 255.0)) {
complexMaterialParallax = true;
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexEnvMaskSampler);
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexEnvMaskSampler, screenNoise);
uv = ExtendedMaterials::GetParallaxCoords(viewPosition.z, uv, mipLevel, viewDirection, tbnTr, screenNoise, TexEnvMaskSampler, SampTerrainParallaxSampler, 3, displacementParams, pixelOffset);
if (SharedData::extendedMaterialSettings.EnableShadows && (parallaxShadowQuality > 0.0f || SharedData::extendedMaterialSettings.ExtendShadows))
sh0 = TexEnvMaskSampler.SampleLevel(SampEnvMaskSampler, uv, mipLevel).w;
Expand Down Expand Up @@ -1137,7 +1137,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
{
displacementParams.HeightScale *= PBRParams1.y;
}
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexParallaxSampler);
mipLevel = ExtendedMaterials::GetMipLevel(uv, TexParallaxSampler, screenNoise);
uv = ExtendedMaterials::GetParallaxCoords(viewPosition.z, uv, mipLevel, refractedViewDirection, tbnTr, screenNoise, TexParallaxSampler, SampParallaxSampler, 0, displacementParams, pixelOffset);
if (SharedData::extendedMaterialSettings.EnableShadows && (parallaxShadowQuality > 0.0f || SharedData::extendedMaterialSettings.ExtendShadows))
sh0 = TexParallaxSampler.SampleLevel(SampParallaxSampler, uv, mipLevel).x;
Expand Down Expand Up @@ -1205,12 +1205,12 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
# else
if (SharedData::extendedMaterialSettings.EnableTerrainParallax || (SharedData::extendedMaterialSettings.EnableParallax && Permutation::ExtraFeatureDescriptor & Permutation::ExtraFeatureFlags::THLandHasDisplacement)) {
# endif
mipLevels[0] = ExtendedMaterials::GetMipLevel(uv, TexColorSampler);
mipLevels[1] = ExtendedMaterials::GetMipLevel(uv, TexLandColor2Sampler);
mipLevels[2] = ExtendedMaterials::GetMipLevel(uv, TexLandColor3Sampler);
mipLevels[3] = ExtendedMaterials::GetMipLevel(uv, TexLandColor4Sampler);
mipLevels[4] = ExtendedMaterials::GetMipLevel(uv, TexLandColor5Sampler);
mipLevels[5] = ExtendedMaterials::GetMipLevel(uv, TexLandColor6Sampler);
mipLevels[0] = ExtendedMaterials::GetMipLevel(uv, TexColorSampler, screenNoise);
mipLevels[1] = ExtendedMaterials::GetMipLevel(uv, TexLandColor2Sampler, screenNoise);
mipLevels[2] = ExtendedMaterials::GetMipLevel(uv, TexLandColor3Sampler, screenNoise);
mipLevels[3] = ExtendedMaterials::GetMipLevel(uv, TexLandColor4Sampler, screenNoise);
mipLevels[4] = ExtendedMaterials::GetMipLevel(uv, TexLandColor5Sampler, screenNoise);
mipLevels[5] = ExtendedMaterials::GetMipLevel(uv, TexLandColor6Sampler, screenNoise);

displacementParams[1] = displacementParams[0];
displacementParams[2] = displacementParams[0];
Expand Down Expand Up @@ -1255,12 +1255,12 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
# if defined(TERRAIN_VARIATION)
else if (useTerrainVariation) {
// Calculate proper mip levels for terrain variation when parallax is disabled but EMAT is available
mipLevels[0] = ExtendedMaterials::GetMipLevel(uv, TexColorSampler);
mipLevels[1] = ExtendedMaterials::GetMipLevel(uv, TexLandColor2Sampler);
mipLevels[2] = ExtendedMaterials::GetMipLevel(uv, TexLandColor3Sampler);
mipLevels[3] = ExtendedMaterials::GetMipLevel(uv, TexLandColor4Sampler);
mipLevels[4] = ExtendedMaterials::GetMipLevel(uv, TexLandColor5Sampler);
mipLevels[5] = ExtendedMaterials::GetMipLevel(uv, TexLandColor6Sampler);
mipLevels[0] = ExtendedMaterials::GetMipLevel(uv, TexColorSampler, screenNoise);
mipLevels[1] = ExtendedMaterials::GetMipLevel(uv, TexLandColor2Sampler, screenNoise);
mipLevels[2] = ExtendedMaterials::GetMipLevel(uv, TexLandColor3Sampler, screenNoise);
mipLevels[3] = ExtendedMaterials::GetMipLevel(uv, TexLandColor4Sampler, screenNoise);
mipLevels[4] = ExtendedMaterials::GetMipLevel(uv, TexLandColor5Sampler, screenNoise);
mipLevels[5] = ExtendedMaterials::GetMipLevel(uv, TexLandColor6Sampler, screenNoise);
}
# endif
# else
Expand Down