DecalExampleComponent.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <DecalExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  10. #include <Atom/RPI.Public/View.h>
  11. #include <Atom/RPI.Public/Image/StreamingImage.h>
  12. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  13. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  14. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  15. #include <SampleComponentManager.h>
  16. #include <SampleComponentConfig.h>
  17. #include <Automation/ScriptableImGui.h>
  18. #include <Automation/ScriptRunnerBus.h>
  19. #include <RHI/BasicRHIComponent.h>
  20. namespace AtomSampleViewer
  21. {
  22. namespace
  23. {
  24. static constexpr const char* TargetMeshName = "objects/plane.fbx.azmodel";
  25. static constexpr const char* TargetMaterialName = "materials/defaultpbr.azmaterial";
  26. }
  27. void DecalExampleComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  30. {
  31. serializeContext->Class < DecalExampleComponent, AZ::Component>()
  32. ->Version(0)
  33. ;
  34. }
  35. }
  36. void DecalExampleComponent::Activate()
  37. {
  38. m_sampleName = "DecalExampleComponent";
  39. CreateDecalContainer();
  40. m_decalContainer->SetNumDecalsActive(m_decalContainer->GetMaxDecals());
  41. m_imguiSidebar.Activate();
  42. // List of all assets this example needs.
  43. AZStd::vector<AZ::AssetCollectionAsyncLoader::AssetToLoadInfo> assetList = {
  44. { TargetMeshName, azrtti_typeid<AZ::RPI::ModelAsset>() }, // The model
  45. };
  46. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  47. PreloadAssets(assetList);
  48. }
  49. void DecalExampleComponent::OnAllAssetsReadyActivate()
  50. {
  51. CreatePlaneObject();
  52. EnableArcBallCameraController();
  53. ConfigureCameraToLookDownAtObject();
  54. AddImageBasedLight();
  55. AcquireDirectionalLightFeatureProcessor();
  56. CreateDirectionalLight();
  57. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  58. AZ::TickBus::Handler::BusConnect();
  59. }
  60. void DecalExampleComponent::CreatePlaneObject()
  61. {
  62. const auto meshAsset = m_assetLoadManager.GetAsset<AZ::RPI::ModelAsset>(TargetMeshName);
  63. const auto materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(TargetMaterialName, AZ::RPI::AssetUtils::TraceLevel::Assert);
  64. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor(meshAsset, AZ::RPI::Material::FindOrCreate(materialAsset)));
  65. ScaleObjectToFitDecals();
  66. }
  67. void DecalExampleComponent::ScaleObjectToFitDecals()
  68. {
  69. const AZ::Vector3 nonUniformScale(4.0f, 1.0f, 1.0f);
  70. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity(), nonUniformScale);
  71. }
  72. void DecalExampleComponent::Deactivate()
  73. {
  74. m_decalContainer = nullptr;
  75. AZ::TickBus::Handler::BusDisconnect();
  76. m_imguiSidebar.Deactivate();
  77. m_defaultIbl.Reset();
  78. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  79. m_directionalLightFeatureProcessor->ReleaseLight(m_directionalLightHandle);
  80. }
  81. void DecalExampleComponent::AddImageBasedLight()
  82. {
  83. m_defaultIbl.Init(m_scene);
  84. }
  85. void DecalExampleComponent::AcquireDirectionalLightFeatureProcessor()
  86. {
  87. using namespace AZ;
  88. m_directionalLightFeatureProcessor = m_scene->GetFeatureProcessor<Render::DirectionalLightFeatureProcessorInterface>();
  89. }
  90. void DecalExampleComponent::CreateDirectionalLight()
  91. {
  92. using namespace AZ;
  93. const auto directionalLightHandle = m_directionalLightFeatureProcessor->AcquireLight();
  94. const Render::PhotometricColor<Render::PhotometricUnit::Lux> lightColor(AZ::Color::CreateOne());
  95. m_directionalLightFeatureProcessor->SetRgbIntensity(directionalLightHandle, lightColor);
  96. m_directionalLightHandle = directionalLightHandle;
  97. }
  98. void DecalExampleComponent::EnableArcBallCameraController()
  99. {
  100. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  101. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  102. }
  103. void DecalExampleComponent::ConfigureCameraToLookDownAtObject()
  104. {
  105. const AZ::Vector3 CameraPanOffet(0.0f, 0.5f, -0.5f);
  106. const float CameraDistance = 1.5f;
  107. const float CameraPitch = -0.8f;
  108. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPan, CameraPanOffet);
  109. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetPitch, CameraPitch);
  110. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, CameraDistance);
  111. }
  112. void DecalExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  113. {
  114. DrawSidebar();
  115. UpdateDirectionalLight();
  116. }
  117. void DecalExampleComponent::DrawSidebar()
  118. {
  119. if (!m_imguiSidebar.Begin())
  120. {
  121. return;
  122. }
  123. int numDecalsActive = m_decalContainer->GetNumDecalsActive();
  124. if (ScriptableImGui::SliderInt("Point count", &numDecalsActive, 0, m_decalContainer->GetMaxDecals()))
  125. {
  126. m_decalContainer->SetNumDecalsActive(numDecalsActive);
  127. }
  128. if (ScriptableImGui::Checkbox("Clone decals", &m_cloneDecalsEnabled))
  129. {
  130. if (m_cloneDecalsEnabled)
  131. {
  132. m_decalContainerClone->CloneFrom(*m_decalContainer.get());
  133. }
  134. else
  135. {
  136. m_decalContainerClone->SetNumDecalsActive(0);
  137. }
  138. }
  139. ScriptableImGui::SliderAngle("Direction##Directional", &m_directionalLightRotationAngle, 0, 360);
  140. m_imguiSidebar.End();
  141. }
  142. void DecalExampleComponent::CreateDecalContainer()
  143. {
  144. const auto decalFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::DecalFeatureProcessorInterface>();
  145. m_decalContainer = AZStd::make_unique<DecalContainer>(decalFeatureProcessor, AZ::Vector3(1,0,0));
  146. m_decalContainerClone = AZStd::make_unique<DecalContainer>(decalFeatureProcessor, AZ::Vector3(-1,0,0));
  147. }
  148. void DecalExampleComponent::UpdateDirectionalLight()
  149. {
  150. using namespace AZ;
  151. constexpr float directionalLightDist = 10.0f;
  152. const auto lightLocation = Vector3(directionalLightDist * sinf(m_directionalLightRotationAngle), directionalLightDist * cosf(m_directionalLightRotationAngle), 10.0f);
  153. const auto lightTransform = Transform::CreateLookAt(lightLocation, Vector3::CreateZero());
  154. m_directionalLightFeatureProcessor->SetDirection(m_directionalLightHandle, lightTransform.GetBasis(1));
  155. }
  156. }