Search Unity

How do i Extrude mesh with the MeshExtrusion.cs with another C# script?

Discussion in 'Scripting' started by alexander11, Aug 28, 2016.

  1. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hello i have been having some trouble trying to Extrude Mesh with the MeshExtrusion.cs(From Unity Procedural Examples Asset) with another C# script, i have been trying to do this for more than two months now i've had no success in doing so, i don't like to ask for code(C#) but since this has caused me a lot of frustration on this project i would like to continue to my next Task.

    So if anyone can help me out on this, by coding a C# script that can access the MeshExtrusion.cs and Extrude a Mesh like in Pic1, I'll be very pleased, Thanks in advance.

    (I have looked at the MeshExtrusionTrail.js in the UPE but its in js and also i dont want to make a trail, i just want something like segments like in Pic1)

    -Pic1 (For example the "int segments" on how many extrusion points(i guess).(however there might be an int offset so the segments are seperated))
    3d-Rectangle4.png
    (Edit in Pic1: "...i dont know if the base mesh counts as a point)
    The MeshExtrusion.cs(from the Unity Procedural Examples).
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. /*
    4. * An algorithm to extrude an arbitrary mesh along a number of sections.
    5. The mesh extrusion has 2 steps:
    6. 1. Extracting an edge representation from an arbitrary mesh
    7. - A general edge extraction algorithm is employed. (Same algorithm as traditionally used for stencil shadows) Once all unique edges are found, all edges that connect to only one triangle are extracted.
    8. Thus we end up with the outline of the mesh.
    9. 2. extruding the mesh from the edge representation.
    10. We simply generate a segments joining the edges
    11. */
    12. public class MeshExtrusion
    13. {
    14.     public class Edge
    15.     {
    16.         // The indiex to each vertex
    17.         public int[]  vertexIndex = new int[2];
    18.         // The index into the face.
    19.         // (faceindex[0] == faceindex[1] means the edge connects to only one triangle)
    20.         public int[]  faceIndex = new int[2];
    21.     }
    22.     public static void ExtrudeMesh (Mesh srcMesh, Mesh extrudedMesh, Matrix4x4[] extrusion, bool invertFaces)
    23.     {
    24.         Edge[] edges = BuildManifoldEdges(srcMesh);
    25.         ExtrudeMesh(srcMesh, extrudedMesh, extrusion, edges, invertFaces);
    26.     }
    27.     public static void ExtrudeMesh (Mesh srcMesh, Mesh extrudedMesh, Matrix4x4[] extrusion, Edge[] edges, bool invertFaces)
    28.     {
    29.         int extrudedVertexCount = edges.Length * 2 * extrusion.Length;
    30.         int triIndicesPerStep = edges.Length * 6;
    31.         int extrudedTriIndexCount = triIndicesPerStep * (extrusion.Length -1);
    32.    
    33.         Vector3[] inputVertices = srcMesh.vertices;
    34.         Vector2[] inputUV = srcMesh.uv;
    35.         int[] inputTriangles = srcMesh.triangles;
    36.         Vector3[] vertices = new Vector3[extrudedVertexCount + srcMesh.vertexCount * 2];
    37.         Vector2[] uvs = new Vector2[vertices.Length];
    38.         int[] triangles = new int[extrudedTriIndexCount + inputTriangles.Length * 2];
    39.         // Build extruded vertices
    40.         int v = 0;
    41.         for (int i=0;i<extrusion.Length;i++)
    42.         {
    43.             Matrix4x4 matrix = extrusion[i];
    44.             float vcoord = (float)i / (extrusion.Length -1);
    45.             foreach (Edge e in edges)
    46.             {
    47.                 vertices[v+0] = matrix.MultiplyPoint(inputVertices[e.vertexIndex[0]]);
    48.                 vertices[v+1] = matrix.MultiplyPoint(inputVertices[e.vertexIndex[1]]);
    49.                 uvs[v+0] = new Vector2 (inputUV[e.vertexIndex[0]].x, vcoord);
    50.                 uvs[v+1] = new Vector2 (inputUV[e.vertexIndex[1]].x, vcoord);
    51.            
    52.                 v += 2;
    53.             }
    54.         }  
    55.    
    56.         // Build cap vertices
    57.         // * The bottom mesh we scale along it's negative extrusion direction. This way extruding a half sphere results in a capsule.
    58.         for (int c=0;c<2;c++)
    59.         {
    60.             Matrix4x4 matrix = extrusion[c == 0 ? 0 : extrusion.Length-1];
    61.             int firstCapVertex = c == 0 ? extrudedVertexCount : extrudedVertexCount + inputVertices.Length;
    62.             for (int i=0;i<inputVertices.Length;i++)
    63.             {
    64.                 vertices[firstCapVertex + i] = matrix.MultiplyPoint(inputVertices[i]);
    65.                 uvs[firstCapVertex + i] = inputUV[i];
    66.             }
    67.         }
    68.    
    69.         // Build extruded triangles
    70.         for (int i=0;i<extrusion.Length-1;i++)
    71.         {
    72.             int baseVertexIndex = (edges.Length * 2) * i;
    73.             int nextVertexIndex = (edges.Length * 2) * (i+1);
    74.             for (int e=0;e<edges.Length;e++)
    75.             {
    76.                 int triIndex = i * triIndicesPerStep + e * 6;
    77.                 triangles[triIndex + 0] = baseVertexIndex + e * 2;
    78.                 triangles[triIndex + 1] = nextVertexIndex  + e * 2;
    79.                 triangles[triIndex + 2] = baseVertexIndex + e * 2 + 1;
    80.                 triangles[triIndex + 3] = nextVertexIndex + e * 2;
    81.                 triangles[triIndex + 4] = nextVertexIndex + e * 2 + 1;
    82.                 triangles[triIndex + 5] = baseVertexIndex  + e * 2 + 1;
    83.             }
    84.         }
    85.    
    86.         // build cap triangles
    87.         int triCount = inputTriangles.Length / 3;
    88.         // Top
    89.         {
    90.             int firstCapVertex = extrudedVertexCount;
    91.             int firstCapTriIndex = extrudedTriIndexCount;
    92.             for (int i=0;i<triCount;i++)
    93.             {
    94.                 triangles[i*3 + firstCapTriIndex + 0] = inputTriangles[i * 3 + 1] + firstCapVertex;
    95.                 triangles[i*3 + firstCapTriIndex + 1] = inputTriangles[i * 3 + 2] + firstCapVertex;
    96.                 triangles[i*3 + firstCapTriIndex + 2] = inputTriangles[i * 3 + 0] + firstCapVertex;
    97.             }
    98.         }
    99.    
    100.         // Bottom
    101.         {
    102.             int firstCapVertex = extrudedVertexCount + inputVertices.Length;
    103.             int firstCapTriIndex = extrudedTriIndexCount + inputTriangles.Length;
    104.             for (int i=0;i<triCount;i++)
    105.             {
    106.                 triangles[i*3 + firstCapTriIndex + 0] = inputTriangles[i * 3 + 0] + firstCapVertex;
    107.                 triangles[i*3 + firstCapTriIndex + 1] = inputTriangles[i * 3 + 2] + firstCapVertex;
    108.                 triangles[i*3 + firstCapTriIndex + 2] = inputTriangles[i * 3 + 1] + firstCapVertex;
    109.             }
    110.         }
    111.    
    112.         if (invertFaces)
    113.         {
    114.             for (int i=0;i<triangles.Length/3;i++)
    115.             {
    116.                 int temp = triangles[i*3 + 0];
    117.                 triangles[i*3 + 0] = triangles[i*3 + 1];
    118.                 triangles[i*3 + 1] = temp;
    119.             }
    120.         }
    121.    
    122.         extrudedMesh.Clear();
    123.         extrudedMesh.name= "extruded";
    124.         extrudedMesh.vertices = vertices;
    125.         extrudedMesh.uv = uvs;
    126.         extrudedMesh.triangles = triangles;
    127.         extrudedMesh.RecalculateNormals();
    128.     }
    129.     /// Builds an array of edges that connect to only one triangle.
    130.     /// In other words, the outline of the mesh
    131.     public static Edge[] BuildManifoldEdges (Mesh mesh)
    132.     {
    133.         // Build a edge list for all unique edges in the mesh
    134.         Edge[] edges = BuildEdges(mesh.vertexCount, mesh.triangles);
    135.    
    136.         // We only want edges that connect to a single triangle
    137.         ArrayList culledEdges = new ArrayList();
    138.         foreach (Edge edge in edges)
    139.         {
    140.             if (edge.faceIndex[0] == edge.faceIndex[1])
    141.             {
    142.                 culledEdges.Add(edge);
    143.             }
    144.         }
    145.         return culledEdges.ToArray(typeof(Edge)) as Edge[];
    146.     }
    147.     /// Builds an array of unique edges
    148.     /// This requires that your mesh has all vertices welded. However on import, Unity has to split
    149.     /// vertices at uv seams and normal seams. Thus for a mesh with seams in your mesh you
    150.     /// will get two edges adjoining one triangle.
    151.     /// Often this is not a problem but you can fix it by welding vertices
    152.     /// and passing in the triangle array of the welded vertices.
    153.     public static Edge[] BuildEdges(int vertexCount, int[] triangleArray)
    154.     {
    155.         int maxEdgeCount = triangleArray.Length;
    156.         int[] firstEdge = new int[vertexCount + maxEdgeCount];
    157.         int nextEdge = vertexCount;
    158.         int triangleCount = triangleArray.Length / 3;
    159.    
    160.         for (int a = 0; a < vertexCount; a++)
    161.             firstEdge[a] = -1;
    162.        
    163.         // First pass over all triangles. This finds all the edges satisfying the
    164.         // condition that the first vertex index is less than the second vertex index
    165.         // when the direction from the first vertex to the second vertex represents
    166.         // a counterclockwise winding around the triangle to which the edge belongs.
    167.         // For each edge found, the edge index is stored in a linked list of edges
    168.         // belonging to the lower-numbered vertex index i. This allows us to quickly
    169.         // find an edge in the second pass whose higher-numbered vertex index is i.
    170.         Edge[] edgeArray = new Edge[maxEdgeCount];
    171.    
    172.         int edgeCount = 0;
    173.         for (int a = 0; a < triangleCount; a++)
    174.         {
    175.             int i1 = triangleArray[a*3 + 2];
    176.             for (int b = 0; b < 3; b++)
    177.             {
    178.                 int i2 = triangleArray[a*3 + b];
    179.                 if (i1 < i2)
    180.                 {
    181.                     Edge newEdge = new Edge();
    182.                     newEdge.vertexIndex[0] = i1;
    183.                     newEdge.vertexIndex[1] = i2;
    184.                     newEdge.faceIndex[0] = a;
    185.                     newEdge.faceIndex[1] = a;
    186.                     edgeArray[edgeCount] = newEdge;
    187.                
    188.                     int edgeIndex = firstEdge[i1];
    189.                     if (edgeIndex == -1)
    190.                     {
    191.                         firstEdge[i1] = edgeCount;
    192.                     }
    193.                     else
    194.                     {
    195.                         while (true)
    196.                         {
    197.                             int index = firstEdge[nextEdge + edgeIndex];
    198.                             if (index == -1)
    199.                             {
    200.                                 firstEdge[nextEdge + edgeIndex] = edgeCount;
    201.                                 break;
    202.                             }
    203.                    
    204.                             edgeIndex = index;
    205.                         }
    206.                     }
    207.        
    208.                     firstEdge[nextEdge + edgeCount] = -1;
    209.                     edgeCount++;
    210.                 }
    211.        
    212.                 i1 = i2;
    213.             }
    214.         }
    215.    
    216.         // Second pass over all triangles. This finds all the edges satisfying the
    217.         // condition that the first vertex index is greater than the second vertex index
    218.         // when the direction from the first vertex to the second vertex represents
    219.         // a counterclockwise winding around the triangle to which the edge belongs.
    220.         // For each of these edges, the same edge should have already been found in
    221.         // the first pass for a different triangle. Of course we might have edges with only one triangle
    222.         // in that case we just add the edge here
    223.         // So we search the list of edges
    224.         // for the higher-numbered vertex index for the matching edge and fill in the
    225.         // second triangle index. The maximum number of comparisons in this search for
    226.         // any vertex is the number of edges having that vertex as an endpoint.
    227.    
    228.         for (int a = 0; a < triangleCount; a++)
    229.         {
    230.             int i1 = triangleArray[a*3+2];
    231.             for (int b = 0; b < 3; b++)
    232.             {
    233.                 int i2 = triangleArray[a*3+b];
    234.                 if (i1 > i2)
    235.                 {
    236.                     bool foundEdge = false;
    237.                     for (int edgeIndex = firstEdge[i2]; edgeIndex != -1;edgeIndex = firstEdge[nextEdge + edgeIndex])
    238.                     {
    239.                         Edge edge = edgeArray[edgeIndex];
    240.                         if ((edge.vertexIndex[1] == i1) && (edge.faceIndex[0] == edge.faceIndex[1]))
    241.                         {
    242.                             edgeArray[edgeIndex].faceIndex[1] = a;
    243.                             foundEdge = true;
    244.                             break;
    245.                         }
    246.                     }
    247.                
    248.                     if (!foundEdge)
    249.                     {
    250.                         Edge newEdge = new Edge();
    251.                         newEdge.vertexIndex[0] = i1;
    252.                         newEdge.vertexIndex[1] = i2;
    253.                         newEdge.faceIndex[0] = a;
    254.                         newEdge.faceIndex[1] = a;
    255.                         edgeArray[edgeCount] = newEdge;
    256.                         edgeCount++;
    257.                     }
    258.                 }
    259.            
    260.                 i1 = i2;
    261.             }
    262.         }
    263.    
    264.         Edge[] compactedEdges = new Edge[edgeCount];
    265.         for (int e=0;e<edgeCount;e++)
    266.             compactedEdges[e] = edgeArray[e];
    267.    
    268.         return compactedEdges;
    269.     }
    270. }
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Please do not duplicate threads.
     
  3. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    I tried not to, but i cant delete the other one for some reason