Author Topic: Vertex shader to address only part of the screen?  (Read 2677 times)

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Vertex shader to address only part of the screen?
« on: January 23, 2019, 03:59:46 PM »
Hey there. I have a few shaders I've "written" (and by "written," I mean largely stolen from shadertoy and tweaked), but they affect the entire screen, and that's not what I'm going for. I want to be able to affect only a specific portion of the screen (or, more specifically, a surface that's smaller than the screen). I think a vertex shader might help, but I have no idea how to write one, and all my googling returns results for Unity or some other 3D-centric environment, and I'm not math-savvy enough to figure out what I need to do.

Has anyone run into anything similar? And come up with a working solution? Would such a person be willing to help a guy out? I appreciate it! Thanks!

(I'm attaching my kinda-sorta-working shaders just for laughs.)

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Vertex shader to address only part of the screen?
« Reply #1 on: January 23, 2019, 08:24:04 PM »
Just written.... I mean stolen..... Love the onisty........ ⁿ :



help a friend....

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1233
    • View Profile
    • My deviantart page
Re: Vertex shader to address only part of the screen?
« Reply #2 on: January 25, 2019, 12:07:15 AM »
It depends on what you apply the shader to, can you share also the layout code where you are using these shaders?

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Re: Vertex shader to address only part of the screen?
« Reply #3 on: January 25, 2019, 06:59:52 AM »
Hi zpaolo11x,

I'm including a screen shot of what the screen looks like in addition to the code that doesn't work:

Code: [Select]
local snap = snapSurface.add_artwork( "snap", 0, 0 ); // This gets resized depending on the dimensions of the actual snap. Function below.
snap.set_pos( snapSurface.width / 2, snapSurface.height / 2 );

local snapShader = fe.add_shader( Shader.Fragment, "pseudo_scanlines.glsl" );
snapShader.set_texture_param( "source", snap );
snapShader.set_param( "rez", snap.width, snap.height );
snapShader.set_param( "lineWeight", snap.width, snap.height );

snap.shader = snapShader;

What happens is I get a mostly black image where the snap used to be. Maybe it's all black. I can't tell sometimes. If I modify the "rez" param like so:

snapShader.set_param( "rez", fe.layout.width, fe.layout.height );

I get a scaled up, upside-down snap! I don't know if a vertex shader would solve my problems or just introduce new ones. The snap size/position changes based on its actual dimensions:

Code: [Select]
function updateSnapSize (ttype, var, ttime) {
// Get proper aspect ratio:
    local _rez = null;
local _r = null;

    if ( snap.texture_width == 0 || snap.texture_height == 0 ) {
        _rez = { "w": snap.width, "h": snap.height }
    }
    else {
        _rez = {"w": snap.texture_width, "h": snap.texture_height }
    }

    _r = _rez.w >= _rez.h ? _rez.h.tofloat() / _rez.w.tofloat() : _rez.w.tofloat() / _rez.h.tofloat();

switch ( ttype ) {
case Transition.StartLayout:
case Transition.ToNewList:
case Transition.ToNewSelection:
case Transition.EndNavigation:
// Resize snap:
if ( _rez.w >= _rez.h ) {
// Landscape (or square):
snap.width = snapVars.max;
snap.height = snapVars.max * _r;
}
else {
// Portrait:
snap.width = snapVars.max * _r;
snap.height = snapVars.max;
}

// Resize snapFrame:
snapFrame.width = snap.width + snapVars.frameWeight;
snapFrame.height = snap.height + snapVars.frameWeight;

            // Update origins:
            snapFrame.origin_x = snapFrame.width / 2;
            snapFrame.origin_y = snapFrame.height / 2;

            snap.origin_x = snap.width / 2;
            snap.origin_y = snap.height / 2;

            // Update drop shadow:
            // NOTE: This works! It's probably voodoo, so don't look too closely!
            snapDS.surface.origin_x = snapFrame.width / 2;
            snapDS.surface.origin_y = snapFrame.height / 2;
            snapDS.surface.set_pos(
                snapVars.dropShadowOffset.x + (snapOrigin.x - snapVars.dropShadowSize),
                snapVars.dropShadowOffset.y + (snapOrigin.y - snapVars.dropShadowSize),
                snapFrame.width + (snapVars.dropShadowSize * 2),
                snapFrame.height + (snapVars.dropShadowSize * 2)
            );

            // Reposition gameCount:
            gameCount.set_pos( snapOrigin.x - (gameCount.width / 2), snapOrigin.y + (snapFrame.height / 2.0).tofloat() + 16);

            // Adjust zorder:
            snap.zorder = snapFrame.zorder + 1;
            snapSurface.zorder = snapDS.surface.zorder + 1;
            gameCount.zorder = snapSurface.zorder + 1;
break;
}
}

That size/position changing stuff may or may not mess with the shader. Not sure.

If you or anyone else is able to deduce a solution, I'm all ears! Thanks, as always, for taking a look!

Edit: Holy smokes, that's a large screen shot. Apologies!  (Edit 2: Resized attachment)
« Last Edit: January 25, 2019, 02:27:20 PM by Bgoulette »