Displace.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (C) 2020 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 Displace
  8. \inqmlmodule Qt5Compat.GraphicalEffects
  9. \since QtGraphicalEffects 1.0
  10. \inherits QtQuick2::Item
  11. \ingroup qtgraphicaleffects-distortion
  12. \brief Moves the pixels of the source item according to the given
  13. displacement map.
  14. \table
  15. \header
  16. \li Source
  17. \li DisplacementSource
  18. \li Effect applied
  19. \row
  20. \li \image Original_bug.png
  21. \li \image Displace_map.png
  22. \li \image Displace_bug.png
  23. \endtable
  24. \section1 Example
  25. The following example shows how to apply the effect.
  26. \snippet Displace-example.qml example
  27. */
  28. Item {
  29. id: rootItem
  30. /*!
  31. This property defines the source item for the pixels that are going to
  32. be displaced according to the data from
  33. \l{Displace::displacementSource}{displacementSource}.
  34. \note It is not supported to let the effect include itself, for
  35. instance by setting source to the effect's parent.
  36. */
  37. property variant source
  38. /*!
  39. This property defines the item that is going to be used as the
  40. displacement map. The displacementSource item gets rendered into the
  41. intermediate pixel buffer. The red and green component values from the
  42. result determine the displacement of the pixels from the source item.
  43. The format for the displacement map is similar to the tangent space
  44. normal maps, which can be created with most 3D-modeling tools. Many
  45. image processing tools include the support for generating normal maps.
  46. Alternatively, the displacement map for this effect can also be a QML
  47. element which is colored appropriately. Like any QML element, it can be
  48. animated. It is recommended that the size of the diplacement map matches
  49. the size of the \l{Displace::source}{source}.
  50. The displace data is interpreted in the RGBA format. For every pixel:
  51. the red channel stores the x-axis displacement, and the green channel
  52. stores the y-axis displacement. Blue and alpha channels are ignored for
  53. this effect.
  54. Assuming that red channel value 1.0 is fully red (0.0 having no red at
  55. all), this effect considers pixel component value 0.5 to cause no
  56. displacement at all. Values above 0.5 shift pixels to the left, values
  57. below 0.5 do the shift to the right. In a similar way, green channel
  58. values above 0.5 displace the pixels upwards, and values below 0.5 shift
  59. the pixels downwards. The actual amount of displacement in pixels
  60. depends on the \l displacement property.
  61. */
  62. property variant displacementSource
  63. /*!
  64. This property defines the scale for the displacement. The bigger scale,
  65. the bigger the displacement of the pixels. The value set to 0.0 causes
  66. no displacement.
  67. The value ranges from -1.0 (inverted maximum shift, according to
  68. displacementSource) to 1.0 (maximum shift, according to
  69. displacementSource). By default, the property is set to \c 0.0 (no
  70. displacement).
  71. \table
  72. \header
  73. \li Output examples with different displacement values
  74. \li
  75. \li
  76. \row
  77. \li \image Displace_displacement1.png
  78. \li \image Displace_displacement2.png
  79. \li \image Displace_displacement3.png
  80. \row
  81. \li \b { displacement: -0.2 }
  82. \li \b { displacement: 0.0 }
  83. \li \b { displacement: 0.2 }
  84. \endtable
  85. */
  86. property real displacement: 0.0
  87. /*!
  88. This property allows the effect output pixels to be cached in order to
  89. improve the rendering performance.
  90. Every time the source or effect properties are changed, the pixels in
  91. the cache must be updated. Memory consumption is increased, because an
  92. extra buffer of memory is required for storing the effect output.
  93. It is recommended to disable the cache when the source or the effect
  94. properties are animated.
  95. By default, the property is set to \c false.
  96. */
  97. property bool cached: false
  98. SourceProxy {
  99. id: sourceProxy
  100. input: rootItem.source
  101. }
  102. SourceProxy {
  103. id: displacementSourceProxy
  104. input: rootItem.displacementSource
  105. }
  106. ShaderEffectSource {
  107. id: cacheItem
  108. anchors.fill: parent
  109. visible: rootItem.cached
  110. smooth: true
  111. sourceItem: shaderItem
  112. live: true
  113. hideSource: visible
  114. }
  115. ShaderEffect {
  116. id: shaderItem
  117. property variant source: sourceProxy.output
  118. property variant displacementSource: displacementSourceProxy.output
  119. property real displacement: rootItem.displacement
  120. property real xPixel: 1.0/width
  121. property real yPixel: 1.0/height
  122. anchors.fill: parent
  123. fragmentShader: "qrc:/qt-project.org/imports/Qt5Compat/GraphicalEffects/shaders_ng/displace.frag.qsb"
  124. }
  125. }