aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick/shadereffects/shadereffects.qml
blob: b820c8131e914ae5002cc09f8df78231459abe5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.0
import "content"

Image {
    width: 320
    height: 480
/*!
    \title QML Examples - Shader Effects
    \example declarative/shadereffects
    \image qml-shadereffects-example.png
    \brief This is a shader effects example

    This example demonstrates a couple of visual effects that you can perform
    with shaders in QtQuick 2.0
*/
    source: "../snake/content/pics/background.png"

    Flickable {
        anchors.fill: parent
        contentWidth: 640
        contentHeight: 360

        ShaderEffectSource {
            id: theSource
            sourceItem: theItem
            smooth: true
        }

        function saturate(x) {
            return Math.min(Math.max(x, 0), 1)
        }

        function sliderToColor(x) {
            return Qt.rgba(saturate(Math.max(2 - 6 * x, 6 * x - 4)),
                            saturate(Math.min(6 * x, 4 - 6 * x)),
                            saturate(Math.min(6 * x - 2, 6 - 6 * x)))
        }

        Grid {
            anchors.centerIn: parent
            columns: 3

            Item {
                id: theItem
                width: 180
                height: 180
                ListView {
                    anchors.centerIn: parent
                    width: 160
                    height: 140
                    clip: true
                    snapMode: ListView.SnapOneItem
                    model: VisualItemModel {
                        Text {
                            width: 160
                            height: 140
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                            font.pixelSize: 120
                            font.family: "Times"
                            color: "blue"
                            text: "Qt"
                        }
                        Image {
                            width: 160
                            height: 140
                            source: "content/qt-logo.png"
                            smooth: true
                        }
                        Image {
                            width: 160
                            height: 140
                            source: "content/face-smile.png"
                            smooth: true
                        }
                    }
                }
            }
            ShaderEffect {
                width: 180
                height: 180
                property variant source: theSource
                property real amplitude: 0.04 * wobbleSlider.value
                property real frequency: 20
                property real time: 0
                NumberAnimation on time { loops: Animation.Infinite; from: 0; to: Math.PI * 2; duration: 600 }
                fragmentShader:
                    "uniform lowp float qt_Opacity;" +
                    "uniform highp float amplitude;" +
                    "uniform highp float frequency;" +
                    "uniform highp float time;" +
                    "uniform sampler2D source;" +
                    "varying highp vec2 qt_TexCoord0;" +
                    "void main() {" +
                    "    highp vec2 p = sin(time + frequency * qt_TexCoord0);" +
                    "    gl_FragColor = texture2D(source, qt_TexCoord0 + amplitude * vec2(p.y, -p.x)) * qt_Opacity;" +
                    "}"
                Slider {
                    id: wobbleSlider
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    height: 40
                }
            }
            ShaderEffect {
                width: 180
                height: 180
                property variant source: theSource
                property variant shadow: ShaderEffectSource {
                    smooth: true
                    sourceItem: ShaderEffect {
                        width: theItem.width
                        height: theItem.height
                        property variant delta: Qt.size(0.0, 1.0 / height)
                        property variant source: ShaderEffectSource {
                            smooth: true
                            sourceItem: ShaderEffect {
                                width: theItem.width
                                height: theItem.height
                                property variant delta: Qt.size(1.0 / width, 0.0)
                                property variant source: theSource
                                fragmentShader: "
                                    uniform lowp float qt_Opacity;
                                    uniform sampler2D source;
                                    uniform highp vec2 delta;
                                    varying highp vec2 qt_TexCoord0;
                                    void main() {
                                        gl_FragColor =(0.0538 * texture2D(source, qt_TexCoord0 - 3.182 * delta)
                                                     + 0.3229 * texture2D(source, qt_TexCoord0 - 1.364 * delta)
                                                     + 0.2466 * texture2D(source, qt_TexCoord0)
                                                     + 0.3229 * texture2D(source, qt_TexCoord0 + 1.364 * delta)
                                                     + 0.0538 * texture2D(source, qt_TexCoord0 + 3.182 * delta)) * qt_Opacity;
                                    }"
                            }
                        }
                        fragmentShader: "
                            uniform lowp float qt_Opacity;
                            uniform sampler2D source;
                            uniform highp vec2 delta;
                            varying highp vec2 qt_TexCoord0;
                            void main() {
                                gl_FragColor =(0.0538 * texture2D(source, qt_TexCoord0 - 3.182 * delta)
                                             + 0.3229 * texture2D(source, qt_TexCoord0 - 1.364 * delta)
                                             + 0.2466 * texture2D(source, qt_TexCoord0)
                                             + 0.3229 * texture2D(source, qt_TexCoord0 + 1.364 * delta)
                                             + 0.0538 * texture2D(source, qt_TexCoord0 + 3.182 * delta)) * qt_Opacity;
                            }"
                    }
                }
                property real angle: 0
                property variant offset: Qt.point(15.0 * Math.cos(angle), 15.0 * Math.sin(angle))
                NumberAnimation on angle { loops: Animation.Infinite; from: 0; to: Math.PI * 2; duration: 6000 }
                property variant delta: Qt.size(offset.x / width, offset.y / height)
                property real darkness: shadowSlider.value
                fragmentShader: "
                    uniform lowp float qt_Opacity;
                    uniform highp vec2 offset;
                    uniform sampler2D source;
                    uniform sampler2D shadow;
                    uniform highp float darkness;
                    uniform highp vec2 delta;
                    varying highp vec2 qt_TexCoord0;
                    void main() {
                        lowp vec4 fg = texture2D(source, qt_TexCoord0);
                        lowp vec4 bg = texture2D(shadow, qt_TexCoord0 + delta);
                        gl_FragColor = (fg + vec4(0., 0., 0., darkness * bg.a) * (1. - fg.a)) * qt_Opacity;
                    }"
                Slider {
                    id: shadowSlider
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    height: 40
                }
            }
            ShaderEffect {
                width: 180
                height: 180
                property variant source: theSource
                property variant delta: Qt.size(0.5 / width, 0.5 / height)
                fragmentShader: "
                    uniform sampler2D source;
                    uniform highp vec2 delta;
                    uniform highp float qt_Opacity;
                    varying highp vec2 qt_TexCoord0;
                    void main() {
                        lowp vec4 tl = texture2D(source, qt_TexCoord0 - delta);
                        lowp vec4 tr = texture2D(source, qt_TexCoord0 + vec2(delta.x, -delta.y));
                        lowp vec4 bl = texture2D(source, qt_TexCoord0 - vec2(delta.x, -delta.y));
                        lowp vec4 br = texture2D(source, qt_TexCoord0 + delta);
                        lowp vec4 gx = (tl + bl) - (tr + br);
                        lowp vec4 gy = (tl + tr) - (bl + br);
                        gl_FragColor.xyz = vec3(0.);
                        gl_FragColor.w = clamp(dot(sqrt(gx * gx + gy * gy), vec4(1.)), 0., 1.) * qt_Opacity;
                    }"
            }
            ShaderEffect {
                width: 180
                height: 180
                property variant source: theSource
                property color tint: sliderToColor(colorizeSlider.value)
                fragmentShader: "
                    uniform sampler2D source;
                    uniform lowp vec4 tint;
                    uniform lowp float qt_Opacity;
                    varying highp vec2 qt_TexCoord0;
                    void main() {
                        lowp vec4 c = texture2D(source, qt_TexCoord0);
                        lowp float lo = min(min(c.x, c.y), c.z);
                        lowp float hi = max(max(c.x, c.y), c.z);
                        gl_FragColor = qt_Opacity * vec4(mix(vec3(lo), vec3(hi), tint.xyz), c.w);
                    }"
                Slider {
                    id: colorizeSlider
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    height: 40
                }
            }
            ShaderEffect {
                width: 180
                height: 180
                mesh: Qt.size(10, 10)
                property variant source: theSource
                property real bend: 0
                property real minimize: 0
                property real side: genieSlider.value
                SequentialAnimation on bend {
                    loops: Animation.Infinite
                    NumberAnimation { to: 1; duration: 700; easing.type: Easing.InOutSine }
                    PauseAnimation { duration: 1600 }
                    NumberAnimation { to: 0; duration: 700; easing.type: Easing.InOutSine }
                    PauseAnimation { duration: 1000 }
                }
                SequentialAnimation on minimize {
                    loops: Animation.Infinite
                    PauseAnimation { duration: 300 }
                    NumberAnimation { to: 1; duration: 700; easing.type: Easing.InOutSine }
                    PauseAnimation { duration: 1000 }
                    NumberAnimation { to: 0; duration: 700; easing.type: Easing.InOutSine }
                    PauseAnimation { duration: 1300 }
                }
                vertexShader: "
                    uniform highp mat4 qt_Matrix;
                    uniform highp float bend;
                    uniform highp float minimize;
                    uniform highp float side;
                    uniform highp float width;
                    uniform highp float height;
                    attribute highp vec4 qt_Vertex;
                    attribute highp vec2 qt_MultiTexCoord0;
                    varying highp vec2 qt_TexCoord0;
                    void main() {
                        qt_TexCoord0 = qt_MultiTexCoord0;
                        highp vec4 pos = qt_Vertex;
                        pos.y = mix(qt_Vertex.y, height, minimize);
                        highp float t = pos.y / height;
                        t = (3. - 2. * t) * t * t;
                        pos.x = mix(qt_Vertex.x, side * width, t * bend);
                        gl_Position = qt_Matrix * pos;
                    }"
                Slider {
                    id: genieSlider
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    height: 40
                }
            }
        }
    }
}