EyeMaterialExampleComponent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.fbx.azmodel";
  17. static const char* MaterialPath = "materials/eye/001_EyeBasic.azmaterial";
  18. static const float DefaultCameraHeadingDegrees = 129.6f;
  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. const float headingRadians = AZ::DegToRad(DefaultCameraHeadingDegrees);
  83. AZ::Debug::ArcBallControllerRequestBus::Event(
  84. GetCameraEntityId(), &AZ::Debug::ArcBallControllerRequestBus::Events::SetHeading, headingRadians);
  85. // Lighting
  86. m_defaultIbl.Init(m_scene);
  87. // Model
  88. m_modelAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>(MeshPath, AZ::RPI::AssetUtils::TraceLevel::Assert);
  89. // Material
  90. m_materialAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::MaterialAsset>(MaterialPath, AZ::RPI::AssetUtils::TraceLevel::Assert);
  91. }
  92. void EyeMaterialExampleComponent::InitializeMaterialProperties()
  93. {
  94. // Get material indices of properties
  95. m_irisColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisColorName));
  96. m_irisColorFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisColorFactorName));
  97. m_irisRoughnessIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisRoughnessName));
  98. m_scleraColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraColorName));
  99. m_scleraColorFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraColorFactorName));
  100. m_scleraRoughnessIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraRoughnessName));
  101. m_scleraNormalFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(ScleraNormalFactorName));
  102. m_irisDepthIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisDepthName));
  103. m_irisRadiusIndex = m_materialInstance->FindPropertyIndex(AZ::Name(IrisRadiusName));
  104. m_innerEyeIORIndex = m_materialInstance->FindPropertyIndex(AZ::Name(InnerEyeIORName));
  105. m_limbusSizeIndex = m_materialInstance->FindPropertyIndex(AZ::Name(LimbusSizeName));
  106. m_specularFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SpecularFactorName));
  107. m_SSSEnableIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSEnableName));
  108. m_SSSColorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSColorName));
  109. m_SSSFactorIndex = m_materialInstance->FindPropertyIndex(AZ::Name(SSSFactorName));
  110. // Assign material property values to the GUI variables so that ImGui displays them properly
  111. m_materialInstance->GetPropertyValue(m_irisColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_irisColor);
  112. m_irisColorFactor = m_materialInstance->GetPropertyValue(m_irisColorFactorIndex).GetValue<float>();
  113. m_irisRoughness = m_materialInstance->GetPropertyValue(m_irisRoughnessIndex).GetValue<float>();
  114. m_materialInstance->GetPropertyValue(m_scleraColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_scleraColor);
  115. m_scleraColorFactor = m_materialInstance->GetPropertyValue(m_scleraColorFactorIndex).GetValue<float>();
  116. m_scleraRoughness = m_materialInstance->GetPropertyValue(m_scleraRoughnessIndex).GetValue<float>();
  117. m_scleraNormalFactor = m_materialInstance->GetPropertyValue(m_scleraNormalFactorIndex).GetValue<float>();
  118. m_irisDepth = m_materialInstance->GetPropertyValue(m_irisDepthIndex).GetValue<float>();
  119. m_irisRadius = m_materialInstance->GetPropertyValue(m_irisRadiusIndex).GetValue<float>();
  120. m_innerEyeIOR = m_materialInstance->GetPropertyValue(m_innerEyeIORIndex).GetValue<float>();
  121. m_limbusSize = m_materialInstance->GetPropertyValue(m_limbusSizeIndex).GetValue<float>();
  122. m_specularFactor = m_materialInstance->GetPropertyValue(m_specularFactorIndex).GetValue<float>();
  123. m_SSSEnable = m_materialInstance->GetPropertyValue(m_SSSEnableIndex).GetValue<bool>();
  124. m_materialInstance->GetPropertyValue(m_SSSColorIndex).GetValue<AZ::Color>().GetAsVector3().StoreToFloat3(m_SSSColor);
  125. m_SSSFactor = m_materialInstance->GetPropertyValue(m_SSSFactorIndex).GetValue<float>();
  126. }
  127. void EyeMaterialExampleComponent::DrawSidebar()
  128. {
  129. using namespace AZ::Render;
  130. if (m_imguiSidebar.Begin())
  131. {
  132. ImGui::Spacing();
  133. DrawSidebarMaterialProperties();
  134. ImGui::Separator();
  135. if (ScriptableImGui::Button("Material Details..."))
  136. {
  137. m_imguiMaterialDetails.OpenDialog();
  138. }
  139. m_imguiSidebar.End();
  140. }
  141. m_imguiMaterialDetails.Tick(&GetMeshFeatureProcessor()->GetDrawPackets(m_meshHandle), "Eye Mesh");
  142. }
  143. void EyeMaterialExampleComponent::DrawSidebarMaterialProperties()
  144. {
  145. bool eyeSettingsChanged = false;
  146. if (ImGui::CollapsingHeader("Iris", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  147. {
  148. ImGui::Indent();
  149. if (ScriptableImGui::ColorEdit3("Color##irisColor", m_irisColor, ImGuiColorEditFlags_None))
  150. {
  151. m_materialInstance->SetPropertyValue(m_irisColorIndex, AZ::Color(m_irisColor[0], m_irisColor[1], m_irisColor[2], 1.0));
  152. eyeSettingsChanged = true;
  153. }
  154. if (ScriptableImGui::SliderFloat("Color Factor##irisColorFactor", &m_irisColorFactor, 0.f, 1.f, "%.3f"))
  155. {
  156. m_materialInstance->SetPropertyValue(m_irisColorFactorIndex, m_irisColorFactor);
  157. eyeSettingsChanged = true;
  158. }
  159. if (ScriptableImGui::SliderFloat("Roughness##irisRoughness", &m_irisRoughness, 0.f, 1.f, "%.3f"))
  160. {
  161. m_materialInstance->SetPropertyValue(m_irisRoughnessIndex, m_irisRoughness);
  162. eyeSettingsChanged = true;
  163. }
  164. ImGui::Unindent();
  165. }
  166. if (ImGui::CollapsingHeader("Sclera", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  167. {
  168. ImGui::Indent();
  169. if (ScriptableImGui::ColorEdit3("Color##scleraColor", m_scleraColor, ImGuiColorEditFlags_None))
  170. {
  171. m_materialInstance->SetPropertyValue(m_scleraColorIndex, AZ::Color(m_scleraColor[0], m_scleraColor[1], m_scleraColor[2], 1.0));
  172. eyeSettingsChanged = true;
  173. }
  174. if (ScriptableImGui::SliderFloat("Color Factor##scleraColorFactor", &m_scleraColorFactor, 0.f, 1.f, "%.3f"))
  175. {
  176. m_materialInstance->SetPropertyValue(m_scleraColorFactorIndex, m_scleraColorFactor);
  177. eyeSettingsChanged = true;
  178. }
  179. if (ScriptableImGui::SliderFloat("Roughness##scleraRoughness", &m_scleraRoughness, 0.f, 1.f, "%.3f"))
  180. {
  181. m_materialInstance->SetPropertyValue(m_scleraRoughnessIndex, m_scleraRoughness);
  182. eyeSettingsChanged = true;
  183. }
  184. if (ScriptableImGui::SliderFloat("Normal Factor", &m_scleraNormalFactor, 0.f, 1.f, "%.3f"))
  185. {
  186. m_materialInstance->SetPropertyValue(m_scleraNormalFactorIndex, m_scleraNormalFactor);
  187. eyeSettingsChanged = true;
  188. }
  189. ImGui::Unindent();
  190. }
  191. if (ImGui::CollapsingHeader("General Eye Properties", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  192. {
  193. ImGui::Indent();
  194. if (ScriptableImGui::SliderFloat("Iris Depth", &m_irisDepth, 0.f, 0.5f, "%.3f"))
  195. {
  196. m_materialInstance->SetPropertyValue(m_irisDepthIndex, m_irisDepth);
  197. eyeSettingsChanged = true;
  198. }
  199. if (ScriptableImGui::SliderFloat("Inner Radius", &m_irisRadius, 0.f, 0.5f, "%.3f"))
  200. {
  201. m_materialInstance->SetPropertyValue(m_irisRadiusIndex, m_irisRadius);
  202. eyeSettingsChanged = true;
  203. }
  204. if (ScriptableImGui::SliderFloat("Inner IOR", &m_innerEyeIOR, 1.f, 2.f, "%.3f"))
  205. {
  206. m_materialInstance->SetPropertyValue(m_innerEyeIORIndex, m_innerEyeIOR);
  207. eyeSettingsChanged = true;
  208. }
  209. if (ScriptableImGui::SliderFloat("Limbus Size", &m_limbusSize, 0.f, 0.5f, "%.3f", ImGuiSliderFlags_Logarithmic))
  210. {
  211. m_materialInstance->SetPropertyValue(m_limbusSizeIndex, m_limbusSize);
  212. eyeSettingsChanged = true;
  213. }
  214. ImGui::Unindent();
  215. }
  216. if (ImGui::CollapsingHeader("Specular F0", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  217. {
  218. ImGui::Indent();
  219. if (ScriptableImGui::SliderFloat("Factor##specularF0Factor", &m_specularFactor, 0.f, 1.f, "%.3f"))
  220. {
  221. m_materialInstance->SetPropertyValue(m_specularFactorIndex, m_specularFactor);
  222. eyeSettingsChanged = true;
  223. }
  224. ImGui::Unindent();
  225. }
  226. if (ImGui::CollapsingHeader("Subsurface Scattering", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  227. {
  228. ImGui::Indent();
  229. if (ScriptableImGui::Checkbox("Enable##enableSSS", &m_SSSEnable))
  230. {
  231. m_materialInstance->SetPropertyValue(m_SSSEnableIndex, m_SSSEnable);
  232. eyeSettingsChanged = true;
  233. }
  234. if (ScriptableImGui::ColorEdit3("Color##SSSColor", m_SSSColor, ImGuiColorEditFlags_None))
  235. {
  236. m_materialInstance->SetPropertyValue(m_SSSColorIndex, AZ::Color(m_SSSColor[0], m_SSSColor[1], m_SSSColor[2], 1.0));
  237. eyeSettingsChanged = true;
  238. }
  239. if (ScriptableImGui::SliderFloat("Factor##SSSColorFactor", &m_SSSFactor, 0.f, 1.f, "%.3f"))
  240. {
  241. m_materialInstance->SetPropertyValue(m_SSSFactorIndex, m_SSSFactor);
  242. eyeSettingsChanged = true;
  243. }
  244. ImGui::Unindent();
  245. }
  246. bool transformChanged = false;
  247. if (ImGui::CollapsingHeader("Eye Orientation", ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_Framed))
  248. {
  249. ImGui::Indent();
  250. if (ScriptableImGui::SliderFloat3("Rotation", m_rotationEuler, -180.0, 180.0)) {
  251. transformChanged = true;
  252. m_eyeTransform.SetRotation(AZ::Quaternion::CreateFromEulerAnglesDegrees(AZ::Vector3(m_rotationEuler[0], m_rotationEuler[1], m_rotationEuler[2])));
  253. }
  254. ImGui::Unindent();
  255. }
  256. if (eyeSettingsChanged)
  257. {
  258. m_materialInstance->Compile();
  259. }
  260. if (transformChanged)
  261. {
  262. GetMeshFeatureProcessor()->SetTransform(m_meshHandle, m_eyeTransform);
  263. }
  264. }
  265. }