Added Spatial Audio

This commit is contained in:
SHAM-DP 2023-02-27 22:02:48 +08:00
parent 725da6254d
commit 50d7ad80a2
5 changed files with 60 additions and 17 deletions

View File

@ -16,6 +16,7 @@
#include <FMOD/fmod.hpp> #include <FMOD/fmod.hpp>
#include <FMOD/fmod_studio.hpp> #include <FMOD/fmod_studio.hpp>
#include <SDL_keyboard.h> #include <SDL_keyboard.h>
#include "Camera/SHCameraSystem.h"
const std::string AUDIO_FOLDER_PATH{ std::string(ASSET_ROOT)+ "/Audio/" }; const std::string AUDIO_FOLDER_PATH{ std::string(ASSET_ROOT)+ "/Audio/" };
@ -53,8 +54,9 @@ namespace SHADE
denseListener = &SHComponentManager::GetDense<SHAudioListenerComponent>(); denseListener = &SHComponentManager::GetDense<SHAudioListenerComponent>();
fmodStudioSystem->getCoreSystem(&fmodSystem); fmodStudioSystem->getCoreSystem(&fmodSystem);
fmodSystem->setSoftwareFormat(0, FMOD_SPEAKERMODE_5POINT1, 0);
result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL, extraDriverData); result = fmodStudioSystem->initialize(AUDIO_SYS_MAX_CHANNELS, AUDIO_SYS_MAX_CHANNELS, FMOD_STUDIO_INIT_NORMAL | FMOD_INIT_NORMAL, extraDriverData);
ErrorCheck(); ErrorCheck();
fmodSystem->setSoftwareFormat(0, speakerMode, 0); fmodSystem->setSoftwareFormat(0, speakerMode, 0);
@ -119,18 +121,44 @@ namespace SHADE
// PlayEventOnce("event:/Characters/sfx_footsteps_raccoon"); // PlayEventOnce("event:/Characters/sfx_footsteps_raccoon");
fmodStudioSystem->update(); fmodStudioSystem->update();
if (!denseListener->empty()) //int listenerID = 0;
//for(auto& listener : *denseListener)
//{
// if(!listener.isActive)
// continue;
// //SHAudioListenerComponent& listener = denseListener->at(0); //Loop through dense
// SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(listener.GetEID());
// if (listenerTransform)
// {
// listener.SetPos(listenerTransform->GetWorldPosition());
// SHQuaternion worldOrientation = listenerTransform->GetWorldOrientation();
// SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
// SHVec3 orientatedUp = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Up) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
// listener.SetForward(orientatedFoward);
// listener.SetUp(orientatedUp);
// FMOD_3D_ATTRIBUTES attribs{ { 0 } };
// attribs.position = { listener.pos.x, listener.pos.y, listener.pos.z };
// attribs.forward = { orientatedFoward.x, orientatedFoward.y, orientatedFoward.z };
// attribs.up = { orientatedUp.x, orientatedUp.y, orientatedUp.z };
//
// fmodStudioSystem->setListenerAttributes(listenerID++, &attribs);
// }
//}
if(auto camSystem = SHSystemManager::GetSystem<SHCameraSystem>())
{ {
SHAudioListenerComponent& listener = denseListener->at(0); auto mainCamEID = camSystem->GetMainCameraEID();
SHTransformComponent* listenerTransform = SHComponentManager::GetComponent_s<SHTransformComponent>(listener.GetEID()); if(auto camComponent = SHComponentManager::GetComponent_s<SHCameraComponent>(mainCamEID))
if (listenerTransform)
{ {
listener.SetPos(listenerTransform->GetWorldPosition()); // TODO: Clean up listener FMOD_3D_ATTRIBUTES attribs{ { 0 } };
listener.SetForward({ (listenerTransform->GetLocalScale()[0] > 0.f) ? 1.f : -1.f, 0.f, 0.f }); //TODO: USE CORRECT FORWARD SHVec3 pos = camComponent->GetPosition();
FMOD_VECTOR pos = { listener.pos[0] ,listener.pos[1] ,0.f }; SHVec3 forward, up, right;
FMOD_VECTOR forward = { listener.forward[0] ,listener.forward[1] ,listener.forward[2] }; camSystem->GetCameraAxis(*camComponent, forward, right, up);
FMOD_VECTOR up = { listener.up[0] ,listener.up[1] ,listener.up[2] }; attribs.position = { pos.x, pos.y, pos.z };
fmodSystem->set3DListenerAttributes(0, &pos, nullptr, &forward, &up); attribs.forward = { forward.x, forward.y, forward.z };
attribs.up = { up.x, up.y, up.z };
fmodStudioSystem->setListenerAttributes(0, &attribs);
} }
} }
@ -141,9 +169,15 @@ namespace SHADE
{ {
if(SHTransformComponent* transformComponent = SHComponentManager::GetComponent_s<SHTransformComponent>(it->transformRef)) if(SHTransformComponent* transformComponent = SHComponentManager::GetComponent_s<SHTransformComponent>(it->transformRef))
{ {
FMOD_3D_ATTRIBUTES attribs{}; //TODO: Set other attribs FMOD_3D_ATTRIBUTES attribs{ { 0 } }; //TODO: Set other attribs
auto pos = transformComponent->GetWorldPosition(); auto pos = transformComponent->GetWorldPosition();
SHQuaternion worldOrientation = transformComponent->GetWorldOrientation();
SHVec3 orientatedFoward = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Forward) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
SHVec3 orientatedUp = (worldOrientation * SHQuaternion::FromEuler(SHVec3::Up) * SHQuaternion::Conjugate(worldOrientation)).ToEuler();
attribs.position = {pos.x, pos.y, pos.z}; attribs.position = {pos.x, pos.y, pos.z};
attribs.forward = {orientatedFoward.x, orientatedFoward.y, orientatedFoward.z};
attribs.up = {orientatedUp.x, orientatedUp.y, orientatedUp.z};
it->instance->set3DAttributes(&attribs); it->instance->set3DAttributes(&attribs);
} }
} }

View File

@ -22,7 +22,7 @@ namespace SHADE
typedef FMOD::Studio::Bank* SHBank; typedef FMOD::Studio::Bank* SHBank;
class SHAudioListenerComponent; class SHAudioListenerComponent;
class SH_API AudioClip class SH_API AudioClip
{ {
public: public:

View File

@ -12,4 +12,5 @@
#include "Graphics/MiddleEnd/Lights/SHLightComponent.h" #include "Graphics/MiddleEnd/Lights/SHLightComponent.h"
#include "Graphics/MiddleEnd/Interface/SHRenderable.h" #include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Physics/Interface/SHColliderComponent.h" #include "Physics/Interface/SHColliderComponent.h"
#include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h" #include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h"
#include "AudioSystem/SHAudioListenerComponent.h"

View File

@ -171,10 +171,15 @@ namespace SHADE
if (auto toggleButton = SHComponentManager::GetComponent_s<SHToggleButtonComponent>(eid)) if (auto toggleButton = SHComponentManager::GetComponent_s<SHToggleButtonComponent>(eid))
{ {
DrawComponent(toggleButton); DrawComponent(toggleButton);
}if (auto slider = SHComponentManager::GetComponent_s<SHSliderComponent>(eid)) }
if (auto slider = SHComponentManager::GetComponent_s<SHSliderComponent>(eid))
{ {
DrawComponent(slider); DrawComponent(slider);
} }
if(auto listenerComponent = SHComponentManager::GetComponent_s<SHAudioListenerComponent>(eid))
{
DrawComponent(listenerComponent);
}
ImGui::Separator(); ImGui::Separator();
// Render Scripts // Render Scripts
SHScriptEngine* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>()); SHScriptEngine* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
@ -198,7 +203,7 @@ namespace SHADE
DrawAddComponentWithEnforcedComponentButton<SHColliderComponent, SHTransformComponent>(eid); DrawAddComponentWithEnforcedComponentButton<SHColliderComponent, SHTransformComponent>(eid);
DrawAddComponentWithEnforcedComponentButton<SHTextRenderableComponent, SHTransformComponent>(eid); DrawAddComponentWithEnforcedComponentButton<SHTextRenderableComponent, SHTransformComponent>(eid);
DrawAddComponentWithEnforcedComponentButton<SHAnimatorComponent, SHTransformComponent, SHRenderable>(eid); DrawAddComponentWithEnforcedComponentButton<SHAnimatorComponent, SHTransformComponent, SHRenderable>(eid);
DrawAddComponentWithEnforcedComponentButton<SHAudioListenerComponent, SHTransformComponent>(eid);
ImGui::EndMenu(); ImGui::EndMenu();
} }

View File

@ -246,6 +246,7 @@ namespace SHADE
AddComponentToComponentNode<SHTextRenderableComponent>(components, eid); AddComponentToComponentNode<SHTextRenderableComponent>(components, eid);
AddComponentToComponentNode<SHAnimatorComponent>(components, eid); AddComponentToComponentNode<SHAnimatorComponent>(components, eid);
AddComponentToComponentNode<SHUIComponent>(components, eid); AddComponentToComponentNode<SHUIComponent>(components, eid);
AddComponentToComponentNode<SHAudioListenerComponent>(components, eid);
node[ComponentsNode] = components; node[ComponentsNode] = components;
@ -306,6 +307,7 @@ namespace SHADE
AddComponentID<SHTextRenderableComponent>(componentIDList, componentsNode); AddComponentID<SHTextRenderableComponent>(componentIDList, componentsNode);
AddComponentID<SHAnimatorComponent>(componentIDList, componentsNode); AddComponentID<SHAnimatorComponent>(componentIDList, componentsNode);
AddComponentID<SHUIComponent>(componentIDList, componentsNode); AddComponentID<SHUIComponent>(componentIDList, componentsNode);
AddComponentID<SHAudioListenerComponent>(componentIDList, componentsNode);
return componentIDList; return componentIDList;
} }
@ -392,5 +394,6 @@ namespace SHADE
SHSerializationHelper::InitializeComponentFromNode<SHLightComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHLightComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHAnimatorComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHAnimatorComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHUIComponent>(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode<SHUIComponent>(componentsNode, eid);
SHSerializationHelper::InitializeComponentFromNode<SHAudioListenerComponent>(componentsNode, eid);
} }
} }