1
\$\begingroup\$

I am able to get the outer vertices of a mesh using this and able to generate cubes on outer edges of the mesh. Here is my code:

  mesh = GetComponent<MeshFilter>().mesh;
        Stopwatch sp = new Stopwatch();
        sp.Start();
        Vector3[] vertices = mesh.vertices;
        List<EdgeHelpers.Edge> edges =  EdgeHelpers.GetEdges(mesh.triangles).FindBoundary();

        for (int i = 0; i < edges.Count; i++)
        {
            
            Vector3 v3 = vertices[edges[i].v1];
            GameObject go1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
            go1.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f);
            go1.transform.localPosition = transform.TransformPoint(v3);
            //go1.transform.localEulerAngles = transform.TransformDirection(v3);
            //go1.transform.rotation = this.transform.rotation;
           // go1.AddComponent<CubeMakerOnEmptySpaces>();
        }
        sp.Stop();
        UnityEngine.Debug.Log(sp.ElapsedMilliseconds / 1000);

Now I want to change the rotation of each cube that each cube z should be pointing against the mesh direction (should be outside of the mesh). Maybe picture representation helpful here

current rotaation

Need to be like this:

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Is your mesh largely level? \$\endgroup\$
    – Basic
    Feb 11 at 19:24
0
\$\begingroup\$

Are you sorting your edge list as suggested in the linked question?

If so, get the location of point n+1, subtract point n-1 and you have a vector for your edge at point n (You'll need to handle either side of the wraparound).

Now all you need to do is decide which orientation is "in".

To reach a good conclusion, we need to know what assumptions we can make about your mesh topology and whether you need a 2D/3D direction for "out".

If the mesh is largely level, meaning the cube doesn't need to tilt in/out to account for hills just inside the edge (aka the cube's +z has no y component)...

The +z vector of the cube is either going to be the cross product of world +y and your edge, or the the exact inverse.

It should be fairly easy to distinguish between the two cases. Compare one option to the point n and whether the result is +/- 90 degrees will tell you which configuration you want.

Thinking of it another way... Start at point n-1, Look at n+1 with "up" being +y. From there, it's all 90 degree rotations.

This provides undefined behaviour on truly vertical edges, and doesn't behave well when the edge of the mesh is a cliff or for a sheet of paper tipped slightly backwards (the sides would work perfectly but the cubes at the top and bottom would be have an up vector of +y)

If you need to solve in "real" 3D, we need some way to work out which verts to include in our calculation of the "in" vector.

How often do you need to do this? On level load? Every frame?

There's a brute force approach where we simply scan all verts for those in proximity to the point in question, then filter by some criteria and get an average.

It all depends on exactly what you need and what assumptions we can make about the mesh.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.