Shaders Shader producing unexpected results

Hello all,
I have a newbie shader problem in gamemaker2 (2.1.5.246)
I apply a blur shader to my background layer from an object's create event and it looks fine. However, when I call shader_reset() in the object's draw event (to draw something unblurred on top), the background looks funny. The shader is still applied but the effect isn't as sharp. I don't understand what is going on. Code below and thank you!

The background layer is 100, the object's layer is 80.

the object's create event:
Code:
layer_script_begin(global.l_background,blur_on)
the object's draw event:
Code:
shader_reset()
draw_set_halign(fa_middle)
draw_set_valign(fa_center)
draw_set_color(c_black);
draw_text(room_width/2,room_height/2,"<animation of picking up thing>")
The blur_on script:
Code:
if event_type == ev_draw and event_number == ev_draw_begin
  {
       shader_set(sha_guassianblur)

   }
The shader itself:

Standard Vertex shader
Code:
//
// Simple passthrough vertex shader
//
attribute vec3 in_Position;                  // (x,y,z)
//attribute vec3 in_Normal;                  // (x,y,z)     unused in this shader.
attribute vec4 in_Colour;                    // (r,g,b,a)
attribute vec2 in_TextureCoord;              // (u,v)

varying vec2 v_vTexcoord;
varying vec4 v_vColour;

void main()
{
    vec4 object_space_pos = vec4( in_Position.x, in_Position.y, in_Position.z, 1.0);
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
 
    v_vColour = in_Colour;
    v_vTexcoord = in_TextureCoord;
}
Fragment shader
Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
vec3 size = vec3(512,512,8);
//uniform vec3 size;//width,height,radius

const int Quality = 8;
const int Directions = 16;
const float Pi = 6.28318530718;//pi * 2

void main()
{
    vec2 radius = size.z/size.xy;
    vec4 Color = texture2D( gm_BaseTexture, v_vTexcoord);
    for( float d=0.0;d<Pi;d+=Pi/float(Directions) )
    {
        for( float i=1.0/float(Quality);i<=1.0;i+=1.0/float(Quality) )
        {
                Color += texture2D( gm_BaseTexture, v_vTexcoord+vec2(cos(d),sin(d))*radius*i);
        }
    }
    Color /= float(Quality)*float(Directions)+1.0;
    gl_FragColor =  Color *  v_vColour;
}
 
Last edited:
Sure, here are some pics:

The background normally:
pantry.jpg
This is the shader working like it should, this is when I comment out shader_reset() in the draw event:
shaderok.jpg


But If I leave in the shader_reset() in the object's draw event I get the drawn stuff ok but the shader looks different, you can see a lot more black lines cutting through. I thought maybe I could adjust the shader's size vec3 variable but the results always have a similar problem.
shaderblurry.jpg
 

Attachments

Okay, what I think is happening is because you didn't turn off the shader, it is actually blurring the background twice. Once when you draw it to the application surface, and again when the application surface is drawn.

But not knowing more about the order in which you are drawing stuff, I can't be sure about that.

Now having said that, I note that the quality of your blur in the last picture is not very good. It could be that you don't have correct values for the size vec3.
 
Also - but not sure if this is relevant - the line
vec3 size = vec3(512,512,8);

in the fragment shader should be
const vec3 size = vec3(512,512,8);

Not sure if GMS does this automatically though.

If its still not good, increase the constant quality at the cost of performance. This blur effect is very simple to implement but very inefficient. So careful with that setting if you target mobiles.
 
Thanks guys. Your replies made me think about what was going on more. I checked out The reverend's tutorials and whipped up the veritcal/horizontal shader which works much better. Thanks!
 
Top