-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds some screenspace transform matrix functions
- Loading branch information
Showing
5 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
scripts/ScreenspaceToWorldspace/ScreenspaceToWorldspace.gml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
scripts/ScreenspaceToWorldspace/ScreenspaceToWorldspace.yy
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.