2
0

DecalContainer.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include "DecalContainer.h"
  13. #include <AzCore/Math/Vector3.h>
  14. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  15. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  17. namespace AtomSampleViewer
  18. {
  19. namespace
  20. {
  21. static const char* const DecalMaterialNames[] =
  22. {
  23. "materials/Decal/scorch_01_decal.azmaterial",
  24. "materials/Decal/brushstoke_01_decal.azmaterial",
  25. "materials/Decal/am_road_dust_decal.azmaterial",
  26. "materials/Decal/am_mud_decal.azmaterial",
  27. "materials/Decal/airship_nose_number_decal.azmaterial",
  28. "materials/Decal/airship_tail_01_decal.azmaterial",
  29. "materials/Decal/airship_tail_02_decal.azmaterial",
  30. "materials/Decal/airship_symbol_decal.azmaterial",
  31. };
  32. }
  33. DecalContainer::DecalContainer(AZ::Render::DecalFeatureProcessorInterface* fp)
  34. : m_decalFeatureProcessor(fp)
  35. {
  36. SetupDecals();
  37. }
  38. void DecalContainer::SetupDecals()
  39. {
  40. const float HalfLength = 0.25f;
  41. const float HalfProjectionDepth = 10.0f;
  42. const AZ::Vector3 halfSize(HalfLength, HalfLength, HalfProjectionDepth);
  43. SetupNewDecal(AZ::Vector3(-0.75f, -0.25f, 1), halfSize, DecalMaterialNames[0]);
  44. SetupNewDecal(AZ::Vector3(-0.25f, -0.25f, 1), halfSize, DecalMaterialNames[1]);
  45. SetupNewDecal(AZ::Vector3(0.25f, -0.25f, 1), halfSize, DecalMaterialNames[2]);
  46. SetupNewDecal(AZ::Vector3(0.75f, -0.25f, 1), halfSize, DecalMaterialNames[3]);
  47. SetupNewDecal(AZ::Vector3(-0.75f, 0.25f, 1), halfSize, DecalMaterialNames[4]);
  48. SetupNewDecal(AZ::Vector3(-0.25f, 0.25f, 1), halfSize, DecalMaterialNames[5]);
  49. SetupNewDecal(AZ::Vector3(0.25f, 0.25f, 1), halfSize, DecalMaterialNames[6]);
  50. SetupNewDecal(AZ::Vector3(0.75f, 0.25f, 1), halfSize, DecalMaterialNames[7]);
  51. }
  52. DecalContainer::~DecalContainer()
  53. {
  54. SetNumDecalsActive(0);
  55. }
  56. void DecalContainer::SetNumDecalsActive(int numDecals)
  57. {
  58. for (int i = 0; i < aznumeric_cast<int>(m_decals.size()); ++i)
  59. {
  60. if (i < numDecals)
  61. {
  62. AcquireDecal(i);
  63. }
  64. else
  65. {
  66. ReleaseDecal(i);
  67. }
  68. }
  69. m_numDecalsActive = numDecals;
  70. }
  71. void DecalContainer::SetupNewDecal(const AZ::Vector3 position, const AZ::Vector3 halfSize, const char* const decalMaterialName)
  72. {
  73. Decal newDecal;
  74. newDecal.m_position = position;
  75. newDecal.m_halfSize = halfSize;
  76. newDecal.m_materialName = decalMaterialName;
  77. m_decals.push_back(newDecal);
  78. }
  79. void DecalContainer::AcquireDecal(int i)
  80. {
  81. Decal& decal = m_decals[i];
  82. if (decal.m_decalHandle.IsValid())
  83. {
  84. return;
  85. }
  86. decal.m_decalHandle = m_decalFeatureProcessor->AcquireDecal();
  87. m_decalFeatureProcessor->SetDecalHalfSize(decal.m_decalHandle, decal.m_halfSize);
  88. m_decalFeatureProcessor->SetDecalPosition(decal.m_decalHandle, decal.m_position);
  89. const AZ::Data::AssetId assetId = AZ::RPI::AssetUtils::GetAssetIdForProductPath(decal.m_materialName);
  90. m_decalFeatureProcessor->SetDecalMaterial(decal.m_decalHandle, assetId);
  91. }
  92. void DecalContainer::ReleaseDecal(int i)
  93. {
  94. Decal& decal = m_decals[i];
  95. if (decal.m_decalHandle.IsNull())
  96. {
  97. return;
  98. }
  99. m_decalFeatureProcessor->ReleaseDecal(decal.m_decalHandle);
  100. decal.m_decalHandle = AZ::Render::DecalFeatureProcessorInterface::DecalHandle::Null;
  101. }
  102. }