AkObstructionAndOcclusionService.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. /*=============================================================================
  16. AkObstructionAndOcclusionService.h:
  17. =============================================================================*/
  18. #pragma once
  19. #include "AkInclude.h"
  20. #include "WorldCollision.h"
  21. #include "HAL/ThreadSafeBool.h"
  22. #include "WwiseUnrealHelper.h"
  23. #include "WwiseUnrealObjectHelper.h"
  24. #include "Wwise/WwiseSoundEngineUtils.h"
  25. #define NUM_BOUNDING_BOX_TRACE_POINTS 12
  26. struct FAkObstructionAndOcclusion
  27. {
  28. float CurrentValue;
  29. float TargetValue;
  30. float Rate;
  31. FAkObstructionAndOcclusion(float InTargetValue = 0.0f, float InCurrentValue = 0.0f);
  32. void SetTarget(float InTargetValue);
  33. bool ReachedTarget();
  34. bool Update(float InDeltaTime);
  35. };
  36. struct FAkObstructionAndOcclusionPair
  37. {
  38. FAkObstructionAndOcclusionPair();
  39. FAkObstructionAndOcclusion Occ;
  40. FAkObstructionAndOcclusion Obs;
  41. FVector Position;
  42. bool Update(float InDeltaTime);
  43. bool ReachedTarget();
  44. /** Trace a ray from a source position to a bounding box point asynchronously */
  45. void AsyncTraceFromSource(const FVector& InSourcePosition, const FVector& InEndPosition, int InBoundingBoxPointIndex, ECollisionChannel InCollisionChannel, UWorld* InWorld, const FCollisionQueryParams& InCollisionParams);
  46. /** Trace a ray from a listener position to a bounding box point asynchronously */
  47. void AsyncTraceFromListener(const FVector& InListenerPosition, const FVector& InEndPosition, int InBoundingBoxPointIndex, ECollisionChannel InCollisionChannel, UWorld* InWorld, const FCollisionQueryParams& InCollisionParams);
  48. /** Get the total number of listener OR source collisions. */
  49. int GetCollisionCount();
  50. void Reset();
  51. /** Iterate through all trace handles and handle the results if ready */
  52. void CheckTraceResults(UWorld* InWorld);
  53. private:
  54. /** Used to check when obstruction and occlusion targets need to be updated (when GetCollisionCount() != CurrentCollisionCount) */
  55. int CurrentCollisionCount = 0;
  56. TArray<FTraceHandle> SourceTraceHandles;
  57. TArray<FTraceHandle> ListenerTraceHandles;
  58. /** Iterate through all listener trace handles and handle the trace results if ready */
  59. void CheckListenerTraceHandles(UWorld* InWorld);
  60. /** Iterate through all source trace handles and handle the trace results if ready */
  61. void CheckSourceTraceHandles(UWorld* InWorld);
  62. TArray<FThreadSafeBool> SourceRayCollisions;
  63. TArray<FThreadSafeBool> ListenerRayCollisions;
  64. };
  65. class WWISEOBSTRUCTIONOCCLUSION_API AkObstructionAndOcclusionService
  66. {
  67. public:
  68. struct FListenerInfo
  69. {
  70. FVector Position;
  71. AkRoomID RoomID;
  72. FListenerInfo(FVector InPosition, AkRoomID InRoomID)
  73. {
  74. Position = InPosition;
  75. RoomID = InRoomID;
  76. }
  77. };
  78. typedef TMap<AkGameObjectID, FListenerInfo, FDefaultSetAllocator, WwiseUnrealHelper::AkGameObjectIdKeyFuncs<FListenerInfo, false>> ListenerMap;
  79. struct FPortalInfo
  80. {
  81. FVector Position;
  82. bool EnableObstruction;
  83. FPortalInfo(FVector InPosition, bool InEnableObstruction)
  84. {
  85. Position = InPosition;
  86. EnableObstruction = InEnableObstruction;
  87. }
  88. };
  89. typedef TMap<AkGameObjectID, FPortalInfo, FDefaultSetAllocator, WwiseUnrealHelper::AkGameObjectIdKeyFuncs<FPortalInfo, false>> PortalMap;
  90. virtual ~AkObstructionAndOcclusionService() {}
  91. void Tick(
  92. const ListenerMap& InListeners,
  93. const PortalMap& InPortals,
  94. const FVector& InSourcePosition,
  95. const AActor* InActor,
  96. const AkRoomID InRoomID,
  97. ECollisionChannel InCollisionChannel,
  98. float InDeltaTime,
  99. float InOcclusionRefreshInterval);
  100. /**
  101. * Calculates updated occlusion and obstruction values synchronously and then sends them to the Wwise engine.
  102. */
  103. void UpdateObstructionAndOcclusion(
  104. const ListenerMap& InListeners,
  105. const PortalMap& InPortals,
  106. const FVector& InSourcePosition,
  107. const AActor* InActor,
  108. const AkRoomID InRoomID,
  109. ECollisionChannel InCollisionChannel,
  110. float InOcclusionRefreshInterval);
  111. void ClearOcclusionValues();
  112. virtual void SetObstructionAndOcclusion(const AkGameObjectID InListenerID, const float InValue) = 0;
  113. virtual void SetPortalObstruction(const AkPortalID InPortalID, const float InValue) = 0;
  114. protected:
  115. void _Init(UWorld* InWorld, float InRefreshInterval);
  116. private:
  117. /**
  118. * Fades active occlusions towards targets, sends updated values to the Wwise engine, then calculates refreshed occlusion and obstruction values asynchronously.
  119. */
  120. void RefreshObstructionAndOcclusion(
  121. const ListenerMap& InListeners,
  122. const PortalMap& InPortals,
  123. const FVector& InSourcePosition,
  124. const AActor* InActor,
  125. const AkRoomID InRoomID,
  126. ECollisionChannel InCollisionChannel,
  127. const float InDeltaTime,
  128. float InOcclusionRefreshInterval);
  129. /**
  130. * Loops through in_Listeners and sends the obstruction occlusion values on each to the Wwise engine.
  131. */
  132. void SetListenerObstructionAndOcclusion(const ListenerMap& InListeners);
  133. void SetListenerObstructionAndOcclusion(const FAkObstructionAndOcclusionPair& InObsOccPair, const AkGameObjectID InListenerID);
  134. /**
  135. * Loops through the portals connected to in_pRoom and sends the obstruction occlusion values on each to the Wwise engine.
  136. */
  137. void SetPortalObstruction(const PortalMap& InPortals, const AkRoomID InRoomID);
  138. /**
  139. * Calculates updated occlusion and obstruction values to listeners.
  140. */
  141. void CalculateObstructionAndOcclusionValuesToListeners(const ListenerMap& InListeners, const FVector& InSourcePosition, const AActor* InActor, ECollisionChannel InCollisionChannel, bool bInAsync = true);
  142. /**
  143. * Calculates updated obstruction values to portals.
  144. */
  145. void CalculateObstructionValuesToPortals(const PortalMap& InPortals, const FVector& InSourcePosition, const AActor* InActor, const AkRoomID InRoomID, ECollisionChannel InCollisionChannel, bool bInAsync = true);
  146. /** Last time occlusion was refreshed */
  147. float LastObstructionAndOcclusionRefresh = -1;
  148. float PreviousRefreshInterval = -1.0f;
  149. bool bClearingObstructionAndOcclusion = false;
  150. typedef WwiseUnrealHelper::AkGameObjectIdKeyFuncs<FAkObstructionAndOcclusionPair, false> ObsOccPairGameObjectIDKeyFuncs;
  151. TMap<AkGameObjectID, FAkObstructionAndOcclusionPair, FDefaultSetAllocator, ObsOccPairGameObjectIDKeyFuncs> ListenerObsOccMap;
  152. typedef WwiseUnrealHelper::AkSpatialAudioIDKeyFuncs<FAkObstructionAndOcclusionPair, false> ObsOccPairSpatialAudioIDKeyFuncs;
  153. typedef TMap<AkPortalID, FAkObstructionAndOcclusionPair, FDefaultSetAllocator, ObsOccPairSpatialAudioIDKeyFuncs> PortalObsOccMap;
  154. typedef WwiseUnrealHelper::AkSpatialAudioIDKeyFuncs<PortalObsOccMap, false> PortalObsOccMapSpatialAudioIDKeyFuncs;
  155. TMap<AkRoomID, PortalObsOccMap, FDefaultSetAllocator, PortalObsOccMapSpatialAudioIDKeyFuncs> PortalObsOccMapPerRoom;
  156. };