AkSpotReflector.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "AkSpotReflector.h"
  16. #include "AkAudioDevice.h"
  17. #include "AkComponent.h"
  18. #include "AkAuxBus.h"
  19. #include "AkRoomComponent.h"
  20. #include "Engine/Texture2D.h"
  21. #include "Components/BillboardComponent.h"
  22. AAkSpotReflector::WorldToSpotReflectorsMap AAkSpotReflector::sWorldToSpotReflectors;
  23. // Sets default values
  24. AAkSpotReflector::AAkSpotReflector(const FObjectInitializer& ObjectInitializer)
  25. : Super(ObjectInitializer)
  26. , AcousticTexture(NULL)
  27. , DistanceScalingFactor(2.f)
  28. , Level(1.f)
  29. {
  30. static const FName ComponentName = TEXT("SpotReclectorRootComponent");
  31. RootComponent = ObjectInitializer.CreateDefaultSubobject<USceneComponent>(this, ComponentName);
  32. #if WITH_EDITORONLY_DATA
  33. static const FName SpriteComponentName = TEXT("Sprite");
  34. SpriteComponent = CreateEditorOnlyDefaultSubobject<UBillboardComponent>(SpriteComponentName);
  35. if (SpriteComponent)
  36. {
  37. SpriteComponent->SetSprite(LoadObject<UTexture2D>(NULL, TEXT("/Wwise/S_AkSpotReflector.S_AkSpotReflector")));
  38. SpriteComponent->SetRelativeScale3D(FVector(0.5f, 0.5f, 0.5f));
  39. SpriteComponent->SetupAttachment(RootComponent);
  40. }
  41. #endif
  42. // AActor properties
  43. SetHidden(true);
  44. SetCanBeDamaged(false);
  45. }
  46. void AAkSpotReflector::PostInitializeComponents()
  47. {
  48. Super::PostInitializeComponents();
  49. AddToWorld();
  50. }
  51. void AAkSpotReflector::EndPlay(const EEndPlayReason::Type EndPlayReason)
  52. {
  53. Super::EndPlay(EndPlayReason);
  54. RemoveFromWorld();
  55. }
  56. void AAkSpotReflector::AddToWorld()
  57. {
  58. UWorld* world = GetWorld();
  59. if (world)
  60. {
  61. SpotReflectorSet& SRSet = sWorldToSpotReflectors.FindOrAdd(world);
  62. SRSet.Add(this);
  63. }
  64. }
  65. void AAkSpotReflector::RemoveFromWorld()
  66. {
  67. UWorld* world = GetWorld();
  68. if (world)
  69. {
  70. SpotReflectorSet* pSRSet = sWorldToSpotReflectors.Find(world);
  71. if (pSRSet)
  72. {
  73. pSRSet->Remove(this);
  74. if (pSRSet->Num() == 0)
  75. {
  76. sWorldToSpotReflectors.Remove(world);
  77. }
  78. }
  79. }
  80. }
  81. AkImageSourceID AAkSpotReflector::GetImageSourceID() const
  82. {
  83. return (AkImageSourceID)(uint64)this;
  84. }
  85. AkAuxBusID AAkSpotReflector::GetAuxBusID() const
  86. {
  87. if (EarlyReflectionAuxBus || !EarlyReflectionAuxBusName.IsEmpty())
  88. {
  89. return FAkAudioDevice::GetShortID(EarlyReflectionAuxBus, EarlyReflectionAuxBusName);
  90. }
  91. else
  92. {
  93. // No early reflection aux bus is set. The one assigned in the Wwise Authoring Tool will be used instead.
  94. // Skipping call to FAkAudioDevice::GetShortID() to avoid warning.
  95. return AK_INVALID_UNIQUE_ID;
  96. }
  97. }
  98. void AAkSpotReflector::SetImageSource(UAkComponent* AkComponent)
  99. {
  100. FAkAudioDevice* pDev = FAkAudioDevice::Get();
  101. if (!pDev)
  102. return;
  103. const auto& RootTransform = RootComponent->GetComponentTransform();
  104. if (SameRoomOnly)
  105. {
  106. AkRoomID roomID;
  107. if (EnableRoomOverride)
  108. {
  109. if (RoomOverride != nullptr)
  110. {
  111. UAkRoomComponent* room = Cast<UAkRoomComponent>(RoomOverride->GetComponentByClass(UAkRoomComponent::StaticClass()));
  112. if (room != nullptr)
  113. roomID = room->GetRoomID();
  114. }
  115. }
  116. else
  117. {
  118. TArray<UAkRoomComponent*> AkRooms = pDev->FindRoomComponentsAtLocation(RootTransform.GetTranslation(), GetWorld());
  119. if (AkRooms.Num() > 0)
  120. roomID = AkRooms[0]->GetRoomID();
  121. }
  122. if (roomID != AkComponent->GetSpatialAudioRoomID())
  123. return;
  124. }
  125. AkImageSourceSettings sourceInfo = AkImageSourceSettings(
  126. FAkAudioDevice::FVectorToAKVector64(RootTransform.GetTranslation()),
  127. DistanceScalingFactor, Level);
  128. if (AcousticTexture)
  129. {
  130. sourceInfo.SetOneTexture(AcousticTexture->GetShortID());
  131. }
  132. pDev->SetImageSource(this, sourceInfo, GetAuxBusID(), AkComponent);
  133. }
  134. void AAkSpotReflector::UpdateSpotReflectors(UAkComponent* AkComponent)
  135. {
  136. FAkAudioDevice* pDev = FAkAudioDevice::Get();
  137. if (pDev)
  138. {
  139. pDev->ClearImageSources(AK_INVALID_AUX_ID, AkComponent);
  140. if (AkComponent->EnableSpotReflectors)
  141. {
  142. UWorld* world = AkComponent->GetWorld();
  143. if (world)
  144. {
  145. SpotReflectorSet* pSRSet = sWorldToSpotReflectors.Find(world);
  146. if (pSRSet)
  147. {
  148. for (auto It = pSRSet->CreateIterator(); It; ++It)
  149. {
  150. (*It)->SetImageSource(AkComponent);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. const FString AAkSpotReflector::GetSpotReflectorName() const
  158. {
  159. #if WITH_EDITOR
  160. return GetActorLabel();
  161. #else
  162. return GetName();
  163. #endif
  164. }