Legacy GM A way to bypass a shader.

G

gaberdell

Guest
Is there anyway to bypass a shader other than using a GUI (to clarify I mean a draw_sprite function in a Draw GUI). Why don't I want to use a GUI. It's sticking to the screen, and I don't know anyway to stop doing this, or remove it without removing the object it's made from. Thank you for reading this, sorry if there any grammar/spelling mistakes.
 

obscene

Member
Not sure what you mean. A shader only affects stuff inside the shader code.

shader_set();
// only stuff here is affected
shader_reset();
 
G

gaberdell

Guest
Oh, okay thank you. I put down draw_surface(application_surface,0,0); (I was fallowing a tutorial). So how could you specify certain objects (I'm new to game maker).
 

obscene

Member
Well, this gets tricky and being new to game maker it probably isn't something you want to jump right into. But here are some guidelines.

By default, all objects draw themselves to the application surface. When you draw_sprite(), you're actually drawing a sprite to the application surface. Game Maker automatically draw that surface after the objects have all been drawn.

Generally you do not need to draw the application surface manually unless you have a good reason for it. One reason might be to apply a shader but only if you want the entire surface to be affected.

Now, if you instead want a single object to be shaded, you put the shader code in that object's draw code. Set the shader, draw the sprites, reset the shader. You don't mess with the application surface in this case.

Ok, say you want multiple objects shaded. You could add shader code to all those objects and be just fine. But let's say you want a LOT of objects shaded and you don't want to add code to ALL those objects. In this case you have a third option....

GameMaker draws objects in order by depth in descending order. You CAN set a shader in the draw code of an object and leave it open. Every object drawn after that will be drawn with the shader applied. Then make sure you have another object with a lower depth to reset the shader when it's done. For instance, you could have a shader_set object at a depth of 10, and a shader_reset object at a depth of 5, and anything with a depth of 9,8,7 or 6 will be shaded.
 
G

gaberdell

Guest
Can I please have some more clarification? I'm having an issue where the wavy shader isn't working, it's just become my regular shader.
Here is my code (to make your life easier):

obj_pan (Draw Gui, Depth = 1):
shader_set(shd_wavy);
draw_self();

obj_water_background (Draw Gui End, Depth = 5):
shader_reset();

obj_tree (Draw Gui Begin, Depth = 0):
shader_set(shd_regular);
draw_self();
shader_reset();

P.S. Still by you explaining it made a lot of this shader stuff make sense.
 

icuurd12b42

TMC Founder
GMC Elder
> I'm having an issue where the wavy shader isn't working, it's just become my regular shader.
Such a shader requires you to pass uniforms, such as a time value, I don't see you setting any uniforms...
 
G

gaberdell

Guest
It was more or less an example, I didn't know that a wavy shader required a time value. My bad, I should have just said a "perspective" shader or a 2.5 shader. It was late and I felt wavy and a perspective shader weren't different. A very foolish error on my part. I'm sorry for troubling you so.
 

icuurd12b42

TMC Founder
GMC Elder
you can draw anything while a shader is active.
you can have shaders the affect the whole screen, sprites in the room or even, more complex, screen parts of the display.

The code you posted should work. anything erasing it?
 
G

gaberdell

Guest
Maybe it's the shader itself that isn't working. I have no clue how to erase shaders. The tutorial wanted me to stop the application surface from being drawn. So, I think that the code for the shader may be broken without the application surface not being drawn. Here if you spot anything weird or odd that I should fix, please let me know.

Code in the perspective shader (line for line exact copy at least I think from the video) in fragment:

varying vec2 v_vTexcoord; // currently reading pixels original position (x, y)
varying vec4 v_vColour; // currently reading pixels color (red, green, blue, alpha)


void main()
{
vec2 coordinates; // srotage for the new set of coordinates to repalce "v_vTexcoord"
float pixelDistanceX; // storage for the distance of current read pixel fron horizontal center
float pixelDistanceY; // storage for the distance of current read pixel fron vertical center
float offset; // storage for the distance we'll displace the pixel on the screen.
float dir; // direction in which we'll displace the pizels.

pixelDistanceX = distance(v_vTexcoord.x, 0.5); // calculate the current pixel distance from horizontal center
pixelDistanceY = distance(v_vTexcoord.y, 0.5); // calculate the current pixel distance from vertical center

offset = (pixelDistanceX*0.2) * pixelDistanceY; // offset will be the Y distance fro, vertical center multiplied by the 0.2 fractiopn of pixelDistanceX
// basically the further the pixel is from horizontal cetner and vertical center, the further the disnplacement will be

if (v_vTexcoord.y <= 0.5)
dir = 1.0; // if the pixel is before the half of the screen (0.5) then dispalce the pixel upwards (1)
else
dir = -1.0; // else displace downwards (-1)

// finally prepare the new texture "vector 2" (vec2)
coordinates = vec2(v_vTexcoord.x , v_vTexcoord.y + pixelDistanceX*(offset*6.0*dir));

// output the resulting shader
gl_FragColor = v_vColour * texture2D(gm_BaseTexture, coordinates);
}
 

icuurd12b42

TMC Founder
GMC Elder
gradually debug the shader, visually, setting pixels to specific colors. shaders are pretty hard to debug

for example
if (v_vTexcoord.y <= 0.5)
{
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
return;
}
turning up stuff to red.

Your code looks to work only for full screen stuff and also looks like it may go out of bounds on the uv
 
G

gaberdell

Guest
Well, I know how it works, but is there any way I can make it work without having to have the application surface not be drawn. Then have the applications surface be drawn again with the shader applied?
 
Last edited by a moderator:
Top