Jump to content

Best scaling factor from the 3D world to resolution.


IIYAMA

Was this usefull?  

25 members have voted

  1. 1. Was this usefull?

    • Maybe, but I do not understand how to use it.
      2
    • No, I do not understand factors.
      1
    • I am not a scripter, but I like to vote...
      2
    • Yes, I was looking for this.
      20


Recommended Posts

  • Moderators

This tutorial is not for beginners.

This code will successfully find the player scaling factor, to convert the 3D world units to 2D pixels on your monitor.

The factor 0.622388530006 is probably a standard, but you never know.

Image1

Image2

Image3

Image4

The function that finds this factor.

local screenX,screenY = guiGetScreenSize() 
local scaleFactorBuffer = nil 
function findScaleFactor () 
    if scaleFactorBuffer then 
        return scaleFactorBuffer 
    else 
        local startX,startY,startZ = getWorldFromScreenPosition ( 0,0, 1 ) 
        local endX,endY,endZ = getWorldFromScreenPosition ( screenX,screenY, 1 ) 
        if startX and endX then 
            scaleFactorBuffer = 1/getDistanceBetweenPoints3D(startX,startY,startZ,endX,endY,endZ) 
            return scaleFactorBuffer 
        end 
    end 
    return false   
end 

The set up for the example below:

-- the set up -- 
local imagePosX,imagePosY,imagePosZ = -21.38184, 5.24524, 3.10965 
local imageResolution = 1000  
---------------- 

The example on how to use:

addEventHandler("onClientRender",root, 
function () 
    local scaleFactor = findScaleFactor () 
    if scaleFactor then 
        dxDrawText("scaleFactor: " .. scaleFactor,300,300) -- The scale factor is circa 0.622388530006 (units). 
        local rectangleCenterX,rectangleCenterY = getScreenFromWorldPosition(imagePosX,imagePosY,imagePosZ) 
        if rectangleCenterX and rectangleCenterY then 
         
            local playerX,playerY,playerZ = getElementPosition(getCamera()) 
             
            local size = (imageResolution*scaleFactor)/getDistanceBetweenPoints3D(playerX,playerY,playerZ,imagePosX,imagePosY,imagePosZ)  
             
            dxDrawText("size: " .. size,300,320) 
             
            dxDrawRectangle(rectangleCenterX+size/2,rectangleCenterY+size/2,size,size,tocolor(0,0,200)) 
             
        end 
    end 
end) 

Edited by Guest
  • Like 1
Link to comment
  • 2 weeks later...
  • Moderators

local playerX,playerY,playerZ = getElementPosition(localPlayer) 
local playerX,playerY,playerZ = getElementPosition(getCamera()) 

I just saw a little mistake in it or well, it might be more a visual.

In the code on top, I took the position of the localPlayer, but the result would be better when you use the position of the camera.

Link to comment
  • 7 months later...
  • Recently Browsing   0 members

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