Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

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

    JamesNJ

    Joined:
    Feb 24, 2017
    Posts:
    3
    I am trying to figure out how to generate an extrusion using C# as well. Did you get anywhere with this?
     
  3. SteelX_AW

    SteelX_AW

    Joined:
    May 29, 2017
    Posts:
    2
    I'm using that MeshExtrusion.cs script as well, and I assume your issue is the translation matrix. The one below is a extrusion along y-axis for distance of 5.

    The first matrix represent the base of the mesh, and the second is the cap.

    Matrix4x4[] matrix = {
    new Matrix4x4(
    new Vector4(1,0,0,0),
    new Vector4(0,1,0,0),
    new Vector4(0,0,1,0),
    new Vector4(0,0,0,1)),

    new Matrix4x4(
    new Vector4(1,0,0,0),
    new Vector4(0,1,0,0),
    new Vector4(0,0,1,0),
    new Vector4(0,5,0,1))
    };

    More details for 4*4 matrix translation: https://www.euclideanspace.com/maths/geometry/affine/matrix4x4/index.htm

    Note that the second matrix looks like:
    1000
    0100
    0010
    0501

    but it's actually
    1000
    0105
    0010
    0001

    in standard format
     
    FlorianBernard and Ryuuguu like this.
  4. SomnambulismGames

    SomnambulismGames

    Joined:
    Jul 11, 2016
    Posts:
    1
    I'm a few years too late for this, but for anyone finding this thread, an easier way of defining your translation matrix is using the built in 4x4 functions. If you want to extrude 5 units along y, for example, you can create a Vector3 that points 5 in Y and use the translate function:

    Matrix4x4[] extMatrix = { Matrix4x4.identity, Matrix4x4.Translate(5f * Vector3.up) };
     
    sdb7, Alxander_Watson and Zapan15 like this.
  5. JPoenisch

    JPoenisch

    Joined:
    Mar 2, 2018
    Posts:
    4
    Wouldn't it be even easier to just use https://docs.unity3d.com/ScriptReference/Matrix4x4.TRS.html

    var extMatrix = Matrix4x4.TRS(Vector3.up * 5, Quaternion.identity, Vector3.one);
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    How is
    Code (CSharp):
    1. Matrix4x4.TRS(Vector3.up * 5, Quaternion.identity, Vector3.one);
    simpler than

    Code (CSharp):
    1. Matrix4x4.Translate(5f * Vector3.up);
    The Translate method can be used when you want to construct a TRS matrix where you only need the T and no R or S, that's the whole point. The same way we have a Rotate and a Scale method in case you only need a rotation or scale matrix.

    Maybe you got confused by the array which contains two matrices? One for the start transformation and one for the end. The "ExtrudeMesh" method expects an array of matrices.