MaterialUtils.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 <Atom/Utils/MaterialUtils.h>
  9. namespace AZ::Render
  10. {
  11. Matrix3x3 CreateUvTransformMatrix(const UvTransformDescriptor& desc, AZStd::span<const TransformType> transformOrder)
  12. {
  13. float translateX = desc.m_translateX;
  14. float translateY = desc.m_translateY;
  15. if (desc.m_scaleX != 0.0f)
  16. {
  17. translateX *= (1.0f / desc.m_scaleX);
  18. }
  19. if (desc.m_scaleY != 0.0f)
  20. {
  21. translateY *= (1.0f / desc.m_scaleY);
  22. }
  23. Matrix3x3 translateCenter2D = Matrix3x3::CreateIdentity();
  24. translateCenter2D.SetBasisZ(-desc.m_center.GetX(), -desc.m_center.GetY(), 1.0f);
  25. Matrix3x3 translateCenterInv2D = Matrix3x3::CreateIdentity();
  26. translateCenterInv2D.SetBasisZ(desc.m_center.GetX(), desc.m_center.GetY(), 1.0f);
  27. Matrix3x3 scale2D = Matrix3x3::CreateDiagonal(AZ::Vector3(desc.m_scaleX * desc.m_scale, desc.m_scaleY * desc.m_scale, 1.0f));
  28. Matrix3x3 translate2D = Matrix3x3::CreateIdentity();
  29. translate2D.SetBasisZ(translateX, translateY, 1.0f);
  30. Matrix3x3 rotate2D = Matrix3x3::CreateRotationZ(AZ::DegToRad(desc.m_rotateDegrees));
  31. Matrix3x3 transform = translateCenter2D;
  32. for (auto transformType : transformOrder)
  33. {
  34. switch (transformType)
  35. {
  36. case TransformType::Scale:
  37. transform = scale2D * transform;
  38. break;
  39. case TransformType::Rotate:
  40. transform = rotate2D * transform;
  41. break;
  42. case TransformType::Translate:
  43. transform = translate2D * transform;
  44. break;
  45. }
  46. }
  47. transform = translateCenterInv2D * transform;
  48. return transform;
  49. }
  50. }