Jump to content

Babylon BoundingSphere merge


DeathSoul
 Share

Recommended Posts

Hello everybody ! :D 

I've got a question about boundingspheres.

I want to set my camera to an appropriate distance from my meshes to see all of them in my frucstrum.

I tried the code below, it's working if boudingspheres of my meshes are not too far or if there is just one mesh.

I want it to work for every meshes importation, so I have to merge my boundingspheres but I don't know how to do it.

 

for (let i = 0; i < newScene.meshes.length; i++) {

test = newScene.meshes.getBoundingInfo().boundingSphere;
camsum += test.radius / Math.sin(this.camera.fov / 2);

}

let camaverage = camsum / newScene.meshes.length;

this.camera.radius = camaverage;

 

Some advices ? :)

 

Thanks for your time !

DeathSoul

 

Link to comment
Share on other sites

Hello Deltakosh,

 

Thank you for answering,

 

I tried to reproduce my work on the playground, and tried it with sphere, it doesn't seems to work like that.

For my part, I want, for any imported meshes, every imported meshes to be in my camera radius.

Here is a link to the playground : http://www.babylonjs-playground.com/#1QQECU#10

Link to comment
Share on other sites

Hello Deltakosh,

 

Indeed centerWorld is working fine (I don't know why but my code was working the same way, I got the center of my meshes).

I still don't understand how to set the camera at the right distance of all meshes to set all of them in my frustrum.

I found that : camsum += mesh.getBoundingInfo().boundingSphere.radius / Math.sin(camera.fov / 2);

It is working to set one mesh in my camera frustrum, but if I want to set more meshes, I tried to add them all into my camsum var,

but I can't just add them, it's ,not working if meshes are far from each other.

meshes far.png

Link to comment
Share on other sites

Yes, your gonna have to somehow figure out the cameras view area vs global positions.

 

if you have your camera as a reference point and have a point to the left of the max view of the cam and one to the right so that way you effectively make a triangle  in 3D space then see if the spheres are within that triangle.

am I making seince?   

 

And be when I say triangle I am misleading that was a simple explanation you would do this by taking your cameras position and come up with a equation that gives the view area from the cameras "focus angle" and the distance in z from the forward vector.  You might have to do some sort of calculation to take aspect ratio into account as well. But yea once you can draw your imaginary "view box" for any location in front of the camera you can then figure out what's located where in your view.  There may be a way easier way to do this like seeing if the object is being drawn with the camera somehow and then if it is see if the whole object is being viewed or clipped, but that's a whole other process and I think you would want to go about it the other way given your description of why you need to do this.

 

 

edit*** after looking at your diagram I think I figured it out. 

 

So yea you have a set camera position and angle I'm assuming? 

If so then you need to iterate through your meshes that you want to take into account and figure out the max and min positions for the spheres hit boxes.  So the sphere on the lefts left most point and the sphere on the right, right most point and this does not need to take in to account depth, you just need the world point to screen view point effectively.  So now that you have the two points you can figure out the distance between them and then set that to your cameras focal angle with some sort of conversion to get it the way you want.

i may be crazy but I think that's what you want.

Link to comment
Share on other sites

Hello Pryme8,

 

Yeah you understood what I want, but I do not now how to find my sphere on the lefts left most point and my sphere on the right right most point.

I don't think it's that hard by the way. But obvisouly my meshes are can be imported anywhere, on 10 meshes, my 3rd can be on the left and my 5th on the right.

Anyway, thanks for your idea, I'll try to figure it out :)

Link to comment
Share on other sites

take the cross product of your cameras up vector and its target then set its y to 0, that will give you a x axis in relation to your camera, use that as your reference point. When I get home I'll sketch up a diagram that shows how to get the left and right point from Pythagoras Therom.

Link to comment
Share on other sites

Thank you for your video example,

 

That's pretty clear to find my angle thanks to it, but I was wondering, I am doing a viewer on which you can view any object.

The problem is I don't know if my 2 dimension axis at the base of my object will be x-,y ; x,z or y,z.

If I can be sure of what axis are used, I can set my camera the right way.

Anyway, thanks for everything :)

Edited by DeathSoul
I just did Pythagoras Therom, working fine for x,y ;)
Link to comment
Share on other sites

Well technically you don't need to worry about anything but be spheres position, but if you want to make sure the position you are tracking is the outer edge so you need to add the value of the spheres radius to the position relative to your established camera x axis.  If it's to the left of the camera subtract to the right add.  This will make sure you offset the center position of the sphere to the true position that you are worried about. Cause your not worried about the center your worried about the outer edge (you could even have a buffer variable to get some whitespace between your spheres and your edge of the viewport). So you could add like the spheres radius times 1.1 or something and then the point you would be tracking would be out from the sphere a little bit and give you some padding.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...