123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <ExposureExampleComponent.h>
- #include <Atom/Component/DebugCamera/CameraControllerBus.h>
- #include <Atom/Component/DebugCamera/NoClipControllerBus.h>
- #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
- #include <Atom/RPI.Public/Model/Model.h>
- #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
- #include <Atom/RPI.Reflect/Model/ModelAsset.h>
- #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
- #include <AzCore/Component/Entity.h>
- #include <AzFramework/Components/CameraBus.h>
- #include <AzFramework/Components/TransformComponent.h>
- #include <SampleComponentManager.h>
- #include <SampleComponentConfig.h>
- #include <EntityUtilityFunctions.h>
- #include <RHI/BasicRHIComponent.h>
- namespace AtomSampleViewer
- {
- void ExposureExampleComponent::Reflect(AZ::ReflectContext* context)
- {
- if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
- {
- serializeContext->Class<ExposureExampleComponent, AZ::Component>()
- ->Version(0)
- ;
- }
- }
- void ExposureExampleComponent::Activate()
- {
- using namespace AZ;
- m_postProcessFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::PostProcessFeatureProcessorInterface>();
- m_pointLightFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::PointLightFeatureProcessorInterface>();
- EnableCameraController();
- m_cameraTransformInitialized = false;
-
- SetupScene();
- CreateExposureEntity();
- m_imguiSidebar.Activate();
- AZ::TickBus::Handler::BusConnect();
- }
- void ExposureExampleComponent::Deactivate()
- {
- using namespace AZ;
- AZ::TickBus::Handler::BusDisconnect();
- DisableCameraController();
- AZ::EntityBus::MultiHandler::BusDisconnect();
- if (m_exposureControlSettings)
- {
- m_exposureControlSettings->SetEnabled(false);
- m_exposureControlSettings->OnConfigChanged();
- }
- if (m_exposureEntity)
- {
- DestroyEntity(m_exposureEntity, GetEntityContextId());
- m_postProcessFeatureProcessor = nullptr;
- }
- GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
- m_pointLightFeatureProcessor->ReleaseLight(m_pointLight);
- m_pointLightFeatureProcessor = nullptr;
- m_imguiSidebar.Deactivate();
- }
- void ExposureExampleComponent::OnTick([[maybe_unused]] float deltaTime, AZ::ScriptTimePoint)
- {
- DrawSidebar();
- }
- void ExposureExampleComponent::OnEntityDestruction(const AZ::EntityId& entityId)
- {
- AZ::EntityBus::MultiHandler::BusDisconnect(entityId);
- if (m_exposureEntity && m_exposureEntity->GetId() == entityId)
- {
- m_postProcessFeatureProcessor->RemoveSettingsInterface(m_exposureEntity->GetId());
- m_exposureEntity = nullptr;
- }
- else
- {
- AZ_Assert(false, "unexpected entity destruction is signaled.");
- }
- }
- void ExposureExampleComponent::SetupScene()
- {
- using namespace AZ;
- const char* sponzaPath = "objects/sponza.fbx.azmodel";
- Data::Asset<RPI::ModelAsset> modelAsset =
- RPI::AssetUtils::GetAssetByProductPath<RPI::ModelAsset>(sponzaPath, RPI::AssetUtils::TraceLevel::Assert);
- Data::Asset<RPI::MaterialAsset> materialAsset =
- RPI::AssetUtils::GetAssetByProductPath<RPI::MaterialAsset>(DefaultPbrMaterialPath, RPI::AssetUtils::TraceLevel::Assert);
- Render::MeshHandleDescriptor descriptor(modelAsset, AZ::RPI::Material::FindOrCreate(materialAsset));
- descriptor.m_modelChangedEventHandler =
- AZ::Render::MeshHandleDescriptor::ModelChangedEvent::Handler{ [this](const AZ::Data::Instance<AZ::RPI::Model>& model)
- {
- OnModelReady(model);
- } };
- m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(descriptor);
-
- // rotate the entity 180 degrees about Z (the vertical axis)
- // This makes it consistent with how it was positioned in the world when the world was Y-up.
- GetMeshFeatureProcessor()->SetTransform(m_meshHandle, Transform::CreateRotationZ(AZ::Constants::Pi));
- SetupLights();
- }
- void ExposureExampleComponent::SetupLights()
- {
- auto lightHandle = m_pointLightFeatureProcessor->AcquireLight();
- m_pointLightFeatureProcessor->SetPosition(lightHandle, AZ::Vector3(12.f, -12.6f, 4.3f));
- AZ::Render::PhotometricColor<AZ::Render::PhotometricUnit::Candela> color(40.0f * AZ::Color(1.f, 1.f, 1.f, 1.f));
- m_pointLightFeatureProcessor->SetRgbIntensity(lightHandle, color);
- m_pointLightFeatureProcessor->SetBulbRadius(lightHandle, 3.f);
- m_pointLight = lightHandle;
- }
- void ExposureExampleComponent::OnModelReady(AZ::Data::Instance<AZ::RPI::Model> model)
- {
- m_sponzaAssetLoaded = true;
- SetInitialCameraTransform();
- }
- void ExposureExampleComponent::SetInitialCameraTransform()
- {
- using namespace AZ;
- if (!m_cameraTransformInitialized)
- {
- const AZ::Vector3 InitPosition = AZ::Vector3(5.0f, 0.0f, 5.0f);
- constexpr float InitPitch = DegToRad(-20.0f);
- constexpr float InitHeading = DegToRad(90.0f);
- AZ::Transform cameraTrans = AZ::Transform::CreateIdentity();
- cameraTrans.SetTranslation(InitPosition);
- TransformBus::Event(
- GetCameraEntityId(),
- &TransformBus::Events::SetWorldTM,
- cameraTrans);
- AZ::Debug::NoClipControllerRequestBus::Event(
- GetCameraEntityId(),
- &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch,
- InitPitch);
- AZ::Debug::NoClipControllerRequestBus::Event(
- GetCameraEntityId(),
- &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading,
- InitHeading);
- m_cameraTransformInitialized = true;
- }
- }
- void ExposureExampleComponent::EnableCameraController()
- {
- AZ::Debug::CameraControllerRequestBus::Event(
- GetCameraEntityId(),
- &AZ::Debug::CameraControllerRequestBus::Events::Enable,
- azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
- }
- void ExposureExampleComponent::DisableCameraController()
- {
- AZ::Debug::CameraControllerRequestBus::Event(
- GetCameraEntityId(),
- &AZ::Debug::CameraControllerRequestBus::Events::Disable);
- }
- void ExposureExampleComponent::CreateExposureEntity()
- {
- using namespace AZ;
- m_exposureEntity = CreateEntity("Exposure", GetEntityContextId());
- // Exposure
- auto* exposureSettings = m_postProcessFeatureProcessor->GetOrCreateSettingsInterface(m_exposureEntity->GetId());
- m_exposureControlSettings = exposureSettings->GetOrCreateExposureControlSettingsInterface();
- m_exposureControlSettings->SetEnabled(true);
- m_exposureControlSettings->SetExposureControlType(Render::ExposureControl::ExposureControlType::EyeAdaptation);
- m_exposureEntity->Activate();
- AZ::EntityBus::MultiHandler::BusConnect(m_exposureEntity->GetId());
- }
- void ExposureExampleComponent::DrawSidebar()
- {
- using namespace AZ::Render;
- if (!m_imguiSidebar.Begin())
- {
- return;
- }
- ImGui::Spacing();
- ImGui::Text("Exposure");
- ImGui::Indent();
- {
- bool exposureEnabled = m_exposureControlSettings->GetEnabled();
- if (ImGui::Checkbox("Enabled Exposure", &exposureEnabled) || !m_isInitParameters)
- {
- m_exposureControlSettings->SetEnabled(exposureEnabled);
- m_exposureControlSettings->OnConfigChanged();
- }
- ImGui::Spacing();
- float manualCompensation = m_exposureControlSettings->GetManualCompensation();
- if (ImGui::SliderFloat("Manual Compensation", &manualCompensation, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
- {
- m_exposureControlSettings->SetManualCompensation(manualCompensation);
- m_exposureControlSettings->OnConfigChanged();
- }
- if (ImGui::CollapsingHeader("EyeAdaptation", ImGuiTreeNodeFlags_DefaultOpen))
- {
- ImGui::Indent();
- bool eyeAdaptationEnabled = m_exposureControlSettings->GetExposureControlType() == ExposureControl::ExposureControlType::EyeAdaptation;
- if (ImGui::Checkbox("Enabled Eye Adaptation", &eyeAdaptationEnabled) || !m_isInitParameters)
- {
- m_exposureControlSettings->SetExposureControlType(eyeAdaptationEnabled ? ExposureControl::ExposureControlType::EyeAdaptation
- : ExposureControl::ExposureControlType::ManualOnly);
- m_exposureControlSettings->OnConfigChanged();
- }
- ImGui::Spacing();
- float minimumExposure = m_exposureControlSettings->GetEyeAdaptationExposureMin();
- if (ImGui::SliderFloat("Minimum Exposure", &minimumExposure, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
- {
- m_exposureControlSettings->SetEyeAdaptationExposureMin(minimumExposure);
- m_exposureControlSettings->OnConfigChanged();
- }
- float maximumExposure = m_exposureControlSettings->GetEyeAdaptationExposureMax();
- if (ImGui::SliderFloat("Maximum Exposure", &maximumExposure, -16.0f, 16.0f, "%0.4f") || !m_isInitParameters)
- {
- m_exposureControlSettings->SetEyeAdaptationExposureMax(maximumExposure);
- m_exposureControlSettings->OnConfigChanged();
- }
- float speedUp = m_exposureControlSettings->GetEyeAdaptationSpeedUp();
- if (ImGui::SliderFloat("Speed Up", &speedUp, 0.01, 10.0f, "%0.4f") || !m_isInitParameters)
- {
- m_exposureControlSettings->SetEyeAdaptationSpeedUp(speedUp);
- m_exposureControlSettings->OnConfigChanged();
- }
- float speedDown = m_exposureControlSettings->GetEyeAdaptationSpeedDown();
- if (ImGui::SliderFloat("Speed Down", &speedDown, 0.01, 10.0f, "%0.4f") || !m_isInitParameters)
- {
- m_exposureControlSettings->SetEyeAdaptationSpeedDown(speedDown);
- m_exposureControlSettings->OnConfigChanged();
- }
- ImGui::Unindent();
- }
- m_isInitParameters = true;
- }
- ImGui::Unindent();
- ImGui::Separator();
- m_imguiSidebar.End();
- }
- } // namespace AtomSampleViewer
|