DirectionalBlur.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Graphical Effects module.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. import QtQuick 2.12
  40. import QtGraphicalEffects.private 1.12
  41. /*!
  42. \qmltype DirectionalBlur
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-motion-blur
  47. \brief Applies blur effect to the specified direction.
  48. Effect creates perceived impression that the source item appears to be
  49. moving in the direction of the blur. Blur is applied to both sides of
  50. each pixel, therefore setting the direction to 0 and 180 provides the
  51. same result.
  52. Other available motionblur effects are \l{QtGraphicalEffects::ZoomBlur}{ZoomBlur} and
  53. \l{QtGraphicalEffects::RadialBlur}{RadialBlur}.
  54. \table
  55. \header
  56. \li Source
  57. \li Effect applied
  58. \row
  59. \li \image Original_bug.png
  60. \li \image DirectionalBlur_bug.png
  61. \endtable
  62. \note This effect is available when running with OpenGL.
  63. \section1 Example
  64. The following example shows how to apply the effect.
  65. \snippet DirectionalBlur-example.qml example
  66. */
  67. Item {
  68. id: rootItem
  69. /*!
  70. This property defines the source item that is going to be blurred.
  71. \note It is not supported to let the effect include itself, for
  72. instance by setting source to the effect's parent.
  73. */
  74. property variant source
  75. /*!
  76. This property defines the perceived amount of movement for each pixel.
  77. The movement is divided evenly to both sides of each pixel.
  78. The quality of the blur depends on \l{DirectionalBlur::samples}{samples}
  79. property. If length value is large, more samples are needed to keep the
  80. visual quality at high level.
  81. The value ranges from 0.0 to inf.
  82. By default the property is set to \c 0.0 (no blur).
  83. \table
  84. \header
  85. \li Output examples with different length values
  86. \li
  87. \li
  88. \row
  89. \li \image DirectionalBlur_length1.png
  90. \li \image DirectionalBlur_length2.png
  91. \li \image DirectionalBlur_length3.png
  92. \row
  93. \li \b { length: 0.0 }
  94. \li \b { length: 32.0 }
  95. \li \b { length: 48.0 }
  96. \row
  97. \li \l samples: 24
  98. \li \l samples: 24
  99. \li \l samples: 24
  100. \row
  101. \li \l angle: 0
  102. \li \l angle: 0
  103. \li \l angle: 0
  104. \endtable
  105. */
  106. property real length: 0.0
  107. /*!
  108. This property defines how many samples are taken per pixel when blur
  109. calculation is done. Larger value produces better quality, but is slower
  110. to render.
  111. This property is not intended to be animated. Changing this property may
  112. cause the underlying OpenGL shaders to be recompiled.
  113. Allowed values are between 0 and inf (practical maximum depends on GPU).
  114. By default the property is set to \c 0 (no samples).
  115. */
  116. property int samples: 0
  117. /*!
  118. This property defines the direction for the blur. Blur is applied to
  119. both sides of each pixel, therefore setting the direction to 0 and 180
  120. produces the same result.
  121. The value ranges from -180.0 to 180.0.
  122. By default the property is set to \c 0.0.
  123. \table
  124. \header
  125. \li Output examples with different angle values
  126. \li
  127. \li
  128. \row
  129. \li \image DirectionalBlur_angle1.png
  130. \li \image DirectionalBlur_angle2.png
  131. \li \image DirectionalBlur_angle3.png
  132. \row
  133. \li \b { angle: 0.0 }
  134. \li \b { angle: 45.0 }
  135. \li \b { angle: 90.0 }
  136. \row
  137. \li \l samples: 24
  138. \li \l samples: 24
  139. \li \l samples: 24
  140. \row
  141. \li \l length: 32
  142. \li \l length: 32
  143. \li \l length: 32
  144. \endtable
  145. */
  146. property real angle: 0.0
  147. /*!
  148. This property defines the blur behavior near the edges of the item,
  149. where the pixel blurring is affected by the pixels outside the source
  150. edges.
  151. If the property is set to \c true, the pixels outside the source are
  152. interpreted to be transparent, which is similar to OpenGL
  153. clamp-to-border extension. The blur is expanded slightly outside the
  154. effect item area.
  155. If the property is set to \c false, the pixels outside the source are
  156. interpreted to contain the same color as the pixels at the edge of the
  157. item, which is similar to OpenGL clamp-to-edge behavior. The blur does
  158. not expand outside the effect item area.
  159. By default, the property is set to \c false.
  160. */
  161. property bool transparentBorder: false
  162. /*!
  163. This property allows the effect output pixels to be cached in order to
  164. improve the rendering performance.
  165. Every time the source or effect properties are changed, the pixels in
  166. the cache must be updated. Memory consumption is increased, because an
  167. extra buffer of memory is required for storing the effect output.
  168. It is recommended to disable the cache when the source or the effect
  169. properties are animated.
  170. By default, the property is set to \c false.
  171. */
  172. property bool cached: false
  173. SourceProxy {
  174. id: sourceProxy
  175. input: rootItem.source
  176. sourceRect: rootItem.transparentBorder ? Qt.rect(-1, -1, parent.width + 2.0, parent.height + 2.0) : Qt.rect(0, 0, 0, 0)
  177. }
  178. ShaderEffectSource {
  179. id: cacheItem
  180. anchors.fill: shaderItem
  181. visible: rootItem.cached
  182. smooth: true
  183. sourceItem: shaderItem
  184. live: true
  185. hideSource: visible
  186. }
  187. ShaderEffect {
  188. id: shaderItem
  189. property variant source: sourceProxy.output
  190. property real len: rootItem.length
  191. property bool transparentBorder: rootItem.transparentBorder
  192. property real samples: rootItem.samples
  193. property real weight: 1.0 / Math.max(1.0, rootItem.samples)
  194. property variant expandPixels: transparentBorder ? Qt.size(rootItem.samples, rootItem.samples) : Qt.size(0,0)
  195. property variant expand: transparentBorder ? Qt.size(expandPixels.width / width, expandPixels.height / height) : Qt.size(0,0)
  196. property variant delta: Qt.size(1.0 / rootItem.width * Math.cos((rootItem.angle + 90) * Math.PI/180), 1.0 / rootItem.height * Math.sin((rootItem.angle + 90) * Math.PI/180))
  197. x: transparentBorder ? -expandPixels.width - 1: 0
  198. y: transparentBorder ? -expandPixels.height - 1 : 0
  199. width: transparentBorder ? parent.width + 2.0 * expandPixels.width + 2 : parent.width
  200. height: transparentBorder ? parent.height + 2.0 * expandPixels.height + 2 : parent.height
  201. property string fragmentShaderSkeleton: "
  202. varying highp vec2 qt_TexCoord0;
  203. uniform highp float qt_Opacity;
  204. uniform lowp sampler2D source;
  205. uniform highp float len;
  206. uniform highp float samples;
  207. uniform highp float weight;
  208. uniform highp vec2 expand;
  209. uniform highp vec2 delta;
  210. void main(void) {
  211. highp vec2 shift = delta * len / max(1.0, samples - 1.0);
  212. mediump vec2 texCoord = qt_TexCoord0;
  213. gl_FragColor = vec4(0.0);
  214. PLACEHOLDER_EXPAND_STEPS
  215. texCoord -= shift * max(0.0, samples - 1.0) * 0.5;
  216. PLACEHOLDER_UNROLLED_LOOP
  217. gl_FragColor *= weight * qt_Opacity;
  218. }
  219. "
  220. function buildFragmentShader() {
  221. var shader = ""
  222. if (GraphicsInfo.profile === GraphicsInfo.OpenGLCoreProfile)
  223. shader += "#version 150 core\n#define varying in\n#define texture2D texture\nout vec4 fragColor;\n#define gl_FragColor fragColor\n"
  224. shader += fragmentShaderSkeleton
  225. var expandSteps = ""
  226. if (transparentBorder) {
  227. expandSteps += "texCoord = (texCoord - expand) / (1.0 - 2.0 * expand);"
  228. }
  229. var unrolledLoop = "gl_FragColor += texture2D(source, texCoord);\n"
  230. if (rootItem.samples > 1) {
  231. unrolledLoop = ""
  232. for (var i = 0; i < rootItem.samples; i++)
  233. unrolledLoop += "gl_FragColor += texture2D(source, texCoord); texCoord += shift;\n"
  234. }
  235. shader = shader.replace("PLACEHOLDER_EXPAND_STEPS", expandSteps)
  236. fragmentShader = shader.replace("PLACEHOLDER_UNROLLED_LOOP", unrolledLoop)
  237. }
  238. onFragmentShaderChanged: sourceChanged()
  239. onSamplesChanged: buildFragmentShader()
  240. onTransparentBorderChanged: buildFragmentShader()
  241. Component.onCompleted: buildFragmentShader()
  242. }
  243. }