EyeMaterialExampleComponent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <EyeMaterialExampleComponent.h>
  9. #include <Atom/Component/DebugCamera/ArcBallControllerComponent.h>
  10. #include <Atom/RPI.Public/RPISystemInterface.h>
  11. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  12. #include <RHI/BasicRHIComponent.h>
  13. #include <Automation/ScriptableImGui.h>
  14. namespace AtomSampleViewer
  15. {
  16. static const char* MeshPath = "objects/eye.azmodel";
  17. static const char* MaterialPath = "materials/eye/001_EyeBasic.azmaterial";
  18. static const float DefaultCameraHeading = 40.0f;
  19. static const float DefaultCameraDistance = 2.0f;
  20. static const char* IrisColorName = "iris.baseColor.color";
  21. static const char* IrisColorFactorName = "iris.baseColor.factor";
  22. static const char* IrisRoughnessName = "iris.roughness.factor";
  23. static const char* ScleraColorName = "sclera.baseColor.color";
  24. static const char* ScleraColorFactorName = "sclera.baseColor.factor";
  25. static const char* ScleraRoughnessName = "sclera.roughness.factor";
  26. static const char* ScleraNormalFactorName = "sclera.normal.factor";
  27. static const char* IrisDepthName = "eye.irisDepth";
  28. static const char* IrisRadiusName = "eye.irisRadius";
  29. static const char* InnerEyeIORName = "eye.innerEyeIOR";
  30. static const char* LimbusSizeName = "eye.limbusSize";
  31. static const char* SpecularFactorName = "specularF0.factor";
  32. static const char* SSSEnableName = "subsurfaceScattering.enableSubsurfaceScattering";
  33. static const char* SSSColorName = "subsurfaceScattering.scatterColor";
  34. static const char* SSSFactorName = "subsurfaceScattering.subsurfaceScatterFactor";
  35. void EyeMaterialExampleComponent::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serializeContext->Class<EyeMaterialExampleComponent, AZ::Component>()->Version(0);
  40. }
  41. }
  42. EyeMaterialExampleComponent::EyeMaterialExampleComponent()
  43. {
  44. }
  45. void EyeMaterialExampleComponent::Activate()
  46. {
  47. Prepare();
  48. m_eyeTransform = AZ::Transform::CreateIdentity();
  49. LoadMesh(m_eyeTransform);
  50. InitializeMaterialProperties();
  51. AZ::TickBus::Handler::BusConnect();
  52. }
  53. void EyeMaterialExampleComponent::Deactivate()
  54. {
  55. AZ::Debug::CameraControllerRequestBus::Event(
  56. GetCameraEntityId(),
  57. &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  58. m_defaultIbl.Reset();
  59. GetMeshFeatureProcessor()->ReleaseMesh(m_meshHandle);
  60. AZ::TickBus::Handler::BusDisconnect();
  61. }
  62. void EyeMaterialExampleComponent::OnTick(float deltaTime, AZ::ScriptTimePoint scriptTime)
  63. {
  64. AZ_UNUSED(deltaTime);
  65. AZ_UNUSED(scriptTime);
  66. DrawSidebar();
  67. }
  68. void EyeMaterialExampleComponent::LoadMesh(AZ::Transform transform)
  69. {
  70. m_materialInstance = AZ::RPI::Material::Create(m_materialAsset);
  71. m_meshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor{ m_modelAsset }, m_materialInstance);
  72. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, transform);
  73. }
  74. void EyeMaterialExampleComponent::Prepare()
  75. {
  76. // Camera
  77. AZ::Debug::CameraControllerRequestBus::Event(
  78. GetCameraEntityId(),
  79. &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  80. azrtti_typeid<AZ::Debug::ArcBallControllerComponent>());
  81. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetDistance, DefaultCameraDistance);
  82. AZ::Debug::ArcBallControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, DefaultCameraHeading);
  83. // Lighting
  84. m_defaultIbl.Init(m_scene);
  85. // Model
  86. m_modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>(MeshPath, AZ::RPI::AssetUtils::TraceLevel::Assert);
  87. // Material
  88. m_materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(MaterialPath, AZ::RPI::AssetUtils::TraceLevel::Assert);
  89. }
  90. void EyeMaterialExampleComponent::InitializeMaterialProperties()
  91. {
  92. // Get material indices of properties
  93. m_irisColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisColorName));
  94. m_irisColorFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisColorFactorName));
  95. m_irisRoughnessIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisRoughnessName));
  96. m_scleraColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraColorName));
  97. m_scleraColorFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraColorFactorName));
  98. m_scleraRoughnessIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraRoughnessName));
  99. m_scleraNormalFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraNormalFactorName));
  100. m_irisDepthIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisDepthName));
  101. m_irisRadiusIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisRadiusName));
  102. m_innerEyeIORIndex = m_materialInstance->FindPropertyIndex(AZ::Name(InnerEyeIORName));
  103. m_limbusSizeIndex = m_materialInstance->FindPropertyIndex(AZ::Name(LimbusSizeName));
  104. m_specularFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SpecularFactorName));
  105. m_SSSEnableIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSEnableName));
  106. m_SSSColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSColorName));
  107. m_SSSFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSFactorName));
  108. // Assign material property values to the GUI variables so that ImGui displays them properly
  109. m_materialInstance->GetPropertyValue(m_irisColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_irisColor);
  110. m_irisColorFactor = m_materialInstance->GetPropertyValue(m_irisColorFactorIndex).GetValue<float>();
  111. m_irisRoughness = m_materialInstance->GetPropertyValue(m_irisRoughnessIndex).GetValue<float>();
  112. m_materialInstance->GetPropertyValue(m_scleraColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_scleraColor);
  113. m_scleraColorFactor = m_materialInstance->GetPropertyValue(m_scleraColorFactorIndex).GetValue<float>();
  114. m_scleraRoughness = m_materialInstance->GetPropertyValue(m_scleraRoughnessIndex).GetValue<float>();
  115. m_scleraNormalFactor = m_materialInstance->GetPropertyValue(m_scleraNormalFactorIndex).GetValue<float>();
  116. m_irisDepth = m_materialInstance->GetPropertyValue(m_irisDepthIndex).GetValue<float>();
  117. m_irisRadius = m_materialInstance->GetPropertyValue(m_irisRadiusIndex).GetValue<float>();
  118. m_innerEyeIOR = m_materialInstance->GetPropertyValue(m_innerEyeIORIndex).GetValue<float>();
  119. m_limbusSize = m_materialInstance->GetPropertyValue(m_limbusSizeIndex).GetValue<float>();
  120. m_specularFactor = m_materialInstance->GetPropertyValue(m_specularFactorIndex).GetValue<float>();
  121. m_SSSEnable = m_materialInstance->GetPropertyValue(m_SSSEnableIndex).GetValue<bool>();
  122. m_materialInstance->GetPropertyValue(m_SSSColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_SSSColor);
  123. m_SSSFactor = m_materialInstance->GetPropertyValue(m_SSSFactorIndex).GetValue<float>();
  124. }
  125. void EyeMaterialExampleComponent::DrawSidebar()
  126. {
  127. using namespace AZ::Render;
  128. if (m_imguiSidebar.Begin())
  129. {
  130. ImGui::Spacing();
  131. DrawSidebarMaterialProperties();
  132. ImGui::Separator();
  133. if (ScriptableImGui::Button("Material Details..."))
  134. {
  135. m_imguiMaterialDetails.OpenDialog();
  136. }
  137. m_imguiSidebar.End();
  138. }
  139. m_imguiMaterialDetails.Tick(&GetMeshFeatureProcessor()->GetDrawPackets(m_meshHandle), "Eye Mesh");
  140. }
  141. void EyeMaterialExampleComponent::DrawSidebarMaterialProperties()
  142. {
  143. bool eyeSettingsChanged = false;
  144. if (ImGui::CollapsingHeader("Iris", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  145. {
  146. ImGui::Indent();
  147. if (ScriptableImGui::ColorEdit3("Color##irisColor", m_irisColor, ImGuiColorEditFlags_None))
  148. {
  149. m_materialInstance->SetPropertyValue(m_irisColorIndex, AZ::Color(m_irisColor[0], m_irisColor[1], m_irisColor[2], 1.0));
  150. eyeSettingsChanged = true;
  151. }
  152. if (ScriptableImGui::SliderFloat("Color Factor##irisColorFactor", &m_irisColorFactor, 0.f, 1.f, "%.3f"))
  153. {
  154. m_materialInstance->SetPropertyValue(m_irisColorFactorIndex, m_irisColorFactor);
  155. eyeSettingsChanged = true;
  156. }
  157. if (ScriptableImGui::SliderFloat("Roughness##irisRoughness", &m_irisRoughness, 0.f, 1.f, "%.3f"))
  158. {
  159. m_materialInstance->SetPropertyValue(m_irisRoughnessIndex, m_irisRoughness);
  160. eyeSettingsChanged = true;
  161. }
  162. ImGui::Unindent();
  163. }
  164. if (ImGui::CollapsingHeader("Sclera", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  165. {
  166. ImGui::Indent();
  167. if (ScriptableImGui::ColorEdit3("Color##scleraColor", m_scleraColor, ImGuiColorEditFlags_None))
  168. {
  169. m_materialInstance->SetPropertyValue(m_scleraColorIndex, AZ::Color(m_scleraColor[0], m_scleraColor[1], m_scleraColor[2], 1.0));
  170. eyeSettingsChanged = true;
  171. }
  172. if (ScriptableImGui::SliderFloat("Color Factor##scleraColorFactor", &m_scleraColorFactor, 0.f, 1.f, "%.3f"))
  173. {
  174. m_materialInstance->SetPropertyValue(m_scleraColorFactorIndex, m_scleraColorFactor);
  175. eyeSettingsChanged = true;
  176. }
  177. if (ScriptableImGui::SliderFloat("Roughness##scleraRoughness", &m_scleraRoughness, 0.f, 1.f, "%.3f"))
  178. {
  179. m_materialInstance->SetPropertyValue(m_scleraRoughnessIndex, m_scleraRoughness);
  180. eyeSettingsChanged = true;
  181. }
  182. if (ScriptableImGui::SliderFloat("Normal Factor", &m_scleraNormalFactor, 0.f, 1.f, "%.3f"))
  183. {
  184. m_materialInstance->SetPropertyValue(m_scleraNormalFactorIndex, m_scleraNormalFactor);
  185. eyeSettingsChanged = true;
  186. }
  187. ImGui::Unindent();
  188. }
  189. if (ImGui::CollapsingHeader("General Eye Properties", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  190. {
  191. ImGui::Indent();
  192. if (ScriptableImGui::SliderFloat("Iris Depth", &m_irisDepth, 0.f, 0.5f, "%.3f"))
  193. {
  194. m_materialInstance->SetPropertyValue(m_irisDepthIndex, m_irisDepth);
  195. eyeSettingsChanged = true;
  196. }
  197. if (ScriptableImGui::SliderFloat("Inner Radius", &m_irisRadius, 0.f, 0.5f, "%.3f"))
  198. {
  199. m_materialInstance->SetPropertyValue(m_irisRadiusIndex, m_irisRadius);
  200. eyeSettingsChanged = true;
  201. }
  202. if (ScriptableImGui::SliderFloat("Inner IOR", &m_innerEyeIOR, 1.f, 2.f, "%.3f"))
  203. {
  204. m_materialInstance->SetPropertyValue(m_innerEyeIORIndex, m_innerEyeIOR);
  205. eyeSettingsChanged = true;
  206. }
  207. if (ScriptableImGui::SliderFloat("Limbus Size", &m_limbusSize, 0.f, 0.5f, "%.3f", ImGuiSliderFlags_Logarithmic))
  208. {
  209. m_materialInstance->SetPropertyValue(m_limbusSizeIndex, m_limbusSize);
  210. eyeSettingsChanged = true;
  211. }
  212. ImGui::Unindent();
  213. }
  214. if (ImGui::CollapsingHeader("Specular F0", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  215. {
  216. ImGui::Indent();
  217. if (ScriptableImGui::SliderFloat("Factor##specularF0Factor", &m_specularFactor, 0.f, 1.f, "%.3f"))
  218. {
  219. m_materialInstance->SetPropertyValue(m_specularFactorIndex, m_specularFactor);
  220. eyeSettingsChanged = true;
  221. }
  222. ImGui::Unindent();
  223. }
  224. if (ImGui::CollapsingHeader("Subsurface Scattering", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  225. {
  226. ImGui::Indent();
  227. if (ScriptableImGui::Checkbox("Enable##enableSSS", &m_SSSEnable))
  228. {
  229. m_materialInstance->SetPropertyValue(m_SSSEnableIndex, m_SSSEnable);
  230. eyeSettingsChanged = true;
  231. }
  232. if (ScriptableImGui::ColorEdit3("Color##SSSColor", m_SSSColor, ImGuiColorEditFlags_None))
  233. {
  234. m_materialInstance->SetPropertyValue(m_SSSColorIndex, AZ::Color(m_SSSColor[0], m_SSSColor[1], m_SSSColor[2], 1.0));
  235. eyeSettingsChanged = true;
  236. }
  237. if (ScriptableImGui::SliderFloat("Factor##SSSColorFactor", &m_SSSFactor, 0.f, 1.f, "%.3f"))
  238. {
  239. m_materialInstance->SetPropertyValue(m_SSSFactorIndex, m_SSSFactor);
  240. eyeSettingsChanged = true;
  241. }
  242. ImGui::Unindent();
  243. }
  244. bool transformChanged = false;
  245. if (ImGui::CollapsingHeader("Eye Orientation", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  246. {
  247. ImGui::Indent();
  248. if (ScriptableImGui::SliderFloat3("Rotation", m_rotationEuler, -180.0, 180.0)) {
  249. transformChanged = true;
  250. m_eyeTransform.SetRotation(AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(m_rotationEuler[0], m_rotationEuler[1], m_rotationEuler[2])));
  251. }
  252. ImGui::Unindent();
  253. }
  254. if (eyeSettingsChanged)
  255. {
  256. m_materialInstance->Compile();
  257. }
  258. if (transformChanged)
  259. {
  260. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, m_eyeTransform);
  261. }
  262. }
  263. }