浏览代码

Optimize the volumetric lighting code

/sample_game
Evgenii Golubev 7 年前
当前提交
c4d1ae02
共有 3 个文件被更改,包括 47 次插入31 次删除
  1. 18
      Assets/ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl
  2. 47
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/Resources/VolumetricLighting.compute
  3. 13
      Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.hlsl

18
Assets/ScriptableRenderPipeline/Core/ShaderLibrary/VolumeRendering.hlsl


return INV_FOUR_PI;
}
float HenyeyGreensteinPhaseFunction(float asymmetry, float LdotD)
float HenyeyGreensteinPhasePartConstant(float asymmetry)
// Note: the factor before pow() is a constant, and could therefore be factored out.
return (INV_FOUR_PI * (1 - g * g)) * pow(abs(1 + g * g - 2 * g * LdotD), -1.5);
return INV_FOUR_PI * (1 - g * g);
}
float HenyeyGreensteinPhasePartVarying(float asymmetry, float LdotD)
{
float g = asymmetry;
return pow(abs(1 + g * g - 2 * g * LdotD), -1.5);
}
float HenyeyGreensteinPhaseFunction(float asymmetry, float LdotD)
{
return HenyeyGreensteinPhasePartConstant(asymmetry) *
HenyeyGreensteinPhasePartVarying(asymmetry, LdotD);
}
#endif // UNITY_VOLUME_RENDERING_INCLUDED

47
Assets/ScriptableRenderPipeline/HDRenderPipeline/Lighting/Volumetrics/Resources/VolumetricLighting.compute


float3 positionWS = ray.originWS + t * ray.directionWS;
float3 sampleRadiance = 0;
if (featureFlags & LIGHTFEATUREFLAGS_PUNCTUAL)
{
uint punctualLightCount;

#endif
// Fetch the light.
LightData lightData = _LightDatas[punctualLightIndex];
int lightType = lightData.lightType;
LightData lightData = _LightDatas[punctualLightIndex];
int lightType = lightData.lightType;
float distSq = dot(lightToSample, lightToSample);
float dist = sqrt(distSq);
float3 L = lightToSample * -rsqrt(distSq);
float intensity = GetPunctualShapeAttenuation(lightData, L, distSq);
float3 color = lightData.color;
// Compute the distance to the light, and the associated transmittance values.
float distSq = dot(lightToSample, lightToSample);
float dist = sqrt(distSq);
float3 L = lightToSample * -rsqrt(distSq);
float LdotD = dot(L, ray.directionWS);
float phase = HenyeyGreensteinPhaseFunction(asymmetry, LdotD);
float3 lightT = Transmittance(OpticalDepthHomogeneous(extinction, dist));
float3 sampleT = Transmittance(OpticalDepthHomogeneous(extinction, t));
// Note: we apply the scattering coefficient and the constant part of the phase function later.
intensity *= HenyeyGreensteinPhasePartVarying(asymmetry, dot(L, ray.directionWS));
// Note: lightData.invSqrAttenuationRadius is 0 when applyRangeAttenuation is false
float attenuation = GetDistanceAttenuation(distSq, lightData.invSqrAttenuationRadius);
// Reminder: lights are oriented backward (-Z)
attenuation *= GetAngleAttenuation(L, -lightData.forward, lightData.angleScale, lightData.angleOffset);
float shadow = 1;
color *= Transmittance(OpticalDepthHomogeneous(extinction, dist));
[branch] if (lightData.shadowIndex >= 0)
{

shadow = GetPunctualShadowAttenuation(context.shadowContext, positionWS + offset, 0,
lightData.shadowIndex, float4(L, dist), posInput.unPositionSS);
shadow = lerp(1.0, shadow, lightData.shadowDimmer);
float shadow = GetPunctualShadowAttenuation(context.shadowContext, positionWS + offset, 0,
lightData.shadowIndex, float4(L, dist), posInput.unPositionSS);
intensity *= lerp(1, shadow, lightData.shadowDimmer);
}
// Projector lights always have a cookies, so we can perform clipping inside the if().

// Premultiply.
lightData.color *= cookie.rgb;
attenuation *= cookie.a;
color *= cookie.rgb;
intensity *= cookie.a;
radiance += (dt * scattering * phase * attenuation * shadow) * (lightT * sampleT) * lightData.color;
sampleRadiance += color * (intensity * dt);
radiance += sampleRadiance * Transmittance(OpticalDepthHomogeneous(extinction, t));
return radiance;
float phaseConstant = scattering * HenyeyGreensteinPhasePartConstant(asymmetry);
return radiance * phaseConstant;
}
[numthreads(GROUP_SIZE_2D, 1, 1)]

13
Assets/ScriptableRenderPipeline/HDRenderPipeline/Material/Lit/Lit.hlsl


return cookie;
}
float GetPunctualShapeAttenuation(LightData lightData, float3 L, float distSq)
{
// Note: lightData.invSqrAttenuationRadius is 0 when applyRangeAttenuation is false
float attenuation = GetDistanceAttenuation(distSq, lightData.invSqrAttenuationRadius);
// Reminder: lights are oriented backward (-Z)
return attenuation * GetAngleAttenuation(L, -lightData.forward, lightData.angleScale, lightData.angleOffset);
}
void EvaluateBSDF_Punctual( LightLoopContext lightLoopContext,
float3 V, PositionInputs posInput, PreLightData preLightData, LightData lightData, BSDFData bsdfData,
out float3 diffuseLighting,

float NdotL = dot(bsdfData.normalWS, L);
float illuminance = saturate(NdotL);
// Note: lightData.invSqrAttenuationRadius is 0 when applyRangeAttenuation is false
float attenuation = GetDistanceAttenuation(distSq, lightData.invSqrAttenuationRadius);
// Reminder: lights are oriented backward (-Z)
attenuation *= GetAngleAttenuation(L, -lightData.forward, lightData.angleScale, lightData.angleOffset);
float attenuation = GetPunctualShapeAttenuation(lightData, L, distSq);
// Premultiply.
lightData.diffuseScale *= attenuation;

正在加载...
取消
保存