ShadingExampleComponent.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <ShadingExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  10. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  11. #include <Atom/Feature/LuxCore/LuxCoreBus.h>
  12. #include <Atom/RPI.Public/View.h>
  13. #include <Atom/RPI.Public/Image/StreamingImage.h>
  14. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  15. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  16. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  17. #include <AzFramework/Input/Devices/Keyboard/InputDeviceKeyboard.h>
  18. #include <SampleComponentManager.h>
  19. #include <SampleComponentConfig.h>
  20. #include <RHI/BasicRHIComponent.h>
  21. namespace AtomSampleViewer
  22. {
  23. const char* ShadingExampleComponent::CameraControllerNameTable[CameraControllerCount] =
  24. {
  25. "ArcBall",
  26. "NoClip"
  27. };
  28. void ShadingExampleComponent::Reflect(AZ::ReflectContext* context)
  29. {
  30. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  31. {
  32. serializeContext->Class < ShadingExampleComponent, AZ::Component>()
  33. ->Version(0)
  34. ;
  35. }
  36. }
  37. ShadingExampleComponent::ShadingExampleComponent()
  38. {
  39. }
  40. void ShadingExampleComponent::Activate()
  41. {
  42. AZ::RPI::AssetUtils::TraceLevel traceLevel = AZ::RPI::AssetUtils::TraceLevel::Assert;
  43. auto meshAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/shaderball_simple.azmodel", traceLevel);
  44. auto materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(DefaultPbrMaterialPath, traceLevel);
  45. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ meshAsset }, AZ::RPI::Material::FindOrCreate(materialAsset));
  46. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, AZ::Transform::CreateIdentity());
  47. UseArcBallCameraController();
  48. // Add an Image based light.
  49. m_defaultIbl.Init(m_scene);
  50. // Load default IBL texture asset for LuxCore
  51. // We should be able to extract this information from SkyBox in the future
  52. LoadIBLImage("textures/sampleenvironment/example_iblskyboxcm.dds.streamingimage");
  53. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::SetCameraEntityID, GetCameraEntityId());
  54. AzFramework::InputChannelEventListener::Connect();
  55. AZ::TickBus::Handler::BusConnect();
  56. }
  57. void ShadingExampleComponent::Deactivate()
  58. {
  59. RemoveController();
  60. m_defaultIbl.Reset();
  61. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  62. AzFramework::InputChannelEventListener::Disconnect();
  63. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::ClearLuxCore);
  64. AZ::TickBus::Handler::BusDisconnect();
  65. }
  66. void ShadingExampleComponent::UseArcBallCameraController()
  67. {
  68. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  69. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  70. }
  71. void ShadingExampleComponent::UseNoClipCameraController()
  72. {
  73. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  74. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  75. }
  76. void ShadingExampleComponent::LoadIBLImage(const char* imagePath)
  77. {
  78. auto imageAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::StreamingImageAsset>
  79. (imagePath, AZ::RPI::AssetUtils::TraceLevel::Assert);
  80. // FindOrCreate will synchronously load the image if necessary.
  81. IBLImage = AZ::RPI::StreamingImage::FindOrCreate(imageAsset);
  82. AZ_Assert(IBLImage != nullptr, "Failed to find or create an image instance from image asset");
  83. }
  84. bool ShadingExampleComponent::OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel)
  85. {
  86. const AzFramework::InputChannelId& inputChannelId = inputChannel.GetInputChannelId();
  87. switch (inputChannel.GetState())
  88. {
  89. case AzFramework::InputChannel::State::Ended:
  90. {
  91. // L key pressed
  92. if (inputChannelId == AzFramework::InputDeviceKeyboard::Key::AlphanumericL)
  93. {
  94. if (!m_renderSent)
  95. {
  96. m_renderSent = true;
  97. }
  98. }
  99. }
  100. default:
  101. {
  102. break;
  103. }
  104. }
  105. return false;
  106. }
  107. void ShadingExampleComponent::RemoveController()
  108. {
  109. }
  110. void ShadingExampleComponent::SetArcBallControllerParams()
  111. {
  112. }
  113. void ShadingExampleComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  114. {
  115. AZ_UNUSED(deltaTime);
  116. AZ_UNUSED(time);
  117. if (m_renderSent)
  118. {
  119. if (!m_dataLoaded)
  120. {
  121. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::ClearLuxCore);
  122. AZ::Data::Asset<AZ::RPI::ModelAsset> modelAsset = GetMeshFeatureProcessor()->GetModel(m_meshHandle)->GetModelAsset();
  123. AZ::Data::Instance<AZ::RPI::Material> materialInstance = GetMeshFeatureProcessor()->GetMaterialAssignmentMap(m_meshHandle).at(AZ::Render::DefaultMaterialAssignmentId).m_materialInstance;
  124. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::AddMesh, modelAsset);
  125. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::AddMaterial, materialInstance);
  126. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::AddObject, modelAsset, materialInstance->GetId());
  127. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::AddTexture, IBLImage, AZ::Render::LuxCoreTextureType::IBL);
  128. m_dataLoaded = true;
  129. }
  130. // wait till texture ready
  131. AZ::Render::LuxCoreRequestsBus::BroadcastResult(m_textureReady, &AZ::Render::LuxCoreRequestsBus::Events::CheckTextureStatus);
  132. if (m_textureReady)
  133. {
  134. AZ::Render::LuxCoreRequestsBus::Broadcast(&AZ::Render::LuxCoreRequestsBus::Events::RenderInLuxCore);
  135. m_renderSent = false;
  136. m_dataLoaded = false;
  137. }
  138. }
  139. }
  140. }