Simplify SetMatrix code

This commit is contained in:
ExMatics HydrogenC
2024-06-06 12:13:32 +08:00
parent df8dc9173a
commit 5d188c8c2d
3 changed files with 73 additions and 85 deletions
+4 -15
View File
@@ -46,21 +46,6 @@ void BoundingFrustum::SetMatrix(const Matrix& matrix)
_pBottom.D = matrix.M44 + matrix.M42;
_pBottom.Normalize();
#if FLAX_REVERSE_Z
// Far plane
_pFar.Normal.X = matrix.M13;
_pFar.Normal.Y = matrix.M23;
_pFar.Normal.Z = matrix.M33;
_pFar.D = matrix.M43;
_pFar.Normalize();
// Near plane
_pNear.Normal.X = matrix.M14 - matrix.M13;
_pNear.Normal.Y = matrix.M24 - matrix.M23;
_pNear.Normal.Z = matrix.M34 - matrix.M33;
_pNear.D = matrix.M44 - matrix.M43;
_pNear.Normalize();
#else
// Near plane
_pNear.Normal.X = matrix.M13;
_pNear.Normal.Y = matrix.M23;
@@ -74,6 +59,10 @@ void BoundingFrustum::SetMatrix(const Matrix& matrix)
_pFar.Normal.Z = matrix.M34 - matrix.M33;
_pFar.D = matrix.M44 - matrix.M43;
_pFar.Normalize();
#if FLAX_REVERSE_Z
// Swap far and near planes if reverse z
Swap(_pFar, _pNear);
#endif
}
+59 -70
View File
@@ -188,13 +188,13 @@ namespace FlaxEngine
{
switch (index)
{
case 0: return pLeft;
case 1: return pRight;
case 2: return pTop;
case 3: return pBottom;
case 4: return pNear;
case 5: return pFar;
default: return new Plane();
case 0: return pLeft;
case 1: return pRight;
case 2: return pTop;
case 3: return pBottom;
case 4: return pNear;
case 5: return pFar;
default: return new Plane();
}
}
@@ -230,21 +230,6 @@ namespace FlaxEngine
bottom.D = matrix.M44 + matrix.M42;
bottom.Normalize();
#if FLAX_REVERSE_Z
// Far plane
far.Normal.X = matrix.M13;
far.Normal.Y = matrix.M23;
far.Normal.Z = matrix.M33;
far.D = matrix.M43;
far.Normalize();
// Near plane
near.Normal.X = matrix.M14 - matrix.M13;
near.Normal.Y = matrix.M24 - matrix.M23;
near.Normal.Z = matrix.M34 - matrix.M33;
near.D = matrix.M44 - matrix.M43;
near.Normalize();
#else
// Near plane
near.Normal.X = matrix.M13;
near.Normal.Y = matrix.M23;
@@ -258,6 +243,10 @@ namespace FlaxEngine
far.Normal.Z = matrix.M34 - matrix.M33;
far.D = matrix.M44 - matrix.M43;
far.Normalize();
#if FLAX_REVERSE_Z
// Swap far and near planes if reverse z
(near, far) = (far, near);
#endif
}
@@ -385,37 +374,37 @@ namespace FlaxEngine
{
switch (i)
{
case 0:
planeResult = pNear.Intersects(ref point);
break;
case 1:
planeResult = pFar.Intersects(ref point);
break;
case 2:
planeResult = pLeft.Intersects(ref point);
break;
case 3:
planeResult = pRight.Intersects(ref point);
break;
case 4:
planeResult = pTop.Intersects(ref point);
break;
case 5:
planeResult = pBottom.Intersects(ref point);
break;
case 0:
planeResult = pNear.Intersects(ref point);
break;
case 1:
planeResult = pFar.Intersects(ref point);
break;
case 2:
planeResult = pLeft.Intersects(ref point);
break;
case 3:
planeResult = pRight.Intersects(ref point);
break;
case 4:
planeResult = pTop.Intersects(ref point);
break;
case 5:
planeResult = pBottom.Intersects(ref point);
break;
}
switch (planeResult)
{
case PlaneIntersectionType.Back: return ContainmentType.Disjoint;
case PlaneIntersectionType.Intersecting:
result = PlaneIntersectionType.Intersecting;
break;
case PlaneIntersectionType.Back: return ContainmentType.Disjoint;
case PlaneIntersectionType.Intersecting:
result = PlaneIntersectionType.Intersecting;
break;
}
}
switch (result)
{
case PlaneIntersectionType.Intersecting: return ContainmentType.Intersects;
default: return ContainmentType.Contains;
case PlaneIntersectionType.Intersecting: return ContainmentType.Intersects;
default: return ContainmentType.Contains;
}
}
@@ -502,37 +491,37 @@ namespace FlaxEngine
{
switch (i)
{
case 0:
planeResult = pNear.Intersects(ref sphere);
break;
case 1:
planeResult = pFar.Intersects(ref sphere);
break;
case 2:
planeResult = pLeft.Intersects(ref sphere);
break;
case 3:
planeResult = pRight.Intersects(ref sphere);
break;
case 4:
planeResult = pTop.Intersects(ref sphere);
break;
case 5:
planeResult = pBottom.Intersects(ref sphere);
break;
case 0:
planeResult = pNear.Intersects(ref sphere);
break;
case 1:
planeResult = pFar.Intersects(ref sphere);
break;
case 2:
planeResult = pLeft.Intersects(ref sphere);
break;
case 3:
planeResult = pRight.Intersects(ref sphere);
break;
case 4:
planeResult = pTop.Intersects(ref sphere);
break;
case 5:
planeResult = pBottom.Intersects(ref sphere);
break;
}
switch (planeResult)
{
case PlaneIntersectionType.Back: return ContainmentType.Disjoint;
case PlaneIntersectionType.Intersecting:
result = PlaneIntersectionType.Intersecting;
break;
case PlaneIntersectionType.Back: return ContainmentType.Disjoint;
case PlaneIntersectionType.Intersecting:
result = PlaneIntersectionType.Intersecting;
break;
}
}
switch (result)
{
case PlaneIntersectionType.Intersecting: return ContainmentType.Intersects;
default: return ContainmentType.Contains;
case PlaneIntersectionType.Intersecting: return ContainmentType.Intersects;
default: return ContainmentType.Contains;
}
}
+10
View File
@@ -47,9 +47,19 @@ float3 GetShadowPositionOffset(float offsetScale, float NoL, float3 normal)
float CalculateSubsurfaceOcclusion(float opacity, float sceneDepth, float shadowMapDepth)
{
#if FLAX_REVERSE_Z
float thickness = max(shadowMapDepth - sceneDepth, 0);
#else
float thickness = max(sceneDepth - shadowMapDepth, 0);
#endif
float occlusion = 1 - thickness * lerp(1.0f, 100.0f, opacity);
#if FLAX_REVERSE_Z
return shadowMapDepth < 0.01f ? 1 : occlusion;
#else
return shadowMapDepth > 0.99f ? 1 : occlusion;
#endif
}
#endif