WhiteBoxAtomRenderMesh.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #pragma once
  9. #include <AzCore/Component/TickBus.h>
  10. #include <Rendering/Atom/WhiteBoxAttributeBuffer.h>
  11. #include <Rendering/Atom/WhiteBoxBuffer.h>
  12. #include <Rendering/WhiteBoxRenderData.h>
  13. #include <Rendering/WhiteBoxRenderMeshInterface.h>
  14. #include <Atom/Feature/Mesh/MeshFeatureProcessorInterface.h>
  15. #include <AtomLyIntegration/CommonFeatures/Mesh/MeshHandleStateBus.h>
  16. #include <AzCore/Component/TransformBus.h>
  17. #include <AzCore/Name/Name.h>
  18. namespace AZ::RPI
  19. {
  20. class ModelLodAsset;
  21. class ModelAsset;
  22. class Model;
  23. } // namespace AZ::RPI
  24. namespace WhiteBox
  25. {
  26. class WhiteBoxMeshAtomData;
  27. //! A concrete implementation of RenderMeshInterface to support Atom rendering for the White Box Tool.
  28. class AtomRenderMesh
  29. : public RenderMeshInterface
  30. , private AZ::Render::MeshHandleStateRequestBus::Handler
  31. , private AZ::TickBus::Handler
  32. {
  33. public:
  34. AZ_RTTI(AtomRenderMesh, "{1F48D2F5-037C-400B-977C-7C0C9A34B84C}", RenderMeshInterface);
  35. explicit AtomRenderMesh(AZ::EntityId entityId);
  36. ~AtomRenderMesh();
  37. // RenderMeshInterface ...
  38. void BuildMesh(
  39. const WhiteBoxRenderData& renderData, const AZ::Transform& worldFromLocal) override;
  40. void UpdateTransform(const AZ::Transform& worldFromLocal) override;
  41. void UpdateMaterial(const WhiteBoxMaterial& material) override;
  42. bool IsVisible() const override;
  43. void SetVisiblity(bool visibility) override;
  44. // AZ::TickBus overrides ...
  45. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  46. private:
  47. //! Creates an attribute buffer in the slot dictated by AttributeTypeT.
  48. template<AttributeType AttributeTypeT, typename VertexStreamDataType>
  49. void CreateAttributeBuffer(const AZStd::vector<VertexStreamDataType>& data)
  50. {
  51. const auto attribute_index = static_cast<size_t>(AttributeTypeT);
  52. m_attributes[attribute_index] = AZStd::make_unique<AttributeBuffer<AttributeTypeT>>(data);
  53. }
  54. //! Updates an attribute buffer in the slot dictated by AttributeTypeT.
  55. template<AttributeType AttributeTypeT, typename VertexStreamDataType>
  56. void UpdateAttributeBuffer(const AZStd::vector<VertexStreamDataType>& data)
  57. {
  58. const auto attribute_index = static_cast<size_t>(AttributeTypeT);
  59. auto& att = AZStd::get<attribute_index>(m_attributes[attribute_index]);
  60. att->UpdateData(data);
  61. }
  62. // MeshHandleStateRequestBus overrides ...
  63. const AZ::Render::MeshFeatureProcessorInterface::MeshHandle* GetMeshHandle() const override;
  64. bool CreateMeshBuffers(const WhiteBoxMeshAtomData& meshData);
  65. bool UpdateMeshBuffers(const WhiteBoxMeshAtomData& meshData);
  66. bool MeshRequiresFullRebuild(const WhiteBoxMeshAtomData& meshData) const;
  67. bool CreateMesh(const WhiteBoxMeshAtomData& meshData);
  68. bool CreateLodAsset(const WhiteBoxMeshAtomData& meshData);
  69. void CreateModelAsset();
  70. bool CreateModel();
  71. void AddLodBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator);
  72. void AddMeshBuffers(AZ::RPI::ModelLodAssetCreator& modelLodCreator);
  73. bool AreAttributesValid() const;
  74. bool DoesMeshRequireFullRebuild(const WhiteBoxMeshAtomData& meshData) const;
  75. AZ::EntityId m_entityId;
  76. AZ::Data::Asset<AZ::RPI::ModelLodAsset> m_lodAsset;
  77. AZ::Data::Asset<AZ::RPI::ModelAsset> m_modelAsset;
  78. AZ::Data::Instance<AZ::RPI::Model> m_model;
  79. AZ::Render::MeshFeatureProcessorInterface* m_meshFeatureProcessor = nullptr;
  80. AZ::Render::MeshFeatureProcessorInterface::MeshHandle m_meshHandle;
  81. AZ::Data::Instance<AZ::RPI::Material> m_materialInstance;
  82. uint32_t m_vertexCount = 0;
  83. AZStd::unique_ptr<IndexBuffer> m_indexBuffer;
  84. AZStd::array<
  85. AZStd::variant<
  86. AZStd::unique_ptr<PositionAttribute>, AZStd::unique_ptr<NormalAttribute>,
  87. AZStd::unique_ptr<TangentAttribute>, AZStd::unique_ptr<BitangentAttribute>,
  88. AZStd::unique_ptr<UVAttribute>, AZStd::unique_ptr<ColorAttribute>>,
  89. NumAttributes>
  90. m_attributes;
  91. bool m_visible = true;
  92. //! Default white box mesh material.
  93. static constexpr AZStd::string_view TexturedMaterialPath = "materials/whiteboxdefault.azmaterial";
  94. static constexpr AZ::RPI::ModelMaterialSlot::StableId OneMaterialSlotId = 0;
  95. //! White box model name.
  96. static constexpr AZStd::string_view ModelName = "WhiteBoxMesh";
  97. };
  98. } // namespace WhiteBox