Calculating screen space bounds of an object

I have been trying to calculate the bounding box of an object in screen space. I want to use this calculation in my shaders eventually but right now I’m unable to get it working.

I have been following the source code and calculation as described in the paper:
“2D Polyhedral Bounds of a Clipped, Perspective-Projected 3D Sphere” https://jcgt.org/published/0002/02/05/.

Here is the implementation replicated in a playground:

As you will see the calculations all lead to null coordinates. Ideally these should be bounding box coordinated that I can see in the screen space, I’ve added a post process to check the values.

Other references I’ve followed:

I maybe using the apis incorrectly, so any pointers would be really helpful.

1 Like

if the main problem here is not getting from world space to screen space,

this thread can help out,

here’s a pg as an example

once we get all the 6 faces of the of the bbox cube in,
we can prolly do the same thing as in your PG to shade each face black or whatever color

If you don’t wanna go through the trouble though, you can prolly just make a cube with the same dimensions and draw it on a RenderTargetTexture to get the mask?

1 Like

Thanks a lot for the pointers!

I was able to get a cpu version working: https://playground.babylonjs.com/#H6NHBZ#11

Also trying my hand at a glsl version but looks like something is incorrect:

I’m going to try and debug this but let me know if you spot an obvious error.

1 Like

Hello and welcome :slight_smile:

In this topic I had given this Playground which computes ratio of the mesh seen in the camera (projecting vertices and computing 2D bounding box). If it can help…

++
Tricotou

1 Like

that’s so cool! : )

do you want the worldToScreenPoint function in glsl essentially?

here’s a modified version of the original PG that does that calculation in the shader

here’s your cpu-side PG in glsl entirely : )

1 Like

Thanks that’s exactly what i was looking for!

1 Like