SA:MP дублирование полосы renderDrawLine Lua

Yankay

Участник
Автор темы
81
12
У меня вопрос, как убрать дублирование полосы renderDrawLine, у меня снизу рисуется адекватно, но эта полоса дублируется сверху и следует за персонажем, я двигаюсь и она двигается

Lua:
local function drawRouteLine(pts, width, px, py, pz)
    if not pts or #pts < 2 then return end
    local n = #pts

    local nearest = 1
    local nearestDist = math.huge

    for i = 1, n do
        local pt = pts[i]
        local d = dist3D(px, py, pz, pt.x, pt.y, pt.z)
        if d < nearestDist then
            nearestDist = d
            nearest = i
        end
    end

    local startIndex = math.max(1, nearest - 250)
    local endIndex = math.min(n - 1, nearest + 250)

    for i = startIndex, endIndex do
        local a = pts[i]
        local b = pts[i + 1]

        local mx = (a.x + b.x) * 0.5
        local my = (a.y + b.y) * 0.5
        local mz = (a.z + b.z) * 0.5

        local d = dist3D(px, py, pz, mx, my, mz)
        if d <= DRAW_DISTANCE then

            local dx = b.x - a.x
            local dy = b.y - a.y

            -- защита от битых сегментов
            local segLen = math.sqrt(dx * dx + dy * dy)
            if segLen <= 25.0 then

                local ax, ay = safeProject(a.x, a.y, a.z)
                local bx, by = safeProject(b.x, b.y, b.z)

                if ax and ay and bx and by then

                    local ndx, ndy = normalize2D(dx, dy)
                    local perpX = -ndy
                    local perpY = ndx
                    local half = width * 0.5

                    -- левая граница
                    local laX = a.x + perpX * half
                    local laY = a.y + perpY * half
                    local lbX = b.x + perpX * half
                    local lbY = b.y + perpY * half

                    -- правая граница
                    local raX = a.x - perpX * half
                    local raY = a.y - perpY * half
                    local rbX = b.x - perpX * half
                    local rbY = b.y - perpY * half

                    local lax, lay = safeProject(laX, laY, a.z)
                    local lbx, lby = safeProject(lbX, lbY, b.z)
                    local rax, ray = safeProject(raX, raY, a.z)
                    local rbx, rby = safeProject(rbX, rbY, b.z)

                    -- центральная линия
                    local cdx = bx - ax
                    local cdy = by - ay
                    local centerLen = math.sqrt(cdx * cdx + cdy * cdy)

                    if centerLen < 1000 then
                        renderDrawLine(ax, ay, bx, by, 1.5, CLR_CENTER)
                    end

                    -- левая граница
                    if lax and lay and lbx and lby then
                        local ldx = lbx - lax
                        local ldy = lby - lay
                        local leftLen = math.sqrt(ldx * ldx + ldy * ldy)

                        if leftLen < 1000 then
                            renderDrawLine(lax, lay, lbx, lby, 1.0, CLR_BORDER)
                        end
                    end

                    -- правая граница
                    if rax and ray and rbx and rby then
                        local rdx = rbx - rax
                        local rdy = rby - ray
                        local rightLen = math.sqrt(rdx * rdx + rdy * rdy)

                        if rightLen < 1000 then
                            renderDrawLine(rax, ray, rbx, rby, 1.0, CLR_BORDER)
                        end
                    end
                end
            end
        end
    end
end


1780751339548.png
 

pewpewpewpew

Известный
Модератор
686
205
local startIndex = math.max(1, nearest - 250) local endIndex = math.min(n - 1, nearest + 250)

ты рисуешь 250 точек назад и 250 вперёд от ближайшей точки. то, что сзади, может вылезать сверху.
убирай nearest - 250

Lua:
local startIndex = nearest
local endIndex = math.min(n - 1, nearest + 250)
 
  • Bug
Реакции: Corenale

pewpewpewpew

Известный
Модератор
686
205
Оно переднюю полосу рисует заде
Lua:
local function isPointInFrontOfCamera(x, y, z)
    if type(getActiveCameraCoordinates) ~= 'function' or type(getActiveCameraPointAt) ~= 'function' then
        return true
    end

    local cx, cy, cz = getActiveCameraCoordinates()
    local tx, ty, tz = getActiveCameraPointAt()

    if not cx or not cy or not cz or not tx or not ty or not tz then
        return true
    end

    local fx = tx - cx
    local fy = ty - cy
    local fz = tz - cz

    local px = x - cx
    local py = y - cy
    local pz = z - cz

    return fx * px + fy * py + fz * pz > 0.0
end

local function safeProject(x, y, z)
    if not isPointInFrontOfCamera(x, y, z) then
        return nil, nil
    end

    if type(isPointOnScreen) == 'function' and not isPointOnScreen(x, y, z, 1.0) then
        return nil, nil
    end

    local r1, r2, r3 = convert3DCoordsToScreen(x, y, z)
    local sx, sy

    if type(r1) == 'boolean' then
        if not r1 then
            return nil, nil
        end

        sx = r2
        sy = r3
    else
        sx = r1
        sy = r2
    end

    if not sx or not sy or sx ~= sx or sy ~= sy then
        return nil, nil
    end

    local resX, resY = getScreenResolution()

    if sx < -150 or sx > resX + 150 or sy < -150 or sy > resY + 150 then
        return nil, nil
    end

    return sx, sy
end

local function drawRouteLine(pts, width, px, py, pz)
    if not pts or #pts < 2 then return end
    local n = #pts

    local nearest = 1
    local nearestDist = math.huge

    for i = 1, n do
        local pt = pts[i]
        local d = dist3D(px, py, pz, pt.x, pt.y, pt.z)
        if d < nearestDist then
            nearestDist = d
            nearest = i
        end
    end

    local startIndex = math.max(1, nearest - 250)
    local endIndex = math.min(n - 1, nearest)

    for i = endIndex, startIndex, -1 do
        local a = pts[i + 1]
        local b = pts[i]

        local mx = (a.x + b.x) * 0.5
        local my = (a.y + b.y) * 0.5
        local mz = (a.z + b.z) * 0.5

        local d = dist3D(px, py, pz, mx, my, mz)
        if d <= DRAW_DISTANCE then

            local dx = b.x - a.x
            local dy = b.y - a.y

            -- защита от битых сегментов
            local segLen = math.sqrt(dx * dx + dy * dy)
            if segLen > 0.01 and segLen <= 25.0 then

                local ax, ay = safeProject(a.x, a.y, a.z)
                local bx, by = safeProject(b.x, b.y, b.z)

                if ax and ay and bx and by then

                    local ndx, ndy = normalize2D(dx, dy)
                    local perpX = -ndy
                    local perpY = ndx
                    local half = width * 0.5

                    -- левая граница
                    local laX = a.x + perpX * half
                    local laY = a.y + perpY * half
                    local lbX = b.x + perpX * half
                    local lbY = b.y + perpY * half

                    -- правая граница
                    local raX = a.x - perpX * half
                    local raY = a.y - perpY * half
                    local rbX = b.x - perpX * half
                    local rbY = b.y - perpY * half

                    local lax, lay = safeProject(laX, laY, a.z)
                    local lbx, lby = safeProject(lbX, lbY, b.z)
                    local rax, ray = safeProject(raX, raY, a.z)
                    local rbx, rby = safeProject(rbX, rbY, b.z)

                    -- центральная линия
                    local cdx = bx - ax
                    local cdy = by - ay
                    local centerLen = math.sqrt(cdx * cdx + cdy * cdy)

                    if centerLen < 1000 then
                        renderDrawLine(ax, ay, bx, by, 1.5, CLR_CENTER)
                    end

                    -- левая граница
                    if lax and lay and lbx and lby then
                        local ldx = lbx - lax
                        local ldy = lby - lay
                        local leftLen = math.sqrt(ldx * ldx + ldy * ldy)

                        if leftLen < 1000 then
                            renderDrawLine(lax, lay, lbx, lby, 1.0, CLR_BORDER)
                        end
                    end

                    -- правая граница
                    if rax and ray and rbx and rby then
                        local rdx = rbx - rax
                        local rdy = rby - ray
                        local rightLen = math.sqrt(rdx * rdx + rdy * rdy)

                        if rightLen < 1000 then
                            renderDrawLine(rax, ray, rbx, rby, 1.0, CLR_BORDER)
                        end
                    end
                end
            end
        end
    end
end
 
  • Клоун
  • Bug
  • Эм
Реакции: VanoKLR, chapo и Corenale

Yankay

Участник
Автор темы
81
12
Lua:
local function isPointInFrontOfCamera(x, y, z)
    if type(getActiveCameraCoordinates) ~= 'function' or type(getActiveCameraPointAt) ~= 'function' then
        return true
    end

    local cx, cy, cz = getActiveCameraCoordinates()
    local tx, ty, tz = getActiveCameraPointAt()

    if not cx or not cy or not cz or not tx or not ty or not tz then
        return true
    end

    local fx = tx - cx
    local fy = ty - cy
    local fz = tz - cz

    local px = x - cx
    local py = y - cy
    local pz = z - cz

    return fx * px + fy * py + fz * pz > 0.0
end

local function safeProject(x, y, z)
    if not isPointInFrontOfCamera(x, y, z) then
        return nil, nil
    end

    if type(isPointOnScreen) == 'function' and not isPointOnScreen(x, y, z, 1.0) then
        return nil, nil
    end

    local r1, r2, r3 = convert3DCoordsToScreen(x, y, z)
    local sx, sy

    if type(r1) == 'boolean' then
        if not r1 then
            return nil, nil
        end

        sx = r2
        sy = r3
    else
        sx = r1
        sy = r2
    end

    if not sx or not sy or sx ~= sx or sy ~= sy then
        return nil, nil
    end

    local resX, resY = getScreenResolution()

    if sx < -150 or sx > resX + 150 or sy < -150 or sy > resY + 150 then
        return nil, nil
    end

    return sx, sy
end

local function drawRouteLine(pts, width, px, py, pz)
    if not pts or #pts < 2 then return end
    local n = #pts

    local nearest = 1
    local nearestDist = math.huge

    for i = 1, n do
        local pt = pts[i]
        local d = dist3D(px, py, pz, pt.x, pt.y, pt.z)
        if d < nearestDist then
            nearestDist = d
            nearest = i
        end
    end

    local startIndex = math.max(1, nearest - 250)
    local endIndex = math.min(n - 1, nearest)

    for i = endIndex, startIndex, -1 do
        local a = pts[i + 1]
        local b = pts[i]

        local mx = (a.x + b.x) * 0.5
        local my = (a.y + b.y) * 0.5
        local mz = (a.z + b.z) * 0.5

        local d = dist3D(px, py, pz, mx, my, mz)
        if d <= DRAW_DISTANCE then

            local dx = b.x - a.x
            local dy = b.y - a.y

            -- защита от битых сегментов
            local segLen = math.sqrt(dx * dx + dy * dy)
            if segLen > 0.01 and segLen <= 25.0 then

                local ax, ay = safeProject(a.x, a.y, a.z)
                local bx, by = safeProject(b.x, b.y, b.z)

                if ax and ay and bx and by then

                    local ndx, ndy = normalize2D(dx, dy)
                    local perpX = -ndy
                    local perpY = ndx
                    local half = width * 0.5

                    -- левая граница
                    local laX = a.x + perpX * half
                    local laY = a.y + perpY * half
                    local lbX = b.x + perpX * half
                    local lbY = b.y + perpY * half

                    -- правая граница
                    local raX = a.x - perpX * half
                    local raY = a.y - perpY * half
                    local rbX = b.x - perpX * half
                    local rbY = b.y - perpY * half

                    local lax, lay = safeProject(laX, laY, a.z)
                    local lbx, lby = safeProject(lbX, lbY, b.z)
                    local rax, ray = safeProject(raX, raY, a.z)
                    local rbx, rby = safeProject(rbX, rbY, b.z)

                    -- центральная линия
                    local cdx = bx - ax
                    local cdy = by - ay
                    local centerLen = math.sqrt(cdx * cdx + cdy * cdy)

                    if centerLen < 1000 then
                        renderDrawLine(ax, ay, bx, by, 1.5, CLR_CENTER)
                    end

                    -- левая граница
                    if lax and lay and lbx and lby then
                        local ldx = lbx - lax
                        local ldy = lby - lay
                        local leftLen = math.sqrt(ldx * ldx + ldy * ldy)

                        if leftLen < 1000 then
                            renderDrawLine(lax, lay, lbx, lby, 1.0, CLR_BORDER)
                        end
                    end

                    -- правая граница
                    if rax and ray and rbx and rby then
                        local rdx = rbx - rax
                        local rdy = rby - ray
                        local rightLen = math.sqrt(rdx * rdx + rdy * rdy)

                        if rightLen < 1000 then
                            renderDrawLine(rax, ray, rbx, rby, 1.0, CLR_BORDER)
                        end
                    end
                end
            end
        end
    end
end
Теперь рисуется только задняя часть, но уже без дублирования

Теперь рисуется только задняя часть, но уже без дублирования
исправил
 

pewpewpewpew

Известный
Модератор
686
205
Теперь рисуется только задняя часть, но уже без дублирования
переверни направление

Lua:
local startIndex = math.max(1, nearest - 250)
local endIndex = math.min(n - 1, nearest)

for i = endIndex, startIndex, -1 do

замени на

Lua:
local startIndex = nearest
local endIndex = math.min(n - 1, nearest + 250)

for i = startIndex, endIndex do
 
  • Клоун
  • Bug
Реакции: chapo и Corenale

chromiusj

Известный
Модератор
6,018
4,341
 

elyrin

Известный
261
161
Lua:
local function isPointInFrontOfCamera(x, y, z)
    if type(getActiveCameraCoordinates) ~= 'function' or type(getActiveCameraPointAt) ~= 'function' then
        return true
    end

    local cx, cy, cz = getActiveCameraCoordinates()
    local tx, ty, tz = getActiveCameraPointAt()

    if not cx or not cy or not cz or not tx or not ty or not tz then
        return true
    end

    local fx = tx - cx
    local fy = ty - cy
    local fz = tz - cz

    local px = x - cx
    local py = y - cy
    local pz = z - cz

    return fx * px + fy * py + fz * pz > 0.0
end

local function safeProject(x, y, z)
    if not isPointInFrontOfCamera(x, y, z) then
        return nil, nil
    end

    if type(isPointOnScreen) == 'function' and not isPointOnScreen(x, y, z, 1.0) then
        return nil, nil
    end

    local r1, r2, r3 = convert3DCoordsToScreen(x, y, z)
    local sx, sy

    if type(r1) == 'boolean' then
        if not r1 then
            return nil, nil
        end

        sx = r2
        sy = r3
    else
        sx = r1
        sy = r2
    end

    if not sx or not sy or sx ~= sx or sy ~= sy then
        return nil, nil
    end

    local resX, resY = getScreenResolution()

    if sx < -150 or sx > resX + 150 or sy < -150 or sy > resY + 150 then
        return nil, nil
    end

    return sx, sy
end

local function drawRouteLine(pts, width, px, py, pz)
    if not pts or #pts < 2 then return end
    local n = #pts

    local nearest = 1
    local nearestDist = math.huge

    for i = 1, n do
        local pt = pts[i]
        local d = dist3D(px, py, pz, pt.x, pt.y, pt.z)
        if d < nearestDist then
            nearestDist = d
            nearest = i
        end
    end

    local startIndex = math.max(1, nearest - 250)
    local endIndex = math.min(n - 1, nearest)

    for i = endIndex, startIndex, -1 do
        local a = pts[i + 1]
        local b = pts[i]

        local mx = (a.x + b.x) * 0.5
        local my = (a.y + b.y) * 0.5
        local mz = (a.z + b.z) * 0.5

        local d = dist3D(px, py, pz, mx, my, mz)
        if d <= DRAW_DISTANCE then

            local dx = b.x - a.x
            local dy = b.y - a.y

            -- защита от битых сегментов
            local segLen = math.sqrt(dx * dx + dy * dy)
            if segLen > 0.01 and segLen <= 25.0 then

                local ax, ay = safeProject(a.x, a.y, a.z)
                local bx, by = safeProject(b.x, b.y, b.z)

                if ax and ay and bx and by then

                    local ndx, ndy = normalize2D(dx, dy)
                    local perpX = -ndy
                    local perpY = ndx
                    local half = width * 0.5

                    -- левая граница
                    local laX = a.x + perpX * half
                    local laY = a.y + perpY * half
                    local lbX = b.x + perpX * half
                    local lbY = b.y + perpY * half

                    -- правая граница
                    local raX = a.x - perpX * half
                    local raY = a.y - perpY * half
                    local rbX = b.x - perpX * half
                    local rbY = b.y - perpY * half

                    local lax, lay = safeProject(laX, laY, a.z)
                    local lbx, lby = safeProject(lbX, lbY, b.z)
                    local rax, ray = safeProject(raX, raY, a.z)
                    local rbx, rby = safeProject(rbX, rbY, b.z)

                    -- центральная линия
                    local cdx = bx - ax
                    local cdy = by - ay
                    local centerLen = math.sqrt(cdx * cdx + cdy * cdy)

                    if centerLen < 1000 then
                        renderDrawLine(ax, ay, bx, by, 1.5, CLR_CENTER)
                    end

                    -- левая граница
                    if lax and lay and lbx and lby then
                        local ldx = lbx - lax
                        local ldy = lby - lay
                        local leftLen = math.sqrt(ldx * ldx + ldy * ldy)

                        if leftLen < 1000 then
                            renderDrawLine(lax, lay, lbx, lby, 1.0, CLR_BORDER)
                        end
                    end

                    -- правая граница
                    if rax and ray and rbx and rby then
                        local rdx = rbx - rax
                        local rdy = rby - ray
                        local rightLen = math.sqrt(rdx * rdx + rdy * rdy)

                        if rightLen < 1000 then
                            renderDrawLine(rax, ray, rbx, rby, 1.0, CLR_BORDER)
                        end
                    end
                end
            end
        end
    end
end
а обязательно каждый раз проверять функцию на то что она является функцией?
нынче модеры странные пошли
 
  • Нравится
  • Ха-ха
Реакции: Vespan, chapo и VanoKLR