From 25df082d8882f11c6daf831962845e7280e77893 Mon Sep 17 00:00:00 2001 From: Elias Steurer Date: Sun, 13 Sep 2020 19:51:20 +0200 Subject: [PATCH] Add tmp --- ScreenPlay/main.qml | 5 + ScreenPlayShader/ScreenPlayShader.qml | 170 +++++++++++++++++++ ScreenPlayShader/Testyyy.qml | 0 ScreenPlayShader/screenplayshader_plugin.cpp | 7 +- ScreenPlayShader/shader.qrc | 4 + ScreenPlayShader/shaderlibrary.cpp | 8 +- ScreenPlayShader/shaderlibrary.h | 18 ++ ScreenPlayShader/water.frag | 68 +++++--- ScreenPlayWallpaper/Wallpaper.qml | 4 + 9 files changed, 254 insertions(+), 30 deletions(-) create mode 100644 ScreenPlayShader/ScreenPlayShader.qml create mode 100644 ScreenPlayShader/Testyyy.qml diff --git a/ScreenPlay/main.qml b/ScreenPlay/main.qml index a4d8cafc..5e62d069 100644 --- a/ScreenPlay/main.qml +++ b/ScreenPlay/main.qml @@ -9,6 +9,9 @@ import Qt.labs.platform 1.0 import ScreenPlay 1.0 import Settings 1.0 +import ScreenPlay.Shader 1.0 +import "ShaderWrapper" as ShaderWrapper + import "qml/" import "qml/Monitors" as Monitors import "qml/Common" as Common @@ -34,6 +37,8 @@ ApplicationWindow { } } + + // Partial workaround for // https://bugreports.qt.io/browse/QTBUG-86047 Material.accent: Material.color(Material.Orange) diff --git a/ScreenPlayShader/ScreenPlayShader.qml b/ScreenPlayShader/ScreenPlayShader.qml new file mode 100644 index 00000000..04a4e454 --- /dev/null +++ b/ScreenPlayShader/ScreenPlayShader.qml @@ -0,0 +1,170 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 + +ShaderEffect { + id: root + + property real speed: 1 + + readonly property vector3d defaultResolution: Qt.vector3d( + root.width, + root.height, + root.width / root.height) + function calcResolution(channel) { + if (channel) { + return Qt.vector3d(channel.width, channel.height, + channel.width / channel.height) + } else { + return defaultResolution + } + } + + // based on shadertoy default vartiables + readonly property vector3d iResolution: defaultResolution + property real iTime: 0 + property real iTimeDelta: 100 + property int iFrame: 10 + property real iFrameRate + property double shaderSpeed: 1.0 + property vector4d iMouse + + //only Image or ShaderEffectSource + property var iChannel0: ich0 + property var iChannel1: ich1 + property var iChannel2: ich2 + property var iChannel3: ich3 + + property var iChannelTime: [0, 1, 2, 3] + property var iChannelResolution: [calcResolution(iChannel0), calcResolution( + iChannel1), calcResolution(iChannel2), calcResolution(iChannel3)] + property vector4d iDate + property real iSampleRate: 44100 + + Image { + id: ich0 + visible: false + } + Image { + id: ich1 + visible: false + } + Image { + id: ich2 + visible: false + } + Image { + id: ich3 + visible: false + } + + property bool hoverEnabled: false + property bool running: true + function restart() { + root.iTime = 0 + running = true + timer1.restart() + } + Timer { + id: timer1 + running: root.running + triggeredOnStart: true + interval: 16 + repeat: true + onTriggered: { + root.iTime += 0.016 * speed + } + } + Timer { + running: root.running + interval: 1000 + property date currentDate: new Date() + onTriggered: { + currentDate = new Date() + root.iDate.x = currentDate.getFullYear() + root.iDate.y = currentDate.getMonth() + root.iDate.z = currentDate.getDay() + root.iDate.w = currentDate.getSeconds() + } + } + + readonly property string gles2Ver: " +#define texture texture2D +precision mediump float;" + + readonly property string gles3Ver: "#version 300 es +#define varying in +#define gl_FragColor fragColor +precision mediump float; +out vec4 fragColor;" + + readonly property string gl3Ver: " +#version 150 +#define varying in +#define gl_FragColor fragColor +#define lowp +#define mediump +#define highp +out vec4 fragColor;" + + readonly property string gl3Ver_igpu: " +#version 130 +#define varying in +#define gl_FragColor fragColor +#define lowp +#define mediump +#define highp +out vec4 fragColor;" + + readonly property string gl2Ver: " +#version 110 +#define texture texture2D" + + property string versionString: GraphicsInfo.majorVersion === 3 ? "gl3Ver" : "gl2Ver" + + vertexShader: " +uniform mat4 qt_Matrix; +attribute vec4 qt_Vertex; +attribute vec2 qt_MultiTexCoord0; +varying vec2 qt_TexCoord0; +varying vec4 vertex; +void main() { +vertex = qt_Vertex; +gl_Position = qt_Matrix * vertex; +qt_TexCoord0 = qt_MultiTexCoord0; +}" + + readonly property string forwardString: versionString + " +varying vec2 qt_TexCoord0; +varying vec4 vertex; +uniform lowp float qt_Opacity; +uniform vec3 iResolution; +uniform float iTime; +uniform float iTimeDelta; +uniform int iFrame; +uniform float iFrameRate; +uniform float iChannelTime[4]; +uniform vec3 iChannelResolution[4]; +uniform vec4 iMouse; +uniform vec4 iDate; +uniform float iSampleRate; +uniform sampler2D iChannel0; +uniform sampler2D iChannel1; +uniform sampler2D iChannel2; +uniform sampler2D iChannel3;" + + readonly property string startCode: " +void main(void) +{ +mainImage(gl_FragColor, vec2(vertex.x, iResolution.y - vertex.y)); +}" + + readonly property string defaultPixelShader: " +void mainImage(out vec4 fragColor, in vec2 fragCoord) +{ +fragColor = vec4(fragCoord, fragCoord.x, fragCoord.y); +}" + + property bool runShader: true + property string pixelShader: "" + fragmentShader: forwardString + (pixelShader ? pixelShader : defaultPixelShader) + startCode +} diff --git a/ScreenPlayShader/Testyyy.qml b/ScreenPlayShader/Testyyy.qml new file mode 100644 index 00000000..e69de29b diff --git a/ScreenPlayShader/screenplayshader_plugin.cpp b/ScreenPlayShader/screenplayshader_plugin.cpp index ebed379a..b7954131 100644 --- a/ScreenPlayShader/screenplayshader_plugin.cpp +++ b/ScreenPlayShader/screenplayshader_plugin.cpp @@ -1,18 +1,19 @@ #include "screenplayshader_plugin.h" #include +#include QObject* ScreenPlayShaderLibrarySingleton(QQmlEngine* engine, QJSEngine* scriptEngine) { - Q_UNUSED(engine) Q_UNUSED(scriptEngine) - + //Add QRC + // engine->addImportPath("qrc:/ShaderWrapper/"); + // engine->addImportPath(".."); return new ShaderLibrary(); } void ScreenPlayShaderPlugin::registerTypes(const char* uri) { - Q_UNUSED(uri) qmlRegisterSingletonType(uri, 1, 0, "ShaderLibrary", ScreenPlayShaderLibrarySingleton); } diff --git a/ScreenPlayShader/shader.qrc b/ScreenPlayShader/shader.qrc index bedcfb1a..367d8b4e 100644 --- a/ScreenPlayShader/shader.qrc +++ b/ScreenPlayShader/shader.qrc @@ -5,4 +5,8 @@ water.frag water.vert + + ScreenPlayShader.qml + Testyyy.qml + diff --git a/ScreenPlayShader/shaderlibrary.cpp b/ScreenPlayShader/shaderlibrary.cpp index 86ee4411..5984b6c2 100644 --- a/ScreenPlayShader/shaderlibrary.cpp +++ b/ScreenPlayShader/shaderlibrary.cpp @@ -7,7 +7,13 @@ ShaderLibrary::ShaderLibrary(QQuickItem* parent) lightningFragFile.open(QIODevice::ReadOnly); QFile lightningVertFile(":/lightning.vert"); lightningVertFile.open(QIODevice::ReadOnly); - m_lightning = std::make_unique(lightningVertFile.readAll(),lightningFragFile.readAll()); + m_lightning = std::make_unique(lightningVertFile.readAll(), lightningFragFile.readAll()); + + QFile waterFragFile(":/water.frag"); + waterFragFile.open(QIODevice::ReadOnly); + QFile waterVertFile(":/water.vert"); + waterVertFile.open(QIODevice::ReadOnly); + m_water = std::make_unique(waterVertFile.readAll(), waterFragFile.readAll()); } ShaderLibrary::~ShaderLibrary() diff --git a/ScreenPlayShader/shaderlibrary.h b/ScreenPlayShader/shaderlibrary.h index a9259d8a..cc870d06 100644 --- a/ScreenPlayShader/shaderlibrary.h +++ b/ScreenPlayShader/shaderlibrary.h @@ -61,6 +61,7 @@ class ShaderLibrary : public QQuickItem { Q_DISABLE_COPY(ShaderLibrary) Q_PROPERTY(Shader* lightning READ lightning WRITE setLightning NOTIFY lightningChanged) + Q_PROPERTY(Shader* water READ water WRITE setWater NOTIFY waterChanged) public: explicit ShaderLibrary(QQuickItem* parent = nullptr); @@ -71,6 +72,11 @@ public: return m_lightning.get(); } + Shader* water() const + { + return m_water.get(); + } + public slots: void setLightning(Shader* lightning) { @@ -81,9 +87,21 @@ public slots: emit lightningChanged(m_lightning.get()); } + void setWater(Shader* water) + { + if (m_water.get() == water) + return; + + m_water.reset(water); + emit waterChanged(m_water.get()); + } + signals: void lightningChanged(Shader* lightning); + void waterChanged(Shader* water); + private: std::unique_ptr m_lightning; + std::unique_ptr m_water; }; diff --git a/ScreenPlayShader/water.frag b/ScreenPlayShader/water.frag index 30bd4b5d..04428084 100644 --- a/ScreenPlayShader/water.frag +++ b/ScreenPlayShader/water.frag @@ -1,43 +1,59 @@ + +#ifdef GL_ES +precision mediump float; +#endif + uniform sampler2D qt_Texture0; varying vec4 qt_TexCoord0; -uniform float reflectionOffset = 0; // allows player to control reflection position -uniform float reflectionBlur = 0; // works only if projec's driver is set to GLES3, more information here https://docs.godotengine.org/ru/stable/tutorials/shading/screen-reading_shaders.html -uniform float calculatedOffset = 0; // this is controlled by script, it takes into account camera position and water object position, that way reflection stays in the same place when camera is moving -uniform float calculatedAspect = 1; // is controlled by script, ensures that noise is not affected by object scale -uniform sampler2D noiseTexture; -uniform float offsetStrength; -uniform float maxOffset; +//uniform float reflectionOffset; // allows player to control reflection position +//uniform float reflectionBlur ; // works only if projec's driver is set to GLES3, more information here https://docs.godotengine.org/ru/stable/tutorials/shading/screen-reading_shaders.html +//uniform float calculatedOffset ; // this is controlled by script, it takes into account camera position and water object position, that way reflection stays in the same place when camera is moving +//uniform float calculatedAspect ; // is controlled by script, ensures that noise is not affected by object scale +//uniform sampler2D noiseTexture; +//uniform float offsetStrength; +//uniform float maxOffset; -uniform vec2 distortionScale = vec2(0.3, 0.3); -uniform vec2 distortionSpeed = vec2(0.01, 0.02); +//uniform vec2 distortionScale; +//uniform vec2 distortionSpeed; -uniform float waveSmoothing = .01; +//uniform float waveSmoothing; -uniform float mainWaveSpeed = 2.5; -uniform float mainWaveFrequency = 20; -uniform float mainWaveAmplitude = 0.005; +//uniform float mainWaveSpeed ; +//uniform float mainWaveFrequency ; +//uniform float mainWaveAmplitude; -uniform float secondWaveSpeed = 2.5; -uniform float secondWaveFrequency = 20; -uniform float secondWaveAmplitude = 0.015; +//uniform float secondWaveSpeed ; +//uniform float secondWaveFrequency ; +//uniform float secondWaveAmplitude ; -uniform float thirdWaveSpeed = 2.5; -uniform float thirdWaveFrequency = 20; -uniform float thirdWaveAmplitude = 0.01; +//uniform float thirdWaveSpeed ; +//uniform float thirdWaveFrequency ; +//uniform float thirdWaveAmplitude ; -uniform float squashing = 1.0; +//uniform float squashing ; -uniform vec4 shorelineColor = vec4(1.0, 1.0, 1.0, 1.0); //: hint_color = vec4(1.); -uniform float shorelineSize = 0.0025; //: hint_range(0., 0.1) = 0.0025; -uniform float shorelineFoamSize : hint_range(0., 0.1) = 0.0025; -uniform float foamSpeed; -uniform vec2 foamScale; +//uniform vec4 shorelineColor; //: hint_color = vec4(1.); +//uniform float shorelineSize; //: hint_range(0., 0.1) = 0.0025; +//uniform float shorelineFoamSize ; // : hint_range(0., 0.1) +//uniform float foamSpeed; +//uniform vec2 foamScale; uniform float time; void main(void) { - gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st); + vec2 uv = fragCoord.xy / iResolution.xy; + vec4 texture_color = vec4(0.192156862745098, 0.6627450980392157, 0.9333333333333333, 1.0); + + vec4 k = vec4(time)*0.8; + k.xy = uv * 7.0; + float val1 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.5)); + float val2 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.2)); + float val3 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.5)); + vec4 color = vec4 ( pow(min(min(val1,val2),val3), 7.0) * 3.0)+texture_color; + gl_FragColor = color; + + //gl_FragColor = texture2D(qt_Texture0, qt_TexCoord0.st); } diff --git a/ScreenPlayWallpaper/Wallpaper.qml b/ScreenPlayWallpaper/Wallpaper.qml index 340c7cf4..911cc756 100644 --- a/ScreenPlayWallpaper/Wallpaper.qml +++ b/ScreenPlayWallpaper/Wallpaper.qml @@ -1,6 +1,9 @@ import QtQuick 2.14 import QtQml 2.14 import ScreenPlayWallpaper 1.0 +import ScreenPlay.Shader 1.0 +import "ShaderWrapper" as ShaderWrapper + Rectangle { id: root @@ -218,4 +221,5 @@ Rectangle { } } } + }