GML for basic fragment shader

Create a new Shader called shader_basic, the vertex shader (shader_basic.vsh) does not need any changes.

Fragment (shader_basic.fsh) code:

varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main() {
  // set the pixel color to its correct value
  gl_FragColor = v_vColour * texture2D(gm_BaseTexture, v_vTexcoord);
  // set red value of the pixel to maximum
  gl_FragColor.r = 1.0;
  // make the pixel a quarter more transparent
  gl_FragColor.a -= 0.25;
}

Create an object and add an instance of it to the room.

Add events Create, Step and Draw.

Create code:

// this variable controls when the shader is turned on
shader_active = false;

Step code:

// my project has 60 fps
// 10 makes a good movement amount
var move_amount = 10;
// move the object when keys are pressed
if (keyboard_check(vk_right) || keyboard_check(ord("D"))) x += move_amount;
if (keyboard_check(vk_left)  || keyboard_check(ord("A"))) x -= move_amount;
if (keyboard_check(vk_up)    || keyboard_check(ord("W"))) y -= move_amount;
if (keyboard_check(vk_down)  || keyboard_check(ord("S"))) y += move_amount;
// make sure avatar cannot leave the window
if (x < 0)           x = 0;
if (y < 0)           y = 0;
if (x > room_width)  x = room_width;
if (y > room_height) y = room_height;
// make the avatar rotate to follow the mouse
image_angle = point_direction(x, y, mouse_x, mouse_y);
// control the avatar size
var scale_amount = 0.1;
// scroll up makes avatar bigger
if (mouse_wheel_up()) {
  image_xscale += scale_amount;
  image_yscale += scale_amount;
}
// scroll down makes the avatar smaller
if (mouse_wheel_down()) {
  image_xscale -= scale_amount;
  image_yscale -= scale_amount;
}
// make sure the avatar does not get too small
if (image_xscale < scale_amount) image_xscale = scale_amount;
if (image_yscale < scale_amount) image_yscale = scale_amount;
// toggle the shader when the mouse is clicked
if  (mouse_check_button_pressed(mb_left)) shader_active = !shader_active;

Draw code:

if (shader_active) shader_set(shader_basic);
draw_self();
shader_reset();

Get Basic Fragment Shader

Leave a comment

Log in with your itch.io account to leave a comment.