Screen size of a mesh

is this possible to get screen size of a mesh in blueprint ? i want to check if its bigger than screen size then lock my camera zoom.

thanks

any chance to use this value ? :

&stc=1&d=1441813020

I imagine you could do this, but likely with the use of c++.

You could get the extent bounds of an object, convert those to screen space, then find the length of the resulting 2D vector.

The simplest way is to get the bounding sphere, divide by the distance to the object, and multiply by a factor that depends on camera FOV.

approx_screen_percentage = bounding_sphere_radius / (distance_to_object * sin(fov_of_camera))

2 Likes

is this possible to do that in blueprint ?

1 Like

thanks a lot jwatte but i want to make that in blueprint if this possible

That is a math equation, not code. Each of those names/variables in the equation is pretty much a node or property you can get.

i got that , for those who have my question , here is the solution , first i make a distance macro to get distance between camera component to an actor location :

&stc=1&d=1441873857

then i use jwatte formula :

&stc=1&d=1441873865

thanks a lot jwatte

2 Likes

For the distance, all you need to do is subtract location 1 from location 2, and connect the result to a vector length node.

Anyone coming from the future: screen size is calculated by ComputeBoundsScreenSize() in SceneManagement.cpp. It’s not defined in the .h so you’ll have to copy-paste it into your game code:


float ComputeBoundsScreenSize(const FVector4& BoundsOrigin, const float SphereRadius, const FVector4& ViewOrigin, const FMatrix& ProjMatrix)
{
const float Dist = FVector::Dist(BoundsOrigin, ViewOrigin);

// Get projection multiple accounting for view scaling.
const float ScreenMultiple = FMath::Max(0.5f * ProjMatrix.M[0][0], 0.5f * ProjMatrix.M[1][1]);

// Calculate screen-space projected radius
const float ScreenRadius = ScreenMultiple * SphereRadius / FMath::Max(1.0f, Dist);

// For clarity, we end up comparing the diameter
return ScreenRadius * 2.0f;
}

Using @jwatte equation and some tweaking of the result (multiplying and power) I was able to come up with a somewhat material LOD function. I pass the Camera FOV variable to the material via Dynamic Material Instance on my base class and set up the material instance on begin play. My sample below includes if nodes for debugging to show various levels via colors so that I could adjust the multiply and power to get the results I wanted. I plan on multiplying the end result by a fresnel, as well as using LERP nodes to adjust the “exponetIn” and “basereflectfractionin” inputs to fresnel node dynamically based on objects screen size for a highlight effect that will brighten as object gets smaller.

This text will be hidden

Hope this helps someone else…
Screen size of mesh

I figured it out after hours of testing different combinations
This is how to get the screen size of the mesh in screen in the material editor:

And this is the result:

For those who want to try it, it works, I would only warn that there is a margin of error of 0.001 only when changing the screen percentage of the viewport. Maybe someone will be interested in posting a fix to that, but for me it wasn’t worth the time. Hope it works for you

Sorry - was there supposed to be something attached to this?

approx_screen_percentage = bounding_sphere_radius / (distance_to_object * sin(fov_of_camera))

Isnt this dictated by the longest axis? So imagine you have a column, in fact, a column 50k units tall, standing straight Now, the bounding sphere in this case will be defined by Y. Imagine we are looking at this column with our Camera from the above, completely orthogonal, so you only see the flat end of the column/cylinder. As you go further up by Y-axis the VISIBLE screen size of the object will be decreasing drastically because you only see basically a circle. However, its bounding sphere stays the same and even if we scale it down by some distance-based value it still gonna be min size of longest axis.

So now, if you are far up your actual pixel size is like 20x20 pixels on the screen but because your bound sphere is so big the computed value will be incorrect.

Yes, this is a fast approximation! If you want an accurate value, you should use some other method, such as a render condition query or something (requires some C++.)

What does engine do for its viewport stats where it shows fairly-ish accurate value for “Screen pixel size for object”?