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