From 465d6741411064b5424faac9602e160caa1a4548 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Mon, 6 Mar 2023 21:10:44 +0100 Subject: photosurface demo: Add a procedural texture background Adapted from https://www.shadertoy.com/view/WdscWN via the Qt Quick Effects Maker and further tweaking by hand. Because we do not have a defined pattern to compile shaders from sources with qmake, building this demo with qmake is no longer supported. Task-number: QTBUG-108924 Change-Id: I0b19b6aa9865f2e8c259fb1d12dffd9a28eae72e Reviewed-by: Oliver Eftevaag (cherry picked from commit b766d2f1570825092bdb58ded83f7de9aa45b533) Reviewed-by: Qt Cherry-pick Bot --- examples/demos/demos.pro | 1 - examples/demos/photosurface/CMakeLists.txt | 12 ++- examples/demos/photosurface/photosurface.pro | 12 --- examples/demos/photosurface/photosurface.qml | 7 ++ examples/demos/photosurface/photosurface.qrc | 7 -- .../photosurface/resources/shaders/gneiss.frag | 92 ++++++++++++++++++++++ 6 files changed, 110 insertions(+), 21 deletions(-) delete mode 100644 examples/demos/photosurface/photosurface.pro delete mode 100644 examples/demos/photosurface/photosurface.qrc create mode 100644 examples/demos/photosurface/resources/shaders/gneiss.frag diff --git a/examples/demos/demos.pro b/examples/demos/demos.pro index 62046935..5622baa5 100644 --- a/examples/demos/demos.pro +++ b/examples/demos/demos.pro @@ -7,7 +7,6 @@ qtHaveModule(quick) { clocks \ tweetsearch \ maroon \ - photosurface \ stocqt qtHaveModule(quickcontrols2) { diff --git a/examples/demos/photosurface/CMakeLists.txt b/examples/demos/photosurface/CMakeLists.txt index d696882f..d60b9d18 100644 --- a/examples/demos/photosurface/CMakeLists.txt +++ b/examples/demos/photosurface/CMakeLists.txt @@ -12,7 +12,7 @@ endif() set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/demos/photosurface") -find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Quick) +find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Quick ShaderTools) qt_standard_project_setup(REQUIRES 6.5) @@ -55,6 +55,16 @@ qt_add_qml_module(photosurfaceexample "resources/folder.png" ) +qt6_add_shaders(photosurfaceexample "shaders" + BATCHABLE + PRECOMPILE + OPTIMIZED + PREFIX + "/qt/qml/photosurface" + FILES + "resources/shaders/gneiss.frag" +) + install(TARGETS photosurfaceexample RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}" BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}" diff --git a/examples/demos/photosurface/photosurface.pro b/examples/demos/photosurface/photosurface.pro deleted file mode 100644 index 21c2214b..00000000 --- a/examples/demos/photosurface/photosurface.pro +++ /dev/null @@ -1,12 +0,0 @@ -TEMPLATE = app - -QT += qml quick -SOURCES += main.cpp -RESOURCES += photosurface.qrc - -target.path = $$[QT_INSTALL_EXAMPLES]/demos/photosurface -INSTALLS += target -ICON = resources/icon.png -macos: ICON = resources/photosurface.icns -win32: RC_FILE = resources/photosurface.rc - diff --git a/examples/demos/photosurface/photosurface.qml b/examples/demos/photosurface/photosurface.qml index 7b4fd1d0..c70e0b34 100644 --- a/examples/demos/photosurface/photosurface.qml +++ b/examples/demos/photosurface/photosurface.qml @@ -25,6 +25,13 @@ Window { onAccepted: folderModel.folder = selectedFolder } + ShaderEffect { + readonly property vector3d iResolution: Qt.vector3d(width, height, 1.0) + + fragmentShader: "resources/shaders/gneiss.frag.qsb" + anchors.fill: parent + } + ScrollView { id: flick anchors.fill: parent diff --git a/examples/demos/photosurface/photosurface.qrc b/examples/demos/photosurface/photosurface.qrc deleted file mode 100644 index 9834eecb..00000000 --- a/examples/demos/photosurface/photosurface.qrc +++ /dev/null @@ -1,7 +0,0 @@ - - - photosurface.qml - resources/folder.png - resources/MomentumAnimation.qml - - diff --git a/examples/demos/photosurface/resources/shaders/gneiss.frag b/examples/demos/photosurface/resources/shaders/gneiss.frag new file mode 100644 index 00000000..67e3caaa --- /dev/null +++ b/examples/demos/photosurface/resources/shaders/gneiss.frag @@ -0,0 +1,92 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause +// simplified from https://www.shadertoy.com/view/WdscWN + +#version 450 + +layout(location = 0) in vec2 qt_TexCoord0; +layout(location = 0) out vec4 fragColor; +layout(std140, binding = 0) uniform buf { + mat4 qt_Matrix; + float qt_Opacity; + vec2 iResolution; +}; + +const float mag = 12.0; + +// this function is from https://www.shadertoy.com/view/4djSRW +int hash12(vec2 p, float num_colors) +{ + // set the number of colors to be randomly generated + vec3 p3 = fract(vec3(p.xyx) * 0.1031); + p3 += dot(p3, p3.yzx + 33.33); + return int(floor(fract((p3.x + p3.y) * p3.z) * num_colors)); +} + +int magnify(vec2 fragCoord, float mag, float num_colors) { + for (float i = 1.0; i < mag; i++) { + fragCoord += vec2(sin(fragCoord.y / (100.0 * i)) * 10.0, + sin(fragCoord.x / (100.0 * mag * i)) * 10.0) * mag * mag; + } + + return hash12(floor(fragCoord / pow(3.0, mag)), num_colors); +} + +ivec4 get_neighbors(vec2 fragCoord, float mag, float colors) { + return ivec4(magnify(fragCoord+vec2(0, 1), mag, colors), + magnify(fragCoord+vec2(1, 0), mag, colors), + magnify(fragCoord-vec2(0, 1), mag, colors), + magnify(fragCoord-vec2(1, 0), mag, colors)); +} + +bool is_next_to(int color, ivec4 neighbors) { + return (neighbors.x == color) + || (neighbors.y == color) + || (neighbors.z == color) + || (neighbors.w == color); +} + +int next_biome(inout int color1, ivec4 neighbors) { + return (color1 == 0 && is_next_to(2, neighbors)) ? 3 + : (color1 == 3 && is_next_to(2, neighbors)) ? 4 + : (color1 == 2) ? 0 + : (color1 == 1 && is_next_to(0, neighbors)) ? 0 + : (color1 == 4 && is_next_to(0, neighbors)) ? 0 + : color1; +} + +int biome(in vec2 fragCoord, float mag, int colors[5]) { + float num_colors = float(colors.length()); + int color1 = magnify(fragCoord, mag, num_colors); + ivec4 neighbors; + while(mag > 1.0) { + neighbors = get_neighbors(fragCoord, mag, num_colors); + color1 = next_biome(color1, neighbors); + mag -= 1.0; + } + return colors[int(color1)]; +} + +void main() { + vec2 fc = qt_TexCoord0 * iResolution.y * mag; + vec2 uv = qt_TexCoord0; + + switch (biome(fc, mag, int[](0, 1, 2, 3, 4))) { + case 0: + fragColor = vec4(0.2, 0.2, 0.24, 1.0); + break; + case 1: + fragColor = vec4(0.24, 0.16, 0.28, 1.0); + break; + case 2: + fragColor = vec4(0.16, 0.18, 0.22, 1.0); + break; + case 3: + fragColor = vec4(0.10, 0.10, 0.10, 1.0); + break; + default: + fragColor = vec4(0.0, 0.252, 0.27, 1.0); + break; + } + fragColor *= (0.35 + 0.65 * sin(uv.x * 2.35)) * (0.35 + 0.65 * sin(uv.y * 4.2)) * qt_Opacity; +} -- cgit v1.2.1