• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders How to/ is it possible to temporarily apply a shader to an object through a with statement?

Y

yeahbuddy28

Guest
So I have a cursor object and when it hovers over any object that is a child of "selectable" I want the object to have a outline around it. The way I would like to accomplish this is through a with statement located in the cursor object that applies a shader to the object it is hovering over. This is my first time working with shaders so I really just kinda copied stuff from this video. The code I have in the cursors step event is as follows
Code:
///behavior
//stay with mouse
x = mouse_x
y = mouse_y
//selecting objects
var selected = instance_place(x,y,selectable)
with (selected){
    upixelH = shader_get_uniform(shader_outline_1, "pixelH");
    upixelW = shader_get_uniform(shader_outline_1, "pixelW");
    texelW = texture_get_texel_width(sprite_get_texture(sprite_index,0));
    texelH = texture_get_texel_height(sprite_get_texture(sprite_index,0));
    //this part is supposed to be in a draw event I think
    shader_set(shader_outline_1);
    shader_set_uniform_f(upixelW, texelW);
    shader_set_uniform_f(upixelH, texelH);
    draw_self();
    shader_reset();
}
The code for the fragment part of the outline shader is as follows
Code:
//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform float pixelH;
uniform float pixelW;

void main()
{
    vec2 offsetx;
    offsetx.x = pixelW;
    vec2 offsety;
    offsety.y = pixelH;

    float alpha = texture2D(gm_BaseTexture, v_vTexcoord).a;

    alpha = max(alpha, texture2D(gm_BaseTexture, v_vTexcoord + offsetx).a);
    alpha = max(alpha, texture2D(gm_BaseTexture, v_vTexcoord - offsetx).a);
    alpha = max(alpha, texture2D(gm_BaseTexture, v_vTexcoord + offsety).a);
    alpha = max(alpha, texture2D(gm_BaseTexture, v_vTexcoord - offsety).a);

    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );
    gl_FragColor.a = alpha;
}
When I hover the cursor over the object nothing happens, I think the reason may be that part of the code in the with statement is supposed to be done in a draw event, however i'm not sure how to use a draw event when I am accessing the object through a with statement. Also I am on gamemaker 1.4.
 
It's typically not ideal to perform calculations and such in any Draw events, so most would recommend making "selected" an instance variable instead and checking if it doesn't equal noone before drawing, but realistically, you still could just switch it all to a Draw event. Do whichever works for you.
 
Top