ThresholdMask.qml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 ThresholdMask
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-mask
  47. \brief Masks the source item with another item and applies a threshold
  48. value.
  49. The masking behavior can be controlled with the \l threshold value for the
  50. mask pixels.
  51. \table
  52. \header
  53. \li Source
  54. \li MaskSource
  55. \li Effect applied
  56. \row
  57. \li \image Original_bug.png
  58. \li \image ThresholdMask_mask.png
  59. \li \image ThresholdMask_bug.png
  60. \endtable
  61. \note This effect is available when running with OpenGL.
  62. \section1 Example
  63. The following example shows how to apply the effect.
  64. \snippet ThresholdMask-example.qml example
  65. */
  66. Item {
  67. id: rootItem
  68. /*!
  69. This property defines the source item that is going to be masked.
  70. \note It is not supported to let the effect include itself, for
  71. instance by setting source to the effect's parent.
  72. */
  73. property variant source
  74. /*!
  75. This property defines the item that is going to be used as the mask.
  76. Mask item gets rendered into an intermediate pixel buffer and the alpha
  77. values from the result are used to determine the source item's pixels
  78. visibility in the display.
  79. \table
  80. \header
  81. \li Original
  82. \li Mask
  83. \li Effect applied
  84. \row
  85. \li \image Original_bug.png
  86. \li \image ThresholdMask_mask.png
  87. \li \image ThresholdMask_bug.png
  88. \endtable
  89. \note It is not supported to let the effect include itself, for
  90. instance by setting maskSource to the effect's parent.
  91. */
  92. property variant maskSource
  93. /*!
  94. This property defines a threshold value for the mask pixels. The mask
  95. pixels that have an alpha value below this property are used to
  96. completely mask away the corresponding pixels from the source item. The
  97. mask pixels that have a higher alpha value are used to alphablend the
  98. source item to the display.
  99. The value ranges from 0.0 (alpha value 0) to 1.0 (alpha value 255). By
  100. default, the property is set to \c 0.0.
  101. \table
  102. \header
  103. \li Output examples with different threshold values
  104. \li
  105. \li
  106. \row
  107. \li \image ThresholdMask_threshold1.png
  108. \li \image ThresholdMask_threshold2.png
  109. \li \image ThresholdMask_threshold3.png
  110. \row
  111. \li \b { threshold: 0.0 }
  112. \li \b { threshold: 0.5 }
  113. \li \b { threshold: 0.7 }
  114. \row
  115. \li \l spread: 0.2
  116. \li \l spread: 0.2
  117. \li \l spread: 0.2
  118. \endtable
  119. */
  120. property real threshold: 0.0
  121. /*!
  122. This property defines the smoothness of the mask edges near the
  123. \l{ThresholdMask::threshold}{threshold} alpha value. Setting spread to
  124. 0.0 uses mask normally with the specified threshold. Setting higher
  125. spread values softens the transition from the transparent mask pixels
  126. towards opaque mask pixels by adding interpolated values between them.
  127. The value ranges from 0.0 (sharp mask edge) to 1.0 (smooth mask edge).
  128. By default, the property is set to \c 0.0.
  129. \table
  130. \header
  131. \li Output examples with different spread values
  132. \li
  133. \li
  134. \row
  135. \li \image ThresholdMask_spread1.png
  136. \li \image ThresholdMask_spread2.png
  137. \li \image ThresholdMask_spread3.png
  138. \row
  139. \li \b { spread: 0.0 }
  140. \li \b { spread: 0.2 }
  141. \li \b { spread: 0.8 }
  142. \row
  143. \li \l threshold: 0.4
  144. \li \l threshold: 0.4
  145. \li \l threshold: 0.4
  146. \endtable
  147. */
  148. property real spread: 0.0
  149. /*!
  150. This property allows the effect output pixels to be cached in order to
  151. improve the rendering performance.
  152. Every time the source or effect properties are changed, the pixels in
  153. the cache must be updated. Memory consumption is increased, because an
  154. extra buffer of memory is required for storing the effect output.
  155. It is recommended to disable the cache when the source or the effect
  156. properties are animated.
  157. By default, the property is set to \c false.
  158. */
  159. property bool cached: false
  160. SourceProxy {
  161. id: sourceProxy
  162. input: rootItem.source
  163. }
  164. SourceProxy {
  165. id: maskSourceProxy
  166. input: rootItem.maskSource
  167. }
  168. ShaderEffectSource {
  169. id: cacheItem
  170. anchors.fill: parent
  171. visible: rootItem.cached
  172. smooth: true
  173. sourceItem: shaderItem
  174. live: true
  175. hideSource: visible
  176. }
  177. ShaderEffect {
  178. id: shaderItem
  179. property variant source: sourceProxy.output
  180. property variant maskSource: maskSourceProxy.output
  181. property real threshold: rootItem.threshold
  182. property real spread: rootItem.spread
  183. anchors.fill: parent
  184. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/thresholdmask.frag"
  185. }
  186. }