-
-
Save TheiLLeniumStudios/1f9135f0fab99e79ac1a409e4a82804c to your computer and use it in GitHub Desktop.
screen2dToWorld3d for Fivem !
This file contains hidden or 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 : https://rage.mp/forums/topic/1916-code-snippets/ | |
-- | |
-- | |
-- | |
-- | |
function screen2dToWorld3d(absoluteX, absoluteY) { | |
var camPos = GetGameplayCamCoords() | |
var rX, rY = processCoordinates(absoluteX, absoluteY) | |
var target = s2w(camPos, rX, rY) | |
var dir = sub(target, camPos) | |
var from = add(camPos, mulNumber(dir, 0.5)) | |
var to = add(camPos, mulNumber(dir, 300)) | |
var ray = CastRayPointToPoint(from, to, undefined, 287) | |
return ray === undefined ? undefined : ray.position | |
} | |
-- | |
-- | |
-- | |
function s2w(camPos, relX, relY) { | |
var camRot = GetGameplayCamRot() | |
var camForward = rotationToDirection(camRot) | |
var rotUp = add(camRot, vector3(10, 0, 0)) | |
var rotDown = add(camRot, vector3(-10, 0, 0)) | |
var rotLeft = add(camRot, vector3(0, 0, -10)) | |
var rotRight = add(camRot, vector3(0, 0, 10)) | |
var camRight = sub(rotationToDirection(rotRight), rotationToDirection(rotLeft)) | |
var camUp = sub(rotationToDirection(rotUp), rotationToDirection(rotDown)) | |
var rollRad = -degToRad(camRot.y) | |
var camRightRoll = sub(mulNumber(camRight, Math.cos(rollRad)), mulNumber(camUp, Math.sin(rollRad))) | |
var camUpRoll = add(mulNumber(camRight, Math.sin(rollRad)), mulNumber(camUp, Math.cos(rollRad))) | |
var point3D = add( | |
add( | |
add(camPos, mulNumber(camForward, 10.0)), | |
camRightRoll | |
), | |
camUpRoll) | |
var point2D = w2s(point3D) | |
if (point2D === undefined) { | |
return add(camPos, mulNumber(camForward, 10.0)) | |
} | |
var point3DZero = add(camPos, mulNumber(camForward, 10.0)) | |
var point2DZero = w2s(point3DZero) | |
if (point2DZero === undefined) { | |
return add(camPos, mulNumber(camForward, 10.0)) | |
} | |
var eps = 0.001 | |
if (math.abs(point2D.x - point2DZero.x) < eps || math.abs(point2D.y - point2DZero.y) < eps) { | |
return add(camPos, mulNumber(camForward, 10.0)) | |
} | |
var scaleX = (relX - point2DZero.x) / (point2D.x - point2DZero.x) | |
var scaleY = (relY - point2DZero.y) / (point2D.y - point2DZero.y) | |
var point3Dret = add( | |
add( | |
add(camPos, mulNumber(camForward, 10.0)), | |
mulNumber(camRightRoll, scaleX) | |
), | |
mulNumber(camUpRoll, scaleY)) | |
return point3Dret | |
} | |
-- | |
-- | |
-- | |
function processCoordinates(x, y) { | |
var screenX, screenY = GetActiveScreenResolution() | |
let relativeX = (1 - ((x / screenX) * 1.0) * 2) | |
let relativeY = (1 - ((y / screenY) * 1.0) * 2) | |
if (relativeX > 0.0) { | |
relativeX = -relativeX | |
} else { | |
relativeX = math.abs(relativeX) | |
} | |
if (relativeY > 0.0) { | |
relativeY = -relativeY | |
} else { | |
relativeY = math.abs(relativeY) | |
} | |
return relativeX, relativeY | |
} | |
-- | |
-- | |
-- | |
function w2s(position) { | |
var result = World3dToScreen2d(position.x, position.y, position.z) | |
if (result === nil) { | |
return nil | |
} | |
return vector3((result.x - 0.5) * 2, (result.y - 0.5) * 2, 0) | |
} | |
-- | |
-- | |
-- | |
function rotationToDirection(rotation) { | |
var z = degToRad(rotation.z) | |
var x = degToRad(rotation.x) | |
var num = math.abs(Math.cos(x)) | |
return vector3((-Math.sin(z) * num), (Math.cos(z) * num), Math.sin(x)) | |
} | |
-- | |
-- | |
-- | |
function degToRad(deg) { | |
return deg * Math.PI / 180.0 | |
} | |
-- | |
-- | |
-- | |
function add(vector1, vector2) { | |
return vector3(vector1.x + vector2.x, vector1.y + vector2.y, vector1.z + vector2.z) | |
} | |
-- | |
-- | |
-- | |
function sub(vector1, vector2) { | |
return vector3(vector1.x - vector2.x, vector1.y - vector2.y, vector1.z - vector2.z) | |
} | |
-- | |
-- | |
-- | |
function mulNumber(vector1, value) { | |
return vector3(vector1.x * value, vector1.y * value, vector1.z * value) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment