3
0

MaterialAssetCreator.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <Atom/RPI.Reflect/Material/MaterialAssetCreator.h>
  9. #include <Atom/RPI.Reflect/Image/StreamingImageAsset.h>
  10. #include <Atom/RPI.Reflect/Image/AttachmentImageAsset.h>
  11. #include <AzCore/std/sort.h>
  12. #include <AzCore/std/bind/bind.h>
  13. namespace AZ
  14. {
  15. namespace RPI
  16. {
  17. void MaterialAssetCreator::Begin(const Data::AssetId& assetId, const Data::Asset<MaterialTypeAsset>& materialType)
  18. {
  19. BeginCommon(assetId);
  20. if (ValidateIsReady())
  21. {
  22. m_asset->m_materialTypeAsset = materialType;
  23. m_asset->m_materialTypeAsset.SetAutoLoadBehavior(AZ::Data::AssetLoadBehavior::PreLoad);
  24. if (!m_asset->m_materialTypeAsset)
  25. {
  26. ReportError("MaterialTypeAsset is null, the MaterialAsset cannot be finalized");
  27. }
  28. }
  29. }
  30. bool MaterialAssetCreator::End(Data::Asset<MaterialAsset>& result)
  31. {
  32. if (!ValidateIsReady())
  33. {
  34. return false;
  35. }
  36. m_asset->Finalize(
  37. [this](const char* message) { ReportWarning("%s", message); },
  38. [this](const char* message) { ReportError("%s", message); });
  39. // Finalize() doesn't clear the raw property data because that's the same function used at runtime, which does need to maintain the raw data
  40. // to support hot reload. But here we are pre-baking with the assumption that AP build dependencies will keep the material type
  41. // and material asset in sync, so we can discard the raw property data and just rely on the data in the material type asset.
  42. m_asset->m_rawPropertyValues.clear();
  43. m_asset->SetReady();
  44. return EndCommon(result);
  45. }
  46. void MaterialAssetCreator::SetMaterialTypeVersion(uint32_t version)
  47. {
  48. if (ValidateIsReady())
  49. {
  50. m_asset->m_materialTypeVersion = version;
  51. }
  52. }
  53. void MaterialAssetCreator::SetPropertyValue(const Name& name, const MaterialPropertyValue& value)
  54. {
  55. if (ValidateIsReady())
  56. {
  57. // Here we are careful to keep the properties in the same order they were encountered. When the MaterialAsset
  58. // is later finalized with a MaterialTypeAsset, there could be a version update procedure that includes renamed
  59. // properties. So it's possible that the same property could be encountered twice but with two different names.
  60. // Preserving the original order will ensure that the later properties still overwrite the earlier ones even after
  61. // renames have been applied.
  62. AZStd::erase_if(
  63. m_asset->m_rawPropertyValues,
  64. [&name](const AZStd::pair<Name, MaterialPropertyValue>& pair)
  65. {
  66. return pair.first == name;
  67. });
  68. m_asset->m_rawPropertyValues.emplace_back(name, value);
  69. }
  70. }
  71. void MaterialAssetCreator::SetPropertyValue(const Name& name, const Data::Asset<ImageAsset>& imageAsset)
  72. {
  73. SetPropertyValue(name, MaterialPropertyValue{imageAsset});
  74. }
  75. void MaterialAssetCreator::SetPropertyValue(const Name& name, const Data::Asset<StreamingImageAsset>& imageAsset)
  76. {
  77. SetPropertyValue(name, Data::Asset<ImageAsset>(imageAsset));
  78. }
  79. void MaterialAssetCreator::SetPropertyValue(const Name& name, const Data::Asset<AttachmentImageAsset>& imageAsset)
  80. {
  81. SetPropertyValue(name, Data::Asset<ImageAsset>(imageAsset));
  82. }
  83. } // namespace RPI
  84. } // namespace AZ