MeshBuilderVertexAttributeLayers.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 required headers
  10. #include <AzCore/Math/MathUtils.h>
  11. #include <AzCore/Math/Vector2.h>
  12. #include <AzCore/Math/Vector3.h>
  13. #include <AzCore/Math/Vector4.h>
  14. #include <AzCore/Memory/Memory.h>
  15. #include <AzCore/std/containers/vector.h>
  16. #include <AzCore/std/string/string.h>
  17. #include <AzCore/std/typetraits/is_floating_point.h>
  18. #include <AzCore/std/typetraits/is_integral.h>
  19. #include "AzCore/std/numeric.h"
  20. #include "MeshBuilderInvalidIndex.h"
  21. namespace AZ::SceneAPI::DataTypes{ struct Color; }
  22. namespace AZ::MeshBuilder
  23. {
  24. struct MeshBuilderVertexLookup
  25. {
  26. size_t mOrgVtx = InvalidIndex;
  27. size_t mDuplicateNr = InvalidIndex;
  28. MeshBuilderVertexLookup() = default;
  29. MeshBuilderVertexLookup(size_t orgVtx, size_t duplicateNr)
  30. : mOrgVtx(orgVtx)
  31. , mDuplicateNr(duplicateNr)
  32. {}
  33. };
  34. class MeshBuilderVertexAttributeLayer
  35. {
  36. public:
  37. AZ_CLASS_ALLOCATOR_DECL
  38. MeshBuilderVertexAttributeLayer(bool isScale = false, bool isDeformable = false)
  39. : mIsScale(isScale)
  40. , mDeformable(isDeformable)
  41. {
  42. }
  43. virtual ~MeshBuilderVertexAttributeLayer() = default;
  44. bool GetIsScale() const { return mIsScale; }
  45. bool GetIsDeformable() const { return mDeformable; }
  46. void SetName(AZStd::string name) { mName = AZStd::move(name); }
  47. const AZStd::string& GetName() const { return mName; }
  48. virtual size_t GetAttributeSizeInBytes() const = 0;
  49. virtual size_t GetNumOrgVertices() const = 0;
  50. virtual size_t GetNumDuplicates(size_t orgVertexNr) const = 0;
  51. virtual size_t CalcLayerSizeInBytes() const { return GetAttributeSizeInBytes() * CalcNumVertices(); }
  52. virtual size_t CalcNumVertices() const = 0;
  53. virtual bool CheckIfIsVertexEqual(size_t orgVtx, size_t duplicate) const = 0;
  54. virtual void AddVertex(size_t orgVertexNr) = 0;
  55. protected:
  56. AZStd::string mName;
  57. bool mIsScale;
  58. bool mDeformable;
  59. };
  60. template<class AttribType>
  61. class MeshBuilderVertexAttributeLayerT : public MeshBuilderVertexAttributeLayer
  62. {
  63. public:
  64. AZ_CLASS_ALLOCATOR_DECL
  65. struct Vertex
  66. {
  67. AttribType mValue{};
  68. size_t mOrgVertex = InvalidIndex;
  69. Vertex() = default;
  70. Vertex(const AttribType& value, size_t orgVtx)
  71. : mValue(value)
  72. , mOrgVertex(orgVtx)
  73. {}
  74. };
  75. MeshBuilderVertexAttributeLayerT(size_t numOrgVerts, bool isScale = false, bool isDeformable = false)
  76. : MeshBuilderVertexAttributeLayer(isScale, isDeformable)
  77. , mVertices(numOrgVerts)
  78. {}
  79. size_t GetAttributeSizeInBytes() const override { return sizeof(AttribType); }
  80. size_t GetNumOrgVertices() const override { return mVertices.size(); }
  81. size_t GetNumDuplicates(size_t orgVertexNr) const override { return mVertices[orgVertexNr].size(); }
  82. size_t CalcNumVertices() const override
  83. {
  84. return AZStd::accumulate(mVertices.begin(), mVertices.end(), size_t(0), [](size_t sum, const auto& vertsPerPolygon) { return sum + vertsPerPolygon.size(); });
  85. }
  86. bool CheckIfIsVertexEqual(size_t orgVtx, size_t duplicate) const override
  87. {
  88. if constexpr (AZStd::is_integral_v<AttribType>)
  89. {
  90. return mVertices[orgVtx][duplicate].mValue == mVertexValue;
  91. }
  92. else if constexpr (AZStd::is_floating_point_v<AttribType>)
  93. {
  94. return AZ::IsClose(mVertices[orgVtx][duplicate].mValue, mVertexValue, 0.00001f);
  95. }
  96. else
  97. {
  98. return mVertices[orgVtx][duplicate].mValue.IsClose(mVertexValue, 0.00001f);
  99. }
  100. }
  101. void SetCurrentVertexValue(const AttribType& value) { mVertexValue = value; }
  102. const AttribType& GetCurrentVertexValue() const { return mVertexValue; }
  103. void AddVertex(size_t orgVertexNr) override
  104. {
  105. mVertices[orgVertexNr].emplace_back(mVertexValue, orgVertexNr);
  106. }
  107. void AddVertexValue(size_t orgVertexNr, const AttribType& value)
  108. {
  109. mVertices[orgVertexNr].emplace_back(value, orgVertexNr);
  110. }
  111. const AttribType& GetVertexValue(size_t orgVertexNr, size_t duplicateNr) const
  112. {
  113. return mVertices[orgVertexNr][duplicateNr].mValue;
  114. }
  115. private:
  116. AZStd::vector<AZStd::vector<Vertex>> mVertices;
  117. AttribType mVertexValue;
  118. };
  119. // some standard layer types
  120. using MeshBuilderVertexAttributeLayerVector2 = MeshBuilderVertexAttributeLayerT<AZ::Vector2>;
  121. using MeshBuilderVertexAttributeLayerVector3 = MeshBuilderVertexAttributeLayerT<AZ::Vector3>;
  122. using MeshBuilderVertexAttributeLayerVector4 = MeshBuilderVertexAttributeLayerT<AZ::Vector4>;
  123. using MeshBuilderVertexAttributeLayerUInt32 = MeshBuilderVertexAttributeLayerT<AZ::u32>;
  124. using MeshBuilderVertexAttributeLayerFloat = MeshBuilderVertexAttributeLayerT<float>;
  125. } // namespace AZ::MeshBuilder