AkSurfaceReflectorSetComponentVisualizer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. AkWwiseAcousticsComponentVisualizer.cpp:
  17. =============================================================================*/
  18. #include "AkSurfaceReflectorSetComponentVisualizer.h"
  19. #include "AkAudioDevice.h"
  20. #include "AkSurfaceReflectorSetComponent.h"
  21. #include "AkSpatialAudioVolume.h"
  22. #include "WwiseUEFeatures.h"
  23. #include "Editor.h"
  24. #include "EditorModeManager.h"
  25. #include "EditorModes.h"
  26. #include "Components/BrushComponent.h"
  27. #include "Components/TextRenderComponent.h"
  28. #include "Model.h"
  29. #include "Engine/Polys.h"
  30. #include "EngineGlobals.h"
  31. #include "DynamicMeshBuilder.h"
  32. #include "Materials/Material.h"
  33. #include "Editor.h"
  34. #include "LevelEditorViewport.h"
  35. #include "AkSpatialAudioDrawUtils.h"
  36. #include "Components/BrushComponent.h"
  37. #include "Engine/Canvas.h"
  38. #include <cmath>
  39. #include "SceneView.h"
  40. #define AK_VISUALIZE_HIT_MATERIALS 0
  41. void FAkSurfaceReflectorSetComponentVisualizer::DrawVisualization(const UActorComponent* Component, const FSceneView* View, FPrimitiveDrawInterface* PDI)
  42. {
  43. if (Component == nullptr)
  44. {
  45. return;
  46. }
  47. const UAkSurfaceReflectorSetComponent* SurfaceReflectorSet = Cast<const UAkSurfaceReflectorSetComponent>(Component);
  48. const AAkSpatialAudioVolume* SpatialAudioVolume = Cast<const AAkSpatialAudioVolume>(Component->GetOwner());
  49. if (SpatialAudioVolume == nullptr)
  50. return;
  51. auto ModelBrush = SurfaceReflectorSet->ParentBrush;
  52. if (SurfaceReflectorSet != nullptr &&
  53. ModelBrush != nullptr &&
  54. !SpatialAudioVolume->IsHiddenEd())
  55. {
  56. // Build a mesh by basically drawing the triangles of each
  57. for (int32 NodeIdx = 0;
  58. NodeIdx < ModelBrush->Nodes.Num() && NodeIdx < SurfaceReflectorSet->TextVisualizers.Num();
  59. ++NodeIdx)
  60. {
  61. FDynamicMeshBuilder MeshBuilder(ERHIFeatureLevel::Type::ES3_1);
  62. int32 VertStartIndex = SurfaceReflectorSet->ParentBrush->Nodes[NodeIdx].iVertPool;
  63. FUnrealFloatVector normal(ModelBrush->Nodes[NodeIdx].Plane);
  64. if (SurfaceReflectorSet->AcousticPolys.Num() > NodeIdx)
  65. {
  66. if (SurfaceReflectorSet->AcousticPolys[NodeIdx].EnableSurface &&
  67. ModelBrush->Nodes[NodeIdx].NumVertices > 2)
  68. {
  69. // Shift the vertices in towards the center (negatively along the normal) so that they don't obstruct face selection in geometry mode / brush editing mode.
  70. FUnrealFloatVector offset = normal * 0.1f;
  71. const FVert& Vert0 = ModelBrush->Verts[VertStartIndex + 0];
  72. const FVert& Vert1 = ModelBrush->Verts[VertStartIndex + 1];
  73. const FVert& Vert2 = ModelBrush->Verts[VertStartIndex + 2];
  74. FUnrealFloatVector Vertex0 = ModelBrush->Points[Vert0.pVertex] - offset;
  75. FUnrealFloatVector Vertex1 = ModelBrush->Points[Vert1.pVertex] - offset;
  76. FUnrealFloatVector Vertex2 = ModelBrush->Points[Vert2.pVertex] - offset;
  77. MeshBuilder.AddVertex(Vertex0, FUnrealFloatVector2D::ZeroVector, FUnrealFloatVector(1, 0, 0), FUnrealFloatVector(0, 1, 0), FUnrealFloatVector(0, 0, 1), FColor::White);
  78. MeshBuilder.AddVertex(Vertex1, FUnrealFloatVector2D::ZeroVector, FUnrealFloatVector(1, 0, 0), FUnrealFloatVector(0, 1, 0), FUnrealFloatVector(0, 0, 1), FColor::White);
  79. MeshBuilder.AddVertex(Vertex2, FUnrealFloatVector2D::ZeroVector, FUnrealFloatVector(1, 0, 0), FUnrealFloatVector(0, 1, 0), FUnrealFloatVector(0, 0, 1), FColor::White);
  80. MeshBuilder.AddTriangle(0, 2, 1);
  81. MeshBuilder.AddTriangle(1, 2, 0);
  82. for (int32 VertexIdx = 3; VertexIdx < ModelBrush->Nodes[NodeIdx].NumVertices; ++VertexIdx)
  83. {
  84. const FVert& Vert3 = ModelBrush->Verts[VertStartIndex + VertexIdx];
  85. FUnrealFloatVector Vertex3 = ModelBrush->Points[Vert3.pVertex] - offset;
  86. MeshBuilder.AddVertex(Vertex3, FUnrealFloatVector2D::ZeroVector, FUnrealFloatVector(1, 0, 0), FUnrealFloatVector(0, 1, 0), FUnrealFloatVector(0, 0, 1), FColor::White);
  87. MeshBuilder.AddTriangle(0, VertexIdx, VertexIdx - 1);
  88. MeshBuilder.AddTriangle(VertexIdx - 1, VertexIdx, 0);
  89. }
  90. FLinearColor SurfaceColor = AkSpatialAudioColors::GetSurfaceReflectorColor(SurfaceReflectorSet, NodeIdx, SpatialAudioVolume->IsDragging);
  91. auto* renderProxy = GEngine->GeomMaterial->GetRenderProxy();
  92. // Limit the color's value (in HSV space) so that it doesn't obscure the text.
  93. // In the new color, R = H, G = S, B = V, A = A (From Color.cpp line ~271)
  94. FLinearColor hsv = SurfaceColor.LinearRGBToHSV();
  95. hsv.B = FMath::Min(0.25f, hsv.B);
  96. SurfaceColor = hsv.HSVToLinearRGB();
  97. FDynamicColoredMaterialRenderProxy* MatProxy = new FDynamicColoredMaterialRenderProxy(renderProxy, SurfaceColor);
  98. PDI->RegisterDynamicResource(MatProxy);
  99. MeshBuilder.Draw(PDI, SpatialAudioVolume->ActorToWorld().ToMatrixWithScale(), MatProxy, SDPG_World, true);
  100. }
  101. }
  102. }
  103. SurfaceReflectorSet->UpdateTextPositions();
  104. if (!GLevelEditorModeTools().IsModeActive(FEditorModeID(TEXT("EM_Geometry"))))
  105. {
  106. for (auto& KV : SurfaceReflectorSet->EdgeMap)
  107. {
  108. auto& EdgeInfo = KV.Value;
  109. if (!EdgeInfo.IsFlat && (!EdgeInfo.IsBoundary || SurfaceReflectorSet->bEnableDiffractionOnBoundaryEdges))
  110. {
  111. FLinearColor EdgeColor = AkSpatialAudioColors::GetSpatialAudioVolumeOutlineColor();
  112. float Thickness = SurfaceReflectorSet->bEnableSurfaceReflectors ? AkDrawConstants::SpatialAudioVolumeOutlineThickness : AkDrawConstants::RoomOutlineThickness;
  113. if (EdgeInfo.IsEnabled &&
  114. SurfaceReflectorSet->bEnableSurfaceReflectors &&
  115. SurfaceReflectorSet->bEnableDiffraction)
  116. {
  117. EdgeColor = EdgeInfo.IsBoundary ? AkSpatialAudioColors::GetBoundaryDiffractionEdgeColor() : AkSpatialAudioColors::GetDiffractionEdgeColor();
  118. Thickness = AkDrawConstants::DiffractionEdgeThickness;
  119. }
  120. PDI->DrawLine(
  121. SpatialAudioVolume->GetActorTransform().TransformPosition(EdgeInfo.V0()),
  122. SpatialAudioVolume->GetActorTransform().TransformPosition(EdgeInfo.V1()),
  123. EdgeColor, SDPG_World, Thickness);
  124. }
  125. }
  126. }
  127. }
  128. if (GEditor->GetSelectedActorCount() == 1 &&
  129. (!SpatialAudioVolume->IsHiddenEd() || SpatialAudioVolume->IsDragging) &&
  130. SpatialAudioVolume->FitToGeometry)
  131. {
  132. for (int i = 0; i < SpatialAudioVolume->GetRaycastHits().Num(); ++i)
  133. {
  134. int Threshold = (int)(SpatialAudioVolume->GetFitScale() * SpatialAudioVolume->GetRaycastHits().Num());
  135. FColor col = i <= Threshold ? FColor::Green : FColor::Red;
  136. PDI->DrawLine(SpatialAudioVolume->FitPoints[i], SpatialAudioVolume->FitPoints[i] + SpatialAudioVolume->FitNormals[i] * 10.f, col, 100);
  137. PDI->DrawPoint(SpatialAudioVolume->GetRaycastHits()[i], col, 5.f, 100);
  138. }
  139. if (SpatialAudioVolume->IsDragging)
  140. {
  141. for (auto& EdgePair : SpatialAudioVolume->PreviewOutline)
  142. {
  143. PDI->DrawLine(EdgePair.Key, EdgePair.Value, AkSpatialAudioColors::GetSpatialAudioVolumeOutlineColor() * 1.35f, SDPG_Foreground, 7.0f);
  144. }
  145. }
  146. if (SpatialAudioVolume->FitFailed &&
  147. !SpatialAudioVolume->IsDragging &&
  148. SpatialAudioVolume->GetBrushComponent())
  149. {
  150. static const float kFlashPeriodSeconds = 0.3f;
  151. static const float kFlashDutyCycle = 0.5f;
  152. static const float kFlashDurationSeconds = 0.6f;
  153. if (FlashTimer == FLT_MAX)
  154. FlashTimer = 0.f;
  155. if (fmod(FlashTimer, kFlashPeriodSeconds) < kFlashDutyCycle*kFlashPeriodSeconds &&
  156. FlashTimer < kFlashDurationSeconds)
  157. {
  158. UModel* Brush = SpatialAudioVolume->GetBrushComponent()->Brush;
  159. if (Brush)
  160. {
  161. for (int32 NodeIdx = 0; NodeIdx < Brush->Nodes.Num(); ++NodeIdx)
  162. {
  163. int32 VertStartIndex = Brush->Nodes[NodeIdx].iVertPool;
  164. int32 NumVerts = Brush->Nodes[NodeIdx].NumVertices;
  165. for (int32 j = NumVerts - 1, i = 0; i < NumVerts; j = i, ++i)
  166. {
  167. auto V0 = Brush->Points[Brush->Verts[VertStartIndex + i].pVertex];
  168. auto V1 = Brush->Points[Brush->Verts[VertStartIndex + j].pVertex];
  169. FVector WorldV0 = SpatialAudioVolume->GetActorTransform().TransformPosition(FVector(V0));
  170. FVector WorldV1 = SpatialAudioVolume->GetActorTransform().TransformPosition(FVector(V1));
  171. PDI->DrawLine(WorldV0, WorldV1, AkSpatialAudioColors::GetBadFitSpatialAudioVolumeOutlineColor(), SDPG_Foreground, 7.0f);
  172. }
  173. }
  174. }
  175. }
  176. #if UE_5_0_OR_LATER
  177. FlashTimer += View->Family->Time.GetDeltaWorldTimeSeconds();
  178. #else
  179. FlashTimer += View->Family->DeltaWorldTime;
  180. #endif
  181. }
  182. else
  183. {
  184. FlashTimer = FLT_MAX;
  185. }
  186. }
  187. }
  188. void FAkSurfaceReflectorSetComponentVisualizer::DrawVisualizationHUD(const UActorComponent* Component, const FViewport* Viewport, const FSceneView* View, FCanvas* Canvas)
  189. {
  190. if (GLevelEditorModeTools().IsModeActive(FEditorModeID(TEXT("EM_Geometry"))) || Component == nullptr)
  191. {
  192. return;
  193. }
  194. const AAkSpatialAudioVolume* SpatialAudioVolume = Cast<const AAkSpatialAudioVolume>(Component->GetOwner());
  195. if (SpatialAudioVolume == nullptr)
  196. return;
  197. if (SpatialAudioVolume->FitToGeometry &&
  198. SpatialAudioVolume->FitFailed &&
  199. !SpatialAudioVolume->IsDragging)
  200. {
  201. static FText Text = FText::FromString("Fit to geometry failed. Try moving Spatial Audio Volume to a new location.");
  202. FCanvasTextItem TextItem(FVector2D::ZeroVector, Text, GEngine->GetSmallFont(), AkSpatialAudioColors::GetBadFitSpatialAudioVolumeOutlineColor());
  203. float YPos = (Canvas->GetViewRect().Height() / Canvas->GetDPIScale()) - GEngine->GetSmallFont()->GetMaxCharHeight();
  204. Canvas->DrawItem(TextItem, 0, YPos);
  205. }
  206. #if AK_VISUALIZE_HIT_MATERIALS
  207. if (SpatialAudioVolume->FitToGeometry)
  208. {
  209. for (int i = 0; i < SpatialAudioVolume->GetRaycastHits().Num()*SpatialAudioVolume->GetFitScale(); ++i)
  210. {
  211. FVector Location = SpatialAudioVolume->GetRaycastHits()[i];
  212. FPlane Proj = View->Project(Location);
  213. if (Proj.W > 0.f)
  214. {
  215. float Height = Canvas->GetViewRect().Height() / Canvas->GetDPIScale();
  216. float Width = Canvas->GetViewRect().Width() / Canvas->GetDPIScale();
  217. float X = (Width / 2.f) + (Proj.X * (Width / 2.f));
  218. float Y = (Height / 2.f) + (-Proj.Y * (Height / 2.f));
  219. FCanvasTextItem TextItem(FVector2D::ZeroVector, FText::FromString(SpatialAudioVolume->FitMaterials[i]->GetName()), GEngine->GetTinyFont(), FColor::Green);
  220. Canvas->DrawItem(TextItem, X, Y);
  221. }
  222. }
  223. }
  224. #endif
  225. }