123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- #pragma once
- #include "Math/GenericOctree.h"
- #include "WwiseUnrealDefines.h"
- #include "Components/SceneComponent.h"
- #include "EngineDefines.h"
- class UAkEnvironmentOctree;
- #if UE_4_26_OR_LATER
- #define AK_OCTREE_TYPE TOctree2
- #define AK_OCTREE_ELEMENT_ID FOctreeElementId2
- #else
- #define AK_OCTREE_TYPE TOctree
- #define AK_OCTREE_ELEMENT_ID FOctreeElementId
- #endif
- struct FAkEnvironmentOctreeElement
- {
- USceneComponent* Component;
- FBoxCenterAndExtent BoundingBox;
- FAkEnvironmentOctreeElement(USceneComponent* in_Component)
- {
- Component = in_Component;
- BoundingBox = FBoxCenterAndExtent(Component->Bounds.GetBox().GetCenter(), Component->Bounds.GetBox().GetExtent());
- }
- };
- struct FAkEnvironmentOctreeSemantics
- {
- typedef AK_OCTREE_TYPE<FAkEnvironmentOctreeElement, FAkEnvironmentOctreeSemantics> FOctree;
- enum { MaxElementsPerLeaf = 16 };
- enum { MinInclusiveElementsPerNode = 7 };
- enum { MaxNodeDepth = 12 };
- typedef TInlineAllocator<MaxElementsPerLeaf> ElementAllocator;
- FORCEINLINE static FBoxCenterAndExtent GetBoundingBox(const FAkEnvironmentOctreeElement& Element)
- {
- return Element.BoundingBox;
- }
- FORCEINLINE static bool AreElementsEqual(const FAkEnvironmentOctreeElement& A, const FAkEnvironmentOctreeElement& B)
- {
- return (A.Component == B.Component);
- }
- static void SetElementId(FOctree& OctreeOwner, const FAkEnvironmentOctreeElement& Element, AK_OCTREE_ELEMENT_ID Id);
- };
- class UAkEnvironmentOctree : public AK_OCTREE_TYPE<FAkEnvironmentOctreeElement, FAkEnvironmentOctreeSemantics>
- {
- public:
- UAkEnvironmentOctree() : AK_OCTREE_TYPE<FAkEnvironmentOctreeElement, FAkEnvironmentOctreeSemantics>(FVector::ZeroVector, HALF_WORLD_MAX) {}
- TMap<uint32, AK_OCTREE_ELEMENT_ID> ObjectToOctreeId;
- };
- class FAkEnvironmentIndex
- {
- public:
-
- template <typename EnvironmentType>
- TArray<EnvironmentType*> Query(const FVector& Location, const UWorld* World)
- {
- TArray<EnvironmentType*> Result;
- TUniquePtr<UAkEnvironmentOctree>* Octree = Map.Find(World);
- if (Octree != nullptr)
- {
- #if UE_4_26_OR_LATER
- FBoxCenterAndExtent BoxBounds(Location, FVector::ZeroVector);
- (*Octree)->FindElementsWithBoundsTest(BoxBounds, [&Result, Location](const FAkEnvironmentOctreeElement& Element)
- {
- EnvironmentType* Env = Cast<EnvironmentType>(Element.Component);
- if (Env &&
- Env->bEnable &&
- Env->HasEffectOnLocation(Location))
- {
- Result.Add(Env);
- }
- });
- #else
- for (UAkEnvironmentOctree::TConstElementBoxIterator<> It(**Octree, FBoxCenterAndExtent(Location, FVector(ForceInitToZero)));
- It.HasPendingElements();
- It.Advance())
- {
- const FAkEnvironmentOctreeElement& Element = It.GetCurrentElement();
- EnvironmentType* Env = Cast<EnvironmentType>(Element.Component);
- if (Env &&
- Env->bEnable &&
- Env->HasEffectOnLocation(Location))
- {
- Result.Add(Env);
- }
- }
- #endif
- }
-
- if (Result.Num() > 1)
- {
- Result.Sort([](const EnvironmentType& A, const EnvironmentType& B)
- {
- return A.Priority > B.Priority;
- });
- }
- return Result;
- }
-
- void Add(USceneComponent* EnvironmentToAdd);
-
-
- bool Remove(USceneComponent* EnvironmentToRemove);
-
- void Update(USceneComponent* EnvironmentToUpdate);
-
- void Clear(const UWorld* WorldToClear);
-
- bool IsEmpty(const UWorld* World);
- private:
- TMap<UWorld*, TUniquePtr<UAkEnvironmentOctree> > Map;
- };
|