Home > Mobile >  Unity 3D Mesh Find aligning vertices on the same Y axis Height
Unity 3D Mesh Find aligning vertices on the same Y axis Height

Time:05-03

I'm trying to find adjacent vertices from just one selected vertex. One condition these neighboring vertices have to fulfill is that they must be on the same height as the selected original vertex.

I have an int[] vertices array, and a Dictionary<int, float> plateauDictionary.

The vertices array holds all vertices of the entire mesh. The Dictionary holds a collection of vertices that have a height(y-Axis) greater than 0.2f.

Im creating big flat plateaus, so finding adjacent vertices at the same height should be easy as most vertices are adjacent to each other at the same height.

Heres what I wrote so far:

    Matrix4x4 localToWorld = transform.localToWorldMatrix;
    Vector3[] worldVectors = new Vector3[totalVertices];

    foreach (KeyValuePair<int, float> u in plateauDictionary)
        worldVectors[u.Key] = localToWorld.MultiplyPoint3x4(mesh.vertices[u.Key]);

This returns an array of Vector3s consisting of all the world position of each vertex.

Now I want to pick one or a few randomly, and then get the amount of adjacent vertices at the same height, maybe in an assigned radius or maybe in a list of neighbors? How do I solve this? I'm a newb with LINQ but Im thinking that might be the way to go?

Thanks in advance!

My other code(Not really important for this question but for the sake of completeness):

    plateauDictionary = new Dictionary<int, float>();

    for (int j = 0; j < plateaus; j  )
    {
        var rndSize = plateauSizes[Random.Range(0, plateauSizes.Length)];
        var rndHeight = plateauHeights[Random.Range(0, plateauHeights.Length)];
        var rndVertex = ((xSize   2)   (Random.Range(0, xSize - rndSize - 1) * (xSize   1)))  
                                        Random.Range(0, xSize - rndSize - 1);

        plateauVertexArray = new int[rndSize * rndSize];

        for (int k = 0, i = 0; k < rndSize; k  )
        {
            for (int l = 0; l < rndSize; l  , i  )
            {
                plateauVertexArray[i] = (rndVertex   ((xSize   1) * k))   l;

                if (!plateauDictionary.ContainsKey(plateauVertexArray[i]))
                    plateauDictionary.Add(plateauVertexArray[i], rndHeight);

CodePudding user response:

You could compare the remaining worldVectors.y value with your selected selectedWorldVector.y. I have no idea about linq but i think its a good option if you want to avoid if else spaghetti code.

Aside from that you could also instantiate an empty object at each vertex world position, add an empty box collider to each, and then use a Raycast.SphereCastAll() from your selectedWorldVector position. Set your radius in SphereCast to see each close vertex and then filter out ones that are too low/high.

  • Related