Pixel dimensions of 3D model


Is it possible to get the pixel size of a 3D model after it has been loaded up and its orientation has been changed? For example, I load in a 3D cube, I then scale it to 0.25 and stretch it along X axis by 1.2x, I want to get its dimensions, in screen pixels, as it would be if the camera were directly in front of it looking forward. Reason being, I’m trying to use this model as a “cutter” to cut out transparent sections in Texture2Ds that I apply to other 3D objects. If I can get the model’s dimensions in pixels, then I can use the ratio of that measure to the target model’s dimensions to figure out what percentage of the Texture2D data to clip.


For simple primitive and certain orientations it is analytically possible. However you will have to do the math yourself. For arbitrary shape you can use stencil and query occluded pixels.


Thank you, stencils look like they could work, but is there a way to do this using mesh.Draw() and BasicEffect? That’s all I have right now in my 3D environment. The examples I see using stencil are using SpriteBatch.

Spritebatch is just a batched wrapper for geometry rendering, in that case, geometry being quads.

Get the bounding vertices (basically a 3D Bounding Box), multiply corners with WorldViewProjection and you get the Position in ProjectionSpace (0-1 afaik). That basically is the position on the screen, now get the AABB of them and multiply with actual resolution and you have the screen position in pixels.

But for what you wanna do, I would actually do it other way round, having a fixed size for the “cutout” I want to render to and scale the model so it fits in the cameras cone, if you render that to the stencilbuffer before anything else, you basically got the cutout - of course this all depends a bit on how you wanna apply it.

Thanks, I appreciate the tips, I’ll have to look more into the stencils, it sounds like that’s the recommended way to do it. I ended up using bitmasks for now while I look into the other stuff which I’m not too familiar with.