AkDrawPortalComponent.cpp 9.3 KB

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