Files
UnrealEngineUWP/Engine/Shaders/Private/IntersectionUtils.ush
charles derousiers 00ba5d9d67 Move common intersection methods into IntersectionUtils.ush
[FYI] krzysztof.narkowicz

[CL 33856461 by charles derousiers in ue5-main branch]
2024-05-23 04:43:28 -04:00

66 lines
2.3 KiB
Plaintext

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
bool SphereIntersectCone(float4 SphereCenterAndRadius, float3 ConeVertex, float3 ConeAxis, float ConeAngleCos, float ConeAngleSin)
{
float3 U = ConeVertex - (SphereCenterAndRadius.w / ConeAngleSin) * ConeAxis;
float3 D = SphereCenterAndRadius.xyz - U;
float DSizeSq = dot(D, D);
float E = dot(ConeAxis, D);
if (E > 0 && E * E >= DSizeSq * ConeAngleCos * ConeAngleCos)
{
D = SphereCenterAndRadius.xyz - ConeVertex;
DSizeSq = dot(D, D);
E = -dot(ConeAxis, D);
if (E > 0 && E * E >= DSizeSq * ConeAngleSin * ConeAngleSin)
{
return DSizeSq <= SphereCenterAndRadius.w * SphereCenterAndRadius.w;
}
else
{
return true;
}
}
return false;
}
bool SphereIntersectConeWithMaxDistance(float4 SphereCenterAndRadius, float3 ConeVertex, float3 ConeAxis, float ConeAngleCos, float ConeAngleSin, float MaxDistanceAlongAxis)
{
if (SphereIntersectCone(SphereCenterAndRadius, ConeVertex, ConeAxis, ConeAngleCos, ConeAngleSin))
{
float ConeAxisDistance = dot(SphereCenterAndRadius.xyz - ConeVertex, ConeAxis);
float ConeAxisDistanceMax = ConeAxisDistance - SphereCenterAndRadius.w;
return ConeAxisDistanceMax < MaxDistanceAlongAxis;
}
return false;
}
bool SphereIntersectConeWithDepthRanges(float4 SphereCenterAndRadius, float3 ConeVertex, float3 ConeAxis, float ConeAngleCos, float ConeAngleSin, float4 ConeAxisDepthRanges)
{
if (SphereIntersectCone(SphereCenterAndRadius, ConeVertex, ConeAxis, ConeAngleCos, ConeAngleSin))
{
float ConeAxisDistance = dot(SphereCenterAndRadius.xyz - ConeVertex, ConeAxis);
float2 ConeAxisDistanceMinMax = float2(ConeAxisDistance + SphereCenterAndRadius.w, ConeAxisDistance - SphereCenterAndRadius.w);
if ((ConeAxisDistanceMinMax.x > ConeAxisDepthRanges.x && ConeAxisDistanceMinMax.y < ConeAxisDepthRanges.y)
|| (ConeAxisDistanceMinMax.x > ConeAxisDepthRanges.z && ConeAxisDistanceMinMax.y < ConeAxisDepthRanges.w))
{
return true;
}
}
return false;
}
bool SphereIntersectSphere(float4 SphereCenterAndRadius, float4 OtherSphereCenterAndRadius)
{
float CombinedRadii = SphereCenterAndRadius.w + OtherSphereCenterAndRadius.w;
float3 VectorBetweenCenters = SphereCenterAndRadius.xyz - OtherSphereCenterAndRadius.xyz;
return dot(VectorBetweenCenters, VectorBetweenCenters) < CombinedRadii * CombinedRadii;
}