EntityLatticeTestComponent.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <EntityLatticeTestComponent.h>
  9. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  10. #include <AzCore/Component/Entity.h>
  11. #include <AzFramework/Components/TransformComponent.h>
  12. #include <SampleComponentManager.h>
  13. #include <SampleComponentConfig.h>
  14. #include <Automation/ScriptableImGui.h>
  15. #include <RHI/BasicRHIComponent.h>
  16. #include <EntityLatticeTestComponent_Traits_Platform.h>
  17. namespace AtomSampleViewer
  18. {
  19. using namespace AZ;
  20. using namespace RPI;
  21. constexpr int32_t s_latticeSizeMax = ENTITY_LATTEST_TEST_COMPONENT_MAX;
  22. void EntityLatticeTestComponent::Reflect(ReflectContext* context)
  23. {
  24. if (SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context))
  25. {
  26. serializeContext->Class<EntityLatticeTestComponent, Component>()
  27. ->Version(0)
  28. ;
  29. }
  30. }
  31. void EntityLatticeTestComponent::Activate()
  32. {
  33. Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &Debug::CameraControllerRequestBus::Events::Enable,
  34. azrtti_typeid<Debug::NoClipControllerComponent>());
  35. m_defaultIbl.Init(RPI::RPISystemInterface::Get()->GetDefaultScene().get());
  36. BuildLattice();
  37. }
  38. void EntityLatticeTestComponent::Deactivate()
  39. {
  40. Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &Debug::CameraControllerRequestBus::Events::Disable);
  41. DestroyLatticeInstances();
  42. m_defaultIbl.Reset();
  43. }
  44. void EntityLatticeTestComponent::RebuildLattice()
  45. {
  46. DestroyLatticeInstances();
  47. BuildLattice();
  48. }
  49. void EntityLatticeTestComponent::BuildLattice()
  50. {
  51. PrepareCreateLatticeInstances(GetInstanceCount());
  52. // We first rotate the model by 180 degrees before translating it. This is to make it face the camera as it did
  53. // when the world was Y-up.
  54. Transform transform = Transform::CreateRotationZ(Constants::Pi);
  55. static Vector3 distance(5.0f, 5.0f, 5.0f);
  56. for (int32_t x = 0; x < m_latticeWidth; ++x)
  57. {
  58. for (int32_t y = 0; y < m_latticeDepth; ++y)
  59. {
  60. for (int32_t z = 0; z < m_latticeHeight; ++z)
  61. {
  62. Vector3 position(
  63. static_cast<float>(x) * distance.GetX(),
  64. static_cast<float>(y) * distance.GetY(),
  65. static_cast<float>(z) * distance.GetZ());
  66. transform.SetTranslation(position);
  67. CreateLatticeInstance(transform);
  68. }
  69. }
  70. }
  71. FinalizeLatticeInstances();
  72. }
  73. uint32_t EntityLatticeTestComponent::GetInstanceCount() const
  74. {
  75. return m_latticeWidth * m_latticeHeight * m_latticeDepth;
  76. }
  77. void EntityLatticeTestComponent::SetLatticeDimensions(uint32_t width, uint32_t depth, uint32_t height)
  78. {
  79. m_latticeWidth = GetClamp<int32_t>(width, 1, s_latticeSizeMax);
  80. m_latticeHeight = GetClamp<int32_t>(height, 1, s_latticeSizeMax);
  81. m_latticeDepth = GetClamp<int32_t>(depth, 1, s_latticeSizeMax);
  82. }
  83. void EntityLatticeTestComponent::RenderImGuiLatticeControls()
  84. {
  85. bool latticeChanged = false;
  86. ImGui::Text("Lattice Width");
  87. latticeChanged |= ScriptableImGui::SliderInt("##LatticeWidth", &m_latticeWidth, 1, s_latticeSizeMax);
  88. ImGui::Spacing();
  89. ImGui::Text("Lattice Height");
  90. latticeChanged |= ScriptableImGui::SliderInt("##LatticeHeight", &m_latticeHeight, 1, s_latticeSizeMax);
  91. ImGui::Spacing();
  92. ImGui::Text("Lattice Depth");
  93. latticeChanged |= ScriptableImGui::SliderInt("##LatticeDepth", &m_latticeDepth, 1, s_latticeSizeMax);
  94. if (latticeChanged)
  95. {
  96. RebuildLattice();
  97. }
  98. }
  99. } // namespace AtomSampleViewer