EntityLatticeTestComponent.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <EntityLatticeTestComponent.h>
  13. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  14. #include <AzCore/Component/Entity.h>
  15. #include <AzFramework/Components/TransformComponent.h>
  16. #include <SampleComponentManager.h>
  17. #include <SampleComponentConfig.h>
  18. #include <Automation/ScriptableImGui.h>
  19. #include <RHI/BasicRHIComponent.h>
  20. namespace AtomSampleViewer
  21. {
  22. using namespace AZ;
  23. using namespace RPI;
  24. constexpr int32_t s_latticeSizeMax = 20;
  25. void EntityLatticeTestComponent::Reflect(ReflectContext* context)
  26. {
  27. if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
  28. {
  29. serializeContext->Class<EntityLatticeTestComponent, Component>()
  30. ->Version(0)
  31. ;
  32. }
  33. }
  34. void EntityLatticeTestComponent::Activate()
  35. {
  36. Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &Debug::CameraControllerRequestBus::Events::Enable,
  37. azrtti_typeid<Debug::NoClipControllerComponent>());
  38. m_defaultIbl.Init(RPI::RPISystemInterface::Get()->GetDefaultScene().get());
  39. BuildLattice();
  40. }
  41. void EntityLatticeTestComponent::Deactivate()
  42. {
  43. Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &Debug::CameraControllerRequestBus::Events::Disable);
  44. DestroyLatticeInstances();
  45. m_defaultIbl.Reset();
  46. }
  47. void EntityLatticeTestComponent::RebuildLattice()
  48. {
  49. DestroyLatticeInstances();
  50. BuildLattice();
  51. }
  52. void EntityLatticeTestComponent::BuildLattice()
  53. {
  54. PrepareCreateLatticeInstances(GetInstanceCount());
  55. // We first rotate the model by 180 degrees before translating it. This is to make it face the camera as it did
  56. // when the world was Y-up.
  57. Transform transform = Transform::CreateRotationZ(Constants::Pi);
  58. static Vector3 distance(5.0f, 5.0f, 5.0f);
  59. for (int32_t x = 0; x < m_latticeWidth; ++x)
  60. {
  61. for (int32_t y = 0; y < m_latticeDepth; ++y)
  62. {
  63. for (int32_t z = 0; z < m_latticeHeight; ++z)
  64. {
  65. Vector3 position(
  66. static_cast<float>(x) * distance.GetX(),
  67. static_cast<float>(y) * distance.GetY(),
  68. static_cast<float>(z) * distance.GetZ());
  69. transform.SetTranslation(position);
  70. CreateLatticeInstance(transform);
  71. }
  72. }
  73. }
  74. FinalizeLatticeInstances();
  75. }
  76. uint32_t EntityLatticeTestComponent::GetInstanceCount() const
  77. {
  78. return m_latticeWidth * m_latticeHeight * m_latticeDepth;
  79. }
  80. void EntityLatticeTestComponent::SetLatticeDimensions(uint32_t width, uint32_t depth, uint32_t height)
  81. {
  82. m_latticeWidth = GetClamp<int32_t>(width, 1, s_latticeSizeMax);
  83. m_latticeHeight = GetClamp<int32_t>(height, 1, s_latticeSizeMax);
  84. m_latticeDepth = GetClamp<int32_t>(depth, 1, s_latticeSizeMax);
  85. }
  86. void EntityLatticeTestComponent::RenderImGuiLatticeControls()
  87. {
  88. bool latticeChanged = false;
  89. ImGui::Text("Lattice Width");
  90. latticeChanged |= ScriptableImGui::SliderInt("##LatticeWidth", &m_latticeWidth, 1, s_latticeSizeMax);
  91. ImGui::Spacing();
  92. ImGui::Text("Lattice Height");
  93. latticeChanged |= ScriptableImGui::SliderInt("##LatticeHeight", &m_latticeHeight, 1, s_latticeSizeMax);
  94. ImGui::Spacing();
  95. ImGui::Text("Lattice Depth");
  96. latticeChanged |= ScriptableImGui::SliderInt("##LatticeDepth", &m_latticeDepth, 1, s_latticeSizeMax);
  97. if (latticeChanged)
  98. {
  99. RebuildLattice();
  100. }
  101. }
  102. } // namespace AtomSampleViewer