EditorModeCommon.azsli 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <viewsrg_all.srgi>
  9. #include <Atom/Features/SrgSemantics.azsli>
  10. #include <Atom/Features/PostProcessing/FullscreenPixelInfo.azsli>
  11. #include <Atom/Features/PostProcessing/FullscreenVertex.azsli>
  12. //! Returns the linear depth value of the specified non-linear depth value.
  13. float CalculateLinearDepth(const float zDepth)
  14. {
  15. return abs(((ViewSrg::GetFarZTimesNearZ()) / (ViewSrg::GetFarZMinusNearZ() * zDepth - ViewSrg::GetFarZ())));
  16. }
  17. //! Returns the calculated color after blending original framebuffer color with the calculated feedback effect.
  18. float4 CalculateOutputColor(const float3 inColor, const float3 finalEffect, const float t)
  19. {
  20. float4 outColor;
  21. outColor.rgb = lerp(inColor, finalEffect, t);
  22. outColor.a = 1.0;
  23. return outColor;
  24. }
  25. ShaderResourceGroup PassSrg : SRG_PerPass_WithFallback
  26. {
  27. //! The color buffer to apply the editor mode feedback effects to.
  28. Texture2D<float4> m_framebuffer;
  29. //! The non-linear depth buffer data for fragments in the color buffer.
  30. Texture2D<float4> m_depth;
  31. //! The entity mask containing the visible fragments of entities of interest.
  32. //! @note Red channel contains the visible fragments, green channel contains the visible and occluded fragments.
  33. Texture2D<float2> m_entityMask;
  34. //! Dimensions of mask texture (xy is width and height, zw is 1/width and 1/height).
  35. float4 m_maskDimensions;
  36. //! The final blend amount that is used to scale the calculated blend values.
  37. float m_finalBlendAmount;
  38. ///////////////////////////////////
  39. //! EditorModeDesaturation related data
  40. //! Amount of desaturation to apply.
  41. float m_desaturationAmount;
  42. ///////////////////////////////////
  43. ///////////////////////////////////
  44. //! EditorModeOutline related data
  45. //! Outline line thickness.
  46. float m_lineThickness;
  47. //! Outline line color.
  48. float4 m_lineColor;
  49. //! Outline style to use (0 = OutlineStyle::Always, 1 = OutlineStyle::Visible)
  50. uint m_outlineStyle;
  51. ///////////////////////////////////
  52. ///////////////////////////////////
  53. //! EditorModeBlur related data
  54. //! Width of kernel to apply box blur effect.
  55. float m_kernelHalfWidth;
  56. //! The direction to perform the blur (0 = horizontal, 1 = vertical).
  57. uint m_direction;
  58. ///////////////////////////////////
  59. ///////////////////////////////////
  60. //! EditorModeTint related data
  61. //! Amount of tint to apply.
  62. float m_tintAmount;
  63. //! Color of tint to apply.
  64. float4 m_tintColor;
  65. ///////////////////////////////////
  66. ///////////////////////////////////
  67. //! EditorModeDepthTransition related data
  68. /* -----------------
  69. \ E /
  70. \#####|#####/
  71. \####|####/
  72. \###|###/
  73. \##|##/
  74. \-S-/
  75. \ /
  76. V
  77. V = View position
  78. S = Depth transition start
  79. E = Depth transition end
  80. When E is a non-zero value, the binary mask value will be transformed into a value from [m_minDepthTransitionValue, 1.0]
  81. according to the fragment's depth position in the transition band (#) spanning from S to E. This is to allow feedback
  82. effects to be scaled according to the distance of non-mask fragments to the viewer.
  83. */
  84. //! The minimum blend amount that will be calculated through depth transitioning.
  85. float m_minDepthTransitionValue;
  86. //! Start of depth transtion for non-mask effect blending.
  87. //! @note S in the diagram above.
  88. float m_depthTransitionStart;
  89. //! The duration (depth) of the depth transition band (0.0 = no depth transitioning will be applied).
  90. //! @note E - S in the diagram above.
  91. float m_depthTransitionDuration;
  92. ///////////////////////////////////
  93. Sampler LinearSampler
  94. {
  95. MinFilter = Linear;
  96. MagFilter = Linear;
  97. MipFilter = Linear;
  98. AddressU = Clamp;
  99. AddressV = Clamp;
  100. AddressW = Clamp;
  101. };
  102. Sampler PointSampler
  103. {
  104. MinFilter = Point;
  105. MagFilter = Point;
  106. MipFilter = Point;
  107. AddressU = Clamp;
  108. AddressV = Clamp;
  109. AddressW = Clamp;
  110. };
  111. //! Returns the final blend amount after the final blend amount scale is applied.
  112. float CalculateFinalBlendAmount(const float t)
  113. {
  114. // Apply the final blend amount modifier
  115. return lerp(0.0, t, PassSrg::m_finalBlendAmount);
  116. }
  117. //! Calculates the final blend anount and returns the blended output color.
  118. float4 CalculateFinalBlendAmountAndOutputColor(const float3 inColor, const float3 finalEffect, const float t)
  119. {
  120. return CalculateOutputColor(inColor, finalEffect, PassSrg::CalculateFinalBlendAmount(t));
  121. }
  122. //! Calculates the blend amount (0.0 = input color, 1.0 = final effect) as scaled by the fragment's linear depth according to the depth transition values.
  123. float CalculateTransitionBlendAmountFromLinearDepth(const float linearDepth, const float maskValue)
  124. {
  125. if(PassSrg::m_depthTransitionDuration > 0.0)
  126. {
  127. const float depthTransition = clamp((linearDepth - PassSrg::m_depthTransitionStart) / (PassSrg::m_depthTransitionDuration), PassSrg::m_minDepthTransitionValue, 1.0);
  128. return clamp((1.0 - maskValue) * depthTransition, 0.0, 1.0);
  129. }
  130. return 1.0 - maskValue;
  131. }
  132. //! Calculates the blend amount (0.0 = input color, 1.0 = final effect) as scaled by the fragment's non-linear depth according to the depth transition values.
  133. float CalculateTransitionBlendAmountFromDepth(const float zDepth, const float maskValue)
  134. {
  135. const float linearDepth = CalculateLinearDepth(zDepth);
  136. return CalculateTransitionBlendAmountFromLinearDepth(linearDepth, maskValue);
  137. }
  138. }