1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-22 02:32:29 +01:00

Add basic shader plugin

This commit is contained in:
Elias Steurer 2020-09-10 11:40:07 +02:00
parent 6f77e77ffe
commit ddad721574
11 changed files with 306 additions and 1 deletions

View File

@ -51,6 +51,7 @@ project(ScreenPlay)
add_subdirectory(ScreenPlay)
add_subdirectory(ScreenPlaySDK)
add_subdirectory(ScreenPlayShader)
add_subdirectory(ScreenPlayWallpaper)
add_subdirectory(ScreenPlayWidget)
add_subdirectory(ScreenPlaySysInfo)

View File

@ -19,7 +19,7 @@ set(headers
inc/screenplay-sdk_plugin.h
inc/screenplaysdk.h)
add_library(${PROJECT_NAME} ${src} ${headers})
add_library(${PROJECT_NAME} ${src} ${headers})
target_link_libraries(${PROJECT_NAME}
PRIVATE

View File

@ -0,0 +1,66 @@
cmake_minimum_required(VERSION 3.17 )
project(ScreenPlayShader LANGUAGES CXX)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOMOC ON)
find_package(
Qt5
COMPONENTS Quick
Core
REQUIRED)
set(src
screenplayshader_plugin.cpp
shaderlibrary.cpp
)
set(headers
screenplayshader_plugin.h
shaderlibrary.h
)
set(shader shader.qrc)
add_library(${PROJECT_NAME} SHARED ${src} ${headers} ${shader})
target_link_libraries(${PROJECT_NAME}
PRIVATE
Qt5::Core
Qt5::Quick
Qt5::Gui)
target_include_directories(
${PROJECT_NAME}
PUBLIC inc
)
# QML module deployment
set(URI "ScreenPlay/Shader")
string(REPLACE "." "/" TARGETPATH ${URI})
if (NOT DEFINED QT_QMAKE_EXECUTABLE)
get_target_property (QT_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
if (NOT QT_QMAKE_EXECUTABLE)
message(FATAL_ERROR "Cannot find qmake")
endif()
endif()
execute_process(COMMAND ${QT_QMAKE_EXECUTABLE} -query QT_INSTALL_QML OUTPUT_VARIABLE QT_INSTALL_QML_RAW)
string(STRIP ${QT_INSTALL_QML_RAW} QT_INSTALL_QML)
set(DESTDIR "${QT_INSTALL_QML}/${TARGETPATH}")
message("DESTDIR ${DESTDIR}")
file(MAKE_DIRECTORY ${DESTDIR})
configure_file(qmldir ${DESTDIR} COPYONLY)
# Copies ScreenPlayShader.* into qt qml plugins folder
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:ScreenPlayShader>
${DESTDIR}/$<TARGET_FILE_NAME:ScreenPlayShader>)

View File

@ -0,0 +1,84 @@
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 resolution;
float hash( float n ) { return fract(sin(n)*753.5453123); }
// Slight modification of iq's noise function.
float noise( in vec2 x )
{
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
float n = p.x + p.y*157.0;
return mix(
mix( hash(n+ 0.0), hash(n+ 1.0),f.x),
mix( hash(n+157.0), hash(n+158.0),f.x),
f.y);
}
float fbm(vec2 p, vec3 a)
{
float v = 0.0;
v += noise(p*a.x)*1.20;
v += noise(p*a.y)*1.20;
v += noise(p*a.z)*.125;
return v;
}
vec3 drawLines( vec2 uv, vec3 fbmOffset, vec3 color1, vec3 color2 )
{
float timeVal = time * 0.1;
vec3 finalColor = vec3( 0.0 );
for( int i=0; i < 3; ++i )
{
float indexAsFloat = float(i);
float amp = 40.0 + (indexAsFloat*10.0);
float period = 2.0 + (indexAsFloat+2.0);
float thickness = mix( 0.9, 1.0, noise(uv*10.0) );
float t = abs( 0.9 / (sin(uv.x + fbm( uv + timeVal * period, fbmOffset )) * amp) * thickness );
finalColor += t * color1;
}
for( int i=0; i < 0; ++i )
{
float indexAsFloat = float(i);
float amp = 40.0 + (indexAsFloat*7.0);
float period = 2.0 + (indexAsFloat+8.0);
float thickness = mix( 0.7, 1.0, noise(uv*10.0) );
float t = abs( 0.8 / (sin(uv.x + fbm( uv + timeVal * period, fbmOffset )) * amp) * thickness );
finalColor += t * color2 * 0.6;
}
return finalColor;
}
void main( void )
{
vec2 uv = ( gl_FragCoord.xy / resolution.xy ) * 1.0 - 1.8;
uv.x *= resolution.x/resolution.y;
uv.xy = uv.yx;
vec3 lineColor1 = vec3( 2.3, 0.5, .5 );
vec3 lineColor2 = vec3( 0.3, 0.5, 2.5 );
vec3 finalColor = vec3(0.0);
float t = sin( time ) * 0.5 + 0.5;
float pulse = mix( 0.10, 0.20, t);
finalColor += drawLines( uv, vec3( 1.0, 1.0, 8.0), lineColor2, lineColor2 );
gl_FragColor = vec4( finalColor, 1.0 );
}

View File

@ -0,0 +1,9 @@
uniform highp mat4 qt_Matrix;
attribute highp vec4 qt_Vertex;
attribute highp vec2 qt_MultiTexCoord0;
varying highp vec2 coord;
void main() {
coord = qt_MultiTexCoord0;
gl_Position = qt_Matrix * qt_Vertex;
}

2
ScreenPlayShader/qmldir Normal file
View File

@ -0,0 +1,2 @@
module ScreenPlay.Shader
plugin ScreenPlayShader

View File

@ -0,0 +1,18 @@
#include "screenplayshader_plugin.h"
#include <qqml.h>
QObject* ScreenPlayShaderLibrarySingleton(QQmlEngine* engine, QJSEngine* scriptEngine)
{
Q_UNUSED(engine)
Q_UNUSED(scriptEngine)
return new ShaderLibrary();
}
void ScreenPlayShaderPlugin::registerTypes(const char* uri)
{
Q_UNUSED(uri)
qmlRegisterSingletonType<ShaderLibrary>(uri, 1, 0, "ShaderLibrary", ScreenPlayShaderLibrarySingleton);
}

View File

@ -0,0 +1,15 @@
#pragma once
#include <QQmlExtensionPlugin>
#include "shaderlibrary.h"
class ScreenPlayShaderPlugin : public QQmlExtensionPlugin {
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
public:
void registerTypes(const char* uri) override;
private:
ShaderLibrary m_shaderLibrary;
};

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>lightning.frag</file>
<file>lightning.vert</file>
</qresource>
</RCC>

View File

@ -0,0 +1,15 @@
#include "shaderlibrary.h"
ShaderLibrary::ShaderLibrary(QQuickItem* parent)
: QQuickItem(parent)
{
QFile lightningFragFile(":/lightning.frag");
lightningFragFile.open(QIODevice::ReadOnly);
QFile lightningVertFile(":/lightning.vert");
lightningVertFile.open(QIODevice::ReadOnly);
m_lightning = std::make_unique<Shader>(lightningVertFile.readAll(),lightningFragFile.readAll());
}
ShaderLibrary::~ShaderLibrary()
{
}

View File

@ -0,0 +1,89 @@
#pragma once
#include <QObject>
#include <QQuickItem>
#include <QString>
#include <memory>
class Shader : public QObject {
Q_OBJECT
Q_PROPERTY(QString vertex READ vertex WRITE setVertex NOTIFY vertexChanged)
Q_PROPERTY(QString fragment READ fragment WRITE setFragment NOTIFY fragmentChanged)
public:
Shader() { }
Shader(const QString& vertex, const QString& fragment)
{
m_vertex = vertex;
m_fragment = fragment;
}
QString vertex() const
{
return m_vertex;
}
QString fragment() const
{
return m_fragment;
}
public slots:
void setVertex(QString vertex)
{
if (m_vertex == vertex)
return;
m_vertex = vertex;
emit vertexChanged(m_vertex);
}
void setFragment(QString fragment)
{
if (m_fragment == fragment)
return;
m_fragment = fragment;
emit fragmentChanged(m_fragment);
}
signals:
void vertexChanged(QString vertex);
void fragmentChanged(QString fragment);
private:
QString m_vertex;
QString m_fragment;
};
class ShaderLibrary : public QQuickItem {
Q_OBJECT
Q_DISABLE_COPY(ShaderLibrary)
Q_PROPERTY(Shader* lightning READ lightning WRITE setLightning NOTIFY lightningChanged)
public:
explicit ShaderLibrary(QQuickItem* parent = nullptr);
~ShaderLibrary() override;
Shader* lightning() const
{
return m_lightning.get();
}
public slots:
void setLightning(Shader* lightning)
{
if (m_lightning.get() == lightning)
return;
m_lightning.reset(lightning);
emit lightningChanged(m_lightning.get());
}
signals:
void lightningChanged(Shader* lightning);
private:
std::unique_ptr<Shader> m_lightning;
};