DeathSoul Posted March 4, 2016 Share Posted March 4, 2016 Hello everybody ! 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 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 4, 2016 Share Posted March 4, 2016 Hello can you please provide a live example on the playground? Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 7, 2016 Author Share Posted March 7, 2016 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 Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 7, 2016 Share Posted March 7, 2016 Hello ! first of all you should use mesh.getBoundingInfo().boundingSphere.centerWorld to get the position of the sphere's center in world coordinates (same for radiusWorld). then, this seems really suspicious (You are not adding centers here): vecCenters = tempVec.add(j); Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 8, 2016 Author Share Posted March 8, 2016 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. Quote Link to comment Share on other sites More sharing options...
GameMonetize Posted March 8, 2016 Share Posted March 8, 2016 You may find this useful: https://github.com/BabylonJS/Babylon.js/blob/master/src/babylon.scene.ts#L2574 Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 8, 2016 Author Share Posted March 8, 2016 Ok thanks ! I don't have my code right now, I'll take a look tomorrow, but I think I understood I'll tell you Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 9, 2016 Share Posted March 9, 2016 I do t think you can merge the spheres but you could run through an array of all the meshes on your scene and check all the sphere hit boxs that way. Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 9, 2016 Author Share Posted March 9, 2016 Hello Pryme8, Do you mean that I can check with an array if all my bounding spheres can be seen ? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 9, 2016 Share Posted March 9, 2016 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. Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 10, 2016 Author Share Posted March 10, 2016 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 Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 11, 2016 Author Share Posted March 11, 2016 Okay, I found a solution ! Finding the mesh on the right and the mesh on the left is a great idea ! The only problem is that I don't always know on which axis I'm working, but that's good enough for me don't worry So thank you Deltakosh and Pryme8 for your help GameMonetize 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 13, 2016 Share Posted March 13, 2016 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. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 13, 2016 Share Posted March 13, 2016 https://vid.me/q2to sorry about the weird camera angle... And I did not think this through before I made it I prolly should have cause I might have given you some miss information. But for the most part this is on point and should get you in the right direction DeathSoul 1 Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 14, 2016 Author Share Posted March 14, 2016 (edited) 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 March 14, 2016 by DeathSoul I just did Pythagoras Therom, working fine for x,y ;) Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 14, 2016 Share Posted March 14, 2016 Find the spheres up vector so var so sp= sphere.position; sp.y++; sp.y.normalize(). but honestly that's does not matter just the spheres xz position and its radius. Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 14, 2016 Author Share Posted March 14, 2016 I'm sorry, but I didn't really understand there You want me to use boundingSphere again to do it ? I stopped using it with the Therom. I missed something ? Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted March 14, 2016 Share Posted March 14, 2016 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. Quote Link to comment Share on other sites More sharing options...
DeathSoul Posted March 15, 2016 Author Share Posted March 15, 2016 Yeah I understand, I crossed it by 1.2 to be sure to see all my meshes, it's working fine right now Thanks for your help ! (Is there a way to resolve a topic ? I may be blind ) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.