3
0

WhiteBoxMeshAtomData.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "TangentSpaceHelper.h"
  9. #include "WhiteBoxMeshAtomData.h"
  10. #include <numeric>
  11. namespace WhiteBox
  12. {
  13. WhiteBoxMeshAtomData::WhiteBoxMeshAtomData(const WhiteBoxFaces& faceData)
  14. {
  15. const size_t faceCount = faceData.size();
  16. const size_t vertCount = faceCount * 3;
  17. // mesh vertex attribute data in host memory format
  18. AZStd::vector<AZ::Vector3> positions(vertCount);
  19. AZStd::vector<AZ::Vector3> normals(vertCount);
  20. AZStd::vector<AZ::Vector2> uvs(vertCount);
  21. m_indices.resize_no_construct(vertCount);
  22. m_positions.resize_no_construct(vertCount);
  23. m_normals.resize_no_construct(vertCount);
  24. m_tangents.resize_no_construct(vertCount);
  25. m_bitangents.resize_no_construct(vertCount);
  26. m_uvs.resize_no_construct(vertCount);
  27. m_colors.resize(vertCount, AZ::Vector4::CreateOne());
  28. // populate the index vector with a [0, vertCount) sequence
  29. std::iota(std::begin(m_indices), std::end(m_indices), 0);
  30. for (size_t idxFace = 0; idxFace < faceCount; idxFace++)
  31. {
  32. const auto face = faceData[idxFace];
  33. // v1
  34. positions[idxFace * 3 + 0] = face.m_v1.m_position;
  35. uvs[idxFace * 3 + 0] = face.m_v1.m_uv;
  36. // v2
  37. positions[idxFace * 3 + 1] = face.m_v2.m_position;
  38. uvs[idxFace * 3 + 1] = face.m_v2.m_uv;
  39. // v3
  40. positions[idxFace * 3 + 2] = face.m_v3.m_position;
  41. uvs[idxFace * 3 + 2] = face.m_v3.m_uv;
  42. }
  43. // calculate the basis vectors for the TBN matrices
  44. AZTangentSpaceCalculation tangentSpaceCalculation;
  45. tangentSpaceCalculation.Calculate(positions, m_indices, uvs);
  46. for (size_t i = 0; i < vertCount; i++)
  47. {
  48. const auto normal = tangentSpaceCalculation.GetNormal(static_cast<AZ::u32>(i));
  49. const auto tangent = tangentSpaceCalculation.GetTangent(static_cast<AZ::u32>(i));
  50. const auto bitangent = tangentSpaceCalculation.GetBitangent(static_cast<AZ::u32>(i));
  51. m_aabb.AddPoint(positions[i]);
  52. // populate the mesh vertex attribute data in device memory format
  53. m_positions[i] = AZ::PackedVector3f(positions[i]);
  54. m_normals[i] = AZ::PackedVector3f(normal);
  55. m_tangents[i].Set(tangent, 1.0f);
  56. m_bitangents[i] = AZ::PackedVector3f(bitangent);
  57. m_uvs[i] = {uvs[i].GetX(), uvs[i].GetY()};
  58. }
  59. }
  60. const uint32_t WhiteBoxMeshAtomData::VertexCount() const
  61. {
  62. return static_cast<uint32_t>(m_indices.size());
  63. }
  64. const AZStd::vector<uint32_t>& WhiteBoxMeshAtomData::GetIndices() const
  65. {
  66. return m_indices;
  67. }
  68. const AZStd::vector<AZ::PackedVector3f>& WhiteBoxMeshAtomData::GetPositions() const
  69. {
  70. return m_positions;
  71. }
  72. const AZStd::vector<AZ::PackedVector3f>& WhiteBoxMeshAtomData::GetNormals() const
  73. {
  74. return m_normals;
  75. }
  76. const AZStd::vector<AZ::Vector4>& WhiteBoxMeshAtomData::GetTangents() const
  77. {
  78. return m_tangents;
  79. }
  80. const AZStd::vector<AZ::PackedVector3f>& WhiteBoxMeshAtomData::GetBitangents() const
  81. {
  82. return m_bitangents;
  83. }
  84. const AZStd::vector<PackedFloat2>& WhiteBoxMeshAtomData::GetUVs() const
  85. {
  86. return m_uvs;
  87. }
  88. const AZStd::vector<AZ::Vector4>& WhiteBoxMeshAtomData::GetColors() const
  89. {
  90. return m_colors;
  91. }
  92. AZ::Aabb WhiteBoxMeshAtomData::GetAabb() const
  93. {
  94. return m_aabb;
  95. }
  96. } // namespace WhiteBox