Skip to content

Commit

Permalink
Adds some screenspace transform matrix functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JujuAdams committed Dec 22, 2023
1 parent d94b1bd commit fa2824b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions matrices.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions scripts/MouseToWorldspace/MouseToWorldspace.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Returns the point in worldspace on the far clipping plane that lies under the mouse
///
/// @param [viewMatrix] View matrix to use. If not provided, the current view matrix will be used
/// @param [projectionMatrix] Projection matrix to use. If not provided, the current projection matrix will be used

function MouseToWorldspace(_viewMatrix = matrix_get(matrix_view), _projectionMatrix = matrix_get(matrix_projection))
{
return ScreenspaceToWorldspace(window_mouse_get_x(),
window_get_height() - window_mouse_get_y(), //GM's coordinate system requires that we invert this axis
window_get_width(),
window_get_height(),
MatrixInverse(matrix_multiply(_viewMatrix, _projectionMatrix)));
}
11 changes: 11 additions & 0 deletions scripts/MouseToWorldspace/MouseToWorldspace.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions scripts/ScreenspaceToWorldspace/ScreenspaceToWorldspace.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Returns the point in worldspace on the far clipping plane that lies under the given screenspace coordinates
///
/// @param x x-coordinate in screenspace
/// @param y y-coordinate in screenspace
/// @param [width] Width of the screenspace render target. If not provided, the width of th current render target (either a surface or the backbuffer) will be used
/// @param [height] Height of the screenspace render target. If not provided, the height of the current render target (either a surface or the backbuffer) will be used
/// @param [inverseViewProjectionMatrix] The inverse of the view*projection matrix. If not provided, the current view and projection matrix will be used

function ScreenspaceToWorldspace(_x, _y, _width, _height, _inverseMatrix)
{
if ((_width == undefined) || (_height == undefined))
{
var _surface = surface_get_target();
}

if (_width == undefined) _width = (_surface < 0)? display_get_gui_width() : surface_get_width( _surface);
if (_height == undefined) _height = (_surface < 0)? display_get_gui_height() : surface_get_height(_surface);

_x = -1 + 2*_x/_width;
_y = -1 + 2*_y/_height;

if (_inverseMatrix == undefined)
{
_inverseMatrix = MatrixInverse(matrix_multiply(matrix_get(matrix_view), matrix_get(matrix_projection)));
}

var _vector = MatrixTransformVertexExt(_inverseMatrix, _x, _y, 1.0, 1.0);

var _inverseW = 1/_vector[3];
_vector[@ 0] *= _inverseW;
_vector[@ 1] *= _inverseW;
_vector[@ 2] *= _inverseW;

return _vector;
}
11 changes: 11 additions & 0 deletions scripts/ScreenspaceToWorldspace/ScreenspaceToWorldspace.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa2824b

Please sign in to comment.