How to properly center a mesh on the camera?

I’m trying to create a simple and reusable module to make a infinite scrolling parallax background as this playground. But seems to me that it isn’t very reusable since I’m supposing that the original mesh is positioned at the center and this can’t be true at some point. (I will add a property to center the camera on the future, so the camera itself isn’t a problem, well, I believe so :sweat_smile:)

With this I try to find a way to fit the mesh on the center of the camera, but I only found a way to change the camera target, instead of get its center. So, how do I get the center of the camera? Something just like unity does :thinking:

What I want to reach at the end is, for anyone that use the package, could just use its meshes on the InfiniteBackground class and it simply works, without the need to deal with camera or meshes positioning on X, it will just appear on the screen without much effort :grin:

BTW, changing the subject, I’m wondering if is there a way to get the size of a mesh relative to the camera? I mean, lets suppose that the camera has a total width of 100. A mesh of certain width displays on camera as 50 (the mesh fills half of the camera), but if we change its Z to a further value, now the the relative width is 25. I don’t know if I make myself clear, sorry for that, but what I what to do is know how much side-by-side clones of a mesh I need to create to fill the horizontal axis of the camera and I can’t find anything about this. :sweat:

Maybe you could have a look in how our framing behavior works as it does just the centering Babylon.js/framingBehavior.ts at master · BabylonJS/Babylon.js · GitHub

Also @RaananW is a king of camera :slight_smile:

1 Like

What do you mean by “the center of the camera”? You have the camera’s position (camera.position) and you have its target (i.e. direction). You can calculate a center point on your mesh using those two.

I didn’t get that was that easy! :sweat_smile:
Thank you @RaananW :grinning_face_with_smiling_eyes:

1 Like