плавающий renderFontDrawText

demuert

Новичок
Автор темы
2
0
Версия MoonLoader
.027.0-preview
как сделать нормальный renderFontDrawText по 3д координатам, чтобы он не плавал за движением камеры или перса и был всегда на нужных координатах.

я делаю кастомный dl с инфой об авто, мне нужно, чтобы текст всегда был на позиции авто, но он бля плавает при моём движении
__________________
как фиксить братва, мб есть альтернативы рендера текста


Lua:
local imgui = require 'imgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local showhealth = imgui.ImBool(false)
local showid = imgui.ImBool(false)
local showmodel = imgui.ImBool(false)
local showdistance = imgui.ImBool(false)
local windowRender = imgui.ImBool(false)

local distanceLimitValue = 300.0
local distanceLimit = imgui.ImFloat(distanceLimitValue)

local active = false
local font = renderCreateFont("Arial", 9, 7)
local sw, sh = getScreenResolution()


local screenX, screenY = getScreenResolution()

function isVehicleVisible(x, y, z)
    local camX, camY, camZ = getActiveCameraCoordinates()
    local cx, cy, cz = getActiveCameraPointAt()

    local dirX, dirY, dirZ = cx - camX, cy - camY, cz - camZ
    local len = math.sqrt(dirX^2 + dirY^2 + dirZ^2)
    dirX, dirY, dirZ = dirX / len, dirY / len, dirZ / len

    local toVehX, toVehY, toVehZ = x - camX, y - camY, z - camZ
    local toLen = math.sqrt(toVehX^2 + toVehY^2 + toVehZ^2)
    toVehX, toVehY, toVehZ = toVehX / toLen, toVehY / toLen, toVehZ / toLen

    local dot = dirX * toVehX + dirY * toVehY + dirZ * toVehZ
    
    return dot > 0.707
end


function main()
    while not isSampAvailable() do wait(0) end

    sampRegisterChatCommand('cdl', function() active = not active end)
    sampRegisterChatCommand('cdlset', function() windowRender.v = not windowRender.v end)

    while true do
        wait(0)
        imgui.Process = windowRender.v

        if not active then goto continue end

        local myX, myY, myZ = getCharCoordinates(playerPed)

        for i = 1, 2000 do
            local result, vehHandle = sampGetCarHandleBySampVehicleId(i)
            if result and doesVehicleExist(vehHandle) and isCarOnScreen(vehHandle) then
                local x, y, z = getCarCoordinates(vehHandle)
                local dist = getDistanceBetweenCoords3d(myX, myY, myZ, x, y, z)

                if dist <= distanceLimit.v and isVehicleVisible(x, y, z) then
                    local sx, sy = convert3DCoordsToScreen(x, y, z)

                    if sx and sy then
                        local health = getCarHealth(vehHandle)
                        local model = getCarModel(vehHandle)

                        local tId = showid.v and ('ID: ' .. i .. '\n') or ''
                        local tHealth = showhealth.v and ('Health: ' .. math.floor(health) .. '\n') or ''
                        local tModel = showmodel.v and ('Model ID: ' .. model .. '\n') or ''
                        local tDist = showdistance.v and ('Distance: ' .. math.floor(dist)) or ''
                        local text = tId .. tHealth .. tModel .. tDist

                        if text ~= '' then
                            renderFontDrawText(font, text, sx, sy, 0xFFFFFFFF)
                        end
                    end
                end
            end
        end

        ::continue::
    end
end
local arrayVars = {showhealth, showid, showdistance, showmodel}

function imgui.OnDrawFrame()
    if windowRender.v then
        imgui.SetNextWindowSize(imgui.ImVec2(350, 300), imgui.Cond.FirstUseEver)
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 5.5, sh / 2.7), imgui.Cond.FirstUseEver)
        imgui.Begin(u8('Настройки кастомного /dl'), windowRender)

        local text = u8('Включение отображения нужной информации:')
        local textWidth = imgui.CalcTextSize(text).x
        local windowWidth = imgui.GetWindowSize().x
        imgui.SetCursorPosX((windowWidth - textWidth) * 0.5)
        imgui.Text(text)

        imgui.Checkbox(u8('ХП авто'), showhealth)
        imgui.Checkbox(u8('ID авто'), showid)
        imgui.Checkbox(u8('Расстояние к авто'), showdistance)
        imgui.Checkbox(u8('Модель авто'), showmodel)

        local buttonWidth = 150
        local spacing = 10
        local totalWidth = (buttonWidth * 2) + spacing
        imgui.SetCursorPosX((windowWidth - totalWidth) * 0.5)

        if imgui.Button(u8('Включить всё'), imgui.ImVec2(buttonWidth, 25)) then
            for k, v in ipairs(arrayVars) do v.v = true end
        end
        imgui.SameLine()
        if imgui.Button(u8('Выключить всё'), imgui.ImVec2(buttonWidth, 25)) then
            for k, v in ipairs(arrayVars) do v.v = false end
        end

        local distanceText = u8("Макс. дистанция отображения: %.0f м"):format(distanceLimit.v)
        local textDistWidth = imgui.CalcTextSize(distanceText).x
        imgui.SetCursorPosX((windowWidth - textDistWidth) * 0.5)
        imgui.Text(distanceText)

        local sliderWidth = 300
        imgui.SetCursorPosX((windowWidth - sliderWidth) * 0.5)
        imgui.PushItemWidth(sliderWidth)
        imgui.SliderFloat("##distanceSlider", distanceLimit, 1.0, 500.0, "%.0f", 1.0)
        imgui.PopItemWidth()

        imgui.End()
    end
end

function apply_custom_style()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local clr = imgui.Col
    local ImVec4 = imgui.ImVec4
    local ImVec2 = imgui.ImVec2

    style.WindowPadding = ImVec2(15, 15)
    style.WindowRounding = 5.0
    style.FramePadding = ImVec2(5, 5)
    style.FrameRounding = 4.0
    style.ItemSpacing = ImVec2(12, 8)
    style.ItemInnerSpacing = ImVec2(8, 6)
    style.IndentSpacing = 25.0
    style.ScrollbarSize = 15.0
    style.ScrollbarRounding = 9.0
    style.GrabMinSize = 5.0
    style.GrabRounding = 3.0

    colors[clr.Text] = ImVec4(0.80, 0.80, 0.83, 1.00)
    colors[clr.TextDisabled] = ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[clr.WindowBg] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.ChildWindowBg] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.PopupBg] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.Border] = ImVec4(0.80, 0.80, 0.83, 0.88)
    colors[clr.BorderShadow] = ImVec4(0.92, 0.91, 0.88, 0.00)
    colors[clr.FrameBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.FrameBgHovered] = ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[clr.FrameBgActive] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.TitleBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.TitleBgCollapsed] = ImVec4(1.00, 0.98, 0.95, 0.75)
    colors[clr.TitleBgActive] = ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[clr.MenuBarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.ScrollbarBg] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.ScrollbarGrab] = ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[clr.ScrollbarGrabHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.ScrollbarGrabActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.ComboBg] = ImVec4(0.19, 0.18, 0.21, 1.00)
    colors[clr.CheckMark] = ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[clr.SliderGrab] = ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[clr.SliderGrabActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.Button] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.ButtonHovered] = ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[clr.ButtonActive] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.Header] = ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[clr.HeaderHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.HeaderActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.ResizeGrip] = ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[clr.ResizeGripHovered] = ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[clr.ResizeGripActive] = ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[clr.CloseButton] = ImVec4(0.40, 0.39, 0.38, 0.16)
    colors[clr.CloseButtonHovered] = ImVec4(0.40, 0.39, 0.38, 0.39)
    colors[clr.CloseButtonActive] = ImVec4(0.40, 0.39, 0.38, 1.00)
    colors[clr.PlotLines] = ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[clr.PlotLinesHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[clr.PlotHistogram] = ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[clr.PlotHistogramHovered] = ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[clr.TextSelectedBg] = ImVec4(0.25, 1.00, 0.00, 0.43)
    colors[clr.ModalWindowDarkening] = ImVec4(1.00, 0.98, 0.95, 0.73)
end

apply_custom_style()
 
Последнее редактирование:

demuert

Новичок
Автор темы
2
0
Проблема в самом renderFontDrawText. Вряд-ли это можно исправить. Как вариант, используй DrawList из mimgui.
может знаешь какие-то проекты на lua, где нормальная отрисовка текста по координатам.
может есть какие-то костыли, ещё чёт. буду признателен
 

Winstаl

Известный
853
334
может знаешь какие-то проекты на lua, где нормальная отрисовка текста по координатам.
может есть какие-то костыли, ещё чёт. буду признателен
Напрямую через DirectX
 

fokichevskiy

Известный
496
283
переделал всё на mimgui, добавил DrawList вместо renderDrawText, реализовал нормальную систему проверки машины на экране

Lua:
local imgui = require 'mimgui'
local encoding = require 'encoding'
encoding.default = 'CP1251'
u8 = encoding.UTF8

local showhealth = imgui.new.bool()
local showid = imgui.new.bool()
local showmodel = imgui.new.bool()
local showdistance = imgui.new.bool()
local windowRender = imgui.new.bool()

local distanceLimitValue = 300.0
local distanceLimit = imgui.new.float(distanceLimitValue)

local active = false


function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('cdl', function() active = not active end)
    sampRegisterChatCommand('cdlset', function() windowRender[0] = not windowRender[0] end)
end

local arrayVars = {showhealth, showid, showdistance, showmodel}

local cdlDrawList = imgui.OnFrame( -- drawlist вместо renderDrawText
    function() return true end,
    function(self)
        self.HideCursor = true
        local dl = imgui.GetBackgroundDrawList()
        if active then
            local myX, myY, myZ = getCharCoordinates(PLAYER_PED)

            for i = 1, 2000 do
                local result, vehHandle = sampGetCarHandleBySampVehicleId(i)
                if result and doesVehicleExist(vehHandle) and isCarOnScreen(vehHandle) then
                    local x, y, z = getCarCoordinates(vehHandle)
                    local dist = getDistanceBetweenCoords3d(myX, myY, myZ, x, y, z)

                    if dist <= distanceLimit[0] and isCarOnScreen(vehHandle) and isLineOfSightClear(myX, myY, myZ, x, y, z, true, false, false, true, false) then -- добавил нормальную систему проверки машин на экране
                        local sx, sy = convert3DCoordsToScreen(x, y, z)

                        if sx and sy then
                            local health = getCarHealth(vehHandle)
                            local model = getCarModel(vehHandle)

                            local tId = showid[0] and ('ID: ' .. i .. '\n') or ''
                            local tHealth = showhealth[0] and ('Health: ' .. math.floor(health) .. '\n') or ''
                            local tModel = showmodel[0] and ('Model ID: ' .. model .. '\n') or ''
                            local tDist = showdistance[0] and ('Distance: ' .. math.floor(dist)) or ''
                            local text = tId .. tHealth .. tModel .. tDist

                            if text ~= '' then
                                imgui.PushFont(font)
                                dl:AddText(imgui.ImVec2(sx - 20, sy), 0xFFFFFFFF, text)
                                imgui.PopFont()
                            end
                        end
                    end
                end
            end
        end
    end
)

local menu = imgui.OnFrame( -- переделал всё на mimgui
    function() return windowRender[0] end,
    function(player)
        local sw, sh = getScreenResolution()
        imgui.SetNextWindowSize(imgui.ImVec2(350, 300), imgui.Cond.FirstUseEver)
        imgui.SetNextWindowPos(imgui.ImVec2(sw / 5.5, sh / 2.7), imgui.Cond.FirstUseEver)
        imgui.Begin(u8('Настройки кастомного /dl'), windowRender)

        local text = u8('Включение отображения нужной информации:')
        local textWidth = imgui.CalcTextSize(text)
        local windowWidth = imgui.GetWindowSize().x
        imgui.SetCursorPosX((windowWidth - textWidth.x) * 0.5)
        imgui.Text(text)

        imgui.Checkbox(u8('ХП авто'), showhealth)
        imgui.Checkbox(u8('ID авто'), showid)
        imgui.Checkbox(u8('Расстояние к авто'), showdistance)
        imgui.Checkbox(u8('Модель авто'), showmodel)

        local buttonWidth = 150
        local spacing = 10
        local totalWidth = (buttonWidth * 2) + spacing
        imgui.SetCursorPosX((windowWidth - totalWidth) * 0.5)

        if imgui.Button(u8('Включить всё'), imgui.ImVec2(buttonWidth, 25)) then
            for k, v in ipairs(arrayVars) do v[0] = true end
        end
        imgui.SameLine()
        if imgui.Button(u8('Выключить всё'), imgui.ImVec2(buttonWidth, 25)) then
            for k, v in ipairs(arrayVars) do v[0] = false end
        end

        local distanceText = u8("Макс. дистанция отображения: %.0f м"):format(distanceLimit[0])
        local textDistWidth = imgui.CalcTextSize(distanceText)
        imgui.SetCursorPosX((windowWidth - textDistWidth.x) * 0.5)
        imgui.Text(distanceText)

        local sliderWidth = 300
        imgui.SetCursorPosX((windowWidth - sliderWidth) * 0.5)
        imgui.PushItemWidth(sliderWidth)
        imgui.SliderFloat("##distanceSlider", distanceLimit, 1.0, 500.0, "%.0f", 1.0)
        imgui.PopItemWidth()

        imgui.End()
    end
)

function apply_custom_style()
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    
    style.WindowPadding = imgui.ImVec2(15, 15)
    style.WindowRounding = 5.0
    style.FramePadding = imgui.ImVec2(5, 5)
    style.FrameRounding = 4.0
    style.ItemSpacing = imgui.ImVec2(12, 8)
    style.ItemInnerSpacing = imgui.ImVec2(8, 6)
    style.IndentSpacing = 25.0
    style.ScrollbarSize = 15.0
    style.ScrollbarRounding = 9.0
    style.GrabMinSize = 5.0
    style.GrabRounding = 3.0

    local colors = style.Colors
    colors[imgui.Col.Text] = imgui.ImVec4(0.80, 0.80, 0.83, 1.00)
    colors[imgui.Col.TextDisabled] = imgui.ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[imgui.Col.WindowBg] = imgui.ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[imgui.Col.ChildBg] = imgui.ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[imgui.Col.PopupBg] = imgui.ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[imgui.Col.Border] = imgui.ImVec4(0.80, 0.80, 0.83, 0.88)
    colors[imgui.Col.BorderShadow] = imgui.ImVec4(0.92, 0.91, 0.88, 0.00)
    colors[imgui.Col.FrameBg] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.FrameBgHovered] = imgui.ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[imgui.Col.FrameBgActive] = imgui.ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[imgui.Col.TitleBg] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.TitleBgCollapsed] = imgui.ImVec4(1.00, 0.98, 0.95, 0.75)
    colors[imgui.Col.TitleBgActive] = imgui.ImVec4(0.07, 0.07, 0.09, 1.00)
    colors[imgui.Col.MenuBarBg] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.ScrollbarBg] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.ScrollbarGrab] = imgui.ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[imgui.Col.ScrollbarGrabHovered] = imgui.ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[imgui.Col.ScrollbarGrabActive] = imgui.ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[imgui.Col.CheckMark] = imgui.ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[imgui.Col.SliderGrab] = imgui.ImVec4(0.80, 0.80, 0.83, 0.31)
    colors[imgui.Col.SliderGrabActive] = imgui.ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[imgui.Col.Button] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.ButtonHovered] = imgui.ImVec4(0.24, 0.23, 0.29, 1.00)
    colors[imgui.Col.ButtonActive] = imgui.ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[imgui.Col.Header] = imgui.ImVec4(0.10, 0.09, 0.12, 1.00)
    colors[imgui.Col.HeaderHovered] = imgui.ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[imgui.Col.HeaderActive] = imgui.ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[imgui.Col.ResizeGrip] = imgui.ImVec4(0.00, 0.00, 0.00, 0.00)
    colors[imgui.Col.ResizeGripHovered] = imgui.ImVec4(0.56, 0.56, 0.58, 1.00)
    colors[imgui.Col.ResizeGripActive] = imgui.ImVec4(0.06, 0.05, 0.07, 1.00)
    colors[imgui.Col.PlotLines] = imgui.ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[imgui.Col.PlotLinesHovered] = imgui.ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[imgui.Col.PlotHistogram] = imgui.ImVec4(0.40, 0.39, 0.38, 0.63)
    colors[imgui.Col.PlotHistogramHovered] = imgui.ImVec4(0.25, 1.00, 0.00, 1.00)
    colors[imgui.Col.TextSelectedBg] = imgui.ImVec4(0.25, 1.00, 0.00, 0.43)
end

imgui.OnInitialize(function()
    imgui.GetIO().IniFilename = nil
    local glyph_ranges = imgui.GetIO().Fonts:GetGlyphRangesCyrillic()
    font = imgui.GetIO().Fonts:AddFontFromFileTTF(getFolderPath(0x14)..'\\arial.ttf', 18, _, glyph_ranges) -- тут свой шрифт
    apply_custom_style()
end)