AkDrawPortalComponent.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. AkDrawPortalComponent.cpp:
  17. =============================================================================*/
  18. #include "AkDrawPortalComponent.h"
  19. #include "PrimitiveSceneProxy.h"
  20. #if WITH_EDITOR
  21. #include "DynamicMeshBuilder.h"
  22. #include "Engine/World.h"
  23. #include "AkRoomComponent.h"
  24. #include "AkSpatialAudioDrawUtils.h"
  25. #include "AkSettingsPerUser.h"
  26. #endif // WITH_EDITOR
  27. UDrawPortalComponent::UDrawPortalComponent(const FObjectInitializer& ObjectInitializer)
  28. : Super(ObjectInitializer)
  29. {
  30. }
  31. #if WITH_EDITOR
  32. const UAkPortalComponent* UDrawPortalComponent::GetPortalParent() const
  33. {
  34. return Cast<UAkPortalComponent>(GetAttachParent());
  35. }
  36. void UDrawPortalComponent::DrawPortalOutline(const FSceneView* View, FPrimitiveDrawInterface* PDI, FMeshElementCollector& Collector, int32 ViewIndex) const
  37. {
  38. const UAkPortalComponent* PortalComponent = GetPortalParent();
  39. if (IsValid(PortalComponent) && IsValid(PortalComponent->GetPrimitiveParent()))
  40. {
  41. const UPrimitiveComponent* PrimitiveParent = Cast<UPrimitiveComponent>(PortalComponent->GetPrimitiveParent());
  42. if (PrimitiveParent == nullptr)
  43. return;
  44. // Calculate the unscaled, unrotated box extent of the primitive parent component, at origin.
  45. FVector BoxExtent = PrimitiveParent->CalcBounds(FTransform()).BoxExtent;
  46. FLinearColor FrontDrawColor;
  47. FLinearColor BackDrawColor;
  48. AkSpatialAudioColors::GetPortalColors(PortalComponent, FrontDrawColor, BackDrawColor);
  49. FLinearColor OutlineColor = AkSpatialAudioColors::GetPortalOutlineColor(PortalComponent);
  50. float Thickness = AkDrawConstants::PortalOutlineThickness;
  51. FTransform T = PrimitiveParent->GetComponentTransform();
  52. AkDrawBounds DrawBounds(T, BoxExtent);
  53. /** Draw outline of 'front' (positive X) on portal */
  54. PDI->DrawLine(DrawBounds.RU(), DrawBounds.FRU(), FrontDrawColor, SDPG_MAX, Thickness);
  55. PDI->DrawLine(DrawBounds.FRU(), DrawBounds.FRD(), FrontDrawColor, SDPG_MAX, Thickness);
  56. PDI->DrawLine(DrawBounds.FRD(), DrawBounds.RD(), FrontDrawColor, SDPG_MAX, Thickness);
  57. PDI->DrawLine(DrawBounds.LU(), DrawBounds.FLU(), FrontDrawColor, SDPG_MAX, Thickness);
  58. PDI->DrawLine(DrawBounds.FLU(), DrawBounds.FLD(), FrontDrawColor, SDPG_MAX, Thickness);
  59. PDI->DrawLine(DrawBounds.FLD(), DrawBounds.LD(), FrontDrawColor, SDPG_MAX, Thickness);
  60. PDI->DrawLine(DrawBounds.FRU(), DrawBounds.FLU(), FrontDrawColor, SDPG_MAX, Thickness);
  61. PDI->DrawLine(DrawBounds.FRD(), DrawBounds.FLD(), FrontDrawColor, SDPG_MAX, Thickness);
  62. /** Draw outline of 'back' (negative X) on portal */
  63. PDI->DrawLine(DrawBounds.RU(), DrawBounds.BRU(), BackDrawColor, SDPG_MAX, Thickness);
  64. PDI->DrawLine(DrawBounds.BRU(), DrawBounds.BRD(), BackDrawColor, SDPG_MAX, Thickness);
  65. PDI->DrawLine(DrawBounds.BRD(), DrawBounds.RD(), BackDrawColor, SDPG_MAX, Thickness);
  66. PDI->DrawLine(DrawBounds.LU(), DrawBounds.BLU(), BackDrawColor, SDPG_MAX, Thickness);
  67. PDI->DrawLine(DrawBounds.BLU(), DrawBounds.BLD(), BackDrawColor, SDPG_MAX, Thickness);
  68. PDI->DrawLine(DrawBounds.BLD(), DrawBounds.LD(), BackDrawColor, SDPG_MAX, Thickness);
  69. PDI->DrawLine(DrawBounds.BLU(), DrawBounds.BRU(), BackDrawColor, SDPG_MAX, Thickness);
  70. PDI->DrawLine(DrawBounds.BLD(), DrawBounds.BRD(), BackDrawColor, SDPG_MAX, Thickness);
  71. /** Draw outline around centre of portal (YZ plane) */
  72. PDI->DrawLine(DrawBounds.LU(), DrawBounds.LD(), OutlineColor, SDPG_MAX, Thickness);
  73. PDI->DrawLine(DrawBounds.LD(), DrawBounds.RD(), OutlineColor, SDPG_MAX, Thickness);
  74. PDI->DrawLine(DrawBounds.RD(), DrawBounds.RU(), OutlineColor, SDPG_MAX, Thickness);
  75. PDI->DrawLine(DrawBounds.RU(), DrawBounds.LU(), OutlineColor, SDPG_MAX, Thickness);
  76. UWorld* world = GetWorld();
  77. if (world != nullptr)
  78. {
  79. EWorldType::Type worldType = world->WorldType;
  80. if ((!(worldType == EWorldType::Game || worldType == EWorldType::PIE) && PortalComponent->InitialState == AkAcousticPortalState::Closed) ||
  81. ((worldType == EWorldType::Game || worldType == EWorldType::PIE) && PortalComponent->GetCurrentState() == AkAcousticPortalState::Closed))
  82. {
  83. PDI->DrawLine(DrawBounds.FRU(), DrawBounds.BRD(), FrontDrawColor, SDPG_MAX, Thickness);
  84. PDI->DrawLine(DrawBounds.FLD(), DrawBounds.BLU(), BackDrawColor, SDPG_MAX, Thickness);
  85. }
  86. }
  87. Thickness = AkDrawConstants::PortalRoomConnectionThickness;
  88. FVector FrontPoint = FVector(BoxExtent.X, 0.0f, 0.0f);
  89. FVector BackPoint = FVector(-BoxExtent.X, 0.0f, 0.0f);
  90. if (PortalComponent->GetFrontRoomComponent().IsValid() && PortalComponent->GetFrontRoomComponent()->GetPrimitiveParent() != nullptr)
  91. {
  92. // Setup front facing vector to test if line from portal to room points 'backwards' (i.e. if it goes back through the portal). In this case, we extend the 'From' point slightly.
  93. FVector Front = PrimitiveParent->GetComponentTransform().TransformVector(FVector(1.0f, 0.0f, 0.0f));
  94. FVector From = PrimitiveParent->GetComponentTransform().TransformPosition(FrontPoint);
  95. FVector To = PortalComponent->GetFrontRoomComponent()->GetPrimitiveParent()->GetComponentTransform().TransformPosition(FVector(0.0f, 0.0f, 0.0f));
  96. //PDI->DrawLine(From, To, OutlineColor, SDPG_MAX, Thickness);
  97. FVector ToRoom = To - From;
  98. ToRoom.Normalize();
  99. PDI->DrawLine(From, To, OutlineColor, SDPG_MAX, Thickness);
  100. }
  101. Thickness = AkDrawConstants::PortalRoomConnectionThickness;
  102. if (PortalComponent->GetBackRoomComponent().IsValid() && PortalComponent->GetBackRoomComponent()->GetPrimitiveParent() != nullptr)
  103. {
  104. // Setup back facing vector to test if line from portal to room points 'backwards' (i.e. if it goes back through the portal). In this case, we extend the 'From' point slightly
  105. FVector Back = PrimitiveParent->GetComponentTransform().TransformVector(FVector(-1.0f, 0.0f, 0.0f));
  106. FVector From = PrimitiveParent->GetComponentTransform().TransformPosition(BackPoint);
  107. FVector To = PortalComponent->GetBackRoomComponent()->GetPrimitiveParent()->GetComponentTransform().TransformPosition(FVector(0.0f, 0.0f, 0.0f));
  108. //PDI->DrawLine(From, To, OutlineColor, SDPG_MAX, Thickness);
  109. FVector ToRoom = To - From;
  110. ToRoom.Normalize();
  111. PDI->DrawLine(From, To, OutlineColor, SDPG_MAX, Thickness);
  112. }
  113. }
  114. }
  115. /** Represents a portal to the scene manager.
  116. Based on FDrawFrustumSceneProxy (in DrawFrustrumComponent.cpp)
  117. */
  118. class FDrawPortalSceneProxy final : public FPrimitiveSceneProxy
  119. {
  120. public:
  121. /** Based on FDrawFrustumSceneProxy implementation */
  122. SIZE_T GetTypeHash() const override
  123. {
  124. static size_t UniquePointer;
  125. return reinterpret_cast<size_t>(&UniquePointer);
  126. }
  127. /**
  128. * Initialization constructor.
  129. * @param InComponent - game component to draw in the scene
  130. */
  131. FDrawPortalSceneProxy(const UDrawPortalComponent* InComponent)
  132. : FPrimitiveSceneProxy(InComponent)
  133. , PortalDrawer(InComponent)
  134. {}
  135. // FPrimitiveSceneProxy interface.
  136. virtual void GetDynamicMeshElements(const TArray<const FSceneView*>& Views, const FSceneViewFamily& ViewFamily, uint32 VisibilityMap, FMeshElementCollector& Collector) const override
  137. {
  138. if (GetDefault<UAkSettingsPerUser>()->VisualizeRoomsAndPortals)
  139. {
  140. if (PortalDrawer != nullptr)
  141. {
  142. for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++)
  143. {
  144. if (VisibilityMap & (1 << ViewIndex))
  145. {
  146. FPrimitiveDrawInterface* PDI = Collector.GetPDI(ViewIndex);
  147. const FSceneView* View = Views[ViewIndex];
  148. PortalDrawer->DrawPortalOutline(View, PDI, Collector, ViewIndex);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. virtual FPrimitiveViewRelevance GetViewRelevance(const FSceneView* View) const override
  155. {
  156. FPrimitiveViewRelevance Result;
  157. Result.bDrawRelevance = IsShown(View);
  158. Result.bDynamicRelevance = true;
  159. Result.bStaticRelevance = true;
  160. Result.bEditorNoDepthTestPrimitiveRelevance = true;
  161. return Result;
  162. }
  163. /** Based on FDrawFrustumSceneProxy implementation */
  164. virtual uint32 GetMemoryFootprint(void) const override { return(sizeof(*this) + GetAllocatedSize()); }
  165. uint32 GetAllocatedSize(void) const { return(FPrimitiveSceneProxy::GetAllocatedSize()); }
  166. private:
  167. const UDrawPortalComponent* PortalDrawer;
  168. };
  169. FPrimitiveSceneProxy* UDrawPortalComponent::CreateSceneProxy()
  170. {
  171. return new FDrawPortalSceneProxy(this);
  172. }
  173. FBoxSphereBounds UDrawPortalComponent::CalcBounds(const FTransform& LocalToWorld) const
  174. {
  175. return FBoxSphereBounds(LocalToWorld.TransformPosition(FVector::ZeroVector), FVector(AkDrawConstants::CullDepth), AkDrawConstants::CullDepth);
  176. }
  177. #endif // WITH_EDITOR