1f9b284de2
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com> # Conflicts: # Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h # Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h # Code/Editor/Plugins/EditorCommon/stdafx.cpp # Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h # Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h # Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h # Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h # Code/Tools/AssetProcessor/native/precompiled.h # Code/Tools/Standalone/StandaloneTools_precompiled.h # Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h # Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h # Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h # Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h # Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h # Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake # Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h # Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h # Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h # Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp # Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake # Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h # Gems/Blast/Code/Source/StdAfx.cpp # Gems/Camera/Code/Source/Camera_precompiled.h # Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h # Gems/FastNoise/Code/Source/FastNoise_precompiled.h # Gems/Gestures/Code/Source/Gestures_precompiled.h # Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h # Gems/GraphCanvas/Code/precompiled.h # Gems/ImGui/Code/Source/ImGui_precompiled.h # Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h # Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h # Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp # Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h # Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h # Gems/LyShine/Code/Source/LyShine_precompiled.h # Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h # Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h # Gems/Maestro/Code/Source/Maestro_precompiled.h # Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h # Gems/Metastream/Code/Source/Metastream_precompiled.h # Gems/Microphone/Code/Source/Microphone_precompiled.h # Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h # Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h # Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h # Gems/PhysX/Code/Source/PhysX_precompiled.h # Gems/PhysX/Code/physx_unsupported_files.cmake # Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h # Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h # Gems/ScriptCanvas/Code/Editor/precompiled.h # Gems/ScriptCanvas/Code/Source/precompiled.h # Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h # Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h # Gems/ScriptEvents/Code/Source/precompiled.h # Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp # Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h # Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h # Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h # Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h # Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h # Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h # Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h # Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h # Gems/Twitch/Code/Source/Twitch_precompiled.h # Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h # Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
633 lines
25 KiB
C++
633 lines
25 KiB
C++
/*
|
|
* Copyright (c) Contributors to the Open 3D Engine Project.
|
|
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
*
|
|
*/
|
|
|
|
#include <AzCore/PlatformDef.h>
|
|
|
|
#include <AzCore/Component/TransformBus.h>
|
|
#include <AzCore/RTTI/BehaviorContext.h>
|
|
#include <AzCore/Serialization/SerializeContext.h>
|
|
#include <AzCore/Serialization/EditContext.h>
|
|
|
|
#include <Integration/Components/AnimAudioComponent.h>
|
|
|
|
#include <LmbrCentral/Audio/AudioProxyComponentBus.h>
|
|
#include <LmbrCentral/Animation/SkeletalHierarchyRequestBus.h>
|
|
|
|
#include <MathConversion.h>
|
|
|
|
|
|
using namespace LmbrCentral;
|
|
|
|
namespace EMotionFX
|
|
{
|
|
namespace Integration
|
|
{
|
|
void AudioTriggerEvent::Reflect(AZ::ReflectContext* context)
|
|
{
|
|
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
|
|
{
|
|
serializeContext->Class<AudioTriggerEvent>()
|
|
->Version(0)
|
|
->Field("event", &AudioTriggerEvent::m_eventName)
|
|
->Field("trigger", &AudioTriggerEvent::m_triggerName)
|
|
->Field("joint", &AudioTriggerEvent::m_jointName);
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::AddTriggerEvent(const AZStd::string& eventName, const AZStd::string& triggerName, const AZStd::string& jointName)
|
|
{
|
|
AZ::Entity* entity = GetEntity();
|
|
AZ_Assert(entity, "Component must be added to entity prior to adding an audio trigger event.");
|
|
if (entity->GetState() == AZ::Entity::State::Active)
|
|
{
|
|
AddTriggerEventInternal(eventName, triggerName, jointName);
|
|
}
|
|
else
|
|
{
|
|
m_eventsToAdd.emplace_back(eventName, triggerName, jointName);
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::ClearTriggerEvents()
|
|
{
|
|
m_eventsToAdd.clear();
|
|
m_eventsToRemove.clear();
|
|
m_eventTriggerMap.clear();
|
|
}
|
|
|
|
void AnimAudioComponent::RemoveTriggerEvent(const AZStd::string& eventName)
|
|
{
|
|
const AZ::Crc32 eventCrc(eventName.c_str());
|
|
|
|
const AZ::Entity* entity = GetEntity();
|
|
AZ_Assert(entity, "Component must be added to entity prior to removing an audio trigger event.");
|
|
if (entity->GetState() == AZ::Entity::State::Active)
|
|
{
|
|
RemoveTriggerEventInternal(eventCrc);
|
|
}
|
|
else
|
|
{
|
|
m_eventsToRemove.push_back(eventCrc);
|
|
}
|
|
}
|
|
|
|
bool AnimAudioComponent::ExecuteSourceTrigger(
|
|
const Audio::TAudioControlID triggerID,
|
|
const Audio::SAudioCallBackInfos& callbackInfo,
|
|
const Audio::TAudioControlID& sourceID,
|
|
const AZStd::string& jointName)
|
|
{
|
|
if (triggerID == INVALID_AUDIO_CONTROL_ID)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool success = false;
|
|
AZ::s32 jointId = -1;
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName.c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName.empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'ExecuteSourceTrigger' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::EventResult(success, GetEntityId(), &AudioProxyComponentRequests::ExecuteSourceTrigger, triggerID, callbackInfo, sourceID);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'ExecuteSourceTrigger' call not performed on joint '%s'", jointName.c_str());
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->ExecuteSourceTrigger(triggerID, sourceID, callbackInfo);
|
|
success = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
bool AnimAudioComponent::ExecuteTrigger(
|
|
const Audio::TAudioControlID triggerID,
|
|
const Audio::SAudioCallBackInfos& callbackInfo,
|
|
const AZStd::string& jointName)
|
|
{
|
|
if (triggerID == INVALID_AUDIO_CONTROL_ID)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
bool success = false;
|
|
AZ::s32 jointId = -1;
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName.c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName.empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'ExecuteTrigger' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::EventResult(success, GetEntityId(), &AudioProxyComponentRequests::ExecuteTrigger, triggerID, callbackInfo);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'ExecuteTrigger' call not performed on joint '%s'", jointName.c_str());
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->ExecuteTrigger(triggerID, callbackInfo);
|
|
success = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
void AnimAudioComponent::KillTrigger(const Audio::TAudioControlID triggerId, const AZStd::string* jointName)
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (jointName)
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName->c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName->empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'KillTrigger' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::KillTrigger, triggerId);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'KillTrigger' call not performed on joint '%s'", jointName->c_str());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (!jointName || iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->StopTrigger(triggerId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::KillAllTriggers(const AZStd::string* jointName)
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (jointName)
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName->c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName->empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'KillAllTrigger' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::KillAllTriggers);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'KillAllTrigger' call not performed on joint '%s'", jointName->c_str());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (!jointName || iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->StopAllTriggers();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::SetRtpcValue(const Audio::TAudioControlID rtpcID, float value, const AZStd::string* jointName)
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (jointName)
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName->c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName->empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'SetRtpcValue' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::SetRtpcValue, rtpcID, value);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'SetRtpcValue' call not performed on joint '%s'", jointName->c_str());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (!jointName || iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->SetRtpcValue(rtpcID, value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::SetSwitchState(const Audio::TAudioControlID switchID, const Audio::TAudioSwitchStateID stateID, const AZStd::string* jointName)
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (jointName)
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName->c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName->empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'SetSwitchState' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::SetSwitchState, switchID, stateID);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'SetSwitchState' call not performed on joint '%s'", jointName->c_str());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (!jointName || iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->SetSwitchState(switchID, stateID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::SetEnvironmentAmount(const Audio::TAudioEnvironmentID environmentID, float amount, const AZStd::string* jointName)
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (jointName)
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName->c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
if (jointName->empty())
|
|
{
|
|
AZ_Warning("Editor", false, "'SetEnvironmentAmount' called on default entity proxy. If this was the intent, a more explicit practice would be requesting this via the AudioProxyComponentBus.");
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::SetEnvironmentAmount, environmentID, amount);
|
|
}
|
|
else
|
|
{
|
|
AZ_Warning("Editor", false, "Joint not found. 'SetEnvironmentAmount' call not performed on joint '%s'", jointName->c_str());
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (auto const& iter : m_jointProxies)
|
|
{
|
|
if (!jointName || iter.first == jointId)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->SetEnvironmentAmount(environmentID, amount);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::OnTriggerStarted(const Audio::TAudioControlID /* triggerID */)
|
|
{
|
|
if (!m_activeVoices)
|
|
{
|
|
AZ::TickBus::Handler::BusConnect();
|
|
AZ::TransformNotificationBus::Handler::BusConnect(GetEntityId());
|
|
}
|
|
|
|
++m_activeVoices;
|
|
}
|
|
|
|
void AnimAudioComponent::OnTriggerFinished(const Audio::TAudioControlID /* triggerID */)
|
|
{
|
|
--m_activeVoices;
|
|
|
|
if (!m_activeVoices)
|
|
{
|
|
AZ::TickBus::Handler::BusDisconnect();
|
|
AZ::TransformNotificationBus::Handler::BusDisconnect(GetEntityId());
|
|
}
|
|
|
|
}
|
|
|
|
void AnimAudioComponent::Init()
|
|
{
|
|
}
|
|
|
|
void AnimAudioComponent::Activate()
|
|
{
|
|
AZStd::for_each(m_eventsToAdd.begin(), m_eventsToAdd.end(), [this](const auto& triggerEvent)
|
|
{
|
|
AddTriggerEventInternal(triggerEvent.m_eventName, triggerEvent.m_triggerName, triggerEvent.m_jointName);
|
|
});
|
|
m_eventsToAdd.clear();
|
|
|
|
AZStd::for_each(m_eventsToRemove.begin(), m_eventsToRemove.end(), [this](const auto& eventCrc)
|
|
{
|
|
RemoveTriggerEventInternal(eventCrc);
|
|
});
|
|
m_eventsToRemove.clear();
|
|
|
|
ActivateJointProxies();
|
|
|
|
Audio::AudioSystemRequestBus::Broadcast(&Audio::AudioSystemRequestBus::Events::AddRequestListener,
|
|
&AnimAudioComponent::OnAudioEvent,
|
|
this,
|
|
Audio::eART_AUDIO_CALLBACK_MANAGER_REQUEST,
|
|
Audio::eACMRT_REPORT_FINISHED_TRIGGER_INSTANCE);
|
|
|
|
m_callbackInfo.reset(new Audio::SAudioCallBackInfos(
|
|
this,
|
|
static_cast<AZ::u64>(GetEntityId()),
|
|
nullptr,
|
|
(Audio::eARF_PRIORITY_NORMAL | Audio::eARF_SYNC_FINISHED_CALLBACK)
|
|
));
|
|
|
|
ActorNotificationBus::Handler::BusConnect(GetEntityId());
|
|
AnimAudioComponentNotificationBus::Handler::BusConnect(GetEntityId());
|
|
}
|
|
|
|
void AnimAudioComponent::Deactivate()
|
|
{
|
|
AZ::TickBus::Handler::BusDisconnect();
|
|
AZ::TransformNotificationBus::Handler::BusDisconnect(GetEntityId());
|
|
|
|
m_activeVoices = 0;
|
|
|
|
DeactivateJointProxies();
|
|
|
|
Audio::AudioSystemRequestBus::Broadcast(&Audio::AudioSystemRequestBus::Events::RemoveRequestListener,
|
|
&AnimAudioComponent::OnAudioEvent, this);
|
|
|
|
ActorNotificationBus::Handler::BusDisconnect(GetEntityId());
|
|
AnimAudioComponentNotificationBus::Handler::BusDisconnect(GetEntityId());
|
|
}
|
|
|
|
void AnimAudioComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
|
|
{
|
|
AZ_UNUSED(deltaTime);
|
|
AZ_UNUSED(time);
|
|
for (auto& iter : m_jointProxies)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
AZ::Transform jointTransform = AZ::Transform::CreateIdentity();
|
|
auto getJointTransform = &SkeletalHierarchyRequestBus::Events::GetJointTransformCharacterRelative;
|
|
SkeletalHierarchyRequestBus::EventResult(jointTransform, GetEntityId(), getJointTransform, iter.first);
|
|
|
|
Audio::SATLWorldPosition atlTransform(m_transform * jointTransform);
|
|
proxy->SetPosition(m_transform * jointTransform);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::OnTransformChanged(const AZ::Transform& local, const AZ::Transform& world)
|
|
{
|
|
AZ_UNUSED(local);
|
|
m_transform = world;
|
|
}
|
|
|
|
void AnimAudioComponent::OnMotionEvent(EMotionFX::Integration::MotionEvent motionEvent)
|
|
{
|
|
// 1. Check if event is registered
|
|
auto eventIter = m_eventTriggerMap.find(AZ::Crc32(motionEvent.m_eventTypeName));
|
|
if (!motionEvent.m_isEventStart || eventIter == m_eventTriggerMap.end())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 2. If registered but jointId is unset, play on ProxyComponent's proxy
|
|
const AZ::s32 jointId = eventIter->second.GetJointId();
|
|
if (jointId < 0)
|
|
{
|
|
AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequests::ExecuteTrigger,
|
|
eventIter->second.GetTriggerId(), Audio::SAudioCallBackInfos::GetEmptyObject());
|
|
return;
|
|
}
|
|
|
|
// 3. If no joint is registered with the component, then don't play anything
|
|
// (If joints can be removed, then this would occur when event mapping and
|
|
// event call still exist)
|
|
auto jointIter = m_jointProxies.find(jointId);
|
|
if (jointIter == m_jointProxies.end())
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 4. If we have a joint proxy, update its position and play request.
|
|
if (Audio::IAudioProxy* proxy = jointIter->second)
|
|
{
|
|
const Audio::TAudioControlID triggerId = eventIter->second.GetTriggerId();
|
|
|
|
AZ::Transform jointTransform = AZ::Transform::CreateIdentity();
|
|
const auto getJointTransform = &SkeletalHierarchyRequestBus::Events::GetJointTransformCharacterRelative;
|
|
SkeletalHierarchyRequestBus::EventResult(jointTransform, GetEntityId(), getJointTransform, jointId);
|
|
|
|
const Audio::SATLWorldPosition atlTransform(m_transform * jointTransform);
|
|
proxy->SetPosition(atlTransform);
|
|
proxy->ExecuteTrigger(triggerId, *m_callbackInfo);
|
|
AnimAudioComponentNotificationBus::Event(GetEntityId(), &AnimAudioComponentNotificationBus::Events::OnTriggerStarted, triggerId);
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::Reflect(AZ::ReflectContext* context)
|
|
{
|
|
AudioTriggerEvent::Reflect(context);
|
|
if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
|
|
{
|
|
serializeContext->Class<AnimAudioComponent, AZ::Component>()
|
|
->Version(0)
|
|
->Field("AudioTriggerEvents", &AnimAudioComponent::m_eventsToAdd);
|
|
}
|
|
|
|
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
|
|
{
|
|
behaviorContext->EBus<EMotionFX::Integration::AnimAudioComponentRequestBus>("AnimAudioComponentRequestBus")
|
|
->Attribute(AZ::Script::Attributes::Category, "Animation")
|
|
->Event("AddTriggerEvent", &AnimAudioComponentRequestBus::Events::AddTriggerEvent)
|
|
->Event("ClearTriggerEvents", &AnimAudioComponentRequestBus::Events::ClearTriggerEvents)
|
|
->Event("RemoveTriggerEvent", &AnimAudioComponentRequestBus::Events::RemoveTriggerEvent);
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::AddTriggerEventInternal(const AZStd::string& eventName, const AZStd::string& triggerName, const AZStd::string& jointName)
|
|
{
|
|
Audio::TAudioControlID triggerId = INVALID_AUDIO_CONTROL_ID;
|
|
Audio::AudioSystemRequestBus::BroadcastResult(triggerId,
|
|
&Audio::AudioSystemRequestBus::Events::GetAudioTriggerID, triggerName.c_str());
|
|
|
|
if (triggerId == INVALID_AUDIO_CONTROL_ID)
|
|
{
|
|
AZ_Warning("Editor", false, "Audio trigger '%s' not found. Trigger not registered for motion event '%s'",
|
|
triggerName.c_str(), eventName.c_str());
|
|
}
|
|
else
|
|
{
|
|
AZ::s32 jointId = -1;
|
|
if (!jointName.empty())
|
|
{
|
|
SkeletalHierarchyRequestBus::EventResult(jointId, GetEntityId(),
|
|
&SkeletalHierarchyRequestBus::Events::GetJointIndexByName, jointName.c_str());
|
|
|
|
if (jointId < 0)
|
|
{
|
|
AZ_Warning("Editor", false, "Joint name '%s' not found: anim event '%s' audio trigger '%s' will be played on default proxy",
|
|
jointName.c_str(),
|
|
eventName.c_str(),
|
|
triggerName.c_str());
|
|
}
|
|
}
|
|
|
|
const AZ::Crc32 eventCrc(eventName.c_str());
|
|
RemoveTriggerEventInternal(eventCrc);
|
|
|
|
auto entity = GetEntity();
|
|
AZ_Assert(entity, "AnimAudioComponent must be attached to entity prior to adding a trigger event");
|
|
|
|
m_eventTriggerMap.emplace(eventCrc, TriggerEventData(*entity, triggerId, jointId));
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::RemoveTriggerEventInternal(const AZ::Crc32& eventCrc)
|
|
{
|
|
const auto iter = m_eventTriggerMap.find(eventCrc);
|
|
if (iter != m_eventTriggerMap.end())
|
|
{
|
|
m_eventTriggerMap.erase(iter);
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::ActivateJointProxies()
|
|
{
|
|
const AZ::Entity* entity = GetEntity();
|
|
AZ_Assert(entity, "Parent entity not found");
|
|
const AZStd::string& name = entity->GetName();
|
|
for (auto& eventIter : m_eventTriggerMap)
|
|
{
|
|
const AZ::s32 jointId = eventIter.second.GetJointId();
|
|
if (jointId >= 0)
|
|
{
|
|
auto jointIter = m_jointProxies.find(eventIter.second.GetJointId());
|
|
if (jointIter == m_jointProxies.end())
|
|
{
|
|
Audio::IAudioProxy* proxy = nullptr;
|
|
Audio::AudioSystemRequestBus::BroadcastResult(proxy, &Audio::AudioSystemRequestBus::Events::GetFreeAudioProxy);
|
|
AZ_Assert(proxy, "Failed to get free audio proxy");
|
|
|
|
AZStd::string proxyName = AZStd::string::format("%s:%d", name.c_str(), jointId);
|
|
proxy->Initialize(proxyName.c_str());
|
|
proxy->SetObstructionCalcType(Audio::eAOOCT_IGNORE);
|
|
m_jointProxies.emplace(jointId, proxy);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void AnimAudioComponent::DeactivateJointProxies()
|
|
{
|
|
for (auto& iter : m_jointProxies)
|
|
{
|
|
if (Audio::IAudioProxy* proxy = iter.second)
|
|
{
|
|
proxy->StopAllTriggers();
|
|
proxy->Release();
|
|
}
|
|
}
|
|
m_jointProxies.clear();
|
|
}
|
|
|
|
void AnimAudioComponent::OnAudioEvent(const Audio::SAudioRequestInfo* const requestInfo)
|
|
{
|
|
if (requestInfo->eAudioRequestType == Audio::eART_AUDIO_CALLBACK_MANAGER_REQUEST)
|
|
{
|
|
const auto notificationType = static_cast<Audio::EAudioCallbackManagerRequestType>(requestInfo->nSpecificAudioRequest);
|
|
if (notificationType == Audio::eACMRT_REPORT_FINISHED_TRIGGER_INSTANCE)
|
|
{
|
|
if (requestInfo->eResult == Audio::eARR_SUCCESS)
|
|
{
|
|
AZ::EntityId entityId(reinterpret_cast<AZ::u64>(requestInfo->pUserData));
|
|
AnimAudioComponentNotificationBus::Event(entityId, &AnimAudioComponentNotificationBus::Events::OnTriggerFinished, requestInfo->nAudioControlID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
AnimAudioComponent::TriggerEventData::TriggerEventData(const AZ::Entity& entity, Audio::TAudioControlID triggerId, AZ::s32 jointId)
|
|
: m_jointId(jointId)
|
|
, m_triggerId(triggerId)
|
|
{
|
|
AZ_UNUSED(entity);
|
|
}
|
|
|
|
AZ::s32 AnimAudioComponent::TriggerEventData::GetJointId() const
|
|
{
|
|
return m_jointId;
|
|
}
|
|
|
|
Audio::TAudioControlID AnimAudioComponent::TriggerEventData::GetTriggerId() const
|
|
{
|
|
return m_triggerId;
|
|
}
|
|
} // namespace Integration
|
|
} // namespace EMotionFX
|