Skip to content

Commit cadef34

Browse files
2 parents fddab1f + fa3d73c commit cadef34

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ void EvaluateBSDF_Line(LightLoopContext lightLoopContext,
834834
P2 = mul(P2, transpose(basis));
835835

836836
// Compute the binormal.
837-
float3 B = normalize(cross(P2 - P1, P1));
837+
float3 B = normalize(cross(P1, P2));
838838

839839
float ltcValue;
840840

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ float ADD_IDX(GetSurfaceData)(FragInput input, LayerTexCoord layerTexCoord, out
114114
#endif
115115

116116
// TODO: think about using BC5
117-
float3 vertexNormalWS = input.tangentToWorld[2].xyz;
117+
float3 vertexNormalWS = normalize(input.tangentToWorld[2].xyz);
118118

119119
#ifdef _NORMALMAP
120120
#ifdef _NORMALMAP_TANGENT_SPACE
@@ -185,7 +185,7 @@ float ADD_IDX(GetSurfaceData)(FragInput input, LayerTexCoord layerTexCoord, out
185185
surfaceData.tangentWS = SAMPLE_LAYER_TEXTURE2D(ADD_IDX(_TangentMap), ADD_ZERO_IDX(sampler_TangentMap), ADD_IDX(layerTexCoord.base)).rgb;
186186
#endif
187187
#else
188-
surfaceData.tangentWS = input.tangentToWorld[0].xyz;
188+
surfaceData.tangentWS = normalize(input.tangentToWorld[0].xyz);
189189
#endif
190190
// TODO: Is there anything todo regarding flip normal but for the tangent ?
191191

Assets/ScriptableRenderLoop/ShaderLibrary/AreaLighting.hlsl

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ float IntegrateEdge(float3 v1, float3 v2)
77
// Clamp to avoid artifacts. This particular constant gives the best results.
88
cosTheta = Clamp(cosTheta, -0.9999, 0.9999);
99
float theta = FastACos(cosTheta);
10-
float res = cross(v1, v2).z * theta / sin(theta);
10+
float res = cross(v1, v2).z * theta * rsqrt(1.0f - cosTheta * cosTheta); // optimization from * 1 / sin(theta)
1111

1212
return res;
1313
}
@@ -185,7 +185,7 @@ float LTCEvaluate(float4x3 L, float3 V, float3 N, float NdotV, bool twoSided, fl
185185
float LineFpo(float tLDDL, float lrcpD, float rcpD)
186186
{
187187
// Compute: ((l / d) / (d * d + l * l)) + (1.0 / (d * d)) * atan(l / d).
188-
return tLDDL + Square(rcpD) * atan(lrcpD);
188+
return tLDDL + (rcpD * rcpD) * FastATan(lrcpD);
189189
}
190190

191191
float LineFwt(float tLDDL, float l)
@@ -204,24 +204,40 @@ float LineIrradiance(float l1, float l2, float3 normal, float3 tangent)
204204
float d = length(normal);
205205
float l1rcpD = l1 * rcp(d);
206206
float l2rcpD = l2 * rcp(d);
207-
float tLDDL1 = l1rcpD * rcp(Square(d) + Square(l1));
208-
float tLDDL2 = l2rcpD * rcp(Square(d) + Square(l2));
207+
float tLDDL1 = l1rcpD / (d * d + l1 * l1);
208+
float tLDDL2 = l2rcpD / (d * d + l2 * l2);
209209
float intWt = LineFwt(tLDDL2, l2) - LineFwt(tLDDL1, l1);
210210
float intP0 = LineFpo(tLDDL2, l2rcpD, rcp(d)) - LineFpo(tLDDL1, l1rcpD, rcp(d));
211-
return intP0 * normal.z + intWt * tangent.z;
211+
// Guard against numerical precision issues.
212+
return max(intP0 * normal.z + intWt * tangent.z, 0.0);
213+
}
214+
215+
// Computes 1.0 / length(mul(ortho, transpose(inverse(invM)))).
216+
float ComputeLineWidthFactor(float3x3 invM, float3 ortho)
217+
{
218+
// transpose(inverse(M)) = (1.0 / determinant(M)) * cofactor(M).
219+
// Take into account that m12 = m21 = m23 = m32 = 0 and m33 = 1.
220+
float det = invM._11 * invM._22 - invM._22 * invM._31 * invM._13;
221+
float3x3 cof = {invM._22, 0.0, -invM._22 * invM._31,
222+
0.0, invM._11 - invM._13 * invM._31, 0.0,
223+
-invM._13 * invM._22, 0.0, invM._11 * invM._22};
224+
225+
// 1.0 / length(mul(V, (1.0 / s * M))) = abs(s) / length(mul(V, M)).
226+
return abs(det) / length(mul(ortho, cof));
212227
}
213228

214229
// For line lights.
215230
float LTCEvaluate(float3 P1, float3 P2, float3 B, float3x3 invM)
216231
{
217-
// Inverse-transform the endpoints and the binormal.
232+
// Inverse-transform the endpoints.
218233
P1 = mul(P1, invM);
219234
P2 = mul(P2, invM);
220-
B = mul(B, invM);
221235

222236
// Terminate the algorithm if both points are below the horizon.
223237
if (P1.z <= 0.0 && P2.z <= 0.0) return 0.0;
224238

239+
float width = ComputeLineWidthFactor(invM, B);
240+
225241
if (P2.z <= 0.0)
226242
{
227243
// Convention: 'P2' is above the horizon.
@@ -256,11 +272,7 @@ float LTCEvaluate(float3 P1, float3 P2, float3 B, float3x3 invM)
256272
// Integrate the clamped cosine over the line segment.
257273
float irradiance = LineIrradiance(l1, l2, P0, T);
258274

259-
// Compute the width factor. We take the absolute value because the points may be swapped.
260-
float width = abs(dot(B, normalize(cross(T, P1))));
261-
262-
// Guard against numerical precision issues.
263-
return max(INV_PI * width * irradiance, 0.0);
275+
return INV_PI * width * irradiance;
264276
}
265277

266278
#endif // UNITY_AREA_LIGHTING_INCLUDED

Assets/ScriptableRenderLoop/ShaderLibrary/Common.hlsl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ float4 Max3(float4 a, float4 b, float4 c)
128128
}
129129
#endif // INTRINSIC_MINMAX3
130130

131-
float Square(float x)
132-
{
133-
return x * x;
134-
}
135-
136131
void Swap(inout float a, inout float b)
137132
{
138133
float t = a; a = b; b = t;

0 commit comments

Comments
 (0)