I experiment with shaders, I think my code is correct but it actually isn't?

R

RealsLife

Guest
CREATE EVENT
Code:
///Get Shader Uniforms

//distortion effect shader
u_resolution_water = shader_get_uniform(sh_underwater, "iResolution");
u_seconds_water = shader_get_uniform(sh_underwater, "iGlobalTime");
texture_counter = background_get_texture(background1);
sampler_counter = shader_get_sampler_index(sh_underwater, "tex_water");
sec = 0;
DRAW EVENT
Code:
///Draw background with shader
//draw wave distortion effect
    if( shader_is_compiled(sh_underwater))
    {      
    shader_set(sh_underwater);
    shader_set_uniform_f(u_resolution_water,1536.0,864.0);
    shader_set_uniform_f(u_seconds_water,sec);
    texture_set_stage(sampler_counter,texture_counter);
    shader_reset();
    }
So I think or at least I thought I understood it. I give the background1 as a texture and want the "shader" used on it. In the end I reset it. I just get a black screen when I start the game. The shader used to work in the post draw event and used as texture the application surface, but I mean you have these other texture functions so this should work? Also in the game maker studio DEMO files I saw that shaders could be used in the normal draw event so I wanted to give it a go.

EDIT: Also if I try just before the shader_reset(); to draw somthing I think my texture page shows up with all sprites that are inside my current project
 
Hi, you have to draw something in between texture_set_stage and shader_reset, otherwise the shader will have no effect.

If you are getting your whole texture page when drawing with this shader, then one thing you can try is to mark background1 to be "used for 3d", which will put it on its own texture page.
 
R

RealsLife

Guest
Hi, you have to draw something in between texture_set_stage and shader_reset, otherwise the shader will have no effect.

If you are getting your whole texture page when drawing with this shader, then one thing you can try is to mark background1 to be "used for 3d", which will put it on its own texture page.
ow I thought
texture_counter = background_get_texture(background1); was used to draw the background with the shader on it? What does the
texture_counter = background_get_texture(background1); actually do xD?
 
when you use texture_set_stage you are just assigning a texture into one of the sampler indexes for the shader, which can then be sampled from within the fragment shader (i don't know if we have vertex shader texture sampling in GMS). However, none of these will be used unless something is drawn.
 
R

RealsLife

Guest
Using another surface than the application layer doesn't work :/... So what i've tried so far is drawing the new surface after the shader in the post-draw event, I tried using the GUI but with both when I draw it for some reason my viewport doesn't affect it anymore. My camera/view and room are really small 384x216 and my vierwport is 1536x864 but for some reason if I don't draw it inside the draw event the title/tekst doesn't change with the viewport accordingly. If I do put it in the draw event on the new surface the shader still affects it :(. I tried using resize functions after ther post-draw event but that doesn't work. All these things are new and i'm not a pro so at the moment I've an headache & getting a bit crazy over it xD. If you or anyone could fix this I sure owe you one :p!

EDIT: I can even put a project file on google drive If I have 2 or you can even try it out the shader is from a tutorial here on the forums https://forum.yoyogames.com/index.php?threads/video-tutorial-underwater-shader-effect.169/ I just want to try to use that water shader on a background and putting images in front of it who are not affected by it. Is it really that hard to do ugh :(?





EDIT: For anyone finding this in the future I found a solution myself or well another way not really an answer on what I asked I still don't really know what happened.

So my shader was on the application surface and I had a second surface with the menu because it didn't work out I switched them so my shader was on the selfmade surface and my menu on the application surface instead of post-draw event I just used the draw event and everything works fine now and much better I just had to edit some variables in the shader itself because for some reason instead of waves there was suddenly super small little waves(really weird lol).

Code:
if(!surface_exists(menu_surf))
{
   menu_surf = surface_create(384 ,216);
}
else
{
surface_set_target(menu_surf);
draw_clear_alpha(c_black,0);;
draw_background(background1,0,0);
surface_reset_target();
}


//shader
if(shader_is_compiled(sh_underwater))
  {         
  shader_set(sh_underwater);   
  shader_set_uniform_f(u_resolution_water,384.0,216.0);   
  shader_set_uniform_f(u_seconds_water,sec); 
  texture_set_stage(shader_get_sampler_index(sh_underwater, "tex_water"), surface_get_texture(menu_surf));
  draw_surface(menu_surf,0,0);
  shader_reset();
  }

 
for(i = 0; i < array_length_1d(menu); i += 1)
{
  if(i == selection)
     {
     draw_sprite_ext( spr_startmenu, 0+i, room_width/2-sprite_width, 75+(i*space),1 ,1 , 0, kleur,1);
      }
  else
     {
     draw_sprite_ext( spr_startmenu, 0+i, room_width/2-sprite_width, 75+(i*space),1 ,1 , 0, kleur,0.4);
     }               
}
In the end my code looked like this I hope this can help anyone who doesn't know any answer and need some inspiration ^_^! If anyone knows somthing about my previous problem with the app surface feel free to answer i'm active and check GMS forums often.
 
Last edited by a moderator:
Top