- Home /
Finding Radius or Scale from Mesh Vertices Data
I am having trouble visualizing how the vertices relate to quantitative aspects of the geometry of the sphere or object it defines (such as radius, localscale, world coordinates, etc) @ http://unity3d.com/support/documentation/ScriptReference/Mesh.html.
Here is the snippet from the manual:
function Update () { var mesh : Mesh = GetComponent(MeshFilter).mesh; var vertices : Vector3[] = mesh.vertices; var normals : Vector3[] = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
vertices[i] += normals[i] * Mathf.Sin(Time.time);
mesh.vertices = vertices;
}
I understand that this is applying a sinusoidal transformation on the sphere (or other object that it's in). Basically, the object would start at whatever its preset scale is, and then get bigger, and back to the same size again, etc. It would be nice if Unity Inspector could track the instantaneous radius of this procedural manipuulation, but it seems to only track the actual localScale of the sphere, rather than the new scale created by vertices.
How do you track the radius or world coordinate dimensions of a shape given only vertices, like in the example above?
From geometry, I have an idea of how to do this, but it is very expensive in terms of computation - basically collide a plane with the sphere (crossing origin), and the intersection will be the instantaneous circle and fetch the radius from circumference c = 2 \pi r
- is there a more direct way to get these quantities from vertices?
Answer by Jesse Anders · Dec 29, 2010 at 08:29 PM
It would be nice if Unity Inspector could track the instantaneous radius of this procedural manipuulation, but it seems to only track the actual localScale of the sphere, rather than the new scale created by vertices.
You're confusing scale with mesh geometry. The local scale of a game object is completely separate from any geometry associated with it or with the bounds of that geometry, so there's no reason that the former should reflect the latter.
What it sounds like you're looking for is the bounds of the mesh geometry itself. If you need that information, the Mesh class has a function (IIRC) that will recompute the bounds given the current state of the geometry. If you call this function after changing the vertex positions, the bounds should then reflect the updated positions. (There may be some expense involved in recomputing the bounds frequently, but you'll probably just have to factor that in.)
Unity represents the bounds using a box, I think. You can derive a bounding sphere from this box, but if you need a more optimal bounding sphere, you may have to compute it yourself. Computing an approximate bounding sphere is pretty straightforward; computing an optimal or near-optimal bounding sphere isn't necessarily as straightforward. In either case though, it shouldn't require an intersection test (like you described in your post).
It's not so much that they're unrelated, it's that the object's Transform (translation, rotation, scale) are ABOVE the mesh itself...
vertices in unity are but an array of vectors defining their positions in local space. that is before any transform is applied to them. So, by itself, the mesh doesn't really know much about it's own bounds in world-space.
You can calculate the actual size of an object, though, by going like this:
GetComponent($$anonymous$$eshFilter).shared$$anonymous$$esh.bounds.size * transform.localScale
this way you can get the actual size of your mesh.
Cheers
Hm, this was a while ago! Anyway, you said 'it's not so much that they're unrelated', but then went on to articulate exactly what I meant when I said they're unrelated :) So, it seems we're in agreement, more or less ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613093031im_/https://answers.unity.com/themes/thub/images/avi.jpg)