Skip to content

Instantly share code, notes, and snippets.

View DragoniteSpam's full-sized avatar
🐉
I like dragons

Michael DragoniteSpam

🐉
I like dragons
View GitHub Profile
@DragoniteSpam
DragoniteSpam / triangle_normals.gml
Last active December 14, 2021 14:32
Calculate the vector perpendicular to a triangle (GML).
function triangle_normal(x1, y1, z1, x2, y2, z2, x3, y3, z3) {
gml_pragma("forceinline");
var v1x = x2 - x1;
var v1y = y2 - y1;
var v1z = z2 - z1;
var v2x = x3 - x1;
var v2y = y3 - y1;
var v2z = z3 - z1;
var cx = v1y * v2z - v1z * v2y;
var cy = -v1x * v2z + v1z * v2x;
@DragoniteSpam
DragoniteSpam / camera_roll.gml
Last active May 14, 2020 02:55
Calculate the roll of a 3D camera
// I haven't used this in anything yet, but TheSnidr on the GameMaker discord posted
// this in response to a question about camera roll. He's usually pretty good at 3D
// math so I'm going to assume it works. "roll" is the angle around the direction the
// camera is looking at. Use for the view matrix.
var c = dcos(roll);
var s = dsin(roll) / max(math_get_epsilon(), sqrt(sqr(xdir) + sqr(ydir)));
matrix_build_lookat(x, y, z, x + xdir, y + ydir, z + zdir, ydir * s, -xdir * s, c);
/// @description convert_2d_to_3d(x, y, view_mat, proj_mat)
/// @param x
/// @param y
/// @param view_mat
/// @param proj_mat
/*
Transforms a 2D coordinate (in window space) to a 3D vector.
Returns an array of the following format:
[dx, dy, dz, ox, oy, oz]
where [dx, dy, dz] is the direction vector and [ox, oy, oz] is the origin of the ray.
/// @param xx
/// @param yy
/// @param zz
/// @param view_mat
/// @param proj_mat
/*
Transforms a 3D world-space coordinate to a 2D window-space coordinate. Returns an array of the following format:
[xx, yy]
Returns [-1, -1] if the 3D point is not in view
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
#pragma include("lighting.f.xsh")
/// https://github.com/GameMakerDiscord/Xpanda
uniform float lightBuckets;
varying vec4 v_lightColour;
function NVec2(_x, _y) {
x = _x;
y = _y;
Add = function(_val) {
return new NVec2(x + _val.x, y + _val.y);
}
Sub = function(_val) {
return new NVec2(x - _val.x, y - _val.y);
@DragoniteSpam
DragoniteSpam / billboarding.vsh
Created September 1, 2020 05:47
GLSL ES; use with matrix_set and any of the draw_sprite functions in GameMaker. Fragment shader is just the passthrough shader.
attribute vec3 in_Position; // (x,y,z)
//attribute vec3 in_Normal; // (x,y,z) unused in this shader.
attribute vec4 in_Colour; // (r,g,b,a)
attribute vec2 in_TextureCoord; // (u,v)
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
void main() {
vec4 object_space_pos = vec4(in_Position, 1.);
@DragoniteSpam
DragoniteSpam / hex.gml
Created October 19, 2020 02:47
No not the magical kind.
function hex(str) {
var result = 0;
// special unicode values
static ZERO = ord("0");
static NINE = ord("9");
static A = ord("A");
static F = ord("F");
@DragoniteSpam
DragoniteSpam / string_hex
Last active December 21, 2020 22:46
The opposite of hex().
function string_hex(value, padding) {
if (padding == undefined) padding = 0;
var s = sign(value);
var v = abs(value);
var output = "";
while (v > 0) {
var c = v & 0xf;
@DragoniteSpam
DragoniteSpam / ds_collection.gml
Created October 29, 2020 16:29
They're kinda like garbage collected ds_lists
function ds_collection_create() {
return [[undefined], 0, 0];
}
function ds_collection_add(collection, value) {
collection[@ 2]++;
var arr = collection[@ 0];
if (array_length(arr) <= collection[@ 1]) {
var new_arr = array_create(array_length(arr) * 2, undefined);
array_copy(new_arr, 0, arr, 0, array_length(arr));