RadialBlur.qml 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright (C) 2022 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import Qt5Compat.GraphicalEffects.private
  6. /*!
  7. \qmltype RadialBlur
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since Qt5Compat.GraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-motion-blur
  12. \brief Applies directional blur in a circular direction around the items
  13. center point.
  14. Effect creates perceived impression that the source item appears to be
  15. rotating to the direction of the blur.
  16. Other available motionblur effects are
  17. \l{Qt5Compat.GraphicalEffects::ZoomBlur}{ZoomBlur} and
  18. \l{Qt5Compat.GraphicalEffects::DirectionalBlur}{DirectionalBlur}.
  19. \table
  20. \header
  21. \li Source
  22. \li Effect applied
  23. \row
  24. \li \image Original_bug.png
  25. \li \image RadialBlur_bug.png
  26. \endtable
  27. \note This effect is available when running with OpenGL.
  28. \section1 Example Usage
  29. The following example shows how to apply the effect.
  30. \snippet RadialBlur-example.qml example
  31. */
  32. Item {
  33. id: rootItem
  34. /*!
  35. This property defines the source item that is going to be blurred.
  36. \note It is not supported to let the effect include itself, for
  37. instance by setting source to the effect's parent.
  38. */
  39. property variant source
  40. /*!
  41. This property defines the direction for the blur and at the same time
  42. the level of blurring. The larger the angle, the more the result becomes
  43. blurred. The quality of the blur depends on
  44. \l{RadialBlur::samples}{samples} property. If angle value is large, more
  45. samples are needed to keep the visual quality at high level.
  46. Allowed values are between 0.0 and 360.0. By default the property is set
  47. to \c 0.0.
  48. \table
  49. \header
  50. \li Output examples with different angle values
  51. \li
  52. \li
  53. \row
  54. \li \image RadialBlur_angle1.png
  55. \li \image RadialBlur_angle2.png
  56. \li \image RadialBlur_angle3.png
  57. \row
  58. \li \b { angle: 0.0 }
  59. \li \b { angle: 15.0 }
  60. \li \b { angle: 30.0 }
  61. \row
  62. \li \l samples: 24
  63. \li \l samples: 24
  64. \li \l samples: 24
  65. \row
  66. \li \l horizontalOffset: 0
  67. \li \l horizontalOffset: 0
  68. \li \l horizontalOffset: 0
  69. \row
  70. \li \l verticalOffset: 0
  71. \li \l verticalOffset: 0
  72. \li \l verticalOffset: 0
  73. \endtable
  74. */
  75. property real angle: 0.0
  76. /*!
  77. This property defines how many samples are taken per pixel when blur
  78. calculation is done. Larger value produces better quality, but is slower
  79. to render.
  80. This property is not intended to be animated. Changing this property may
  81. cause the underlying OpenGL shaders to be recompiled.
  82. Allowed values are between 0 and inf (practical maximum depends on GPU).
  83. By default the property is set to \c 0 (no samples).
  84. */
  85. property int samples: 0
  86. /*!
  87. \qmlproperty real QtGraphicalEffects::RadialBlur::horizontalOffset
  88. \qmlproperty real QtGraphicalEffects::RadialBlur::verticalOffset
  89. These properties define the offset in pixels for the perceived center
  90. point of the rotation.
  91. Allowed values are between -inf and inf.
  92. By default these properties are set to \c 0.
  93. \table
  94. \header
  95. \li Output examples with different horizontalOffset values
  96. \li
  97. \li
  98. \row
  99. \li \image RadialBlur_horizontalOffset1.png
  100. \li \image RadialBlur_horizontalOffset2.png
  101. \li \image RadialBlur_horizontalOffset3.png
  102. \row
  103. \li \b { horizontalOffset: 75.0 }
  104. \li \b { horizontalOffset: 0.0 }
  105. \li \b { horizontalOffset: -75.0 }
  106. \row
  107. \li \l samples: 24
  108. \li \l samples: 24
  109. \li \l samples: 24
  110. \row
  111. \li \l angle: 20
  112. \li \l angle: 20
  113. \li \l angle: 20
  114. \row
  115. \li \l verticalOffset: 0
  116. \li \l verticalOffset: 0
  117. \li \l verticalOffset: 0
  118. \endtable
  119. */
  120. property real horizontalOffset: 0.0
  121. property real verticalOffset: 0.0
  122. /*!
  123. This property defines the blur behavior near the edges of the item,
  124. where the pixel blurring is affected by the pixels outside the source
  125. edges.
  126. If the property is set to \c true, the pixels outside the source are
  127. interpreted to be transparent, which is similar to OpenGL
  128. clamp-to-border extension. The blur is expanded slightly outside the
  129. effect item area.
  130. If the property is set to \c false, the pixels outside the source are
  131. interpreted to contain the same color as the pixels at the edge of the
  132. item, which is similar to OpenGL clamp-to-edge behavior. The blur does
  133. not expand outside the effect item area.
  134. By default, the property is set to \c false.
  135. */
  136. property bool transparentBorder: false
  137. /*!
  138. This property allows the effect output pixels to be cached in order to
  139. improve the rendering performance.
  140. Every time the source or effect properties are changed, the pixels in
  141. the cache must be updated. Memory consumption is increased, because an
  142. extra buffer of memory is required for storing the effect output.
  143. It is recommended to disable the cache when the source or the effect
  144. properties are animated.
  145. By default, the property is set to \c false.
  146. */
  147. property bool cached: false
  148. SourceProxy {
  149. id: sourceProxy
  150. input: rootItem.source
  151. sourceRect: shaderItem.transparentBorder ? Qt.rect(-1, -1, parent.width + 2.0, parent.height + 2.0) : Qt.rect(0, 0, 0, 0)
  152. }
  153. ShaderEffectSource {
  154. id: cacheItem
  155. anchors.fill: shaderItem
  156. visible: rootItem.cached
  157. smooth: true
  158. sourceItem: shaderItem
  159. live: true
  160. hideSource: visible
  161. }
  162. ShaderEffect {
  163. id: shaderItem
  164. property variant source: sourceProxy.output
  165. property variant center: Qt.point(0.5 + rootItem.horizontalOffset / parent.width, 0.5 + rootItem.verticalOffset / parent.height)
  166. property bool transparentBorder: rootItem.transparentBorder && rootItem.samples > 1
  167. property int samples: rootItem.samples
  168. property real weight: 1.0 / Math.max(1.0, rootItem.samples)
  169. property real angleSin: Math.sin(rootItem.angle/2 * Math.PI/180)
  170. property real angleCos: Math.cos(rootItem.angle/2 * Math.PI/180)
  171. property real angleSinStep: Math.sin(-rootItem.angle * Math.PI/180 / Math.max(1.0, rootItem.samples - 1))
  172. property real angleCosStep: Math.cos(-rootItem.angle * Math.PI/180 / Math.max(1.0, rootItem.samples - 1))
  173. property variant expandPixels: transparentBorder ? Qt.size(0.5 * parent.height, 0.5 * parent.width) : Qt.size(0,0)
  174. property variant expand: transparentBorder ? Qt.size(expandPixels.width / width, expandPixels.height / height) : Qt.size(0,0)
  175. property variant delta: Qt.size(1.0 / rootItem.width, 1.0 / rootItem.height)
  176. property real w: parent.width
  177. property real h: parent.height
  178. x: transparentBorder ? -expandPixels.width - 1 : 0
  179. y: transparentBorder ? -expandPixels.height - 1 : 0
  180. width: transparentBorder ? parent.width + expandPixels.width * 2.0 + 2 : parent.width
  181. height: transparentBorder ? parent.height + expandPixels.height * 2.0 + 2 : parent.height
  182. property string fragmentShaderSkeleton: "#version 440
  183. layout(location = 0) in vec2 qt_TexCoord0;
  184. layout(location = 0) out vec4 fragColor;
  185. layout(std140, binding = 0) uniform buf {
  186. mat4 qt_Matrix;
  187. float qt_Opacity;
  188. float angleSin;
  189. float angleCos;
  190. float angleSinStep;
  191. float angleCosStep;
  192. float weight;
  193. vec2 expand;
  194. vec2 center;
  195. vec2 delta;
  196. float w;
  197. float h;
  198. };
  199. layout(binding = 1) uniform sampler2D source;
  200. void main() {
  201. mat2 m;
  202. fragColor = vec4(0.0);
  203. vec2 texCoord = qt_TexCoord0;
  204. PLACEHOLDER_EXPAND_STEPS
  205. vec2 dir = vec2(texCoord.s * w - w * center.x, texCoord.t * h - h * center.y);
  206. m[0] = vec2(angleCos, -angleSin);
  207. m[1] = vec2(angleSin, angleCos);
  208. dir *= m;
  209. m[0] = vec2(angleCosStep, -angleSinStep);
  210. m[1] = vec2(angleSinStep, angleCosStep);
  211. PLACEHOLDER_UNROLLED_LOOP
  212. fragColor *= weight * qt_Opacity;
  213. }
  214. "
  215. function buildFragmentShader() {
  216. var shader = fragmentShaderSkeleton
  217. var expandSteps = ""
  218. if (transparentBorder) {
  219. expandSteps += "texCoord = (texCoord - expand) / (1.0 - 2.0 * expand);"
  220. }
  221. var unrolledLoop = "fragColor += texture(source, texCoord);\n"
  222. if (rootItem.samples > 1) {
  223. unrolledLoop = ""
  224. for (var i = 0; i < rootItem.samples; i++)
  225. unrolledLoop += "fragColor += texture(source, center + dir * delta); dir *= m;\n"
  226. }
  227. shader = shader.replace("PLACEHOLDER_EXPAND_STEPS", expandSteps)
  228. fragmentShader = ShaderBuilder.buildFragmentShader(shader.replace("PLACEHOLDER_UNROLLED_LOOP", unrolledLoop))
  229. }
  230. onFragmentShaderChanged: sourceChanged()
  231. onSamplesChanged: buildFragmentShader()
  232. onTransparentBorderChanged: buildFragmentShader()
  233. Component.onCompleted: buildFragmentShader()
  234. }
  235. }