- Home /
Sweep for vertex positions?
Hi, I wrote a script that would find the position and direction of the vertices of a mesh. How do I do somewhat like a 360 sweep from the player's position to arrange the positions of vertices from 0 to 360 degrees?
function Update ()
{
var wallVerts = target.GetComponent.<MeshFilter>().mesh.vertices;
var wallVertLength = wallVerts.length;
for (var i:int=0;i<wallVertLength;i++)
{
var vertPos = target.transform.TransformPoint(wallVerts[i]);
var dir = (vertPos - transform.position).normalized;
Seems like there's no other way rather than doing it the long way. I'm trying to sort the vertices in a clockwise manner so that I can use the information to "fill" the vertices with tris properly rather than having them form in completely random patterns each time I return the array.
Answer by Bunny83 · Dec 08, 2012 at 10:31 AM
I'm also a bit lost on this question. Just sorting the vertices in a circle around one axis won't help you to generate a proper mesh. Do you have already your triangle list? Do you just want to make a blend-in-animation?
Anyway, sorting (no matter what) requires you to have a value you can sort on. In your case you need the angle from a certain start direction. Since we are in 3D you have to sort around one axis first. So to do the angle calculation i would "project" the direction to an axis aligned plane (x-z-plane for example so you rotate yround the y-axis)
import System.Collections.Generic;
class SortVertex
{
var index : int;
var angle : float;
}
static function compare(A : SortVertex, B : SortVertex) : int
{
return A.angle.CompareTo(B.angle);
}
function DoIt()
{
var wallVerts = target.GetComponent.<MeshFilter>().mesh.vertices;
var wallVertLength = wallVerts.length;
var wallVertValues = new List.<SortVertex>(wallVertLength);
for (var i = 0; i < wallVertLength; i++)
{
var vertPos = target.transform.TransformPoint(wallVerts[i]);
var dir = (vertPos - transform.position);
dir.y = 0.0f;
dir.Normalize();
var V = Mathf.Atan2(dir.z, dir.x);
var SV = new SortVertex();
SV.index = i;
SV.value = angle;
wallVertValues.Add(SV);
}
wallVertValues.Sort(compare);
for (var vert in wallVertValues)
{
var P = wallVerts[vert.index];
// Process the vertices here in a sorted manner
}
}
Note: Sorting, expecially a lot elements, can become quite slow. You should prepare the data once and save it somehow.
Your answer
Follow this Question
Related Questions
Create a Wireframe Shader with Shader Forge 1 Answer
Mesh breaks apart when using vertex displacement with shadergraph. 3 Answers
Reding from Mesh.colors doesn't display vertex colors of fbx 0 Answers
when I try to move the vertices of a cube a face disapears though the vertices stay intact 1 Answer
Access skinned mesh vertices 2 Answers