2
0

EntityLatticeTestComponent.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 float s_spacingMax = 100.0f;
  22. constexpr float s_spacingMin = 0.5f;
  23. constexpr float s_entityScaleMax = 10.0f;
  24. constexpr float s_entityScaleMin = 0.1f;
  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(m_scene);
  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. m_worldAabb = AZ::Aabb::CreateNull();
  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) * m_spacingX,
  67. static_cast<float>(y) * m_spacingY,
  68. static_cast<float>(z) * m_spacingZ);
  69. transform.SetTranslation(position);
  70. transform.SetUniformScale(m_entityScale);
  71. CreateLatticeInstance(transform);
  72. m_worldAabb.AddPoint(transform.GetTranslation());
  73. }
  74. }
  75. }
  76. FinalizeLatticeInstances();
  77. }
  78. uint32_t EntityLatticeTestComponent::GetInstanceCount() const
  79. {
  80. return m_latticeWidth * m_latticeHeight * m_latticeDepth;
  81. }
  82. AZ::Aabb EntityLatticeTestComponent::GetLatticeAabb() const
  83. {
  84. return m_worldAabb;
  85. }
  86. void EntityLatticeTestComponent::SetLatticeMaxDimension(uint32_t max)
  87. {
  88. m_latticeSizeMax = max;
  89. }
  90. void EntityLatticeTestComponent::SetLatticeDimensions(uint32_t width, uint32_t depth, uint32_t height)
  91. {
  92. m_latticeWidth = AZ::GetClamp<int32_t>(width, 1, m_latticeSizeMax);
  93. m_latticeHeight = AZ::GetClamp<int32_t>(height, 1, m_latticeSizeMax);
  94. m_latticeDepth = AZ::GetClamp<int32_t>(depth, 1, m_latticeSizeMax);
  95. }
  96. void EntityLatticeTestComponent::SetLatticeSpacing( float spaceX, float spaceY, float spaceZ)
  97. {
  98. m_spacingX = AZ::GetClamp<float>(spaceX, s_spacingMin, s_spacingMax);
  99. m_spacingY = AZ::GetClamp<float>(spaceY, s_spacingMin, s_spacingMax);
  100. m_spacingZ = AZ::GetClamp<float>(spaceZ, s_spacingMin, s_spacingMax);
  101. }
  102. void EntityLatticeTestComponent::SetLatticeEntityScale(float scale)
  103. {
  104. m_entityScale = AZ::GetClamp<float>(scale, s_entityScaleMin, s_entityScaleMax);
  105. }
  106. void EntityLatticeTestComponent::SetIBLExposure(float exposure)
  107. {
  108. m_defaultIbl.SetExposure(exposure);
  109. }
  110. void EntityLatticeTestComponent::RenderImGuiLatticeControls()
  111. {
  112. bool latticeChanged = false;
  113. ImGui::Text("Lattice Width");
  114. latticeChanged |= ScriptableImGui::SliderInt("##LatticeWidth", &m_latticeWidth, 1, m_latticeSizeMax);
  115. ImGui::Spacing();
  116. ImGui::Text("Lattice Height");
  117. latticeChanged |= ScriptableImGui::SliderInt("##LatticeHeight", &m_latticeHeight, 1, m_latticeSizeMax);
  118. ImGui::Spacing();
  119. ImGui::Text("Lattice Depth");
  120. latticeChanged |= ScriptableImGui::SliderInt("##LatticeDepth", &m_latticeDepth, 1, m_latticeSizeMax);
  121. ImGui::Spacing();
  122. ImGui::Separator();
  123. ImGui::Spacing();
  124. ImGui::Text("Lattice Spacing X");
  125. latticeChanged |= ScriptableImGui::SliderFloat("##LatticeSpaceX", &m_spacingX, 0.5, s_spacingMax);
  126. ImGui::Spacing();
  127. ImGui::Text("Lattice Spacing Y");
  128. latticeChanged |= ScriptableImGui::SliderFloat("##LatticeSpaceY", &m_spacingY, 0.5, s_spacingMax);
  129. ImGui::Spacing();
  130. ImGui::Text("Lattice Spacing Z");
  131. latticeChanged |= ScriptableImGui::SliderFloat("##LatticeSpaceZ", &m_spacingZ, 0.5, s_spacingMax);
  132. ImGui::Spacing();
  133. ImGui::Separator();
  134. ImGui::Spacing();
  135. ImGui::Text("Entity Scale");
  136. latticeChanged |= ScriptableImGui::SliderFloat("##EntityScale", &m_entityScale, 0.01, s_entityScaleMax);
  137. if (latticeChanged)
  138. {
  139. RebuildLattice();
  140. }
  141. }
  142. } // namespace AtomSampleViewer