Skip to content

Commit 3730965

Browse files
committed
Implement the reference line light code
1 parent 20f400c commit 3730965

File tree

1 file changed

+66
-13
lines changed
  • Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit

1 file changed

+66
-13
lines changed

Assets/ScriptableRenderLoop/HDRenderLoop/Material/Lit/Lit.hlsl

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -751,19 +751,25 @@ void IntegrateGGXAreaRef( float3 V, float3 positionWS, PreLightData preLightDa
751751

752752
float4x4 localToWorld = float4x4(float4(lightData.right, 0.0), float4(lightData.up, 0.0), float4(lightData.forward, 0.0), float4(lightData.positionWS, 1.0));
753753

754-
if (lightData.lightType == GPULIGHTTYPE_SPHERE)
755-
SampleSphere(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
756-
else if (lightData.lightType == GPULIGHTTYPE_HEMISPHERE)
757-
SampleHemisphere(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
758-
else if (lightData.lightType == GPULIGHTTYPE_CYLINDER)
759-
SampleCylinder(u, localToWorld, lightData.size.x, lightData.size.y, lightPdf, P, Ns);
760-
else if (lightData.lightType == GPULIGHTTYPE_RECTANGLE)
761-
SampleRectangle(u, localToWorld, lightData.size.x, lightData.size.y, lightPdf, P, Ns);
762-
else if (lightData.lightType == GPULIGHTTYPE_DISK)
763-
SampleDisk(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
764-
else if (lightData.lightType == GPULIGHTTYPE_LINE)
765-
// SampleLine(u, localToWorld, areaLight.lightRadius0, lightPdf, P, Ns);
766-
; // TODO
754+
switch (lightData.lightType)
755+
{
756+
case GPULIGHTTYPE_SPHERE:
757+
SampleSphere(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
758+
break;
759+
case GPULIGHTTYPE_HEMISPHERE:
760+
SampleHemisphere(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
761+
break;
762+
case GPULIGHTTYPE_CYLINDER:
763+
SampleCylinder(u, localToWorld, lightData.size.x, lightData.size.y, lightPdf, P, Ns);
764+
break;
765+
case GPULIGHTTYPE_RECTANGLE:
766+
SampleRectangle(u, localToWorld, lightData.size.x, lightData.size.y, lightPdf, P, Ns);
767+
break;
768+
case GPULIGHTTYPE_DISK:
769+
SampleDisk(u, localToWorld, lightData.size.x, lightPdf, P, Ns);
770+
break;
771+
// case GPULIGHTTYPE_LINE: handled by a separate function.
772+
}
767773

768774
// Get distance
769775
float3 unL = P - positionWS;
@@ -900,6 +906,53 @@ void EvaluateBSDF_Area( LightLoopContext lightLoopContext,
900906
#endif
901907
}
902908

909+
//-----------------------------------------------------------------------------
910+
// EvaluateBSDFLine - Reference
911+
//-----------------------------------------------------------------------------
912+
913+
void IntegrateBSDFLineRef(float3 V, float3 positionWS, PreLightData preLightData,
914+
LightData lightData, BSDFData bsdfData,
915+
out float3 diffuseLighting, out float3 specularLighting,
916+
int sampleCount = 64)
917+
{
918+
diffuseLighting = float3(0.0, 0.0, 0.0);
919+
specularLighting = float3(0.0, 0.0, 0.0);
920+
921+
const float len = lightData.size.x;
922+
const float3 p0 = lightData.positionWS - lightData.right * (0.5 * len);
923+
const float3 dir = lightData.right;
924+
const float dt = len * rcp(sampleCount);
925+
const float off = 0.5 * dt;
926+
927+
// Uniformly sample the line segment with the Pdf = 1 / len.
928+
const float invPdf = len;
929+
930+
for (int i = 0; i < sampleCount; ++i)
931+
{
932+
// Place the sample in the middle of the interval.
933+
float t = off + i * dt;
934+
float3 sPos = p0 + t * dir;
935+
float3 unL = sPos - positionWS;
936+
float dist2 = dot(unL, unL);
937+
float3 L = normalize(L);
938+
float sinLD = length(cross(L, dir));
939+
float NdotL = saturate(dot(bsdfData.normalWS, L));
940+
941+
float3 lightDiff, lightSpec;
942+
943+
BSDF(V, L, positionWS, preLightData, bsdfData, lightDiff, lightSpec);
944+
945+
diffuseLighting += lightDiff * (sinLD / dist2 * NdotL);
946+
specularLighting += lightSpec * (sinLD / dist2 * NdotL);
947+
}
948+
949+
// The factor of 2 is due to the fact: Integral{0, 2 PI}{max(0, cos(x))dx} = 2.
950+
float normFactor = 2.0 * invPdf * rcp(sampleCount);
951+
952+
diffuseLighting *= normFactor * lightData.diffuseScale * lightData.color;
953+
specularLighting *= normFactor * lightData.specularScale * lightData.color;
954+
}
955+
903956
//-----------------------------------------------------------------------------
904957
// EvaluateBSDF_Env - Reference
905958
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)