Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mesh Lines - Horizontal and Vertical Edges
#11
(09-20-2020, 06:20 PM)_Aka_ Wrote: Same as here:
(09-19-2020, 04:59 PM)_Aka_ Wrote: Using Unity's API, you can get the list of vertices and triangles of that mesh. From that, you can produce the list of lines making your mesh. Via dot product (or other means), you can deduce what lines are horizontal or vertical.
To get vertices : CGVMesh.Vertex.To get triangles CGVMesh.SubMeshes.Triangles
Examples of "other means": check the value of Vector3.Angle(Vector3.up, yourVector)

I'm kinda looking for something like what Procedural Primitive has done with his example:  Below is his code for making a cylinder.. 

//example ==========================================================
//example ==========================================================
//example ==========================================================
public struct Edge
{
public Vector3 a;
public Vector3 b;
public Edge(Vector3 p1, Vector3 p2)
{
a = p1; b = p2;
}
}

public List<Edge> edgesH = new List<Edge>();    //horizontal edges
public List<Edge> edgesV = new List<Edge>();    //vertival edges

protected void CreateCylinder(Vector3[] pDown, Vector3[] pUp, Vector3 centerDown, Vector3 centerUp, int seg, bool generateUV, Vector2 uvOffset, Vector2 uvTiling, bool flip, bool smooth)
        {
            if (smooth)
            {
                int index = m_vertices.Count;
                Vector3 v = (centerUp - centerDown) / seg;
                float f = flip ? -1.0f : 1.0f;

                if (generateUV)
                {
                    Vector2 vWuv = Vector2.right * uvTiling.x / (pDown.Length - 1);
                    Vector2 vLuv = Vector2.up * uvTiling.y / seg;
                    for (int i = 0; i < pDown.Length; ++i)
                    {
                        Vector3 normal = ((pDown[i] - centerDown) + (pUp[i] - centerUp)).normalized * f;
                        for (int j = 0; j <= seg; ++j)
                        {
                            m_vertices.Add(pDown[i] + v * j);
                            m_normals.Add(normal);
                            m_uv.Add(uvOffset + vWuv * i + vLuv * j);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < pDown.Length; ++i)
                    {
                        Vector3 normal = ((pDown[i] - centerDown) + (pUp[i] - centerUp)).normalized * f;
                        if (flip) normal = -normal;
                        for (int j = 0; j <= seg; ++j)
                        {
                            m_vertices.Add(pDown[i] + v * j);
                            m_normals.Add(normal);
                        }
                    }
                }
               
                for (int i = 0; i < pDown.Length - 1; ++i)
                {
                    for (int j = 0; j < seg; ++j)
                    {
                        m_triangles.Add(index + (seg + 1) * (i + 1) + j + 1);
                        m_triangles.Add(index + (seg + 1) * i + j + 1);
                        m_triangles.Add(index + (seg + 1) * (i + 1) + j);
                        m_triangles.Add(index + (seg + 1) * (i + 1) + j);
                        m_triangles.Add(index + (seg + 1) * i + j + 1);
                        m_triangles.Add(index + (seg + 1) * i + j);

                        //example ==========================================================
                        //example ==========================================================
                        //example ==========================================================
                        //      2
                        //      |\
                        //      | \
                        //      1--3    <= the triangle, v1-v2 is the vertical line, v1-v3 is the horizontal line
                        //outer loop for horizontal, inner loop for vertival
                        //copy index above accordingly
                        int index1 = index + (seg + 1) * i + j;        //i+0, j+0 is v1
                        int index2 = index + (seg + 1) * i + j + 1;    //i+0, j+1 is v2
                        int index3 = index + (seg + 1) * (i + 1) + j;  //i+1, j+0 is v3
                        edgesH.Add(new Edge(m_vertices[index1], m_vertices[index3]));
                        edgesV.Add(new Edge(m_vertices[index1], m_vertices[index2]));
                        //example ==========================================================
                        //example ==========================================================
                        //example ==========================================================
                    }
                }
            }
            else
            {
                float offset = uvTiling.x / (pDown.Length - 1);
                float tiling = 1.0f / (pDown.Length - 1);
                for (int i = 0; i < pDown.Length - 1; ++i)
                {
                    Vector3 normal = Vector3.Cross(pUp[i] - pDown[i + 1], pDown[i] - pUp[i + 1]).normalized;
                    CreatePlane(pDown[i + 1], pUp[i + 1], pUp[i], pDown[i], normal, 1, seg, generateUV, new Vector2(uvOffset.x + offset * (i + 1), uvOffset.y), new Vector2(-uvTiling.x * tiling, uvTiling.y));
                }
            }
        }


        Linefy.Lines testLinesH; //test
        Linefy.Lines testLinesV; //test
        private void OnValidate()
        {
            Apply();

            //test ============================================
            //test ============================================
            //test ============================================
            testLinesH = new Linefy.Lines(edgesH.Count);
            testLinesV = new Linefy.Lines(edgesV.Count);
            for (int i = 0; i < edgesV.Count; ++i)
            {
                testLinesV[i] = new Linefy.Line(edgesV[i].a, edgesV[i].b, Color.blue, 2.0f);
            }
            for (int i = 0; i < edgesH.Count; ++i)
            {
                testLinesH[i] = new Linefy.Line(edgesH[i].a, edgesH[i].b, Color.red, 2.0f);
            }
            //test ============================================
            //test ============================================
            //test ============================================
        }

        //test ============================================
        //test ============================================
        //test ============================================
        private void Update()
        {
            Matrix4x4 objTM = transform.localToWorldMatrix;
            testLinesH.Draw(objTM);
            testLinesV.Draw(objTM);
        }
        //test ============================================
        //test ============================================
        //test ============================================
Reply


Messages In This Thread
RE: Mesh Lines - Horizontal and Vertical Edges - by Krypton - 09-21-2020, 12:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adjust radius of generated mesh via script? Shackman 1 3 03-26-2024, 01:12 PM
Last Post: _Aka_
  Not seeing mesh extended after following YT PaulM 1 3 02-02-2024, 12:01 PM
Last Post: _Aka_
  Trigger Zones along Spline Mesh dlees9191 1 5 01-05-2024, 10:18 AM
Last Post: _Aka_
  Get spline from generated mesh beartrox 1 5 11-27-2023, 12:30 PM
Last Post: _Aka_

Forum Jump: