Skip to content

Commit

Permalink
Added screen_to_ray() and fixed tan01()
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxyOfJungle committed Apr 8, 2023
1 parent 54e7db8 commit a93111f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 38 deletions.
2 changes: 1 addition & 1 deletion objects/obj_player/Step_0.gml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//

input_h = keyboard_check(vk_right) - keyboard_check(vk_left);
input_v = keyboard_check(vk_down) - keyboard_check(vk_up);
input_dir = point_direction(0, 0, input_h, input_v);
Expand Down
35 changes: 28 additions & 7 deletions objects/obj_tests/Draw_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ draw_debug_button(40, 40, "TEST");
draw_debug_slider(40, 100, 200, "Test", 0.5, -5, 5);


/*var _a = smoothstep(0, 255, gui_mouse_x_normalized*255); // returns 0 - 1
var _b = linearstep(0, 255, gui_mouse_x_normalized*255); // returns 0 - 1
var _c = lerp(0, 255, gui_mouse_x_normalized); // returns 0 - 255
var _d = step(0.5, gui_mouse_x_normalized); // returns 0 or 1
var _e = relerp(0, 1, gui_mouse_x_normalized, 0, room_width); // returns 0 - 255
draw_text(10, 200, _a);
draw_text(10, 220, _b);
draw_text(10, 240, _c);
draw_text(10, 260, _d);
draw_text(10, 280, _e);
draw_circle(room_width*_a, 200+10, 8, true);
draw_circle(room_width*_b, 220+10, 8, true);
draw_circle(room_width*(_c/255), 240+10, 8, true);
draw_circle(room_width*_d, 260+10, 8, true);
draw_circle(_e, 280+10, 8, true);*/


/*var _xx = 400;
var _yy = 180;
var _size = 256;
for (var i = 0; i < _size; i+=GoldenAngle) {
var dist = relerp(0, _size, i, 4, 200*gui_mouse_x_normalized);
draw_circle(_xx+(cos(i)*dist), _yy-(sin(i)*dist), relerp(0, _size, i, 3, 6), true);
}*/



//draw_set_color(c_white);
//var sc = gui_mouse_x_normalized * 30;
Expand Down Expand Up @@ -108,10 +136,6 @@ draw_text(10, 120, string_zeros(125, 10));
*/

/*
draw_text(400, 20, gui_mouse_x_delta);
draw_text(400, 40, gui_mouse_y_delta);
draw_text(400, 60, DELTA_TIME);
angle4 += 0.1;
draw_cone(room_width/2, room_height/2, angle4, 100, 45);
Expand All @@ -120,9 +144,6 @@ draw_text(10, 150, _dist);
draw_text(10, 200, point_direction_normalized(room_width/2, room_height/2, mouse_x, mouse_y));
draw_sprite_pos_persp(Sprite7, 0, obj_b1.x, obj_b1.y, obj_b2.x, obj_b2.y, obj_b3.x, obj_b3.y, obj_b4.x, obj_b4.y, 1);
var transform = new Vector2(mouse_x, mouse_y);
Expand Down
126 changes: 96 additions & 30 deletions scripts/__tgm_core/__tgm_core.gml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

/*------------------------------------------------------------------------------------------
/*--------------------------------------------------------------------------------------------
TurboGML. A complete library with must-have functionality.
- Library by FoxyOfJungle (Mozart Junior). (C) 2023, MIT License.
Expand All @@ -11,12 +11,12 @@
..............................
Special Thanks, contributions:
YellowAfterLife, Cecil, TheSnidr, Xot, Shaun Spalding, gnysek, icuurd12b42
YellowAfterLife, Cecil, TheSnidr, Xot, Shaun Spalding, gnysek, icuurd12b42, DragoniteSpam.
(authors' names written in comment inside the functions used)
Supporters:
RookTKO
-------------------------------------------------------------------------------------------*/
---------------------------------------------------------------------------------------------*/

/*
MIT License
Expand Down Expand Up @@ -74,7 +74,7 @@ function linearstep(minv, maxv, value) {
/// @param {Real} minv The value of the lower edge of the Hermite function.
/// @param {Real} maxv The value of the upper edge of the Hermite function.
/// @param {Real} value The source value for interpolation.
/// @returns {Real} Description
/// @returns {Real}
function smoothstep(minv, maxv, value) {
var t = clamp((value - minv) / (maxv - minv), 0, 1);
return t * t * (3 - 2 * t);
Expand All @@ -83,33 +83,33 @@ function smoothstep(minv, maxv, value) {
/// @desc 0 is returned if value < edge, and 1 is returned otherwise.
/// @param {Real} edge The location of the edge of the step function.
/// @param {Real} value The value to be used to generate the step function.
/// @returns {real} Description
/// @returns {real}
function step(edge, value) {
return (value < edge) ? 0 : 1;
}

/// @desc Returns the cosine, but with a normalized range of 0 to 1
/// @param {real} radians_angle Description
/// @returns {real} Description
/// @param {real} radians_angle Angle in radians.
/// @returns {real}
function cos01(radians_angle) {
gml_pragma("forceinline");
return (cos(radians_angle) * 0.5 + 0.5);
}

/// @desc Returns the sine, but with a normalized range of 0 to 1
/// @param {real} radians_angle Description
/// @returns {real} Description
/// @param {real} radians_angle Angle in radians.
/// @returns {real}
function sin01(radians_angle) {
gml_pragma("forceinline");
return (sin(radians_angle) * 0.5 + 0.5);
}

/// @desc Returns the tangent, but with a normalized range of 0 to 1
/// @param {real} radians_angle Description
/// @returns {real} Description
/// @param {real} radians_angle Angle in radians.
/// @returns {real}
function tan01(radians_angle) {
gml_pragma("forceinline");
return (sin(radians_angle) * 0.5 + 0.5);
return (tan(radians_angle) * 0.5 + 0.5);
}

/// @desc Calculates the distance traveled by an object in free fall under the influence of friction
Expand Down Expand Up @@ -275,6 +275,10 @@ function point_in_arc(px, py, x, y, angle, dist, fov) {
return (point_distance(px, py, x, y) < dist && abs(angle_difference(angle, point_direction(x, y, px, py))) < fov/2);
}

/// @desc This function prevents it from returning 0, returning another value instead, if this happen.
/// @param {Real} value The value.
/// @param {Real} zero_value Value to return.
/// @returns {real}
function non_zero(value, zero_value=1) {
return value != 0 ? value : zero_value;
}
Expand All @@ -301,12 +305,18 @@ function is_prime_number(number) {
return true;
}

function pow2_next(val) {
return 1 << ceil(log2(val));
/// @desc Returns the next power of two number, based on the value.
/// @param {real} value The value to check.
/// @returns {real}
function pow2_next(value) {
return 1 << ceil(log2(value));
}

function pow2_previous(val) {
return 1 << floor(log2(val));
/// @desc Returns the previous power of two number, based on the value.
/// @param {real} value The value to check.
/// @returns {real}
function pow2_previous(value) {
return 1 << floor(log2(value));
}

function fibonacci(n) {
Expand Down Expand Up @@ -1679,10 +1689,7 @@ function gui_to_room_dimension_ext(x1, y1, camera, angle, gui_width, gui_height,
}


/// @desc 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.
/// @desc Transforms a 2D coordinate (in window space) to a 3D vector (x, y, z). Z is the camera's near plane.
/// Works for both orthographic and perspective projections.
function screen_to_world_dimension(view_mat, proj_mat, xx, yy) {
// credits: TheSnidr
Expand Down Expand Up @@ -1711,15 +1718,51 @@ function screen_to_world_dimension(view_mat, proj_mat, xx, yy) {
}
var _xx = _matrix[0] * _matrix[5] / -_matrix[2] + _matrix[3];
var _yy = _matrix[1] * _matrix[5] / -_matrix[2] + _matrix[4];
return new Vector2(_xx, _yy);
return new Vector3(_xx, _yy, camera_get_near_plane(proj_mat));
}


/// @desc Transforms a 3D coordinate to a 2D coordinate. Returns a Vector2 with x and y.
/// Returns [-1, -1] if the 3D point is behind the camera
/// Returns a ray whose origin is the camera position (ray origin) Vector3(x, y, z). It also returns the direction of the vector Vector3(x, y, z).
///
/// Works for both orthographic and perspective projections.
function screen_to_ray(view_mat, proj_mat, xx, yy) {
// credits: TheSnidr / DragoniteSpam / FoxyOfJungle
var _mx = 2 * (xx / window_get_width() - 0.5) / proj_mat[0];
var _my = 2 * (yy / window_get_height() - 0.5) / proj_mat[5];
var _cam_x = - (view_mat[12] * view_mat[0] + view_mat[13] * view_mat[1] + view_mat[14] * view_mat[2]);
var _cam_y = - (view_mat[12] * view_mat[4] + view_mat[13] * view_mat[5] + view_mat[14] * view_mat[6]);
var _cam_z = - (view_mat[12] * view_mat[8] + view_mat[13] * view_mat[9] + view_mat[14] * view_mat[10]);
var _matrix = undefined; // [dx, dy, dz, ox, oy, oz]
if (proj_mat[15] == 0) {
// perspective projection
_matrix = [view_mat[2] + _mx * view_mat[0] + _my * view_mat[1],
view_mat[6] + _mx * view_mat[4] + _my * view_mat[5],
view_mat[10] + _mx * view_mat[8] + _my * view_mat[9],
_cam_x,
_cam_y,
_cam_z];
} else {
// orthographic projection
_matrix = [view_mat[2],
view_mat[6],
view_mat[10],
_cam_x + _mx * view_mat[0] + _my * view_mat[1],
_cam_y + _mx * view_mat[4] + _my * view_mat[5],
_cam_z + _mx * view_mat[8] + _my * view_mat[9]];
}
return {
origin : new Vector3(_matrix[3], _matrix[4], _matrix[5]),
direction : new Vector3(_matrix[0], _matrix[1], _matrix[2]),
}
}


/// @desc Transforms a 3D coordinate to a 2D coordinate. Returns a Vector2(x, y).
/// Returns Vector2(-1, -1) if the 3D point is behind the camera.
///
/// Works for both orthographic and perspective projections.
function world_to_screen_dimension(view_mat, proj_mat, xx, yy, zz, normalized=false) {
// credits: TheSnidr
// credits: TheSnidr / FoxyOfJungle
var _w = view_mat[2] * xx + view_mat[6] * yy + view_mat[10] * zz + view_mat[14];
if (_w <= 0) return new Vector2(-1, -1);
var _cx, _cy;
Expand All @@ -1734,9 +1777,9 @@ function world_to_screen_dimension(view_mat, proj_mat, xx, yy, zz, normalized=fa
}

if (normalized) {
return new Vector2((0.5+0.5*_cx), (0.5+0.5*_cy));
return new Vector2((_cx*0.5+0.5), (0.5+0.5*_cy));
} else {
return new Vector2((0.5+0.5*_cx) * window_get_width(), (0.5+0.5*_cy) * window_get_height());
return new Vector2((_cx*0.5+0.5) * window_get_width(), (_cy*0.5+0.5) * window_get_height());
}
}

Expand Down Expand Up @@ -3172,12 +3215,13 @@ function audio_create_stream_wav(file_audio) {

#region 3D

// vertex format
vertex_format_begin();
vertex_format_add_position_3d();
vertex_format_add_normal();
vertex_format_add_texcoord();
vertex_format_add_colour();
global.vbf_default_format = vertex_format_end();
vertex_format_add_color();
global.__vbf_3d_format = vertex_format_end();

/// @func vertex_add_point(vbuff, xx, yy, zz, nx, ny, nz, u, v, color, alpha)
function vertex_add_point(vbuff, xx, yy, zz, nx, ny, nz, u, v, color, alpha) {
Expand All @@ -3192,7 +3236,7 @@ function vertex_add_point(vbuff, xx, yy, zz, nx, ny, nz, u, v, color, alpha) {
function model_build_plane(x1, y1, z1, x2, y2, z2, hrepeat, vrepeat, color=c_white, alpha=1) {
var _vbuff = vertex_create_buffer();

vertex_begin(_vbuff, global.vf_default);
vertex_begin(_vbuff, global.__vbf_3d_format);
vertex_add_point(_vbuff, x1, y1, z1, 0, 0, 1, 0, 0, color, alpha);
vertex_add_point(_vbuff, x2, y1, z1, 0, 0, 1, hrepeat, 0, color, alpha);
vertex_add_point(_vbuff, x1, y2, z2, 0, 0, 1, 0, vrepeat, color, alpha);
Expand All @@ -3210,7 +3254,7 @@ function model_build_plane(x1, y1, z1, x2, y2, z2, hrepeat, vrepeat, color=c_whi
function model_build_cube(x1, y1, z1, x2, y2, z2, hrepeat, vrepeat, color=c_white, alpha=1) {
var _vbuff = vertex_create_buffer();

vertex_begin(_vbuff, global.vf_default);
vertex_begin(_vbuff, global.__vbf_3d_format);

// top
vertex_add_point(_vbuff, x1, y1, z2, 0, 0, 1, 0, 0, color, alpha);
Expand Down Expand Up @@ -3314,6 +3358,28 @@ function model_build_cube(x1, y1, z1, x2, y2, z2, hrepeat, vrepeat, color=c_whit
// return _vbuff;
//}

//global.__vbf_debug = vertex_create_buffer();

//vertex_format_begin();
//vertex_format_add_position_3d();
//vertex_format_add_color();
//global.vbf_default_format = vertex_format_end();


//function draw_line_3d(x1, y1, z1, x2, y2, z2) {
// var _vbf_line = global.__vbf_debug;
// vertex_begin(_vbf_line, global.vbf_default_format);

// vertex_position_3d(_vbf_line, x1, y1, z1);
// vertex_color(_vbf_line, c_white, 1);

// vertex_position_3d(_vbf_line, x2, y2, z2);
// vertex_color(_vbf_line, c_white, 1);

// vertex_end(_vbf_line);
//}


#endregion


Expand Down

0 comments on commit a93111f

Please sign in to comment.