AkGeometryComponent.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #pragma once
  16. #include "Platforms/AkUEPlatform.h"
  17. #include "AkAcousticTexture.h"
  18. #include "WwiseUnrealDefines.h"
  19. #include "Components/SceneComponent.h"
  20. #include "PhysicalMaterials/PhysicalMaterial.h"
  21. #include "AkAcousticTextureSetComponent.h"
  22. #include "AkGeometryData.h"
  23. #include "AkGeometryComponent.generated.h"
  24. class UAkSettings;
  25. #if UE_5_0_OR_LATER
  26. class UMaterialInterface;
  27. #endif
  28. DECLARE_DELEGATE(FOnRefreshDetails);
  29. UENUM()
  30. enum class AkMeshType : uint8
  31. {
  32. StaticMesh,
  33. CollisionMesh UMETA(DisplayName = "Simple Collision")
  34. };
  35. USTRUCT(BlueprintType)
  36. struct FAkGeometrySurfaceOverride
  37. {
  38. GENERATED_BODY()
  39. /** The Acoustic Texture represents the sound absorption on the surface of the geometry when a sound bounces off of it.
  40. * If left to None, the mesh's physical material will be used to fetch an acoustic texture.
  41. */
  42. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry")
  43. UAkAcousticTexture* AcousticTexture = nullptr;
  44. /** Enable Transmission Loss Override */
  45. UPROPERTY(EditAnywhere, BlueprintReadWrite, DisplayName = "Enable Transmission Loss Override", Category = "Geometry")
  46. bool bEnableOcclusionOverride = false;
  47. /** Transmission loss value to set when modeling sound transmission through geometry. Transmission is modeled only when there is no direct line of sight from the emitter to the listener.
  48. * If there is more than one surface between the emitter and the listener, the maximum of each surface's transmission loss value is used. If the emitter and listener are in different rooms, the room's transmission loss value is taken into account.
  49. * Valid range : (0.0, 1.0)
  50. */
  51. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", DisplayName = "Transmission Loss", meta = (EditCondition = "bEnableOcclusionOverride", ClampMin = "0.0", ClampMax = "1.0"))
  52. float OcclusionValue = 1.f;
  53. void SetSurfaceArea(float area) { SurfaceArea = area; }
  54. FAkGeometrySurfaceOverride()
  55. {
  56. AcousticTexture = nullptr;
  57. bEnableOcclusionOverride = false;
  58. OcclusionValue = 1.f;
  59. }
  60. private:
  61. UPROPERTY()
  62. float SurfaceArea = 0.0f;
  63. };
  64. UCLASS(ClassGroup = Audiokinetic, BlueprintType, hidecategories = (Transform, Rendering, Mobility, LOD, Component, Activation, Tags), meta = (BlueprintSpawnableComponent))
  65. class AKAUDIO_API UAkGeometryComponent : public UAkAcousticTextureSetComponent
  66. {
  67. GENERATED_BODY()
  68. public:
  69. UAkGeometryComponent(const class FObjectInitializer& ObjectInitializer);
  70. /** Convert the mesh into a local representation suited for Wwise:
  71. * a set of vertices, triangles, surfaces, acoustic textures and transmission loss values. */
  72. UFUNCTION(BlueprintCallable, Category = "Audiokinetic|AkGeometry")
  73. void ConvertMesh();
  74. /** Add or update a geometry in Spatial Audio by sending the converted mesh, as well as the rest of the AkGeometryParams to Wwise.
  75. * It is necessary to create at least one geometry instance for each geometry set that is to be used for diffraction and reflection simulation. See UpdateGeometry(). */
  76. UFUNCTION(BlueprintCallable, Category = "Audiokinetic|AkGeometry")
  77. void SendGeometry();
  78. /** Add or update an instance of the geometry by sending the transform of this component to Wwise.
  79. * A geometry instance is a unique instance of a geometry set with a specified transform (position, rotation and scale).
  80. * It is necessary to create at least one geometry instance for each geometry set that is to be used for diffraction and reflection simulation. */
  81. UFUNCTION(BlueprintCallable, Category = "Audiokinetic|AkGeometry")
  82. void UpdateGeometry();
  83. /** Remove the geometry and the corresponding instance from Wwise. */
  84. UFUNCTION(BlueprintCallable, Category = "Audiokinetic|AkGeometry")
  85. void RemoveGeometry();
  86. virtual void OnComponentDestroyed(bool bDestroyingHierarchy) override;
  87. UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Geometry")
  88. AkMeshType MeshType = AkMeshType::CollisionMesh;
  89. /** The Static Mesh's LOD to use */
  90. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", meta = (ClampMin = "0.0"))
  91. int LOD = 0;
  92. /** The local distance in Unreal units between two vertices to be welded together.
  93. * Any two vertices closer than this threshold will be treated as the same unique vertex and assigned the same position.
  94. * Increasing this threshold decreases the number of gaps between triangles, resulting in a more continuous mesh and less sound leaking though, as well as eliminating triangles that are too small to be significant.
  95. * Increasing this threshold also helps Spatial Audio's edge-finding algorithm to find more valid diffraction edges.
  96. */
  97. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", meta = (ClampMin = "0.0"))
  98. float WeldingThreshold = .0f;
  99. /** Override the acoustic properties of this mesh per material.*/
  100. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", DisplayName = "Acoustic Properties Override")
  101. TMap<UMaterialInterface*, FAkGeometrySurfaceOverride> StaticMeshSurfaceOverride;
  102. /** Override the acoustic properties of the collision mesh.*/
  103. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", DisplayName = "Acoustic Properties Override")
  104. FAkGeometrySurfaceOverride CollisionMeshSurfaceOverride;
  105. /** Enable or disable geometric diffraction for this mesh. Check this box to have Wwise Spatial Audio generate diffraction edges on the geometry. The diffraction edges will be visible in the Wwise game object viewer when connected to the game. */
  106. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry")
  107. bool bEnableDiffraction = false;
  108. /** Enable or disable geometric diffraction on boundary edges for this Geometry. Boundary edges are edges that are connected to only one triangle. Depending on the specific shape of the geometry, boundary edges may or may not be useful and it is beneficial to reduce the total number of diffraction edges to process. */
  109. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Geometry", meta = (EditCondition = "bEnableDiffraction"))
  110. bool bEnableDiffractionOnBoundaryEdges = false;
  111. /** (Deprecated) Associate this Geometry component with a Room.
  112. * This property is deprecated and will be removed in a future version. We recommend not using it by leaving it set to None.
  113. * Associating a Geometry component with a particular Room limits the scope in which the geometry is accessible. Doing so reduces the search space for ray casting performed by reflection and diffraction calculations.
  114. * When set to None, this geometry has a global scope.
  115. * Note if one or more geometry sets are associated with a room, that room can no longer access geometry that is in the global scope.
  116. */
  117. UPROPERTY(EditAnywhere, AdvancedDisplay, BlueprintReadWrite, Category = "Geometry")
  118. AActor* AssociatedRoom = nullptr;
  119. float GetSurfaceAreaSquaredMeters(const int& surfaceIndex) const;
  120. void UpdateStaticMeshOverride();
  121. #if WITH_EDITORONLY_DATA
  122. void SetOnRefreshDetails(const FOnRefreshDetails& in_delegate) { OnRefreshDetails = in_delegate; }
  123. void ClearOnRefreshDetails() { OnRefreshDetails.Unbind(); }
  124. const FOnRefreshDetails* GetOnRefreshDetails() { return &OnRefreshDetails; }
  125. bool bMeshMaterialChanged = false;
  126. #endif
  127. #if WITH_EDITOR
  128. virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
  129. virtual void PostEditUndo() override;
  130. virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
  131. #endif
  132. virtual void OnRegister() override;
  133. virtual void OnUnregister() override;
  134. virtual void BeginPlay() override;
  135. virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
  136. virtual void OnUpdateTransform(EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport) override;
  137. virtual bool MoveComponentImpl(
  138. const FVector & Delta,
  139. const FQuat & NewRotation,
  140. bool bSweep,
  141. FHitResult * Hit,
  142. EMoveComponentFlags MoveFlags,
  143. ETeleportType Teleport) override;
  144. virtual void Serialize(FArchive& Ar) override;
  145. void GetTexturesAndSurfaceAreas(TArray<FAkAcousticTextureParams>& textures, TArray<float>& surfaceAreas) const override;
  146. /** Indicates whether this component was added dynamically by a sibling room component in order to send geometry to Wwise. */
  147. bool bWasAddedByRoom = false;
  148. private:
  149. UPrimitiveComponent* Parent = nullptr;
  150. void InitializeParent();
  151. void CalculateSurfaceArea(UStaticMeshComponent* StaticMeshComponent);
  152. void ConvertStaticMesh(UStaticMeshComponent* StaticMeshComponent, const UAkSettings* AkSettings);
  153. void ConvertCollisionMesh(UPrimitiveComponent* PrimitiveComponent, const UAkSettings* AkSettings);
  154. void UpdateMeshAndArchetype(UStaticMeshComponent* StaticMeshComponent);
  155. void _UpdateStaticMeshOverride(UStaticMeshComponent* StaticMeshComponent);
  156. UPROPERTY()
  157. FAkGeometryData GeometryData;
  158. UPROPERTY()
  159. TMap<int, double> SurfaceAreas;
  160. TMap<UMaterialInterface*, FAkGeometrySurfaceOverride> PreviousStaticMeshSurfaceOverride;
  161. void BeginPlayInternal();
  162. #if WITH_EDITOR
  163. virtual void HandleObjectsReplaced(const TMap<UObject*, UObject*>& ReplacementMap) override;
  164. bool bRequiresDeferredBeginPlay = false;
  165. void RegisterAllTextureParamCallbacks() override;
  166. bool ContainsTexture(const FGuid& textureID) override;
  167. #endif
  168. #if WITH_EDITORONLY_DATA
  169. FOnRefreshDetails OnRefreshDetails;
  170. FDelegateHandle OnMeshMaterialChangedHandle;
  171. #endif
  172. };