From 6d7304f6b4d322bbe5dacf951e740320fdc87284 Mon Sep 17 00:00:00 2001 From: anisunity Date: Tue, 16 Nov 2021 18:11:50 +0100 Subject: [PATCH] Fixed Nans happening due to volumetric clouds when the pixel color is perfectly black (case 1379185). --- .../CHANGELOG.md | 1 + .../VolumetricCloudsUtilities.hlsl | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index ba8e4a49688..18516958d31 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed Focus distance in path traced depth of field now takes into account the focus mode setting (volume vs camera). - Fixed stencil buffer resolve when MSAA is enabled so that OR operator is used instead of picking the last sample. - Fixed Lens Flare visible when being behind a camera with Panini Projection on (case 1370214); +- Fixed Nans happening due to volumetric clouds when the pixel color is perfectly black (case 1379185). ### Changed - Use RayTracingAccelerationStructure.CullInstances to filter Renderers and populate the acceleration structure with ray tracing instances for improved CPU performance on the main thread. diff --git a/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricCloudsUtilities.hlsl b/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricCloudsUtilities.hlsl index 9246c6eb166..1e5a73f4409 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricCloudsUtilities.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/Lighting/VolumetricLighting/VolumetricCloudsUtilities.hlsl @@ -502,9 +502,17 @@ void FillCloudUpscaleNeighborhoodData(int2 groupThreadId, int subRegionIdx, out float EvaluateFinalTransmittance(float3 color, float transmittance) { // Due to the high intensity of the sun, we often need apply the transmittance in a tonemapped space - float3 resultColor = color / (1.0 + color) * transmittance; - resultColor = resultColor / (1.0 - resultColor); - return _ImprovedTransmittanceBlend ? Luminance(resultColor / color) : transmittance; + // As we only produce one transmittance, we evaluate the approximation on the luminance of the color + float luminance = Luminance(color); + + // Apply the tone mapping and then the transmittance + float resultLuminance = luminance / (1.0 + luminance) * transmittance; + + // reverse the tone mapping + resultLuminance = resultLuminance / (1.0 - resultLuminance); + + // This approach only makes sense if the color is not black + return (luminance > 0.0 && _ImprovedTransmittanceBlend == 1) ? resultLuminance / luminance : transmittance; } #endif // REAL_TIME_VOLUMETRIC_CLOUDS