浏览代码

Merge pull request #1439 from Unity-Technologies/volumetric-mipmapping

Mipmapping support for local volume mask textures.
/main
GitHub 7 年前
当前提交
2eef215d
共有 8 个文件被更改,包括 44 次插入17 次删除
  1. 7
      com.unity.render-pipelines.core/CoreRP/ShaderLibrary/Common.hlsl
  2. 1
      com.unity.render-pipelines.high-definition/CHANGELOG.md
  3. 2
      com.unity.render-pipelines.high-definition/HDRP/HDRenderPipeline.cs
  4. 9
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/DensityVolume.cs
  5. 5
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/DensityVolumeManager.cs
  6. 4
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumeTextureAtlas.cs
  7. 26
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumeVoxelization.compute
  8. 7
      com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumetricLighting.cs

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


return ComputeTextureLOD(uv);
}
float ComputeTextureLOD(float3 Px, float3 Py, float3 Pz)
{
float d = max(dot(Px, Px), max(dot(Py, Py), dot(Pz, Pz)));
return max(0.0, 0.5 * log2(d));
}
uint GetMipCount(Texture2D tex)
{
#if defined(SHADER_API_D3D11) || defined(SHADER_API_D3D12) || defined(SHADER_API_D3D11_9X) || defined(SHADER_API_XBOXONE) || defined(SHADER_API_PSSL)

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


- The VolumetricLightingSystem now uses RTHandles, which allows to save memory by sharing buffers between different cameras (history buffers are not shared), and reduce reallocation frequency by reallocating buffers only if the rendering resolution increases (and suballocating within existing buffers if the rendering resolution decreases)
- Add a Volumetric Dimmer slider to lights to control the intensity of the scattered volumetric lighting
- Add UV tiling and offset support for decals.
- Add mipmapping support for volume 3D mask textures
### Changed, Removals and deprecations
- Remove Resource folder of PreIntegratedFGD and add the resource to RenderPipeline Asset

2
com.unity.render-pipelines.high-definition/HDRP/HDRenderPipeline.cs


m_MaterialList.ForEach(material => material.Bind());
// Frustum cull density volumes on the CPU. Can be performed as soon as the camera is set up.
DensityVolumeList densityVolumes = m_VolumetricLightingSystem.PrepareVisibleDensityVolumeList(hdCamera, cmd);
DensityVolumeList densityVolumes = m_VolumetricLightingSystem.PrepareVisibleDensityVolumeList(hdCamera, cmd, m_Time);
// Note: Legacy Unity behave like this for ShadowMask
// When you select ShadowMask in Lighting panel it recompile shaders on the fly with the SHADOW_MASK keyword.

9
com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/DensityVolume.cs


volumeScrollingAmount = textureScrollingSpeed;
}
public void Update()
public void Update(bool animate, float time)
volumeScrollingAmount = volumeScrollingAmount + (textureScrollingSpeed * Time.deltaTime);
float animationTime = animate ? time : 0.0f;
volumeScrollingAmount = (textureScrollingSpeed * animationTime);
}
}

public Action OnTextureUpdated;
//Gather and Update any parameters that may have changed
public void PrepareParameters()
public void PrepareParameters(bool animate, float time)
{
//Texture has been updated notify the manager
if (previousVolumeMask != parameters.volumeMask)

}
parameters.Update();
parameters.Update(animate, time);
}
private void NotifyUpdatedTexure()

5
com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/DensityVolumeManager.cs


TriggerVolumeAtlasRefresh();
}
public DensityVolume[] PrepareDensityVolumeData(CommandBuffer cmd)
public DensityVolume[] PrepareDensityVolumeData(CommandBuffer cmd, Camera currentCam, float time)
bool animate = CoreUtils.AreAnimatedMaterialsEnabled(currentCam);
volume.PrepareParameters();
volume.PrepareParameters(animate, time);
}
if (atlasNeedsRefresh)

4
com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumeTextureAtlas.cs


if (volumeTexture.format != requiredTextureFormat)
{
Debug.LogError(string.Format("VolumeTextureAtlas: Texture format of texture {0} : {1} does not match expected format of {2}", volumeTexture, volumeTexture.format, requiredTextureSize));
Debug.LogError(string.Format("VolumeTextureAtlas: Texture format of texture {0} : {1} does not match expected format of {2}", volumeTexture, volumeTexture.format, requiredTextureFormat));
return;
}

if (textures.Count > 0)
{
Color[] colorArray = new Color[0];
volumeAtlas = new Texture3D(requiredTextureSize, requiredTextureSize, requiredTextureSize * textures.Count, requiredTextureFormat, false);
volumeAtlas = new Texture3D(requiredTextureSize, requiredTextureSize, requiredTextureSize * textures.Count, requiredTextureFormat, true);
foreach (Texture3D tex in textures)
{

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


float4 _VBufferSampleOffset; // Not used by this shader
float _CornetteShanksConstant; // Not used by this shader
uint _NumVisibleDensityVolumes;
float2 _VolumeMaskDimensions; //x = 1/totalTextures , y = 1/textureSize
float3 _VolumeMaskDimensions; //x = 1/totalTextures , y = 1/textureSize, z = textureSize
float SampleVolumeMask(DensityVolumeData volumeData, float3 voxelCenterUV)
float SampleVolumeMask(DensityVolumeData volumeData, float3 voxelCenterUV, float3 VxUV, float3 VyUV, float3 VzUV)
{
float offset = volumeData.textureIndex * _VolumeMaskDimensions.x;
float clampBorder = 0.5f * _VolumeMaskDimensions.y;

voxelCenterUV.z += offset;
voxelCenterUV.z = clamp(voxelCenterUV.z, offset + clampBorder, offset + _VolumeMaskDimensions.x - clampBorder);
float maskValue = SAMPLE_TEXTURE3D_LOD(_VolumeMaskAtlas, s_linear_clamp_sampler, voxelCenterUV, 0).a;
float lod = ComputeTextureLOD(VxUV * _VolumeMaskDimensions.z, VyUV * _VolumeMaskDimensions.z, VzUV * _VolumeMaskDimensions.z);
float maskValue = SAMPLE_TEXTURE3D_LOD(_VolumeMaskAtlas, s_linear_clamp_sampler, voxelCenterUV, lod).a;
return maskValue;
}

float halfDZ = 0.5 * (z1 - z0);
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.

//Sample the volumeMask
if (_VolumeData[volumeIndex].textureIndex != -1)
{
scatteringAndExtinctionMask = SampleVolumeMask(_VolumeData[volumeIndex], voxelCenterUV);
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);
scatteringAndExtinctionMask = SampleVolumeMask(_VolumeData[volumeIndex], voxelCenterUV * 0.5 + 0.5, voxelRightSizeUV, voxelUpSizeUV, voxelDepthSizeUV);
}
// There is an overlap. Sample the 3D texture, or load the constant value.

7
com.unity.render-pipelines.high-definition/HDRP/Lighting/Volumetrics/VolumetricLighting.cs


cmd.SetGlobalTexture(HDShaderIDs._VBufferLighting, m_LightingBufferHandle);
}
public DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd)
public DensityVolumeList PrepareVisibleDensityVolumeList(HDCamera hdCamera, CommandBuffer cmd, float time)
{
DensityVolumeList densityVolumes = new DensityVolumeList();

m_VisibleVolumeData.Clear();
// Collect all visible finite volume data, and upload it to the GPU.
DensityVolume[] volumes = DensityVolumeManager.manager.PrepareDensityVolumeData(cmd);
DensityVolume[] volumes = DensityVolumeManager.manager.PrepareDensityVolumeData(cmd, hdCamera.camera, time);
for (int i = 0; i < Math.Min(volumes.Length, k_MaxVisibleVolumeCount); i++)
{

Matrix4x4 transform = HDUtils.ComputePixelCoordToWorldSpaceViewDirectionMatrix(vFoV, resolution, hdCamera.viewMatrix, false);
Texture3D volumeAtlas = DensityVolumeManager.manager.volumeAtlas.volumeAtlas;
Vector2 volumeAtlasDimensions = new Vector2(0.0f, 0.0f);
Vector3 volumeAtlasDimensions = new Vector3(0.0f, 0.0f, 0.0f);
volumeAtlasDimensions.z = volumeAtlas.width;
}
cmd.SetComputeTextureParam(m_VolumeVoxelizationCS, kernel, HDShaderIDs._VBufferDensity, m_DensityBufferHandle);

正在加载...
取消
保存