Jump to content

Problem with custom Radar rendering


itsHasaN

Recommended Posts

hey, i am trying to make my custom radar, i used HUD mask shader in order to apply a circle shader to map, now my problem is how can i calculate the blips correct position on the map? it should be depend on the map zoom level, uvpostion and uvrotation, i also want to skip rendering the blips that are out of the map texture rendering part

 

local radarConfig = {
    x = 250,
    y = 1080 / 2,
    width = 300,
    height = 300,
    defaultZoom = 13,
}

local currentZoom = radarConfig.defaultZoom  -- Start with a default zoom level
local targetZoom = currentZoom  -- Initialize target zoom to the current zoom

function getElementSpeed(theElement, unit)
    -- Check arguments for errors
    assert(isElement(theElement), "Bad argument 1 @ getElementSpeed (element expected, got " .. type(theElement) .. ")")
    local elementType = getElementType(theElement)
    assert(elementType == "player" or elementType == "ped" or elementType == "object" or elementType == "vehicle" or elementType == "projectile", "Invalid element type @ getElementSpeed (player/ped/object/vehicle/projectile expected, got " .. elementType .. ")")
    assert((unit == nil or type(unit) == "string" or type(unit) == "number") and (unit == nil or (tonumber(unit) and (tonumber(unit) == 0 or tonumber(unit) == 1 or tonumber(unit) == 2)) or unit == "m/s" or unit == "km/h" or unit == "mph"), "Bad argument 2 @ getElementSpeed (invalid speed unit)")
    -- Default to m/s if no unit specified and 'ignore' argument type if the string contains a number
    unit = unit == nil and 0 or ((not tonumber(unit)) and unit or tonumber(unit))
    -- Setup our multiplier to convert the velocity to the specified unit
    local mult = (unit == 0 or unit == "m/s") and 50 or ((unit == 1 or unit == "km/h") and 180 or 111.84681456)
    -- Return the speed by calculating the length of the velocity vector, after converting the velocity to the specified unit
    return (Vector3(getElementVelocity(theElement)) * mult).length
end

addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        hudMaskShader = dxCreateShader("hud_mask.fx")
        maskTexture = dxCreateTexture("images/circle_mask.png")
        radarTexture = dxCreateTexture("images/map.png")

        -- Check everything is ok
		bAllValid = hudMaskShader and radarTexture and maskTexture

		if not bAllValid then
			outputChatBox( "Could not create some things. Please use debugscript 3" )
		else
			dxSetShaderValue( hudMaskShader, "sPicTexture", radarTexture )
			dxSetShaderValue( hudMaskShader, "sMaskTexture", maskTexture )
		end
    end 
)

addEventHandler("onClientRender", root, 
    function()
        if not bAllValid then return end
        
        drawRadar()
    end
)

function drawRadar()
    --
    -- Transform world x,y into -0.5 to 0.5
    --
    local x, y = getElementPosition(localPlayer)
    x = (x) / 6000
    y = (y) / -6000
    dxSetShaderValue(hudMaskShader, "gUVPosition", x, y)

    --
    -- Zoom
    --
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if vehicle and getElementSpeed(vehicle, "m/s") > 20 then
        targetZoom = radarConfig.defaultZoom - 5
    else
        targetZoom = radarConfig.defaultZoom
    end

    -- Smoothly transition to the target zoom level
    currentZoom = currentZoom + (targetZoom - currentZoom) * 0.05  -- Adjust the 0.1 value for speed of transition

    -- Set the shader value with the current zoom
    local scaleValue = 1 / currentZoom
    dxSetShaderValue(hudMaskShader, "gUVScale", scaleValue, scaleValue)

    --
    -- Rotate to camera direction - OPTIONAL
    --
    local _, _, camrot = getElementRotation(getCamera())
    dxSetShaderValue(hudMaskShader, "gUVRotAngle", math.rad(-camrot))

    --
    -- Draw the radar map
    --
    dxDrawImage(radarConfig.x, radarConfig.y, radarConfig.width, radarConfig.height, hudMaskShader, 0, 0, 0, tocolor(255, 255, 255, 230))

    for i, blip in pairs(getElementsByType("blip")) do
        drawRadarBlip(blip)
    end
end

function drawRadarBlip(elm)
  -- this is where i am stuck
end

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...