Skip to content

Instantly share code, notes, and snippets.

@mahulst
Created September 16, 2017 10:02
Show Gist options
  • Save mahulst/3f580f37348b98c20abf508b90b4595b to your computer and use it in GitHub Desktop.
Save mahulst/3f580f37348b98c20abf508b90b4595b to your computer and use it in GitHub Desktop.
getClickPosition
getClickPosition : Model -> Mouse.Position -> Vec3
getClickPosition model pos =
let
x =
toFloat pos.x
y =
toFloat pos.y
normalizedPosition =
( (x * 2) / 1000 - 1, (1 - y / 1000 * 2) )
homogeneousClipCoordinates =
Vec4.vec4
(Tuple.first normalizedPosition)
(Tuple.second normalizedPosition)
-1
1
invertedViewMatrix =
Mat4.inverseOrthonormal (camera model)
invertedProjectionMatrix =
Maybe.withDefault Mat4.identity (Mat4.inverse perspective)
vec4CameraCoordinates =
mulVector invertedProjectionMatrix homogeneousClipCoordinates
direction =
Vec4.vec4 (Vec4.getX vec4CameraCoordinates) (Vec4.getY vec4CameraCoordinates) -1 0
vec4WorldCoordinates =
mulVector invertedViewMatrix direction
vec3WorldCoordinates =
vec3 (Vec4.getX vec4WorldCoordinates) (Vec4.getY vec4WorldCoordinates) (Vec4.getZ vec4WorldCoordinates)
normalizedVec3WorldCoordinates =
Vec3.normalize vec3WorldCoordinates
origin =
model.cameraPos
scaledDirection =
Vec3.scale 20 normalizedVec3WorldCoordinates
destination =
Vec3.add origin scaledDirection
in
destination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment