SkinnedMeshExampleComponent.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <SkinnedMeshExampleComponent.h>
  9. #include <SampleComponentManager.h>
  10. #include <SampleComponentConfig.h>
  11. #include <Automation/ScriptableImGui.h>
  12. #include <Atom/Feature/SkinnedMesh/SkinnedMeshInputBuffers.h>
  13. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  14. #include <Atom/Component/DebugCamera/NoClipControllerBus.h>
  15. #include <AzCore/Script/ScriptTimePoint.h>
  16. #include <Atom/RPI.Public/View.h>
  17. #include <Atom/RPI.Public/Image/StreamingImage.h>
  18. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  19. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  20. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  21. #include <RHI/BasicRHIComponent.h>
  22. namespace AtomSampleViewer
  23. {
  24. void SkinnedMeshExampleComponent::Reflect(AZ::ReflectContext* context)
  25. {
  26. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  27. {
  28. serializeContext->Class < SkinnedMeshExampleComponent, AZ::Component>()
  29. ->Version(0)
  30. ;
  31. }
  32. }
  33. void SkinnedMeshExampleComponent::Activate()
  34. {
  35. CreateSkinnedMeshContainer();
  36. m_skinnedMeshContainer->SetActiveSkinnedMeshCount(1);
  37. AZ::TickBus::Handler::BusConnect();
  38. m_imguiSidebar.Activate();
  39. ConfigureCamera();
  40. AddImageBasedLight();
  41. }
  42. void SkinnedMeshExampleComponent::CreatePlaneObject()
  43. {
  44. auto meshAsset = AZ::RPI::AssetUtils::GetAssetByProductPath<AZ::RPI::ModelAsset>("objects/plane.fbx.azmodel");
  45. m_planeMeshHandle = GetMeshFeatureProcessor()->AcquireMesh(AZ::Render::MeshHandleDescriptor(meshAsset));
  46. GetMeshFeatureProcessor()->SetTransform(m_planeMeshHandle, AZ::Transform::CreateIdentity());
  47. }
  48. void SkinnedMeshExampleComponent::Deactivate()
  49. {
  50. m_skinnedMeshContainer = nullptr;
  51. AZ::TickBus::Handler::BusDisconnect();
  52. m_imguiSidebar.Deactivate();
  53. GetMeshFeatureProcessor()->ReleaseMesh(m_planeMeshHandle);
  54. m_defaultIbl.Reset();
  55. }
  56. void SkinnedMeshExampleComponent::AddImageBasedLight()
  57. {
  58. m_defaultIbl.Init(m_scene);
  59. }
  60. void SkinnedMeshExampleComponent::ConfigureCamera()
  61. {
  62. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  63. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  64. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, AZ::Vector3(0.0f, -1.0f, 1.0f));
  65. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, -0.8f);
  66. }
  67. void SkinnedMeshExampleComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  68. {
  69. m_runTime += deltaTime;
  70. DrawSidebar();
  71. if (!m_useFixedTime)
  72. {
  73. m_skinnedMeshContainer->UpdateAnimation(m_runTime, m_useOutOfSyncBoneAnimation);
  74. }
  75. if (m_drawBones)
  76. {
  77. m_skinnedMeshContainer->DrawBones();
  78. }
  79. }
  80. void SkinnedMeshExampleComponent::DrawSidebar()
  81. {
  82. if (!m_imguiSidebar.Begin())
  83. {
  84. return;
  85. }
  86. SkinnedMeshConfig config = m_skinnedMeshContainer->GetSkinnedMeshConfig();
  87. bool configWasModified = false;
  88. // Imgui limits slider range to half the natural range of the type
  89. float segmentCountFloat = static_cast<float>(config.m_segmentCount);
  90. configWasModified |= ScriptableImGui::SliderFloat("Segments Per-Mesh", &segmentCountFloat, 2.0f, 2048.0f, "%.0f", ImGuiSliderFlags_Logarithmic);
  91. configWasModified |= ScriptableImGui::SliderInt("Vertices Per-Segment", &config.m_verticesPerSegment, 4, 2048);
  92. configWasModified |= ScriptableImGui::SliderInt("Bones Per-Mesh", &config.m_boneCount, 4, 256);
  93. configWasModified |= ScriptableImGui::SliderInt("Influences Per-Vertex", &config.m_influencesPerVertex, 4, 4);
  94. configWasModified |= ScriptableImGui::SliderInt("Sub-mesh count", &config.m_subMeshCount, 1, 128);
  95. if (configWasModified)
  96. {
  97. config.m_segmentCount = static_cast<int>(segmentCountFloat);
  98. m_skinnedMeshContainer->SetSkinnedMeshConfig(config);
  99. }
  100. bool animationWasModified = configWasModified;
  101. animationWasModified |= ScriptableImGui::Checkbox("Use Fixed Animation Time", &m_useFixedTime);
  102. animationWasModified |= ScriptableImGui::SliderFloat("Fixed Animation Time", &m_fixedAnimationTime, 0.0f, 20.0f);
  103. animationWasModified |= ScriptableImGui::Checkbox("Use Out of Sync Bone Animation", &m_useOutOfSyncBoneAnimation);
  104. if (m_useFixedTime && animationWasModified)
  105. {
  106. m_skinnedMeshContainer->UpdateAnimation(m_fixedAnimationTime, m_useOutOfSyncBoneAnimation);
  107. }
  108. ScriptableImGui::Checkbox("Draw bones", &m_drawBones);
  109. if (ScriptableImGui::Button("Reset Clock"))
  110. {
  111. m_runTime = 0;
  112. }
  113. m_imguiSidebar.End();
  114. }
  115. void SkinnedMeshExampleComponent::CreateSkinnedMeshContainer()
  116. {
  117. const auto skinnedMeshFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::SkinnedMeshFeatureProcessorInterface>();
  118. const auto meshFeatureProcessor = m_scene->GetFeatureProcessor<AZ::Render::MeshFeatureProcessorInterface>();
  119. // Default settings for the sample
  120. SkinnedMeshConfig config;
  121. config.m_segmentCount = 10;
  122. config.m_verticesPerSegment = 7;
  123. config.m_boneCount = 4;
  124. config.m_influencesPerVertex = 4;
  125. m_skinnedMeshContainer = AZStd::make_unique<SkinnedMeshContainer>(skinnedMeshFeatureProcessor, meshFeatureProcessor, config);
  126. }
  127. }