Jump to content
Sign in to follow this  

[FUNC] create3DText

Recommended Posts

Posted (edited)

This function lets you create "3D Texts" anywhere in the world with no effort.



create3DText(string, x, y, z, font, fontsize, r, g, b, a, distance, testlos)

Required Arguments:

  • string: the text to create
  • x: A floating point number representing the X coordinate on the map
  • y: A floating point number representing the Y coordinate on the map
  • z: A floating point number representing the Z coordinate on the map

Optional Arguments:

  • font: Either a custom DX font element or the name of a built-in DX font (default (Tahoma), default-bold, clear (Verdana), arial, sans, pricedown, bankgothic, diploma, beckett)
  • fontsize: The size of the text
  • r: The amount of red in the color (0-255)
  • g: The amount of green in the color (0-255)
  • b: The amount of blue in the color (0-255)
  • a: The amount of alpha in the color (0-255)
  • distance: Draw distance value in San Andreas units
  • testlos: Test for line of sight (true/false)


local texts = {}
function create3DText(string, x, y, z, font, fontsize, r, g, b, a, distance, testlos)
    local text      =   {}
    text.string     =   string
    text.x          =   x
    text.y          =   y
    text.z          =   z
    text.font       =   font or "arial"
    text.fontsize   =   fontsize or 1.0
    text.r          =   r or 255
    text.g          =   g or 255
    text.b          =   b or 255
    text.a          =   a or 255
    text.distance   =   distance or 150
    text.testlos    =   (type(testlos) == "nil" and false or testlos)
    table.insert(texts, text)
addEventHandler("onClientRender", root, function()
    for i, value in ipairs(texts) do
        local self = texts[i]
        local x, y, z = getElementPosition(getLocalPlayer())
        local distance = getDistanceBetweenPoints3D(self.x, self.y, self.z, x, y, z)
        if distance <= self.distance then
            local cx, cy, cz, clx, cly, clz = getCameraMatrix()
            if not self.testlos or (self.testlos and isLineOfSightClear(cx, cy, cz, self.x, self.y, self.z, true, false, true, true, false, false, true)) then
                local scale = self.fontsize - ((distance / self.distance) * self.fontsize)
                local sx, sy = getScreenFromWorldPosition(self.x, self.y, self.z, dxGetTextWidth(self.string, scale, self.font, true), false)
                if sx and sy then
                    dxDrawText(self.string, sx, sy, _, _, tocolor(self.r, self.g, self.b, self.a), scale, self.font, "center", "center", false, false, false, true)

^ Paste this code somewhere in your script file ^

Example Usage

addEventHandler("onClientResourceStart", root, function()
    create3DText("*some text* no testlos", 705.83, -159.25, 32.01, "beckett", 3.0, 255, 255, 255, 255, 200, false)
    create3DText("*some #FF0000c#ffa500o#ffff00l#00ff00o#0000ffr#551a8be#ff69b4d#FFFFFF text* with testlos", 705.83, -159.25, 25.01, "arial", 2.0, 255, 255, 255, 255, 200, true)


Edited by Infra
  • Like 3

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.