From 4e65b76b8ce2c7e99113f15359c191654877babc Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 11 Apr 2024 15:58:34 +0200 Subject: [PATCH] Optimize `BoundingSphere.Intersects` to be inlined by the compiler --- Source/Engine/Core/Math/BoundingSphere.cpp | 6 +++++- Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Core/Math/BoundingSphere.cpp b/Source/Engine/Core/Math/BoundingSphere.cpp index 5b95ca0cc..dbd312dd2 100644 --- a/Source/Engine/Core/Math/BoundingSphere.cpp +++ b/Source/Engine/Core/Math/BoundingSphere.cpp @@ -51,7 +51,11 @@ bool BoundingSphere::Intersects(const BoundingBox& box) const bool BoundingSphere::Intersects(const BoundingSphere& sphere) const { - return CollisionsHelper::SphereIntersectsSphere(*this, sphere); + const Real radiisum = Radius + sphere.Radius; + const Real x = Center.X - sphere.Center.X; + const Real y = Center.Y - sphere.Center.Y; + const Real z = Center.Z - sphere.Center.Z; + return x * x + y * y + z * z <= radiisum * radiisum; } ContainmentType BoundingSphere::Contains(const Vector3& point) const diff --git a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp index b6d7ef23a..c36fd9758 100644 --- a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp +++ b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp @@ -76,7 +76,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, SpanEnvironmentProbes.Count(); i++) { const RenderEnvironmentProbeData& probe = cache->EnvironmentProbes.Get()[i]; - if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(probe.Position, probe.Radius))) + if (objectBounds.Intersects(BoundingSphere(probe.Position, probe.Radius))) { noEnvProbe = false; probe.SetShaderData(data.EnvironmentProbe); @@ -96,7 +96,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, SpanPointLights.Count() && data.LocalLightsCount < MaxLocalLights; i++) { const auto& light = cache->PointLights[i]; - if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(light.Position, light.Radius))) + if (objectBounds.Intersects(BoundingSphere(light.Position, light.Radius))) { light.SetShaderData(data.LocalLights[data.LocalLightsCount], false); data.LocalLightsCount++; @@ -105,7 +105,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, SpanSpotLights.Count() && data.LocalLightsCount < MaxLocalLights; i++) { const auto& light = cache->SpotLights[i]; - if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(light.Position, light.Radius))) + if (objectBounds.Intersects(BoundingSphere(light.Position, light.Radius))) { light.SetShaderData(data.LocalLights[data.LocalLightsCount], false); data.LocalLightsCount++;