DecalExampleComponent.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <DecalExampleComponent.h>
  13. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  14. #include <Atom/RPI.Public/View.h>
  15. #include <Atom/RPI.Public/Image/StreamingImage.h>
  16. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  17. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  18. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  19. #include <SampleComponentManager.h>
  20. #include <SampleComponentConfig.h>
  21. #include <Automation/ScriptableImGui.h>
  22. #include <Automation/ScriptRunnerBus.h>
  23. #include <RHI/BasicRHIComponent.h>
  24. namespace AtomSampleViewer
  25. {
  26. void DecalExampleComponent::Reflect(AZ::ReflectContext* context)
  27. {
  28. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  29. {
  30. serializeContext->Class < DecalExampleComponent, AZ::Component>()
  31. ->Version(0)
  32. ;
  33. }
  34. }
  35. void DecalExampleComponent::Activate()
  36. {
  37. m_sampleName = "DecalExampleComponent";
  38. CreateDecalContainer();
  39. m_decalContainer->SetNumDecalsActive(m_decalContainer->GetMaxDecals());
  40. m_imguiSidebar.Activate();
  41. // List of all assets this example needs.
  42. AZStd::vector<AZ::AssetCollectionAsyncLoader::AssetToLoadInfo> assetList = {
  43. {"objects/plane.azmodel", azrtti_typeid<AZ::RPI::ModelAsset>()}, // The model
  44. };
  45. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  46. PreloadAssets(assetList);
  47. }
  48. void DecalExampleComponent::OnAllAssetsReadyActivate()
  49. {
  50. CreatePlaneObject();
  51. EnableArcBallCameraController();
  52. ConfigureCameraToLookDownAtObject();
  53. AddImageBasedLight();
  54. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  55. AZ::TickBus::Handler::BusConnect();
  56. }
  57. void DecalExampleComponent::CreatePlaneObject()
  58. {
  59. auto meshAsset = m_assetLoadManager.GetAsset<AZ::RPI::ModelAsset>("objects/plane.azmodel");
  60. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(meshAsset);
  61. ScaleObjectToFitDecals();
  62. }
  63. void DecalExampleComponent::ScaleObjectToFitDecals()
  64. {
  65. const AZ::Transform doubleSize = AZ::Transform::CreateScale(AZ::Vector3(2.0f, 1.0f, 1.0f));
  66. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, doubleSize);
  67. }
  68. void DecalExampleComponent::Deactivate()
  69. {
  70. m_decalContainer = nullptr;
  71. AZ::TickBus::Handler::BusDisconnect();
  72. m_imguiSidebar.Deactivate();
  73. m_defaultIbl.Reset();
  74. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  75. }
  76. void DecalExampleComponent::AddImageBasedLight()
  77. {
  78. m_defaultIbl.Init(AZ::RPI::RPISystemInterface::Get()->GetDefaultScene().get());
  79. }
  80. void DecalExampleComponent::EnableArcBallCameraController()
  81. {
  82. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  83. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  84. }
  85. void DecalExampleComponent::ConfigureCameraToLookDownAtObject()
  86. {
  87. const AZ::Vector3 CameraPanOffet(0.0f, 0.5f, -0.5f);
  88. const float CameraDistance = 1.5f;
  89. const float CameraPitch = -0.8f;
  90. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, CameraPanOffet);
  91. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, CameraPitch);
  92. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, CameraDistance);
  93. }
  94. void DecalExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  95. {
  96. DrawSidebar();
  97. }
  98. void DecalExampleComponent::DrawSidebar()
  99. {
  100. if (!m_imguiSidebar.Begin())
  101. {
  102. return;
  103. }
  104. int numDecalsActive = m_decalContainer->GetNumDecalsActive();
  105. if (ScriptableImGui::SliderInt("Point count", &numDecalsActive, 0, m_decalContainer->GetMaxDecals()))
  106. {
  107. m_decalContainer->SetNumDecalsActive(numDecalsActive);
  108. }
  109. m_imguiSidebar.End();
  110. }
  111. void DecalExampleComponent::CreateDecalContainer()
  112. {
  113. const AZ::RPI::Scene* scene = AZ::RPI::RPISystemInterface::Get()->GetDefaultScene().get();
  114. const auto decalFeatureProcessor = scene->GetFeatureProcessor<AZ::Render::DecalFeatureProcessorInterface>();
  115. m_decalContainer = AZStd::make_unique<DecalContainer>(decalFeatureProcessor);
  116. }
  117. }