Displace.qml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Displace
  43. \inqmlmodule QtGraphicalEffects
  44. \since QtGraphicalEffects 1.0
  45. \inherits QtQuick2::Item
  46. \ingroup qtgraphicaleffects-distortion
  47. \brief Moves the pixels of the source item according to the given
  48. displacement map.
  49. \table
  50. \header
  51. \li Source
  52. \li DisplacementSource
  53. \li Effect applied
  54. \row
  55. \li \image Original_bug.png
  56. \li \image Displace_map.png
  57. \li \image Displace_bug.png
  58. \endtable
  59. \note This effect is available when running with OpenGL.
  60. \section1 Example
  61. The following example shows how to apply the effect.
  62. \snippet Displace-example.qml example
  63. */
  64. Item {
  65. id: rootItem
  66. /*!
  67. This property defines the source item for the pixels that are going to
  68. be displaced according to the data from
  69. \l{Displace::displacementSource}{displacementSource}.
  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
  76. displacement map. The displacementSource item gets rendered into the
  77. intermediate pixel buffer. The red and green component values from the
  78. result determine the displacement of the pixels from the source item.
  79. The format for the displacement map is similar to the tangent space
  80. normal maps, which can be created with most 3D-modeling tools. Many
  81. image processing tools include the support for generating normal maps.
  82. Alternatively, the displacement map for this effect can also be a QML
  83. element which is colored appropriately. Like any QML element, it can be
  84. animated. It is recommended that the size of the diplacement map matches
  85. the size of the \l{Displace::source}{source}.
  86. The displace data is interpreted in the RGBA format. For every pixel:
  87. the red channel stores the x-axis displacement, and the green channel
  88. stores the y-axis displacement. Blue and alpha channels are ignored for
  89. this effect.
  90. Assuming that red channel value 1.0 is fully red (0.0 having no red at
  91. all), this effect considers pixel component value 0.5 to cause no
  92. displacement at all. Values above 0.5 shift pixels to the left, values
  93. below 0.5 do the shift to the right. In a similar way, green channel
  94. values above 0.5 displace the pixels upwards, and values below 0.5 shift
  95. the pixels downwards. The actual amount of displacement in pixels
  96. depends on the \l displacement property.
  97. */
  98. property variant displacementSource
  99. /*!
  100. This property defines the scale for the displacement. The bigger scale,
  101. the bigger the displacement of the pixels. The value set to 0.0 causes
  102. no displacement.
  103. The value ranges from -1.0 (inverted maximum shift, according to
  104. displacementSource) to 1.0 (maximum shift, according to
  105. displacementSource). By default, the property is set to \c 0.0 (no
  106. displacement).
  107. \table
  108. \header
  109. \li Output examples with different displacement values
  110. \li
  111. \li
  112. \row
  113. \li \image Displace_displacement1.png
  114. \li \image Displace_displacement2.png
  115. \li \image Displace_displacement3.png
  116. \row
  117. \li \b { displacement: -0.2 }
  118. \li \b { displacement: 0.0 }
  119. \li \b { displacement: 0.2 }
  120. \endtable
  121. */
  122. property real displacement: 0.0
  123. /*!
  124. This property allows the effect output pixels to be cached in order to
  125. improve the rendering performance.
  126. Every time the source or effect properties are changed, the pixels in
  127. the cache must be updated. Memory consumption is increased, because an
  128. extra buffer of memory is required for storing the effect output.
  129. It is recommended to disable the cache when the source or the effect
  130. properties are animated.
  131. By default, the property is set to \c false.
  132. */
  133. property bool cached: false
  134. SourceProxy {
  135. id: sourceProxy
  136. input: rootItem.source
  137. }
  138. SourceProxy {
  139. id: displacementSourceProxy
  140. input: rootItem.displacementSource
  141. }
  142. ShaderEffectSource {
  143. id: cacheItem
  144. anchors.fill: parent
  145. visible: rootItem.cached
  146. smooth: true
  147. sourceItem: shaderItem
  148. live: true
  149. hideSource: visible
  150. }
  151. ShaderEffect {
  152. id: shaderItem
  153. property variant source: sourceProxy.output
  154. property variant displacementSource: displacementSourceProxy.output
  155. property real displacement: rootItem.displacement
  156. property real xPixel: 1.0/width
  157. property real yPixel: 1.0/height
  158. anchors.fill: parent
  159. fragmentShader: "qrc:/qt-project.org/imports/QtGraphicalEffects/shaders/displace.frag"
  160. }
  161. }