refactor(raycast/client)!: use new native (GetWorldCoordFromScreenCoord)

This native got merged days after I pushed the raycast function. This will break
scripts relying on this function (doubt there's many, if any).
This commit is contained in:
Linden
2022-08-18 01:58:54 +10:00
parent 99cab6d072
commit 8c8da4b119

View File

@@ -1,54 +1,28 @@
---------------------------------------
--- Source: https://github.com/citizenfx/lua/blob/luaglm-dev/cfx/libs/scripts/examples/scripting_gta.lua
--- Credits to gottfriedleibniz
local glm = require 'glm'
local glm_rad = glm.rad
local glm_quatEuler = glm.quatEulerAngleZYX
local glm_rayPicking = glm.rayPicking
local glm_up = glm.up()
local glm_forward = glm.forward()
local GetFinalRenderedCamCoord = GetFinalRenderedCamCoord
local GetFinalRenderedCamRot = GetFinalRenderedCamRot
local function screenPositionToCameraRay(fov, ratio)
local rot = glm_rad(GetFinalRenderedCamRot(2))
local q = glm_quatEuler(rot.z, rot.y, rot.x)
return GetFinalRenderedCamCoord(), glm_rayPicking(
q * glm_forward,
q * glm_up,
glm_rad(fov or GetFinalRenderedCamFov()),
ratio or GetAspectRatio(true),
0.10000, -- GetFinalRenderedCamNearClip(),
10000.0, -- GetFinalRenderedCamFarClip(),
0, 0
)
end
---------------------------------------
local StartShapeTestLosProbe = StartShapeTestLosProbe
local GetShapeTestResultIncludingMaterial = GetShapeTestResultIncludingMaterial
local GetWorldCoordFromScreenCoord = GetWorldCoordFromScreenCoord
local raycast = {}
---@param fov number? GetFinalRenderedCamFov
---@param ratio number? GetAspectRatio
---@param flag number? Defaults to 1|2|8|16 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586)
---@return vector3 endCoords
---@param flags number? Defaults to 1|2|8|16 (see: https://docs.fivem.net/natives/?_0x377906D8A31E5586)
---@param p8 number? A bit mask with bits 1, 2, 4, or 7 relating to collider types. 4 and 7 are usually used.
---@return boolean hit
---@return number entityHit
---@return vector3 endCoords
---@return vector3 surfaceNormal
---@return number materialHash
function raycast.cam(fov, ratio, flag)
local rayPos, rayDir = screenPositionToCameraRay(fov, ratio)
local destination = rayPos + 10 * rayDir
local handle = StartShapeTestLosProbe(rayPos.x, rayPos.y, rayPos.z, destination.x, destination.y, destination.z,
flag or 1 | 2 | 8 | 16, cache.ped, 4)
function raycast.cam(flags, p8)
local coords, normal = GetWorldCoordFromScreenCoord(0.5, 0.5)
local destination = coords + normal * 10
local handle = StartShapeTestLosProbe(coords.x, coords.y, coords.z, destination.x, destination.y, destination.z,
flags or 1 | 2 | 8 | 16, cache.ped, p8 or 4)
while true do
Wait(0)
local retval, _, endCoords, surfaceNormal, materialHash, entityHit = GetShapeTestResultIncludingMaterial(handle)
local retval, hit, endCoords, surfaceNormal, materialHash, entityHit = GetShapeTestResultIncludingMaterial(handle)
if retval ~= 1 then
return entityHit, endCoords, surfaceNormal, materialHash
---@diagnostic disable-next-line: return-type-mismatch
return hit, entityHit, endCoords, surfaceNormal, materialHash
end
end
end