Skip to content

Fixed the bad blending between the sun and the clouds (case 1373282). #6193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 4, 2021
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions com.unity.render-pipelines.high-definition/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed geometry scale issue with the Eye Shader.
- Fixed motion vector buffer not accessible from custom passes in the BeforeTransparent, BeforePreRefraction and AfterDepthAndNormal injection points.
- Fixed the point distribution for the diffuse denoiser sometimes not being properly intialized.
- Fixed the bad blending between the sun and the clouds (case 1373282).

### Changed
- Use RayTracingAccelerationStructure.CullInstances to filter Renderers and populate the acceleration structure with ray tracing instances for improved CPU performance on the main thread.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,4 @@ When importing these two map Textures, disable **sRGB**. For best results, do no
* When enabled for [Reflection Probes](Reflection-Probe.md), the volumetric clouds are rendered at low resolution, without any form of temporal accumulation for performance and stability reasons.
* By default volumetric clouds are enabled on the baked [Reflection Probes](Reflection-Probe.md) if the asset allows it. They are rendered at full resolution without any form of temporal accumulation.
* Volumetric clouds do not appear in ray-traced effects.
* Transmittance is not applied linearly on the camera color to provide a better blending with the sun light (or high intensity pixels). If [Multi-sample anti-aliasing (MSAA)](#MSAA) is enabled on the camera, due to internal limitations, a different blending profile is used that may result in darker cloud edges.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ void UpdateShaderVariableslClouds(ref ShaderVariablesClouds cb, HDCamera hdCamer

// If this is a planar reflection, we need to compute the non oblique matrices
cb._IsPlanarReflection = (cameraData.cameraType == TVolumetricCloudsCameraType.PlanarReflection) ? 1 : 0;
if (cameraData.cameraType == TVolumetricCloudsCameraType.PlanarReflection)
if (cb._IsPlanarReflection == 1)
{
// Build a non-oblique projection matrix
var projectionMatrixNonOblique = Matrix4x4.Perspective(hdCamera.camera.fieldOfView, hdCamera.camera.aspect, hdCamera.camera.nearClipPlane, hdCamera.camera.farClipPlane);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ VolumetricCloudsParameters_Accumulation PrepareVolumetricCloudsParameters_Accumu
cameraData.enableIntegration = true;
UpdateShaderVariableslClouds(ref parameters.commonData.cloudsCB, hdCamera, settings, cameraData, cloudModelData, false);

// If this is a default camera, we want the improved blending, otherwise we don't (in the case of a planar)
parameters.commonData.cloudsCB._ImprovedTransmittanceBlend = parameters.commonData.cameraType == TVolumetricCloudsCameraType.Default ? 1 : 0;
parameters.commonData.cloudsCB._CubicTransmittance = parameters.commonData.cameraType == TVolumetricCloudsCameraType.Default && hdCamera.msaaEnabled ? 1 : 0;

return parameters;
}

Expand Down Expand Up @@ -198,8 +202,8 @@ static void TraceVolumetricClouds_Accumulation(CommandBuffer cmd, VolumetricClou
parameters.commonData.cloudsCB._HistoryViewportSize = new Vector2(previousViewportSize.x, previousViewportSize.y);
parameters.commonData.cloudsCB._HistoryBufferSize = new Vector2(previousHistory0Buffer.rt.width, previousHistory0Buffer.rt.height);

// Bind the constant buffer
ConstantBuffer.Push(cmd, parameters.commonData.cloudsCB, parameters.commonData.volumetricCloudsCS, HDShaderIDs._ShaderVariablesClouds);
// Bind the constant buffer (global as we need it for the .shader as well)
ConstantBuffer.PushGlobal(cmd, parameters.commonData.cloudsCB, HDShaderIDs._ShaderVariablesClouds);

RTHandle currentDepthBuffer = depthPyramid;

Expand Down Expand Up @@ -293,7 +297,7 @@ static void TraceVolumetricClouds_Accumulation(CommandBuffer cmd, VolumetricClou
parameters.cloudCombinePass.SetTexture(HDShaderIDs._VolumetricCloudsUpscaleTextureRW, intermediateUpscaleBuffer);

// Composite the clouds into the MSAA target via hardware blending.
HDUtils.DrawFullScreen(cmd, parameters.cloudCombinePass, colorBuffer);
HDUtils.DrawFullScreen(cmd, parameters.cloudCombinePass, colorBuffer, null, 0);

CoreUtils.SetKeyword(cmd, "USE_INTERMEDIATE_BUFFER", false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1401,13 +1401,13 @@ void UPSAMPLE_KERNEL(uint3 finalCoord : SV_DispatchThreadID,
cloudPosInput.positionWS = -viewDir * cloudDistance;
#endif

#ifdef USE_INTERMEDIATE_BUFFER
// Compute the fog attenuation of the clouds
float3 fogColor;
float3 fogOpacity;
EvaluateAtmosphericScattering(cloudPosInput, viewDir, fogColor, fogOpacity);
currentClouds.xyz = currentClouds.xyz * (1 - fogOpacity) + fogColor * (1.0 - currentClouds.a);
currentClouds.xyz = currentClouds.xyz * (1 - fogOpacity) + fogColor * (1.0 - (_CubicTransmittance ? currentClouds.a * currentClouds.a : currentClouds.a));

#ifdef USE_INTERMEDIATE_BUFFER
// Store the upscaled result only, composite in later pass.
_VolumetricCloudsUpscaleTextureRW[COORD_TEXTURE2D_X(finalCoord.xy)] = currentClouds;
#else
Expand All @@ -1417,8 +1417,18 @@ void UPSAMPLE_KERNEL(uint3 finalCoord : SV_DispatchThreadID,
#else
float4 currentColor = _CameraColorTexture[COORD_TEXTURE2D_X(finalCoord.xy)];
#endif
// If this is a background pixel, we want the cloud value, otherwise we do not.
_VolumetricCloudsUpscaleTextureRW[COORD_TEXTURE2D_X(finalCoord.xy)] = float4(currentColor.xyz * currentClouds.a + currentClouds.xyz, currentColor.a);

// Estimate the transmittance that shall be used
float finalTransmittance = EvaluateFinalTransmittance(currentColor, currentClouds.w);

// Compute the fog attenuation of the clouds
float3 fogColor;
float3 fogOpacity;
EvaluateAtmosphericScattering(cloudPosInput, viewDir, fogColor, fogOpacity);
currentClouds.xyz = currentClouds.xyz * (1 - fogOpacity) + fogColor * (1.0 - finalTransmittance);

// Apply the transmittance
_VolumetricCloudsUpscaleTextureRW[COORD_TEXTURE2D_X(finalCoord.xy)] = float4(currentColor.xyz * finalTransmittance + currentClouds.xyz, currentColor.a);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ Shader "Hidden/HDRP/VolumetricCloudsCombine"
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

// Composite the result via hardware blending.
return LOAD_TEXTURE2D_X(_VolumetricCloudsUpscaleTextureRW, input.positionCS.xy);
// If MSAA is enabled on the camera, due to internal limitations, a different blending profile is used that may result in darker cloud edges.
float4 cloudData = LOAD_TEXTURE2D_X(_VolumetricCloudsUpscaleTextureRW, input.positionCS.xy);
return float4(cloudData.xyz, _CubicTransmittance ? cloudData.w * cloudData.w : cloudData.w);
}
ENDHLSL
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ unsafe struct ShaderVariablesClouds

// Flag that allows us to know if the maxZMask texture is valid
public int _ValidMaxZMask;
// Padding
public int _Padding0;
public Vector2 _Padding1;
// Flag that allows to know if we should be using the improved transmittance blending
public int _ImprovedTransmittanceBlend;
// Flag that defines if the transmittance should follow a cubic profile (For MSAA)
public int _CubicTransmittance;
public int _Padding1;

[HLSLArray(3 * 4, typeof(Vector4))]
public fixed float _DistanceBasedWeights[12 * 4];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ CBUFFER_START(ShaderVariablesClouds)
int _EnableFastToneMapping;
int _IsPlanarReflection;
int _ValidMaxZMask;
int _Padding0;
float2 _Padding1;
int _ImprovedTransmittanceBlend;
int _CubicTransmittance;
int _Padding1;
float4 _DistanceBasedWeights[12];
CBUFFER_END

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@ void FillCloudUpscaleNeighborhoodData(int2 groupThreadId, int subRegionIdx, out
neighborhoodData.lowWeightC = _DistanceBasedWeights[subRegionIdx * 3 + 2].x;
}

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 ? (resultColor / color) : transmittance;
}
#endif // REAL_TIME_VOLUMETRIC_CLOUDS

#endif // VOLUMETRIC_CLOUD_UTILITIES_H