diff --git a/Source/Engine/Core/Math/BoundingSphere.cpp b/Source/Engine/Core/Math/BoundingSphere.cpp index 93d44e480..5b09b4bce 100644 --- a/Source/Engine/Core/Math/BoundingSphere.cpp +++ b/Source/Engine/Core/Math/BoundingSphere.cpp @@ -4,6 +4,7 @@ #include "BoundingBox.h" #include "Matrix.h" #include "Ray.h" +#include "Transform.h" #include "../Types/String.h" const BoundingSphere BoundingSphere::Empty(Vector3(0, 0, 0), 0); @@ -220,3 +221,9 @@ void BoundingSphere::Transform(const BoundingSphere& sphere, const Matrix& matri Vector3::Transform(sphere.Center, matrix, result.Center); result.Radius = sphere.Radius * matrix.GetScaleVector().GetAbsolute().MaxValue(); } + +void BoundingSphere::Transform(const BoundingSphere& sphere, const ::Transform& transform, BoundingSphere& result) +{ + Vector3::Transform(sphere.Center, transform, result.Center); + result.Radius = sphere.Radius * transform.Scale.GetAbsolute().MaxValue(); +} diff --git a/Source/Engine/Core/Math/BoundingSphere.cs b/Source/Engine/Core/Math/BoundingSphere.cs index b57177d77..b6c3deaed 100644 --- a/Source/Engine/Core/Math/BoundingSphere.cs +++ b/Source/Engine/Core/Math/BoundingSphere.cs @@ -393,6 +393,30 @@ namespace FlaxEngine result.Radius = sphere.Radius * matrix.ScaleVector.Absolute.MaxValue; } + /// + /// Transforms the bounding sphere using the specified transformation. + /// + /// The sphere. + /// The transform. + /// The result transformed sphere. + public static BoundingSphere Transform(BoundingSphere sphere, Transform transform) + { + Transform(ref sphere, ref transform, out var result); + return result; + } + + /// + /// Transforms the bounding sphere using the specified transformation. + /// + /// The sphere. + /// The transform. + /// The result transformed sphere. + public static void Transform(ref BoundingSphere sphere, ref Transform transform, out BoundingSphere result) + { + Vector3.Transform(ref sphere.Center, ref transform, out result.Center); + result.Radius = sphere.Radius * transform.Scale.Absolute.MaxValue; + } + /// /// Tests for equality between two objects. /// diff --git a/Source/Engine/Core/Math/BoundingSphere.h b/Source/Engine/Core/Math/BoundingSphere.h index 1802bc9d6..6db237c67 100644 --- a/Source/Engine/Core/Math/BoundingSphere.h +++ b/Source/Engine/Core/Math/BoundingSphere.h @@ -224,6 +224,14 @@ public: /// The matrix. /// The result transformed sphere. static void Transform(const BoundingSphere& sphere, const Matrix& matrix, BoundingSphere& result); + + /// + /// Transforms the bounding sphere using the specified transformation. + /// + /// The sphere. + /// The transform. + /// The result transformed sphere. + static void Transform(const BoundingSphere& sphere, const ::Transform& transform, BoundingSphere& result); }; template<> diff --git a/Source/Engine/Foliage/Foliage.cpp b/Source/Engine/Foliage/Foliage.cpp index 1f3cf55b5..98e7126dd 100644 --- a/Source/Engine/Foliage/Foliage.cpp +++ b/Source/Engine/Foliage/Foliage.cpp @@ -653,7 +653,7 @@ void Foliage::UpdateBounds() typeBounds.Resize(FoliageTypes.Count()); for (int32 i = 0; i < typeBounds.Count(); i++) { - auto& type = FoliageTypes[i]; + const auto& type = FoliageTypes[i]; bool ready = type.IsReady(); typeReady.Set(i, ready); if (ready) @@ -661,16 +661,14 @@ void Foliage::UpdateBounds() } // Update bounds for all instances - Matrix foliageWorld, instanceLocal, instanceWorld; - GetLocalToWorldMatrix(foliageWorld); + Transform globalTransform = _transform, transform; for (auto i = Instances.Begin(); i.IsNotEnd(); ++i) { auto& instance = *i; if (typeReady.Get(instance.Type)) { - instance.Transform.GetWorld(instanceLocal); - Matrix::Multiply(foliageWorld, instanceLocal, instanceWorld); - BoundingSphere::Transform(typeBounds[instance.Type], instanceWorld, instance.Bounds); + globalTransform.LocalToWorld(instance.Transform, transform); + BoundingSphere::Transform(typeBounds[instance.Type], transform, instance.Bounds); } else { @@ -837,11 +835,8 @@ void Foliage::SetInstanceTransform(int32 index, const Transform& value) { BoundingSphere typeBounds; BoundingSphere::FromBox(type.Model->GetBox(), typeBounds); - Matrix foliageWorld, instanceLocal, instanceWorld; - GetLocalToWorldMatrix(foliageWorld); - instance.Transform.GetWorld(instanceLocal); - Matrix::Multiply(foliageWorld, instanceLocal, instanceWorld); - BoundingSphere::Transform(typeBounds, instanceWorld, instance.Bounds); + const Transform transform = _transform.LocalToWorld(instance.Transform); + BoundingSphere::Transform(typeBounds, transform, instance.Bounds); } else { @@ -868,8 +863,7 @@ void Foliage::OnFoliageTypeModelLoaded(int32 index) BoundingSphere typeBounds; BoundingSphere::FromBox(type.Model->GetBox(), typeBounds); - Matrix foliageWorld, instanceLocal, instanceWorld; - GetLocalToWorldMatrix(foliageWorld); + Transform globalTransform = _transform, transform; for (auto i = Instances.Begin(); i.IsNotEnd(); ++i) { @@ -877,9 +871,8 @@ void Foliage::OnFoliageTypeModelLoaded(int32 index) if (instance.Type != index) continue; - instance.Transform.GetWorld(instanceLocal); - Matrix::Multiply(foliageWorld, instanceLocal, instanceWorld); - BoundingSphere::Transform(typeBounds, instanceWorld, instance.Bounds); + globalTransform.LocalToWorld(instance.Transform, transform); + BoundingSphere::Transform(typeBounds, transform, instance.Bounds); #if !FOLIAGE_USE_SINGLE_QUAD_TREE BoundingBox::FromSphere(instance.Bounds, box);