ActorAsset.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <EMotionFX/Source/ActorInstance.h>
  9. #include <EMotionFX/Source/EMotionFXManager.h>
  10. #include <EMotionFX/Source/Importer/Importer.h>
  11. #include <Integration/Assets/ActorAsset.h>
  12. #include <Integration/Rendering/RenderBackendManager.h>
  13. namespace EMotionFX
  14. {
  15. namespace Integration
  16. {
  17. AZ_CLASS_ALLOCATOR_IMPL(ActorAsset, EMotionFXAllocator)
  18. AZ_CLASS_ALLOCATOR_IMPL(ActorAssetHandler, EMotionFXAllocator)
  19. ActorAsset::ActorAsset(AZ::Data::AssetId id)
  20. : EMotionFXAsset(id)
  21. {}
  22. ActorAsset::ActorInstancePtr ActorAsset::CreateInstance(AZ::Entity* entity)
  23. {
  24. AZ_Assert(m_emfxActor, "Actor asset is not loaded");
  25. ActorInstancePtr actorInstance = ActorInstancePtr::MakeFromNew(EMotionFX::ActorInstance::Create(m_emfxActor.get(), entity));
  26. if (actorInstance)
  27. {
  28. actorInstance->SetIsOwnedByRuntime(true);
  29. }
  30. return actorInstance;
  31. }
  32. void ActorAsset::SetData(AZStd::shared_ptr<Actor> actor)
  33. {
  34. m_emfxActor = AZStd::move(actor);
  35. m_status = AZ::Data::AssetData::AssetStatus::Ready;
  36. }
  37. void ActorAsset::InitRenderActor()
  38. {
  39. RenderBackend* renderBackend = AZ::Interface<RenderBackendManager>::Get()->GetRenderBackend();
  40. m_renderActor.reset(renderBackend->CreateActor(this));
  41. }
  42. bool ActorAssetHandler::OnInitAsset(const AZ::Data::Asset<AZ::Data::AssetData>& asset)
  43. {
  44. ActorAsset* assetData = asset.GetAs<ActorAsset>();
  45. Importer::ActorSettings actorSettings;
  46. if (GetEMotionFX().GetEnableServerOptimization())
  47. {
  48. actorSettings.m_optimizeForServer = true;
  49. }
  50. assetData->m_emfxActor = EMotionFX::GetImporter().LoadActor(
  51. assetData->m_emfxNativeData.data(),
  52. assetData->m_emfxNativeData.size(),
  53. &actorSettings,
  54. "");
  55. assetData->m_emfxActor->SetFileName(asset.GetHint().c_str());
  56. assetData->m_emfxActor->Finalize();
  57. // Clear out the EMFX raw asset data.
  58. assetData->ReleaseEMotionFXData();
  59. if (!assetData->m_emfxActor)
  60. {
  61. AZ_Error("EMotionFX", false, "Failed to initialize actor asset %s", asset.ToString<AZStd::string>().c_str());
  62. return false;
  63. }
  64. // Note: Render actor depends on the mesh asset, so we need to manually create it after mesh asset has been loaded.
  65. return static_cast<bool>(assetData->m_emfxActor);
  66. }
  67. AZ::Data::AssetType ActorAssetHandler::GetAssetType() const
  68. {
  69. return azrtti_typeid<ActorAsset>();
  70. }
  71. void ActorAssetHandler::GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions)
  72. {
  73. extensions.push_back("actor");
  74. }
  75. AZ::Uuid ActorAssetHandler::GetComponentTypeId() const
  76. {
  77. // EditorActorComponent
  78. return AZ::Uuid("{A863EE1B-8CFD-4EDD-BA0D-1CEC2879AD44}");
  79. }
  80. const char* ActorAssetHandler::GetAssetTypeDisplayName() const
  81. {
  82. return "EMotion FX Actor";
  83. }
  84. const char* ActorAssetHandler::GetBrowserIcon() const
  85. {
  86. return "Editor/Images/AssetBrowser/Actor_16.svg";
  87. }
  88. int ActorAssetHandler::GetAssetTypeDragAndDropCreationPriority() const
  89. {
  90. // this function is used when the user drags and drops a file that produces
  91. // many different kinds of assets into the viewport (for example, dragging a FBX file
  92. // that produces both an actor and a mesh). It is used to select which component
  93. // is ultimately chosen to spawn in the viewport, since only one can be chosen to represent
  94. // the dropped object. In the case of an imported file which produces an actor, its very likely
  95. // that the actor is representative of the file, moreso than other parts.
  96. return AZ::AssetTypeInfo::s_HighPriority;
  97. }
  98. } //namespace Integration
  99. } // namespace EMotionFX