GameMaker Black silhouette when using shader

Suzaku

Member
Well this is my first time trying to use shaders, so basically I went to yoyo marketplace and downloaded a shader for example(in this case im using a shader that creates scanlines on the screen). I imported only the shader files(I hope this is ok), and went to my draw event in a test object, and inserted this code below:
1590367842690.png

And the result im having is this:
1590367420738.png
A totally black sprite, and at his origin point GM is drawing a surface that is nothing more than the background color that Im using on that room. I tried other shaders and the same thing happens.
I would like some advices on how to fix that problem. Thank you.
 

Suzaku

Member
Well the shader has this code inside:
GML:
attribute vec3 in_Position;
attribute vec4 in_Colour;
attribute vec2 in_TextureCoord;

varying vec2 v_texcoord;

void main()
{
  
    gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.0);
    v_texcoord = in_TextureCoord;
}
GML:
varying vec2 v_texcoord;

uniform vec2 resolution;
 
void main()
{

    vec4 colour = texture2D(gm_BaseTexture, v_texcoord);
    
    float half_y = v_texcoord.y * resolution.y * 0.5;
    float delta = floor(half_y) - half_y;
    if (delta * delta < 0.1) { colour.rgb = vec3(0.0); }
  
    gl_FragColor = colour;
}

GM is also saying this in the output window:
"Trying to set texture that is also bound as depth buffer - bailing..."
 
Last edited:

ophelius

Member
You have a uniform called resolution, which is a value you pass to the shader from your program.
Without passing anything, the value is 0, which means the variable half_y would = 0, making delta 0,
and by the time you get to this line:
if (delta * delta < 0.1) { colour.rgb = vec3(0.0); }
your color vector is set to black because delta * delta < 0.1;

So pass a value to resolution using the shader_set_uniform_f function, make sure to pass 2 values since the uniform is a vec2 (x and y components).
But you'll need to first get the uniform handle with shader_get_uniform() before passing a value to it.
I can't tell you what values to pass, understanding what the shader needs is up to you.

More info here:
shader_set_uniform_f
 
Last edited:

Suzaku

Member
Good explanation, thank you. I managed to get it working but now the shader made everything in my game to not appear, like if my sprites were not being drawn anymore and all that remains is my background color and some text that is being drawn after the shader code.
1590442408280.png
This is the shader calling code, in my draw gui event:
GML:
if shader_enabled shader_set(shd_scanlines);
    shader_set_uniform_f(uni_time, var_time_var);
    shader_set_uniform_f(uni_mouse_pos, var_mouse_pos_x, var_mouse_pos_y);
    shader_set_uniform_f(uni_resolution, var_resolution_x, var_resolution_y);
  
draw_surface(surf,0,0);
shader_reset();
 

ophelius

Member
Are you trying to get the shader to work on the sprite alone or the entire screen?
Because your first post shows only the sprite in black. And which Draw gui event is this being called from?
I'm just trying to figure out your goal with this
 

Suzaku

Member
Are you trying to get the shader to work on the sprite alone or the entire screen?
Because your first post shows only the sprite in black. And which Draw gui event is this being called from?
I'm just trying to figure out your goal with this
Well at first I was just discovering how the shader works, trying to draw a surface with the shader to make sure it was working somehow, thats why I was drawing on a sprite. My main goal is to make the shader cover the entire screen, with a scanline effect. The thing is that the scanline effect is working now, it covers the screen with the effect, but my sprites and scenario and everything is not appearing, as if the shader is disabling my drawings or something. All that appears in my screen now is just a background color, the scanlines and some text that is drawn after the shader code in the draw gui event.
 
Top