浏览代码

Merge pull request #1603 from EvgeniiG/master

Improve the quality of trilinear filtering of density volume textures
/main
GitHub 6 年前
当前提交
caff3ee2
共有 3 个文件被更改,包括 40 次插入48 次删除
  1. 6
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Common.hlsl
  2. 3
      com.unity.render-pipelines.high-definition/CHANGELOG.md
  3. 79
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumeVoxelization.compute

6
com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Common.hlsl


return ComputeTextureLOD(uv);
}
float ComputeTextureLOD(float3 Px, float3 Py, float3 Pz)
float ComputeTextureLOD(float3 duvw_dx, float3 duvw_dy, float3 duvw_dz, float scale)
float d = max(dot(Px, Px), max(dot(Py, Py), dot(Pz, Pz)));
return max(0.0, 0.5 * log2(d));
float d = Max3(dot(duvw_dx, duvw_dx), dot(duvw_dy, duvw_dy), dot(duvw_dz, duvw_dz));
return max(0.0, 0.5 * log2(d * (scale * scale)));
}

3
com.unity.render-pipelines.high-definition/CHANGELOG.md


- SSS and Transmission code have been refactored to be able to share it between various material. Guidelines are in SubsurfaceScattering.hlsl
- Change code in area light with LTC for Lit shader. Magnitude is now take from FGD texture instead of a separate texture
- Improve camera relative rendering: We now apply camera translation on the model matrix, so before the TransformObjectToWorld(). Note: unity_WorldToObject and unity_ObjectToWorld must never be used directly.
- Rename positionWS to positionRWS (Camera relative world position) at a lot of places (mainly in interpolator and FragInputs). In case of custom shader user will be required to update their code.
- Rename positionWS to positionRWS (Camera relative world position) at a lot of places (mainly in interpolator and FragInputs). In case of custom shader user will be required to update their code.
- Improve the quality of trilinear filtering of density volume textures.
### Fixed
- Fix contact shadows applied on transmission

79
com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumeVoxelization.compute


//--------------------------------------------------------------------------------------------------
// Implementation
//--------------------------------------------------------------------------------------------------
float SampleVolumeMask(DensityVolumeData volumeData, float3 voxelCenterUV, float3 VxUV, float3 VyUV, float3 VzUV)
float SampleVolumeMask(DensityVolumeData volumeData, float3 voxelCenterUVW, float3 duvw_dx, float3 duvw_dy, float3 duvw_dz)
float clampBorder = 0.5f * _VolumeMaskDimensions.y;
//scale and bias the UVs and then take fractional part, will be in [0,1] range
voxelCenterUV = frac(voxelCenterUV * volumeData.textureTiling + volumeData.textureScroll);
//scale and bias the UVWs and then take fractional part, will be in [0,1] range
voxelCenterUVW = frac(voxelCenterUVW * volumeData.textureTiling + volumeData.textureScroll);
voxelCenterUV.z = voxelCenterUV.z * _VolumeMaskDimensions.x;
voxelCenterUV.z += offset;
voxelCenterUVW.z = voxelCenterUVW.z * _VolumeMaskDimensions.x;
voxelCenterUVW.z += offset;
// TODO: expose the LoD bias parameter.
float lod = ComputeTextureLOD(duvw_dx, duvw_dy, duvw_dz, _VolumeMaskDimensions.z);
// First of all, the distance to the edge should depend on the LoD. In the current implementation, it does not.
// First of all, the distance to the edge should depend on the LoD.
float lod = ComputeTextureLOD(VxUV * _VolumeMaskDimensions.z, VyUV * _VolumeMaskDimensions.z, VzUV * _VolumeMaskDimensions.z);
voxelCenterUV.z = clamp(voxelCenterUV.z, offset + clampBorder, offset + _VolumeMaskDimensions.x - clampBorder);
// For now, we choose the second option.
int textureSize = (int)_VolumeMaskDimensions.z;
int mipSize = textureSize >> (int)ceil(lod);
float clampBorder = 0.5f * rcp(mipSize);
voxelCenterUVW.z = clamp(voxelCenterUVW.z, offset + clampBorder, offset + _VolumeMaskDimensions.x - clampBorder);
// TODO: expose the LoD bias parameter.
// TODO: can we use anisotropic filtering to improve the quality? SampleGrad() doesn't support ddz().
float maskValue = SAMPLE_TEXTURE3D_LOD(_VolumeMaskAtlas, s_trilinear_clamp_sampler, voxelCenterUV, lod).a;
return maskValue;
return SAMPLE_TEXTURE3D_LOD(_VolumeMaskAtlas, s_trilinear_clamp_sampler, voxelCenterUVW, lod).a;
}

float z = z0 + halfDZ;
float3 voxelCenterWS = rayOriginWS + z * rayUnDirWS; // Works due to the length of of the dir
// Dimensions of the voxel as we step along the ray.
float3 voxelRightSize = z * voxelAxisRight;
float3 voxelUpSize = z * voxelAxisUp;
float3 voxelDepthSize = halfDZ * voxelAxisForward;
// TODO: define a function ComputeGlobalFogCoefficients(float3 voxelCenterWS),
// which allows procedural definition of extinction and scattering.
float3 voxelScattering = _GlobalScattering;

{
#endif // USE_CLUSTERED_LIGHTLIST
OrientedBBox obb = _VolumeBounds[volumeIndex];
const OrientedBBox obb = _VolumeBounds[volumeIndex];
float3x3 obbFrame = float3x3(obb.right, obb.up, cross(obb.up, obb.right));
float3 obbExtents = float3(obb.extentX, obb.extentY, obb.extentZ);
const float3x3 obbFrame = float3x3(obb.right, obb.up, cross(obb.up, obb.right));
const float3 obbExtents = float3(obb.extentX, obb.extentY, obb.extentZ);
float3 voxelCenterBS = mul(voxelCenterWS - obb.center, transpose(obbFrame));
float3 voxelCenterUV = (voxelCenterBS / obbExtents);
const float3 voxelCenterBS = mul(voxelCenterWS - obb.center, transpose(obbFrame));
const float3 voxelCenterCS = (voxelCenterBS / obbExtents);
const float3 voxelAxisRightBS = mul(voxelAxisRight, transpose(obbFrame));
const float3 voxelAxisUpBS = mul(voxelAxisUp, transpose(obbFrame));
const float3 voxelAxisForwardBS = mul(voxelAxisForward, transpose(obbFrame));
#if SOFT_VOXELIZATION
// We need to determine which is the face closest to 'voxelCenterBS'.

// We now have to construct the diagonal of the voxel with the longest extent along this normal.
float3 minDiagPointBS, maxDiagPointBS;
float3 voxelAxisRightBS = mul(voxelAxisRight, transpose(obbFrame));
float3 voxelAxisUpBS = mul(voxelAxisUp, transpose(obbFrame));
float3 voxelAxisForwardBS = mul(voxelAxisForward, transpose(obbFrame));
// Start at the center of the voxel.
minDiagPointBS = maxDiagPointBS = voxelCenterBS;

#else // SOFT_VOXELIZATION
bool overlap = abs(voxelCenterUV.x) <= 1 &&
abs(voxelCenterUV.y) <= 1 &&
abs(voxelCenterUV.z) <= 1;
bool overlap = Max3(abs(voxelCenterCS.x), abs(voxelCenterCS.y), abs(voxelCenterCS.z)) <= 1;
float overlapFraction = overlap ? 1 : 0;

{
float scatteringAndExtinctionMask = 1.0f;
float densityMask = 1.0f;
float3 voxelRightSizeBS = mul(voxelRightSize, transpose(obbFrame));
float3 voxelRightSizeUV = (voxelRightSizeBS / obbExtents);
float3 voxelUpSizeBS = mul(voxelUpSize, transpose(obbFrame));
float3 voxelUpSizeUV = (voxelUpSizeBS / obbExtents);
float3 voxelDepthSizeBS = mul(voxelDepthSize, transpose(obbFrame));
float3 voxelDepthSizeUV = (voxelDepthSizeBS / obbExtents);
// We divide extents (half-sizes) by extents here, obtaining full-sized gradients.
float3 voxelGradRightUVW = z * voxelAxisRightBS / obbExtents;
float3 voxelGradUpUVW = z * voxelAxisUpBS / obbExtents;
float3 voxelGradForwardUVW = halfDZ * voxelAxisForwardBS / obbExtents;
float3 voxelCenterUVW = voxelCenterCS * 0.5 + 0.5;
scatteringAndExtinctionMask = SampleVolumeMask(_VolumeData[volumeIndex], voxelCenterUV * 0.5 + 0.5, voxelRightSizeUV, voxelUpSizeUV, voxelDepthSizeUV);
densityMask = SampleVolumeMask(_VolumeData[volumeIndex], voxelCenterUVW, voxelGradRightUVW, voxelGradUpUVW, voxelGradForwardUVW);
voxelScattering += overlapFraction * _VolumeData[volumeIndex].scattering * scatteringAndExtinctionMask;
voxelExtinction += overlapFraction * _VolumeData[volumeIndex].extinction * scatteringAndExtinctionMask;
voxelScattering += overlapFraction * _VolumeData[volumeIndex].scattering * densityMask;
voxelExtinction += overlapFraction * _VolumeData[volumeIndex].extinction * densityMask;
}
#ifndef USE_CLUSTERED_LIGHTLIST

float3 upDirWS = mul(-float3(upCoord, 1), (float3x3)_VBufferCoordToViewDirWS);
// Compute the axes of the voxel. These are not normalized, but rather computed to scale with Z.
// This coordinate system is generally not orthogonal.
float3 voxelAxisForward = centerDirWS;
float3 voxelAxisUp = 0.5 * (upDirWS - centerDirWS);
float3 voxelAxisRight = 0.5 * (centerDirWS - leftDirWS);

正在加载...
取消
保存