3
0

WhiteBoxAtomRenderMesh.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "WhiteBoxAtomRenderMesh.h"
  9. #include <Rendering/Atom/WhiteBoxMeshAtomData.h>
  10. #include <Rendering/WhiteBoxRenderData.h>
  11. #include <Util/WhiteBoxMathUtil.h>
  12. #include <Viewport/WhiteBoxViewportConstants.h>
  13. #include <Atom/RPI.Public/Model/Model.h>
  14. #include <Atom/RPI.Public/Scene.h>
  15. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  16. #include <Atom/RPI.Reflect/Model/ModelAssetCreator.h>
  17. #include <Atom/RPI.Reflect/Model/ModelLodAssetCreator.h>
  18. #include <Atom/RPI.Reflect/ResourcePoolAssetCreator.h>
  19. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentBus.h>
  20. #include <AzCore/Math/PackedVector3.h>
  21. namespace WhiteBox
  22. {
  23. AtomRenderMesh::AtomRenderMesh(AZ::EntityId entityId)
  24. : m_entityId(entityId)
  25. {
  26. AZ::Render::MeshHandleStateRequestBus::Handler::BusConnect(m_entityId);
  27. }
  28. AtomRenderMesh::~AtomRenderMesh()
  29. {
  30. m_materialInstance = {};
  31. if (m_meshHandle.IsValid() && m_meshFeatureProcessor)
  32. {
  33. m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
  34. AZ::Render::MeshHandleStateNotificationBus::Event(
  35. m_entityId, &AZ::Render::MeshHandleStateNotificationBus::Events::OnMeshHandleSet, &m_meshHandle);
  36. }
  37. AZ::Render::MeshHandleStateRequestBus::Handler::BusDisconnect();
  38. AZ::TickBus::Handler::BusDisconnect();
  39. }
  40. bool AtomRenderMesh::AreAttributesValid() const
  41. {
  42. bool attributesAreValid = true;
  43. for (const auto& attribute : m_attributes)
  44. {
  45. AZStd::visit(
  46. [&attributesAreValid](const auto& att)
  47. {
  48. if (!att->IsValid())
  49. {
  50. attributesAreValid = false;
  51. }
  52. },
  53. attribute);
  54. }
  55. return attributesAreValid;
  56. }
  57. bool AtomRenderMesh::CreateMeshBuffers(const WhiteBoxMeshAtomData& meshData)
  58. {
  59. m_indexBuffer = AZStd::make_unique<IndexBuffer>(meshData.GetIndices());
  60. CreateAttributeBuffer<AttributeType::Position>(meshData.GetPositions());
  61. CreateAttributeBuffer<AttributeType::Normal>(meshData.GetNormals());
  62. CreateAttributeBuffer<AttributeType::Tangent>(meshData.GetTangents());
  63. CreateAttributeBuffer<AttributeType::Bitangent>(meshData.GetBitangents());
  64. CreateAttributeBuffer<AttributeType::UV>(meshData.GetUVs());
  65. CreateAttributeBuffer<AttributeType::Color>(meshData.GetColors());
  66. return AreAttributesValid();
  67. }
  68. bool AtomRenderMesh::UpdateMeshBuffers(const WhiteBoxMeshAtomData& meshData)
  69. {
  70. UpdateAttributeBuffer<AttributeType::Position>(meshData.GetPositions());
  71. UpdateAttributeBuffer<AttributeType::Normal>(meshData.GetNormals());
  72. UpdateAttributeBuffer<AttributeType::Tangent>(meshData.GetTangents());
  73. UpdateAttributeBuffer<AttributeType::Bitangent>(meshData.GetBitangents());
  74. UpdateAttributeBuffer<AttributeType::UV>(meshData.GetUVs());
  75. UpdateAttributeBuffer<AttributeType::Color>(meshData.GetColors());
  76. return AreAttributesValid();
  77. }
  78. void AtomRenderMesh::AddLodBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator)
  79. {
  80. modelLodCreator.SetLodIndexBuffer(m_indexBuffer->GetBuffer());
  81. for (auto& attribute : m_attributes)
  82. {
  83. AZStd::visit(
  84. [&modelLodCreator](auto& att)
  85. {
  86. att->AddLodStreamBuffer(modelLodCreator);
  87. },
  88. attribute);
  89. }
  90. }
  91. void AtomRenderMesh::AddMeshBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator)
  92. {
  93. modelLodCreator.SetMeshIndexBuffer(m_indexBuffer->GetBufferAssetView());
  94. for (auto& attribute : m_attributes)
  95. {
  96. AZStd::visit(
  97. [&modelLodCreator](auto&& att)
  98. {
  99. att->AddMeshStreamBuffer(modelLodCreator);
  100. },
  101. attribute);
  102. }
  103. }
  104. bool AtomRenderMesh::CreateLodAsset(const WhiteBoxMeshAtomData& meshData)
  105. {
  106. if (!CreateMeshBuffers(meshData))
  107. {
  108. return false;
  109. }
  110. AZ::RPI::ModelLodAssetCreator modelLodCreator;
  111. modelLodCreator.Begin(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
  112. AddLodBuffers(modelLodCreator);
  113. modelLodCreator.BeginMesh();
  114. modelLodCreator.SetMeshAabb(meshData.GetAabb());
  115. modelLodCreator.SetMeshMaterialSlot(OneMaterialSlotId);
  116. AddMeshBuffers(modelLodCreator);
  117. modelLodCreator.EndMesh();
  118. if (!modelLodCreator.End(m_lodAsset))
  119. {
  120. AZ_Error("CreateLodAsset", false, "Couldn't create LoD asset.");
  121. return false;
  122. }
  123. if (!m_lodAsset.IsReady())
  124. {
  125. AZ_Error("CreateLodAsset", false, "LoD asset is not ready.");
  126. return false;
  127. }
  128. if (!m_lodAsset.Get())
  129. {
  130. AZ_Error("CreateLodAsset", false, "LoD asset is nullptr.");
  131. return false;
  132. }
  133. return true;
  134. }
  135. void AtomRenderMesh::CreateModelAsset()
  136. {
  137. AZ::RPI::ModelAssetCreator modelCreator;
  138. modelCreator.Begin(AZ::Data::AssetId(AZ::Uuid::CreateRandom()));
  139. modelCreator.SetName(ModelName);
  140. modelCreator.AddLodAsset(AZStd::move(m_lodAsset));
  141. if (auto materialAsset = AZ::RPI::AssetUtils::LoadAssetByProductPath<AZ::RPI::MaterialAsset>(TexturedMaterialPath.data()))
  142. {
  143. m_materialInstance = AZ::RPI::Material::FindOrCreate(materialAsset);
  144. AZ::RPI::ModelMaterialSlot materialSlot;
  145. materialSlot.m_stableId = OneMaterialSlotId;
  146. materialSlot.m_defaultMaterialAsset = materialAsset;
  147. modelCreator.AddMaterialSlot(materialSlot);
  148. }
  149. else
  150. {
  151. AZ_Error("CreateLodAsset", false, "Could not load material.");
  152. return;
  153. }
  154. modelCreator.End(m_modelAsset);
  155. }
  156. bool AtomRenderMesh::CreateModel()
  157. {
  158. m_model = AZ::RPI::Model::FindOrCreate(m_modelAsset);
  159. m_meshFeatureProcessor =
  160. AZ::RPI::Scene::GetFeatureProcessorForEntity<AZ::Render::MeshFeatureProcessorInterface>(m_entityId);
  161. if (!m_meshFeatureProcessor)
  162. {
  163. AZ_Error(
  164. "MeshComponentController", m_meshFeatureProcessor,
  165. "Unable to find a MeshFeatureProcessorInterface on the entityId.");
  166. return false;
  167. }
  168. m_meshFeatureProcessor->ReleaseMesh(m_meshHandle);
  169. m_meshHandle = m_meshFeatureProcessor->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset }, m_materialInstance);
  170. AZ::Render::MeshHandleStateNotificationBus::Event(m_entityId, &AZ::Render::MeshHandleStateNotificationBus::Events::OnMeshHandleSet, &m_meshHandle);
  171. return true;
  172. }
  173. bool AtomRenderMesh::MeshRequiresFullRebuild([[maybe_unused]] const WhiteBoxMeshAtomData& meshData) const
  174. {
  175. return meshData.VertexCount() != m_vertexCount;
  176. }
  177. bool AtomRenderMesh::CreateMesh(const WhiteBoxMeshAtomData& meshData)
  178. {
  179. if (!CreateLodAsset(meshData))
  180. {
  181. return false;
  182. }
  183. CreateModelAsset();
  184. if (!CreateModel())
  185. {
  186. return false;
  187. }
  188. m_vertexCount = meshData.VertexCount();
  189. return true;
  190. }
  191. bool AtomRenderMesh::DoesMeshRequireFullRebuild([[maybe_unused]] const WhiteBoxMeshAtomData& meshData) const
  192. {
  193. // this has been disabled due to a some recent updates with Atom that a) cause visual artefacts
  194. // when updating the buffers and b) have a big performance boost when rebuilding from scratch anyway.
  195. //
  196. // this method for building the mesh will probably be replace anyway when the Atom DynamicDraw support
  197. // comes online.
  198. return true; // meshData.VertexCount() != m_vertexCount;
  199. }
  200. void AtomRenderMesh::BuildMesh(const WhiteBoxRenderData& renderData, const AZ::Transform& worldFromLocal)
  201. {
  202. const WhiteBoxFaces culledFaceList = BuildCulledWhiteBoxFaces(renderData.m_faces);
  203. const WhiteBoxMeshAtomData meshData(culledFaceList);
  204. if (DoesMeshRequireFullRebuild(meshData))
  205. {
  206. if (!CreateMesh(meshData))
  207. {
  208. return;
  209. }
  210. }
  211. else
  212. {
  213. if (!UpdateMeshBuffers(meshData))
  214. {
  215. return;
  216. }
  217. }
  218. UpdateTransform(worldFromLocal);
  219. }
  220. void AtomRenderMesh::UpdateTransform(const AZ::Transform& worldFromLocal)
  221. {
  222. m_meshFeatureProcessor->SetTransform(m_meshHandle, worldFromLocal);
  223. }
  224. void AtomRenderMesh::UpdateMaterial(const WhiteBoxMaterial& material)
  225. {
  226. if (m_meshFeatureProcessor && m_materialInstance)
  227. {
  228. if (const auto& materialPropertyIndex = m_materialInstance->FindPropertyIndex(AZ::Name("baseColor.color"));
  229. materialPropertyIndex.IsValid())
  230. {
  231. m_materialInstance->SetPropertyValue(materialPropertyIndex, AZ::Color(material.m_tint));
  232. }
  233. if (const auto& materialPropertyIndex = m_materialInstance->FindPropertyIndex(AZ::Name("baseColor.useTexture"));
  234. materialPropertyIndex.IsValid())
  235. {
  236. m_materialInstance->SetPropertyValue(materialPropertyIndex, material.m_useTexture);
  237. }
  238. // If the material changes were successfully applied then disconnect from the tick bus. Otherwise, make another attempt on the next tick.
  239. if (!m_materialInstance->NeedsCompile() || m_materialInstance->Compile())
  240. {
  241. AZ::TickBus::Handler::BusDisconnect();
  242. }
  243. else if (!AZ::TickBus::Handler::BusIsConnected())
  244. {
  245. AZ::TickBus::Handler::BusConnect();
  246. }
  247. }
  248. }
  249. void AtomRenderMesh::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  250. {
  251. if (!m_materialInstance || !m_materialInstance->NeedsCompile() || m_materialInstance->Compile())
  252. {
  253. AZ::TickBus::Handler::BusDisconnect();
  254. }
  255. }
  256. void AtomRenderMesh::SetVisiblity(bool visibility)
  257. {
  258. m_visible = visibility;
  259. m_meshFeatureProcessor->SetVisible(m_meshHandle, m_visible);
  260. }
  261. bool AtomRenderMesh::IsVisible() const
  262. {
  263. return m_visible;
  264. }
  265. const AZ::Render::MeshFeatureProcessorInterface::MeshHandle* AtomRenderMesh::GetMeshHandle() const
  266. {
  267. return &m_meshHandle;
  268. }
  269. } // namespace WhiteBox