UseTextureFunctor.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "./UseTextureFunctor.h"
  9. #include <Atom/RPI.Public/Material/Material.h>
  10. #include <Atom/RPI.Reflect/Image/Image.h>
  11. #include <Atom/RPI.Reflect/Shader/ShaderOptionGroup.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. void UseTextureFunctor::Reflect(ReflectContext* context)
  17. {
  18. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  19. {
  20. serializeContext->Class<UseTextureFunctor, RPI::MaterialFunctor>()
  21. ->Version(5)
  22. ->Field("texturePropertyIndex", &UseTextureFunctor::m_texturePropertyIndex)
  23. ->Field("useTexturePropertyIndex", &UseTextureFunctor::m_useTexturePropertyIndex)
  24. ->Field("dependentPropertyIndexes", &UseTextureFunctor::m_dependentPropertyIndexes)
  25. ->Field("useTextureOptionName", &UseTextureFunctor::m_useTextureOptionName)
  26. ;
  27. }
  28. }
  29. void UseTextureFunctor::Process(RPI::MaterialFunctorAPI::RuntimeContext& context)
  30. {
  31. using namespace RPI;
  32. auto texture = context.GetMaterialPropertyValue<Data::Instance<Image>>(m_texturePropertyIndex);
  33. bool useTextureFlag = context.GetMaterialPropertyValue<bool>(m_useTexturePropertyIndex);
  34. ShaderOptionValue useTexture{useTextureFlag && nullptr != texture};
  35. context.SetShaderOptionValue(m_useTextureOptionName, useTexture);
  36. }
  37. void UseTextureFunctor::Process(RPI::MaterialFunctorAPI::EditorContext& context)
  38. {
  39. const bool useTextureFlag = context.GetMaterialPropertyValue<bool>(m_useTexturePropertyIndex);
  40. Data::Instance<RPI::Image> image = context.GetMaterialPropertyValue<Data::Instance<RPI::Image>>(m_texturePropertyIndex);
  41. context.SetMaterialPropertyVisibility(
  42. m_useTexturePropertyIndex,
  43. nullptr != image ? RPI::MaterialPropertyVisibility::Enabled : RPI::MaterialPropertyVisibility::Hidden
  44. );
  45. RPI::MaterialPropertyVisibility dependentVisibility = RPI::MaterialPropertyVisibility::Enabled;
  46. if (nullptr == image)
  47. {
  48. dependentVisibility = RPI::MaterialPropertyVisibility::Hidden;
  49. }
  50. else if (!useTextureFlag)
  51. {
  52. dependentVisibility = RPI::MaterialPropertyVisibility::Disabled;
  53. }
  54. // These properties are enabled only when the texture is actually going to be sampled by the shader.
  55. for (RPI::MaterialPropertyIndex index : m_dependentPropertyIndexes)
  56. {
  57. context.SetMaterialPropertyVisibility(index, dependentVisibility);
  58. }
  59. }
  60. } // namespace Render
  61. } // namespace AZ