AkDrawRoomComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. AkDrawRoomComponent.cpp:
  17. =============================================================================*/
  18. #include "AkDrawRoomComponent.h"
  19. #include "PrimitiveSceneProxy.h"
  20. #if WITH_EDITOR
  21. #include "AkRoomComponent.h"
  22. #include "AkSettingsPerUser.h"
  23. #include "AkSpatialAudioDrawUtils.h"
  24. #include "DynamicMeshBuilder.h"
  25. #include "SceneManagement.h"
  26. #endif // WITH_EDITOR
  27. UDrawRoomComponent::UDrawRoomComponent(const FObjectInitializer& ObjectInitializer)
  28. : Super(ObjectInitializer)
  29. {
  30. }
  31. #if WITH_EDITOR
  32. const UAkRoomComponent* UDrawRoomComponent::GetRoomParent() const
  33. {
  34. return Cast<UAkRoomComponent>(GetAttachParent());
  35. }
  36. void UDrawRoomComponent::DrawRoom(const FSceneView* View, FPrimitiveDrawInterface* PDI, FMeshElementCollector& Collector, int32 ViewIndex) const
  37. {
  38. const UAkRoomComponent* RoomComponent = GetRoomParent();
  39. if (IsValid(RoomComponent) && IsValid(RoomComponent->GetPrimitiveParent()) && RoomComponent->bEnable)
  40. {
  41. const UPrimitiveComponent* PrimitiveParent = Cast<UPrimitiveComponent>(RoomComponent->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 RoomColor = AkSpatialAudioColors::GetRoomColor();
  47. FTransform T = PrimitiveParent->GetComponentTransform();
  48. AkDrawBounds DrawBounds(T, BoxExtent);
  49. // Calculate the local room axis vectors
  50. FVector RoomAxisForward = DrawBounds.FRU() - DrawBounds.BRU();
  51. RoomAxisForward.Normalize();
  52. FVector RoomAxisRight = DrawBounds.RU() - DrawBounds.LU();
  53. RoomAxisRight.Normalize();
  54. FVector RoomAxisUp = DrawBounds.RU() - DrawBounds.RD();
  55. RoomAxisUp.Normalize();
  56. // Draw an icon representing the room's orientation.
  57. float Radius = AkDrawConstants::RoomIconRadius;
  58. float IconThickness = AkDrawConstants::RoomIconThickness;
  59. int Sides = AkDrawConstants::RoomIconSides;
  60. FVector RoomCentre = T.TransformPosition(FVector::ZeroVector);
  61. DrawCircle(PDI, RoomCentre, RoomAxisForward, RoomAxisRight, RoomColor, Radius, Sides, SDPG_MAX, IconThickness);
  62. DrawCircle(PDI, RoomCentre, RoomAxisForward, RoomAxisUp, RoomColor, Radius, Sides, SDPG_MAX, IconThickness);
  63. float AxisLength = AkDrawConstants::RoomAxisLength;
  64. PDI->DrawLine(RoomCentre, RoomCentre + RoomAxisForward * AxisLength, RoomColor, SDPG_MAX, AkDrawConstants::RoomAxisThickness);
  65. PDI->DrawLine(RoomCentre, RoomCentre + RoomAxisRight * AxisLength, RoomColor, SDPG_MAX, AkDrawConstants::RoomAxisThickness);
  66. PDI->DrawLine(RoomCentre, RoomCentre + RoomAxisUp * AxisLength, RoomColor, SDPG_MAX, AkDrawConstants::RoomAxisThickness);
  67. }
  68. }
  69. /** Represents a Room to the scene manager.
  70. Based on FDrawFrustumSceneProxy (in DrawFrustrumComponent.cpp)
  71. */
  72. class FDrawRoomSceneProxy final : public FPrimitiveSceneProxy
  73. {
  74. public:
  75. /** Based on FDrawFrustumSceneProxy implementation */
  76. SIZE_T GetTypeHash() const override
  77. {
  78. static size_t UniquePointer;
  79. return reinterpret_cast<size_t>(&UniquePointer);
  80. }
  81. /**
  82. * Initialization constructor.
  83. * @param InComponent - game component to draw in the scene
  84. */
  85. FDrawRoomSceneProxy(const UDrawRoomComponent* InComponent)
  86. : FPrimitiveSceneProxy(InComponent)
  87. , RoomDrawer(InComponent)
  88. {}
  89. // FPrimitiveSceneProxy interface.
  90. virtual void GetDynamicMeshElements(const TArray<const FSceneView*>& Views, const FSceneViewFamily& ViewFamily, uint32 VisibilityMap, FMeshElementCollector& Collector) const override
  91. {
  92. if (GetDefault<UAkSettingsPerUser>()->VisualizeRoomsAndPortals)
  93. {
  94. if (RoomDrawer != nullptr)
  95. {
  96. for (int32 ViewIndex = 0; ViewIndex < Views.Num(); ViewIndex++)
  97. {
  98. if (VisibilityMap & (1 << ViewIndex))
  99. {
  100. FPrimitiveDrawInterface* PDI = Collector.GetPDI(ViewIndex);
  101. const FSceneView* View = Views[ViewIndex];
  102. RoomDrawer->DrawRoom(View, PDI, Collector, ViewIndex);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. virtual FPrimitiveViewRelevance GetViewRelevance(const FSceneView* View) const override
  109. {
  110. FPrimitiveViewRelevance Result;
  111. Result.bDrawRelevance = IsShown(View);
  112. Result.bDynamicRelevance = true;
  113. Result.bStaticRelevance = true;
  114. Result.bEditorNoDepthTestPrimitiveRelevance = true;
  115. return Result;
  116. }
  117. /** Based on FDrawFrustumSceneProxy implementation */
  118. virtual uint32 GetMemoryFootprint(void) const override { return(sizeof(*this) + GetAllocatedSize()); }
  119. uint32 GetAllocatedSize(void) const { return(FPrimitiveSceneProxy::GetAllocatedSize()); }
  120. private:
  121. const UDrawRoomComponent* RoomDrawer;
  122. };
  123. FPrimitiveSceneProxy* UDrawRoomComponent::CreateSceneProxy()
  124. {
  125. return new FDrawRoomSceneProxy(this);
  126. }
  127. FBoxSphereBounds UDrawRoomComponent::CalcBounds(const FTransform& LocalToWorld) const
  128. {
  129. return FBoxSphereBounds(LocalToWorld.TransformPosition(FVector::ZeroVector), FVector(AkDrawConstants::CullDepth), AkDrawConstants::CullDepth);
  130. }
  131. #endif // WITH_EDITOR