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
#12
(09-20-2020, 07:08 PM)Krypton Wrote: "Extending the Curvy Generator is the most sophisticated way to extend Curvy, but the most fun part, too! Though not rocket science, you'll need a good understanding of the Generator internals to successfully write quality modules."

Do you have more detailed documentation of how the Generators internals work?
There no step by step guide explaining how to create custom modules, but information is scattered between: Basically you will need to set the inputs and outputs of your module to the right data type, and set the code for your logic in the Refresh method.
Sorry for not having some simplified guide ready, but I believe it is not very difficult to make a simple module based on the above information.
Let me know if you have any specific question regarding this subject.

About your other post, I will answer it a bit later.
Available for freelance work, feel free to reach out: toolbuddy.net
Please consider leaving a review for Curvy, this helps immensely. Thank you.
Reply
#13
Untested code, but even if it is bugged, you get the idea.
Add this code in the CGVMesh class, and then call public method where appropriate
Code:
public struct Edge
        {
            public Vector3 a;
            public Vector3 b;
            public Edge(Vector3 p1, Vector3 p2)
            {
                a = p1; b = p2;
            }
        }

        public IEnumerable<Edge> GetHorizontalAndVerticalLines()
        {
            var result = new List<Edge>();
            foreach (CGVSubMesh subMesh in SubMeshes)
            {
                for (var i = 0; i < subMesh.Triangles.Length; i++)
                {
                    int triangleStart = subMesh.Triangles[i];

                    TryAddEdge(Vertex[triangleStart + 0], Vertex[triangleStart + 1], result);
                    TryAddEdge(Vertex[triangleStart + 1], Vertex[triangleStart + 2], result);
                    TryAddEdge(Vertex[triangleStart + 2], Vertex[triangleStart + 0], result);
                }
            }

            return result;
        }

        private static void TryAddEdge(Vector3 position1, Vector3 position2, List<Edge> result)
        {
            var direction = position1 - position2;

            if (Vector3.Angle(direction, Vector3.up) < 0.01f || Vector3.Angle(direction, Vector3.right) < 0.01f)
            {
                result.Add(new Edge(position1, position2));
            }
        }
Available for freelance work, feel free to reach out: toolbuddy.net
Please consider leaving a review for Curvy, this helps immensely. Thank you.
Reply
#14
Same logic would work on instances of type Mesh, while using the appropriate members (see my previous posts about that)
I tested horizontal and vertical by testing the angle against 0, but handle the potalso the cases of 180 and -180
Available for freelance work, feel free to reach out: toolbuddy.net
Please consider leaving a review for Curvy, this helps immensely. Thank you.
Reply
#15
(09-21-2020, 04:26 PM)_Aka_ Wrote: Untested code, but even if it is bugged, you get the idea.
Add this code in the CGVMesh class, and then call public method where appropriate
Code:
public struct Edge
        {
            public Vector3 a;
            public Vector3 b;
            public Edge(Vector3 p1, Vector3 p2)
            {
                a = p1; b = p2;
            }
        }

        public IEnumerable<Edge> GetHorizontalAndVerticalLines()
        {
            var result = new List<Edge>();
            foreach (CGVSubMesh subMesh in SubMeshes)
            {
                for (var i = 0; i < subMesh.Triangles.Length; i++)
                {
                    int triangleStart = subMesh.Triangles[i];

                    TryAddEdge(Vertex[triangleStart + 0], Vertex[triangleStart + 1], result);
                    TryAddEdge(Vertex[triangleStart + 1], Vertex[triangleStart + 2], result);
                    TryAddEdge(Vertex[triangleStart + 2], Vertex[triangleStart + 0], result);
                }
            }

            return result;
        }

        private static void TryAddEdge(Vector3 position1, Vector3 position2, List<Edge> result)
        {
            var direction = position1 - position2;

            if (Vector3.Angle(direction, Vector3.up) < 0.01f || Vector3.Angle(direction, Vector3.right) < 0.01f)
            {
                result.Add(new Edge(position1, position2));
            }
        }

Thank you for the code, will try it. 
Cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mesh Generation between two splines vatan 4 670 02-14-2025, 07:11 AM
Last Post: vatan
  Maintaining vertical orientation of extruded meshes rickgplus 3 540 01-24-2025, 09:24 PM
Last Post: _Aka_
  Create Mesh Node, Make Static Options rickgplus 1 349 01-23-2025, 10:12 AM
Last Post: _Aka_
  Extrude mesh along spline. New and confused user GhostStalker 3 531 01-02-2025, 09:58 AM
Last Post: _Aka_

Forum Jump: