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.
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
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));
            }
        }
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
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
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
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
  Generated mesh using bezier spline is not following the path accurately processor 3 154 08-25-2020, 04:39 PM
Last Post: _Aka_
  How to make dashed lines? Econt 3 228 08-13-2020, 01:47 PM
Last Post: _Aka_
  Allow higher amount of mesh-faces on extrusion King_of_Limbs 3 347 07-11-2020, 09:02 PM
Last Post: _Aka_
  Control points close together causing mesh overlap amccraw 5 754 05-23-2020, 06:08 PM
Last Post: _Aka_

Forum Jump: