Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved]Create reuseable shaderEffect

[Solved]Create reuseable shaderEffect

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stereomatching
    wrote on last edited by
    #1

    This is a shaderEffect codes from an example

    @
    // Shader Effect
    ShaderEffect {
    id: shaderEffect
    anchors.fill: bug
    property variant source: bug
    property real amplitude: 0.01
    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;" +
    "}"
    }
    @

    I encapsulate it in a new component

    Wobble.qml
    @
    // Shader Effect
    ShaderEffect {
    id: root

        property variant source
        property real amplitude: 0.01
        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;" +
            "}"
    }
    

    @

    And I use it like this

    main.qml

    @
    import QtQuick 2.0

    Rectangle {
    width : bug.width
    height : bug.height

    Image{
        id: bug
    
        anchors.fill: parent
    
        source: "/Downloads/1359170070532.jpg"
        smooth: true
        fillMode: Image.PreserveAspectFit
    }    
    
    Wobble{
        id: wobble
    
        anchors.fill: bug
        source: bug
    }
    

    @

    However, it can't work, if I replace Wobble by the original ShaderEffect, it works
    What am I miss?Besides, do we have a solution to pass uniform array by qml?

    ex:

    @
    //.....
    uniform vec2 offset[9];

    uniform int kernel[9];

    //......
    @

    1 Reply Last reply
    0
    • slettaS Offline
      slettaS Offline
      sletta
      wrote on last edited by
      #2

      When I try the code you list above (Wobble.qml and WobbleMain.qml) it works just fine. Do you have them in the same directory? What is the error you get?

      There is no way to pass uniform arrays from QML code, but you could do that using the scene graph material API (QSGMaterial or QSGSimpleMaterial).

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stereomatching
        wrote on last edited by
        #3

        Hi, it works now, haven't change the codes at all, don't know what kind of problem before.

        I will take a look on QSGMaterial or QSGSimpleMaterial, or create the shader effect by
        Qt/c++ if I have to, thanks

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved