Advertisement
Guest User

ScreenObject

a guest
Aug 17th, 2020
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --[[
  2.     ScreenObject v1.0 by Waffle
  3.  
  4.     screenObject = ScreenObject.New(object[, dimensionTable]) attaches a given object to the screen and allows for configuration of its size and position.
  5.     screenObject.objectWidth is the width of the object in world space. This is used to determine how to scale it on the screen.
  6.     screenObject.pixelWidth is how many pixels wide the object will be on-screen, given that objectWidth is accurate.
  7.     screenObject.pixelPosX is the horizontal distance from the top left corner of the screen
  8.     screenObject.pixelPosY is the vertical distance from the top left corner of the screen
  9.     screenObject.faceCamera (false by default) causes the object to look directly at the camera position rather than facing the camera plane.
  10.     These properties are all read-write
  11.    
  12.     screenObject:UpdatePosition([newDimensionTable]) re-positions the object on the screen.
  13.         Call this after changing any properties, or include the property changes in newDimensionTable
  14.         e.g. screenObject:UpdatePosition({pixelPosX = ..., pixelPosY = ...})
  15.    
  16.     screenObject:Destroy() destroys the object
  17. ]]
  18.  
  19.  
  20. local SCREEN_OBJECT_GROUP = script:GetCustomProperty("ScreenObjectGroup")
  21.  
  22. local ScreenObject = {}
  23. ScreenObject.__index = ScreenObject
  24.  
  25. function ScreenObject.New(object, dimensionTable)
  26.     local group = World.SpawnAsset(SCREEN_OBJECT_GROUP, {parent = object.parent})
  27.     group:AttachToLocalView()
  28.     object.parent = group
  29.     object:SetRotation(Rotation.New(0, 0, 180))
  30.    
  31.     local screenObject = setmetatable({
  32.         group = group,
  33.         object = object,
  34.         objectWidth = 100,
  35.         pixelWidth = 200,
  36.         pixelPosX = 1920 / 2,
  37.         pixelPosY = 1080 / 2,
  38.         faceCamera = false
  39.     }, ScreenObject)
  40.    
  41.     screenObject:UpdatePosition(dimensionTable)
  42.    
  43.     return screenObject
  44. end
  45.  
  46. function ScreenObject:UpdatePosition(newDimensionTable)
  47.     for k, v in pairs(newDimensionTable) do
  48.         self[k] = v
  49.     end
  50.     local camera = Game.GetLocalPlayer():GetActiveCamera()
  51.     local fov = camera and camera.fieldOfView or 90
  52.    
  53.     local resolution = UI.GetScreenSize()
  54.     local xfactor = math.tan(fov * math.pi / 360)
  55.     local yfactor = xfactor * resolution.y / resolution.x
  56.     local depth = .5 * (self.objectWidth / self.pixelWidth) * resolution.x / xfactor
  57.    
  58.     local xOffset =  xfactor * depth * (self.pixelPosX/resolution.x * 2 - 1)
  59.     local yOffset = -yfactor * depth * (self.pixelPosY/resolution.y * 2 - 1)
  60.    
  61.     local screenOffset = Vector3.New(depth, xOffset, yOffset)
  62.     self.object:SetPosition(screenOffset)
  63.     if self.faceCamera then
  64.         self.object:SetRotation(Rotation.New(-screenOffset, Vector3.UP))
  65.     end
  66. end
  67.  
  68. function ScreenObject:Destroy()
  69.     self.group:Destroy()
  70. end
  71.  
  72. return ScreenObject
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement