Skip to content

Instantly share code, notes, and snippets.

@Skylarity
Created November 30, 2024 20:29
Show Gist options
  • Save Skylarity/a6b425a8ac1e11fd6bac989b5145a8c0 to your computer and use it in GitHub Desktop.
Save Skylarity/a6b425a8ac1e11fd6bac989b5145a8c0 to your computer and use it in GitHub Desktop.
Updated version of `draw_sprite_pos_fixed` to work with modern GameMaker
function draw_pos_fixed(surface, top_left_x, top_left_y, top_right_x, top_right_y, bottom_right_x, bottom_right_y, bottom_left_x, bottom_left_y, colour, alpha) {
var cx, cy, s, t, q, v_buffer;
var texture = surface_get_texture(surface);
var uvs = texture_get_uvs(texture);
var ax = top_right_x - bottom_left_x;
var ay = top_right_y - bottom_left_y;
var bx = top_left_x - bottom_right_x;
var by = top_left_y - bottom_right_y;
var can = ax * by - ay * bx;
var cx, cy, s, t, q, v_buffer;
if (can != 0) {
cx = bottom_left_x - bottom_right_x;
cy = bottom_left_y - bottom_right_y;
s = (ax * cy - ay * cx) / can;
if (s > 0 and s < 1) {
t = (bx * cy - by * cx) / can;
if (t > 0 and t < 1) {
q[0] = 1 / (1 - t);
q[1] = 1 / (1 - s);
q[2] = 1 / t;
q[3] = 1 / s;
v_buffer = vertex_create_buffer();
vertex_begin(v_buffer, global.format_perspective);
// Top left
vertex_position(v_buffer, top_left_x, top_left_y);
vertex_normal(v_buffer, q[3] * uvs[0], q[3] * uvs[1], q[3]);
vertex_colour(v_buffer, colour, alpha);
vertex_texcoord(v_buffer, 0, 0);
// Top right
vertex_position(v_buffer, top_right_x, top_right_y);
vertex_normal(v_buffer, q[2] * uvs[2], q[2] * uvs[1], q[2]);
vertex_colour(v_buffer, colour, alpha);
vertex_texcoord(v_buffer, 0, 1);
// Bottom left
vertex_position(v_buffer, bottom_left_x, bottom_left_y);
vertex_normal(v_buffer, q[0] * uvs[0], q[0] * uvs[3], q[0]);
vertex_colour(v_buffer, colour, alpha);
vertex_texcoord(v_buffer, 1, 0);
// Bottom right
vertex_position(v_buffer, bottom_right_x, bottom_right_y);
vertex_normal(v_buffer, q[1] * uvs[2], q[1] * uvs[3], q[1]);
vertex_colour(v_buffer, colour, alpha);
vertex_texcoord(v_buffer, 1, 1);
vertex_end(v_buffer);
shader_set(sh_perspective);
vertex_submit(v_buffer, pr_trianglestrip, texture);
shader_reset();
vertex_delete_buffer(v_buffer);
}
}
}
}
varying vec4 v_vColour;
varying vec3 v_vNormal;
void main()
{
gl_FragColor = v_vColour * texture2D(gm_BaseTexture, v_vNormal.xy / v_vNormal.z);
}
attribute vec3 in_Position; // (x,y,z)
attribute vec3 in_Normal; // (qu,qv,q)
attribute vec4 in_Colour; // (r,g,b,a)
//attribute vec2 in_TextureCoord; // (u,v) -- unused in this shader.
varying vec4 v_vColour;
varying vec3 v_vNormal;
void main()
{
vec4 object_space_pos = vec4(in_Position.x, in_Position.y, in_Position.z, 1.0);
gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * object_space_pos;
v_vColour = in_Colour;
v_vNormal = in_Normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment