- Home /
Finding farthest point on top of game object (geometry)
In the below scene, I am trying to calculate the farthest point from the camera at the top of each cylinder (designated roughly by the green dots):
Assuming the object's height and radius are known, and the transform position of both the object and the camera, what is the most efficient way to do this?
This is what I came up with:
float xDiff = transform.position.x - Camera.main.transform.position.x;
float zDiff = transform.position.z - Camera.main.transform.position.z;
float angle = Mathf.Atan2( xDiff, zDiff );
float zAdjust = Mathf.Cos( angle ) * radius;
float xAdjust = Mathf.Sin( angle ) * radius;
barPos = Camera.main.WorldToScreenPoint( new Vector3( transform.position.x + xAdjust, transform.position.y + height, transform.position.z + zAdjust ) );
But it seems to have a somewhat heavy hit on my frame rate. I still have some other kinks in my code I need to work out, so I'm not even sure if my geometry is 100% correct, but if it's going to be too expensive to update every frame I figured I should seek a better alternative. Any ideas?
If you are looking for the furthest visible point and the mesh is not too complex, I would suggest such a solution: 1. http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html Get all the vertices of the mesh. 2. use something like Vector3 worldPt = transform.TransformPoint(meshVert);
to get the world coordinate. 3. $$anonymous$$nowing the coordinates of the all the vertices is enough to find out which is the furthest point. If however, you need the furthest visible one - I'd suggest shooting rays at every vertex. Depending on intersection and hit.point you could find out the furthest visible one. It's one way, but it's pretty expensive. Hope it helps in any way!
This looks promising, I'll check it out and get back to you. Do you think the raycasting is more expensive than exa$$anonymous$$ing all of the vertices? If so, I can probably skip that step and just look for a vertex that is as far as possible with a height equal to the object's height.
Raycasting is rather expensive, I would not use it if the model is moving or has lots of vertices.
but if you only need to know the distance in the start of per click or something then its ok doing raycasting. But yes if you run it in an Update() method its gonna suck resources
Answer by DESIMA · Jan 31, 2015 at 04:53 PM
It seems like you just want the longest distance to all you points or transforms.
if so then get a list of your transforms, and for each object do
//get the length of the vector
(obj.position - camera.posion).length
maybe throw and Math.Abs on it depending on the direction
you can then save and compare to next length
float maxDist = Mathf.Max(maxDist, (obj.position - camera.posion).length);
then at the end of the loop you have maxDist.
All this is going to do is find the distance between the camera and the center of each cylinder though (correct me if I'm wrong). I'm trying to find the point at the top of each cylinder that is farthest from the camera.
no you are right, the transforms are the center of the ojbect. If you need the top, one way is to add an empty gameobject and place it on the top by your self, then use that gameobject as the transform in the calculation.
or if you add colliders on the objects you can use collider.bounds, to get the distance offset
Your answer
Follow this Question
Related Questions
Most efficient way to create cubes with multiple textures 2 Answers
Too much triangles and more doubts performance-related 1 Answer
geometry how to find ray local hit point? 1 Answer
How to get random point on the perimeter of a rectangle? 1 Answer
Call different geometry shader functions, for different primitive type: How? 1 Answer