Author Topic: Vertex shader...  (Read 3560 times)

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1239
    • View Profile
    • My deviantart page
Vertex shader...
« on: August 15, 2018, 09:17:45 AM »
I found this code for a vertex shader, it should be a blur shader AFAIK but there's no example layout so I'm lost at how to call it. Anyone has a clue?

Code: [Select]
/* VBlurVertexShader.glsl */
attribute vec4 a_position;
attribute vec2 a_texCoord;
 
varying vec2 v_texCoord;
varying vec2 v_blurTexCoords[14];
 
void main()
{
    gl_Position = a_position;
    v_texCoord = a_texCoord;
    v_blurTexCoords[ 0] = v_texCoord + vec2(0.0, -0.028);
    v_blurTexCoords[ 1] = v_texCoord + vec2(0.0, -0.024);
    v_blurTexCoords[ 2] = v_texCoord + vec2(0.0, -0.020);
    v_blurTexCoords[ 3] = v_texCoord + vec2(0.0, -0.016);
    v_blurTexCoords[ 4] = v_texCoord + vec2(0.0, -0.012);
    v_blurTexCoords[ 5] = v_texCoord + vec2(0.0, -0.008);
    v_blurTexCoords[ 6] = v_texCoord + vec2(0.0, -0.004);
    v_blurTexCoords[ 7] = v_texCoord + vec2(0.0,  0.004);
    v_blurTexCoords[ 8] = v_texCoord + vec2(0.0,  0.008);
    v_blurTexCoords[ 9] = v_texCoord + vec2(0.0,  0.012);
    v_blurTexCoords[10] = v_texCoord + vec2(0.0,  0.016);
    v_blurTexCoords[11] = v_texCoord + vec2(0.0,  0.020);
    v_blurTexCoords[12] = v_texCoord + vec2(0.0,  0.024);
    v_blurTexCoords[13] = v_texCoord + vec2(0.0,  0.028);
}

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Vertex shader...
« Reply #1 on: August 16, 2018, 07:38:07 PM »
i tryed a few things nothing worked....    that code is from arcade bliss theme.

"ArcadeBliss Cab Edition"     it is working in sink with the frag.txt   i think

sorry that i cant help.......mabe you should give him a chime, and see what he says?

if so please report back i would love to know to....

you can mod the frag.txt and it responds well  crazzy blurs....but not the vertex.txt


 :o
help a friend....

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1239
    • View Profile
    • My deviantart page
Re: Vertex shader...
« Reply #2 on: August 16, 2018, 11:23:57 PM »
i tryed a few things nothing worked....    that code is from arcade bliss theme.

"ArcadeBliss Cab Edition"     it is working in sink with the frag.txt   i think

Yes you are right I should ask AracadeBliss directly :) Actually it doesn't seem to work in tandem with frag.txt: I can use frag.txt for gaussian blur but as you say vertex.txt seems to do nothing...
I have no experience in shader programming but I'd love to see a shader that can do horizontal and vertical blur all in one pass (without using nested surfaces), I think I'll had to wait for Oomek shader or start studying myself :D

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1239
    • View Profile
    • My deviantart page
Re: Vertex shader...
« Reply #3 on: August 20, 2018, 01:33:38 AM »
So if someone needs it, I wrote a simple fragment shader that applies a 9x9 gaussian blur in a single pass. You have to call it this way:

Code: [Select]
local shaderblur = fe.add_shader( Shader.Fragment, "blur32.txt" )
shaderblur.set_texture_param( "source")
shaderblur.set_param("pixelBlur", 32)
bgpic.shader = shaderblur

This code assumes you created a "bgpic" image which is, in this case, 32x32 pixel, then the parameter to pass (pixelBlur) is used to scale the shader with respect to the texture. For example if the texture is 32x32 and you pass pixelBlur 32 you are sampling each pixel correctly, otherwise you could see artifacts when you sample 2 or more pixels away.

The code applies a 9x9 gaussian blur with a sigma of 3, so it's good for heavy blurs. Consider that using a single pass gaussian blur is not very efficient since you have to do 81 steps, instead using a two pass gaussian blur you apply just 9 steps + 9 steps. I'm also investigating some code that uses bilinear filtering to make blur even faster...

Here is the shader code:

Code: [Select]
uniform sampler2D source;
uniform float pixelBlur;


void main() {
   vec2 sourceCoordinates = gl_TexCoord[0].xy;
   vec4 color = vec4(0.0);
   
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,-4.0/pixelBlur)) *0.00401;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,-3.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,-2.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,-1.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,0.0/pixelBlur)) *0.009675;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,1.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,2.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,3.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,4.0/pixelBlur)) *0.00401;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,-4.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,-3.0/pixelBlur)) *0.008667;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,-2.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,-1.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,0.0/pixelBlur)) *0.014223;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,1.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,2.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,3.0/pixelBlur)) *0.008667;
color += texture2D(source, sourceCoordinates + vec2 (-3.0/pixelBlur,4.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,-4.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,-3.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,-2.0/pixelBlur)) *0.015028;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,-1.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,0.0/pixelBlur)) *0.018729;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,1.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,2.0/pixelBlur)) *0.015028;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,3.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (-2.0/pixelBlur,4.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,-4.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,-3.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,-2.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,-1.0/pixelBlur)) *0.020909;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,0.0/pixelBlur)) *0.022092;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,1.0/pixelBlur)) *0.020909;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,2.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,3.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (-1.0/pixelBlur,4.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,-4.0/pixelBlur)) *0.009675;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,-3.0/pixelBlur)) *0.014223;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,-2.0/pixelBlur)) *0.018729;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,-1.0/pixelBlur)) *0.022092;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,0.0/pixelBlur)) *0.023342;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,1.0/pixelBlur)) *0.022092;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,2.0/pixelBlur)) *0.018729;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,3.0/pixelBlur)) *0.014223;
color += texture2D(source, sourceCoordinates + vec2 (0.0/pixelBlur,4.0/pixelBlur)) *0.009675;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,-4.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,-3.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,-2.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,-1.0/pixelBlur)) *0.020909;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,0.0/pixelBlur)) *0.022092;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,1.0/pixelBlur)) *0.020909;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,2.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,3.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (1.0/pixelBlur,4.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,-4.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,-3.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,-2.0/pixelBlur)) *0.015028;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,-1.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,0.0/pixelBlur)) *0.018729;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,1.0/pixelBlur)) *0.017726;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,2.0/pixelBlur)) *0.015028;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,3.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (2.0/pixelBlur,4.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,-4.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,-3.0/pixelBlur)) *0.008667;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,-2.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,-1.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,0.0/pixelBlur)) *0.014223;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,1.0/pixelBlur)) *0.013461;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,2.0/pixelBlur)) *0.011412;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,3.0/pixelBlur)) *0.008667;
color += texture2D(source, sourceCoordinates + vec2 (3.0/pixelBlur,4.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,-4.0/pixelBlur)) *0.00401;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,-3.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,-2.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,-1.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,0.0/pixelBlur)) *0.009675;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,1.0/pixelBlur)) *0.009157;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,2.0/pixelBlur)) *0.007763;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,3.0/pixelBlur)) *0.005895;
color += texture2D(source, sourceCoordinates + vec2 (4.0/pixelBlur,4.0/pixelBlur)) *0.00401;

   gl_FragColor = color;
}

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Vertex shader...
« Reply #4 on: August 20, 2018, 07:20:37 AM »
here is your code.

Code: [Select]
local flx = fe.layout.width;
local fly = fe.layout.height;
local flw = fe.layout.width;
local flh = fe.layout.height;

local bgpic = fe.add_image( "fanart.png", flx*0.200, fly*0.200, flw*0.320, flh*0.320 );

local shaderblur = fe.add_shader( Shader.Fragment, "blur32.txt" )
shaderblur.set_texture_param( "source")
shaderblur.set_param("pixelBlur", 32)
bgpic.shader = shaderblur

and this is what i get..


it is not smoth..my thought was you said needs to be 32x32    so 32x10 = 320
so i set the bgpic to that..

any way to make it smooth?
help a friend....

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1239
    • View Profile
    • My deviantart page
Re: Vertex shader...
« Reply #5 on: August 20, 2018, 08:01:02 AM »
it is not smoth..my thought was you said needs to be 32x32    so 32x10 = 320
so i set the bgpic to that..

any way to make it smooth?

That kind of artifacts appears when you are not sampling each pixel, but you are skipping some pixels or sampling half pixels. To make a long story short, you have to modify the shader and add another parameter, so you can separate pixelBlur in two values, one for x and one for y. Best way to do so is to change the shader so that instead of

Code: [Select]
uniform float pixelBlur
you have

Code: [Select]
uniform vec2 pixelBlur
and the lines where the weights are applied from

Code: [Select]
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur,-4.0/pixelBlur)) *0.00401;
to

Code: [Select]
color += texture2D(source, sourceCoordinates + vec2 (-4.0/pixelBlur.x,-4.0/pixelBlur.y)) *0.00401;
there's probably some more elegant way like

Code: [Select]
color += texture2D(source, sourceCoordinates + vec2 (-4.0,-4.0)*pixelBlur *0.00401;
but I didn't check it.

Then when you call your shader use

Code: [Select]
shaderblur.set_param("pixelBlur", 32 , 10 )
In this way the sampling will be different for x and y direction and you shouldn't have that kind of artifact.

32 and 10 are the width and height in pixel of your image in this case. To avoid artifacts always use the correct scaling, and consider that if you apply the shader to the picture, it will use the "texture resolution", not the display resolution AFAIK.

To obtain larger blurs without using large kernels, the trick is to place the picture on a surface with the proper size, and apply the shader to the surface, then scale it up to the size you want.
« Last Edit: August 20, 2018, 08:03:51 AM by zpaolo11x »

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Vertex shader...
« Reply #6 on: August 20, 2018, 06:43:56 PM »
thank you,  ill mess around a bit.....see what i can do :D
help a friend....