DecalContainer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "DecalContainer.h"
  9. #include <AzCore/Math/Vector3.h>
  10. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  11. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  12. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  13. namespace AtomSampleViewer
  14. {
  15. namespace
  16. {
  17. static const char* const DecalMaterialNames[] =
  18. {
  19. "materials/Decal/scorch_01_decal.azmaterial",
  20. "materials/Decal/brushstoke_01_decal.azmaterial",
  21. "materials/Decal/am_road_dust_decal.azmaterial",
  22. "materials/Decal/am_mud_decal.azmaterial",
  23. "materials/Decal/airship_nose_number_decal.azmaterial",
  24. "materials/Decal/airship_tail_01_decal.azmaterial",
  25. "materials/Decal/airship_tail_02_decal.azmaterial",
  26. "materials/Decal/airship_symbol_decal.azmaterial",
  27. };
  28. }
  29. DecalContainer::DecalContainer(AZ::Render::DecalFeatureProcessorInterface* fp, const AZ::Vector3 position)
  30. : m_decalFeatureProcessor(fp), m_position(position)
  31. {
  32. SetupDecals();
  33. }
  34. void DecalContainer::SetupDecals()
  35. {
  36. const float HalfLength = 0.25f;
  37. const float HalfProjectionDepth = 10.0f;
  38. const AZ::Vector3 halfSize(HalfLength, HalfLength, HalfProjectionDepth);
  39. SetupNewDecal(AZ::Vector3(-0.75f, -0.25f, 1) + m_position, halfSize, DecalMaterialNames[0]);
  40. SetupNewDecal(AZ::Vector3(-0.25f, -0.25f, 1) + m_position, halfSize, DecalMaterialNames[1]);
  41. SetupNewDecal(AZ::Vector3(0.25f, -0.25f, 1) + m_position, halfSize, DecalMaterialNames[2]);
  42. SetupNewDecal(AZ::Vector3(0.75f, -0.25f, 1) + m_position, halfSize, DecalMaterialNames[3]);
  43. SetupNewDecal(AZ::Vector3(-0.75f, 0.25f, 1) + m_position, halfSize, DecalMaterialNames[4]);
  44. SetupNewDecal(AZ::Vector3(-0.25f, 0.25f, 1) + m_position, halfSize, DecalMaterialNames[5]);
  45. SetupNewDecal(AZ::Vector3(0.25f, 0.25f, 1) + m_position, halfSize, DecalMaterialNames[6]);
  46. SetupNewDecal(AZ::Vector3(0.75f, 0.25f, 1) + m_position, halfSize, DecalMaterialNames[7]);
  47. }
  48. DecalContainer::~DecalContainer()
  49. {
  50. SetNumDecalsActive(0);
  51. }
  52. void DecalContainer::SetNumDecalsActive(int numDecals)
  53. {
  54. for (int i = 0; i < aznumeric_cast<int>(m_decals.size()); ++i)
  55. {
  56. if (i < numDecals)
  57. {
  58. AcquireDecal(i);
  59. }
  60. else
  61. {
  62. ReleaseDecal(i);
  63. }
  64. }
  65. m_numDecalsActive = numDecals;
  66. }
  67. void DecalContainer::SetupNewDecal(const AZ::Vector3 position, const AZ::Vector3 halfSize, const char* const decalMaterialName)
  68. {
  69. Decal newDecal;
  70. newDecal.m_position = position;
  71. newDecal.m_halfSize = halfSize;
  72. newDecal.m_materialName = decalMaterialName;
  73. m_decals.push_back(newDecal);
  74. }
  75. void DecalContainer::AcquireDecal(int i)
  76. {
  77. Decal& decal = m_decals[i];
  78. if (decal.m_decalHandle.IsValid())
  79. {
  80. return;
  81. }
  82. decal.m_decalHandle = m_decalFeatureProcessor->AcquireDecal();
  83. m_decalFeatureProcessor->SetDecalHalfSize(decal.m_decalHandle, decal.m_halfSize);
  84. m_decalFeatureProcessor->SetDecalPosition(decal.m_decalHandle, decal.m_position);
  85. const AZ::Data::AssetId assetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(decal.m_materialName);
  86. m_decalFeatureProcessor->SetDecalMaterial(decal.m_decalHandle, assetId);
  87. }
  88. void DecalContainer::ReleaseDecal(int i)
  89. {
  90. Decal& decal = m_decals[i];
  91. if (decal.m_decalHandle.IsNull())
  92. {
  93. return;
  94. }
  95. m_decalFeatureProcessor->ReleaseDecal(decal.m_decalHandle);
  96. decal.m_decalHandle = AZ::Render::DecalFeatureProcessorInterface::DecalHandle::Null;
  97. }
  98. void DecalContainer::CloneFrom(const DecalContainer& containerToClone)
  99. {
  100. SetNumDecalsActive(0);
  101. for (size_t i = 0; i < containerToClone.GetNumDecalsActive() ; ++i)
  102. {
  103. Decal& ourDecal = m_decals[i];
  104. const Decal& otherDecal = containerToClone.m_decals[i];
  105. ourDecal.m_decalHandle = m_decalFeatureProcessor->CloneDecal(otherDecal.m_decalHandle);
  106. // Cloning sets the decal position to overlap the existing decal, lets move it so that it is visible
  107. m_decalFeatureProcessor->SetDecalPosition(ourDecal.m_decalHandle, ourDecal.m_position);
  108. }
  109. }
  110. }