Don’t have the formula with me, but you could try messing around with WorldToViewportPoint to transform a point in the 3D world (the corners of your rectangular object, maybe?) into a 2D point on the screen, which you can then translate into pixels by getting the user’s screen resolution with workspace.CurrentCamera.ViewportSize
.
If your object is a rectangle, you can get all 8 corners by using the formula
cf = part.CFrame
s = part.Size
cfCorner = cf * CFrame.new(
s.X / 2,
s.Y / 2,
s.Z / 2
)
And multiplying each of s.X/2
, s.Y/2
, s.Z/2
by either 1 or -1. That gives you 8 possibilities for 8 corners.
You’d also have to figure out which of them are visible on the screen… and once those are all converted to 2D coordinates, calculate the areas of the triangles between each triplet of points that share a face.
Add those areas together and voilà … not super simple though. I don’t know if there’s already a module out there for that, that’d probably make your day easier.
the problem now is what to do if its a mesh that doesnt have only 4 sides on each face
Not much you can do there. The core idea would still be the same: getting the vertices of every visible triangle and calculating their individual area. Or you could raycast a bunch of points on your screen, see what percentage of them landed on your object, and that’d give you the approximate percentage of pixels the object is being projected on. The first one is probably impossible in the general case with the tools you’re given, and the second one is probably way too slow. You’ll probably need to find an approximation to your problem.