浏览代码

Implement biquadratic filtering of the volumetric lighting

/Yibing-Project-2
Evgenii Golubev 7 年前
当前提交
14a2be10
共有 5 个文件被更改,包括 96 次插入6 次删除
  1. 6
      ScriptableRenderPipeline/Core/ShaderLibrary/Random.hlsl
  2. 41
      ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl
  3. 2
      ScriptableRenderPipeline/HDRenderPipeline/Sky/AtmosphericScattering/AtmosphericScattering.hlsl
  4. 44
      ScriptableRenderPipeline/Core/ShaderLibrary/Filtering.hlsl
  5. 9
      ScriptableRenderPipeline/Core/ShaderLibrary/Filtering.hlsl.meta

6
ScriptableRenderPipeline/Core/ShaderLibrary/Random.hlsl


#ifndef UNITY_NOISE_INCLUDED
#define UNITY_NOISE_INCLUDED
#ifndef UNITY_RANDOM_INCLUDED
#define UNITY_RANDOM_INCLUDED
#if !defined(SHADER_API_GLES)

#endif // SHADER_API_GLES
#endif // UNITY_NOISE_INCLUDED
#endif // UNITY_RANDOM_INCLUDED

41
ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl


#ifndef UNITY_VOLUME_RENDERING_INCLUDED
#define UNITY_VOLUME_RENDERING_INCLUDED
#include "Filtering.hlsl"
// Integral{a, b}{Transmittance(0, t) * Li(t) dt} = Transmittance(0, a) * Integral{a, b}{Transmittance(0, t - a) * Li(t) dt}.
// Integral{a, b}{Transmittance(0, t) * L_s(t) dt} = Transmittance(0, a) * Integral{a, b}{Transmittance(0, t - a) * L_s(t) dt}.
float OpticalDepthHomogeneousMedium(float extinction, float intervalLength)
{

// Returns linearly interpolated {volumetric radiance, opacity}. The sampler clamps to edge.
float4 SampleInScatteredRadianceAndTransmittance(TEXTURE3D_ARGS(VBufferLighting, trilinearSampler),
float2 positionNDC, float linearDepth,
float2 VBufferScale, float4 VBufferDepthEncodingParams)
float4 VBufferResolutionAndScale, float4 VBufferDepthEncodingParams)
#ifdef RECONSTRUCTION_FILTER_TRILINEAR
#else
// Perform biquadratic reconstruction in XY, linear in Z, using 4x trilinear taps.
int k = VBUFFER_SLICE_COUNT;
float z = linearDepth;
float d = EncodeLogarithmicDepth(z, VBufferDepthEncodingParams);
// The distance between slices is log-encoded.
float s0 = floor(d * k - 0.5);
float s1 = ceil(d * k - 0.5);
float d0 = saturate(s0 * rcp(k) + (0.5 * rcp(k)));
float d1 = saturate(s1 * rcp(k) + (0.5 * rcp(k)));
float z0 = DecodeLogarithmicDepth(d0, VBufferDepthEncodingParams);
float z1 = DecodeLogarithmicDepth(d1, VBufferDepthEncodingParams);
// Compute the linear Z-interpolation weight.
float a = saturate((z - z0) / (z1 - z0));
float w = d0 + a * rcp(k);
// Account for the visible area of the V-Buffer.
float2 xy = positionNDC * (VBufferResolutionAndScale.xy * VBufferResolutionAndScale.zw); // TODO: precompute
float2 ic = floor(xy);
float2 fc = frac(xy);
float2 weights[2], offsets[2];
BiquadraticFilter(1 - fc, weights, offsets); // Reflect the filter around 0.5
float2 rcpRes = rcp(VBufferResolutionAndScale.xy); // TODO: precompute
float4 L = (weights[0].x * weights[0].y) * SAMPLE_TEXTURE3D_LOD(VBufferLighting, trilinearSampler, float3((ic + float2(offsets[0].x, offsets[0].y)) * rcpRes, w), 0) // Top left
+ (weights[1].x * weights[0].y) * SAMPLE_TEXTURE3D_LOD(VBufferLighting, trilinearSampler, float3((ic + float2(offsets[1].x, offsets[0].y)) * rcpRes, w), 0) // Top right
+ (weights[0].x * weights[1].y) * SAMPLE_TEXTURE3D_LOD(VBufferLighting, trilinearSampler, float3((ic + float2(offsets[0].x, offsets[1].y)) * rcpRes, w), 0) // Bottom left
+ (weights[1].x * weights[1].y) * SAMPLE_TEXTURE3D_LOD(VBufferLighting, trilinearSampler, float3((ic + float2(offsets[1].x, offsets[1].y)) * rcpRes, w), 0); // Bottom right
#endif
// TODO: add some animated noise to the reconstructed radiance.
return float4(L.rgb, 1 - Transmittance(L.a));
}

2
ScriptableRenderPipeline/HDRenderPipeline/Sky/AtmosphericScattering/AtmosphericScattering.hlsl


#ifdef VOLUMETRIC_LIGHTING_ENABLED
return SampleInScatteredRadianceAndTransmittance(TEXTURE3D_PARAM(_VBufferLighting, s_trilinear_clamp_sampler),
posInput.positionNDC, posInput.linearDepth,
_VBufferResolutionAndScale.zw,
_VBufferResolutionAndScale,
_VBufferDepthEncodingParams);
#endif

44
ScriptableRenderPipeline/Core/ShaderLibrary/Filtering.hlsl


#ifndef UNITY_FILTERING_INCLUDED
#define UNITY_FILTERING_INCLUDED
#if !defined(SHADER_API_GLES)
// Cardinal (interpolating) B-Spline of the 2nd degree (3rd order). Support = 3x3.
// The fractional coordinate of each part is assumed to be in the [0, 1] range (centered on 0.5).
// https://www.desmos.com/calculator/47j9r9lolm
float2 BSpline2IntLeft(float2 x)
{
return 0.5 * x * x;
}
float2 BSpline2IntMiddle(float2 x)
{
return (1 - x) * x + 0.5;
}
float2 BSpline2IntRight(float2 x)
{
return (0.5 * x - 1) * x + 0.5;
}
// Compute weights & offsets for 4x bilinear taps for the biquadratic B-Spline filter.
// The fractional coordinate should be in the [0, 1] range (centered on 0.5).
// Inspired by: http://vec3.ca/bicubic-filtering-in-fewer-taps/
void BiquadraticFilter(float2 fracCoord, out float2 weights[2], out float2 offsets[2])
{
float2 l = BSpline2IntLeft(fracCoord);
float2 m = BSpline2IntMiddle(fracCoord);
float2 r = 1 - l - m;
// Compute offsets for 4x bilinear taps for the quadratic B-Spline reconstruction kernel.
// 0: lerp between left and middle
// 1: lerp between middle and right
weights[0] = l + 0.5 * m;
weights[1] = r + 0.5 * m;
offsets[0] = -0.5 + 0.5 * m * rcp(weights[0]);
offsets[1] = 0.5 + r * rcp(weights[1]);
}
#endif // SHADER_API_GLES
#endif // UNITY_FILTERING_INCLUDED

9
ScriptableRenderPipeline/Core/ShaderLibrary/Filtering.hlsl.meta


fileFormatVersion: 2
guid: 996afc944d3a3534a8b9b09d0b79843a
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存