WhiteBoxMeshAssetHandler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "Asset/WhiteBoxMeshAsset.h"
  9. #include "Asset/WhiteBoxMeshAssetHandler.h"
  10. #include <AzCore/IO/FileIO.h>
  11. #include <AzCore/IO/GenericStreams.h>
  12. namespace WhiteBox
  13. {
  14. namespace Pipeline
  15. {
  16. WhiteBoxMeshAssetHandler::WhiteBoxMeshAssetHandler()
  17. {
  18. Register();
  19. }
  20. WhiteBoxMeshAssetHandler::~WhiteBoxMeshAssetHandler()
  21. {
  22. Unregister();
  23. }
  24. void WhiteBoxMeshAssetHandler::Register()
  25. {
  26. const bool assetManagerReady = AZ::Data::AssetManager::IsReady();
  27. AZ_Error("WhiteBoxMesh Asset", assetManagerReady, "Asset manager isn't ready.");
  28. if (assetManagerReady)
  29. {
  30. AZ::Data::AssetManager::Instance().RegisterHandler(this, AZ::AzTypeInfo<WhiteBoxMeshAsset>::Uuid());
  31. }
  32. AZ::AssetTypeInfoBus::Handler::BusConnect(AZ::AzTypeInfo<WhiteBoxMeshAsset>::Uuid());
  33. }
  34. void WhiteBoxMeshAssetHandler::Unregister()
  35. {
  36. AZ::AssetTypeInfoBus::Handler::BusDisconnect();
  37. if (AZ::Data::AssetManager::IsReady())
  38. {
  39. AZ::Data::AssetManager::Instance().UnregisterHandler(this);
  40. }
  41. }
  42. AZ::Data::AssetType WhiteBoxMeshAssetHandler::GetAssetType() const
  43. {
  44. return AZ::AzTypeInfo<WhiteBoxMeshAsset>::Uuid();
  45. }
  46. void WhiteBoxMeshAssetHandler::GetAssetTypeExtensions(AZStd::vector<AZStd::string>& extensions)
  47. {
  48. extensions.emplace_back(AssetFileExtension);
  49. }
  50. const char* WhiteBoxMeshAssetHandler::GetAssetTypeDisplayName() const
  51. {
  52. return "WhiteBoxMesh";
  53. }
  54. const char* WhiteBoxMeshAssetHandler::GetBrowserIcon() const
  55. {
  56. return "Editor/Icons/Components/WhiteBox.svg";
  57. }
  58. const char* WhiteBoxMeshAssetHandler::GetGroup() const
  59. {
  60. return "WhiteBox";
  61. }
  62. AZ::Uuid WhiteBoxMeshAssetHandler::GetComponentTypeId() const
  63. {
  64. return AZ::Uuid("{C9F2D913-E275-49BB-AB4F-2D221C16170A}"); // EditorWhiteBoxComponent
  65. }
  66. AZ::Data::AssetPtr WhiteBoxMeshAssetHandler::CreateAsset(
  67. [[maybe_unused]] const AZ::Data::AssetId& id, const AZ::Data::AssetType& type)
  68. {
  69. if (type == AZ::AzTypeInfo<WhiteBoxMeshAsset>::Uuid())
  70. {
  71. return aznew WhiteBoxMeshAsset();
  72. }
  73. AZ_Error("WhiteBoxMesh", false, "This handler deals only with WhiteBoxMeshAsset type.");
  74. return nullptr;
  75. }
  76. AZ::Data::AssetHandler::LoadResult WhiteBoxMeshAssetHandler::LoadAssetData(
  77. const AZ::Data::Asset<AZ::Data::AssetData>& asset, AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
  78. [[maybe_unused]] const AZ::Data::AssetFilterCB& assetLoadFilterCB)
  79. {
  80. WhiteBoxMeshAsset* whiteBoxMeshAsset = asset.GetAs<WhiteBoxMeshAsset>();
  81. if (!whiteBoxMeshAsset)
  82. {
  83. AZ_Error(
  84. "WhiteBoxMesh Asset", false,
  85. "This should be a WhiteBoxMesh Asset, as this is the only type we process.");
  86. return AZ::Data::AssetHandler::LoadResult::Error;
  87. }
  88. const auto size = stream->GetLength();
  89. Api::WhiteBoxMeshStream whiteBoxData;
  90. whiteBoxData.resize(size);
  91. stream->Read(size, whiteBoxData.data());
  92. auto whiteBoxMesh = WhiteBox::Api::CreateWhiteBoxMesh();
  93. const auto result = WhiteBox::Api::ReadMesh(*whiteBoxMesh, whiteBoxData);
  94. // if result is not 'Full', then whiteBoxMeshAsset could be empty which is most likely an error
  95. // as no data was loaded from the asset, or it was not correctly read in stream->Read(..)
  96. const auto success = result == Api::ReadResult::Full;
  97. if (success)
  98. {
  99. whiteBoxMeshAsset->SetMesh(AZStd::move(whiteBoxMesh));
  100. whiteBoxMeshAsset->SetWhiteBoxData(AZStd::move(whiteBoxData));
  101. }
  102. return success ? AZ::Data::AssetHandler::LoadResult::LoadComplete
  103. : AZ::Data::AssetHandler::LoadResult::Error;
  104. }
  105. bool WhiteBoxMeshAssetHandler::SaveAssetData(
  106. const AZ::Data::Asset<AZ::Data::AssetData>& asset, AZ::IO::GenericStream* stream)
  107. {
  108. WhiteBoxMeshAsset* whiteBoxMeshAsset = asset.GetAs<WhiteBoxMeshAsset>();
  109. if (!whiteBoxMeshAsset)
  110. {
  111. AZ_Error(
  112. "WhiteBoxMesh Asset", false,
  113. "This should be a WhiteBoxMesh Asset. WhiteBoxMeshAssetHandler doesn't handle any other asset "
  114. "type.");
  115. return false;
  116. }
  117. WhiteBox::WhiteBoxMesh* mesh = whiteBoxMeshAsset->GetMesh();
  118. if (!mesh)
  119. {
  120. AZ_Warning("WhiteBoxMesh Asset", false, "There is no WhiteBoxMesh to save.");
  121. return false;
  122. }
  123. const bool success = WhiteBox::Api::SaveToWbm(*mesh, *stream);
  124. if (!success)
  125. {
  126. AZ_Warning("", false, "Failed to write WhiteBoxMesh Asset:", asset.GetHint().c_str());
  127. }
  128. return success;
  129. }
  130. void WhiteBoxMeshAssetHandler::DestroyAsset(AZ::Data::AssetPtr ptr)
  131. {
  132. delete ptr;
  133. }
  134. void WhiteBoxMeshAssetHandler::GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes)
  135. {
  136. assetTypes.push_back(AZ::AzTypeInfo<WhiteBoxMeshAsset>::Uuid());
  137. }
  138. } // namespace Pipeline
  139. } // namespace WhiteBox