Mesh Boarder C#

Good Afternoon, I have created a mesh in this script but I am having troubles figuring out how to create a outside boarder, I got this code from a previous answer and not sure exactly how it works.

Link: Round Procedural Mesh Corners? C# - Questions & Answers - Unity Discussions

//RoundedQuadMesh.cs
 using UnityEngine;
 using System.Collections;
 
 public class RoundedQuadMesh : MonoBehaviour
 {
     public float RoundEdges = 0.5f;
     public float RoundTopLeft = 0.0f;
     public float RoundTopRight = 0.0f;
     public float RoundBottomLeft = 0.0f;
     public float RoundBottomRight = 0.0f;
     public float Size = 1f;
     public int CornerVertexCount = 8;
     public bool CreateUV = true;
     public bool DoubleSided = false;
     public bool AutoUpdate = true;
 
     private MeshFilter m_MeshFilter;
     private Mesh m_Mesh;
     private Vector3[] m_Vertices;
     private Vector3[] m_Normals;
     private Vector2[] m_UV;
     private int[] m_Triangles;
 
     void Start ()
     {
         m_MeshFilter = GetComponent<MeshFilter>();
         if (m_MeshFilter == null)
             m_MeshFilter = gameObject.AddComponent<MeshFilter>();
         if (GetComponent<MeshRenderer>() == null)
             gameObject.AddComponent<MeshRenderer>();
         m_Mesh = new Mesh();
         m_MeshFilter.sharedMesh = m_Mesh;
         UpdateMesh();
     }
 
     public Mesh UpdateMesh()
     {
         if (CornerVertexCount<2)
             CornerVertexCount = 2;
         int sides = DoubleSided ? 2 : 1;
         int vCount = CornerVertexCount * 4 * sides + sides;
         int triCount = (CornerVertexCount * 4) * sides;
         if (m_Vertices == null || m_Vertices.Length != vCount)
         {
             m_Vertices = new Vector3[vCount];
             m_Normals = new Vector3[vCount];
         }
         if (m_Triangles == null || m_Triangles.Length != triCount * 3)
             m_Triangles = new int[triCount * 3];
         if (CreateUV && (m_UV == null || m_UV.Length != vCount))
         { 
             m_UV = new Vector2[vCount];
         }
         float f = 1f / (CornerVertexCount-1);
         m_Vertices[0] = Vector3.zero;
         int count = CornerVertexCount * 4;
         if (CreateUV)
         {
             m_UV[0] = Vector2.one *0.5f;
             if (DoubleSided)
                 m_UV[count + 1] = m_UV[0];
         }
         
         for (int i = 0; i < CornerVertexCount; i++ )
         {
             float s = Mathf.Sin((float)i * Mathf.PI * 0.5f*f);
             float c = Mathf.Cos((float)i * Mathf.PI * 0.5f*f);
             float tl = Mathf.Clamp01(RoundTopLeft + RoundEdges);
             float tr = Mathf.Clamp01(RoundTopRight + RoundEdges);
             float bl = Mathf.Clamp01(RoundBottomLeft + RoundEdges);
             float br = Mathf.Clamp01(RoundBottomRight + RoundEdges);
             Vector2 v1 = new Vector3(-1f + tl - c * tl, 1 - tl + s * tl);
             Vector2 v2 = new Vector3(1f - tr + s * tr, 1f - tr + c * tr);
             Vector2 v3 = new Vector3(1f - br + c * br, -1f + br - s * br);
             Vector2 v4 = new Vector3(-1f + bl - s * bl, -1f + bl - c * bl);
 
             m_Vertices[1 + i] = v1 * Size;
             m_Vertices[1 + CornerVertexCount + i] = v2 * Size;
             m_Vertices[1 + CornerVertexCount * 2 + i] = v3 * Size;
             m_Vertices[1 + CornerVertexCount * 3 + i] = v4 * Size;
             if (CreateUV)
             {
                 m_UV[1 + i] = v1 * 0.5f + Vector2.one * 0.5f;
                 m_UV[1 + CornerVertexCount * 1 + i] = v2 * 0.5f + Vector2.one * 0.5f;
                 m_UV[1 + CornerVertexCount * 2 + i] = v3 * 0.5f + Vector2.one * 0.5f;
                 m_UV[1 + CornerVertexCount * 3 + i] = v4 * 0.5f + Vector2.one * 0.5f;
             }
             if (DoubleSided)
             {
                 // The backside vertices are in reverse order
                 m_Vertices[1 + CornerVertexCount * 7 + CornerVertexCount - i] = v1 * Size;
                 m_Vertices[1 + CornerVertexCount * 6 + CornerVertexCount - i] = v2 * Size;
                 m_Vertices[1 + CornerVertexCount * 5 + CornerVertexCount - i] = v3 * Size;
                 m_Vertices[1 + CornerVertexCount * 4 + CornerVertexCount - i] = v4 * Size;
                 if (CreateUV)
                 {
                     m_UV[1 + CornerVertexCount * 7 + CornerVertexCount - i] = v1 * 0.5f + Vector2.one * 0.5f;
                     m_UV[1 + CornerVertexCount * 6 + CornerVertexCount - i] = v2 * 0.5f + Vector2.one * 0.5f;
                     m_UV[1 + CornerVertexCount * 5 + CornerVertexCount - i] = v3 * 0.5f + Vector2.one * 0.5f;
                     m_UV[1 + CornerVertexCount * 4 + CornerVertexCount - i] = v4 * 0.5f + Vector2.one * 0.5f;
                 }
             }
         }
         for (int i = 0; i < count + 1;i++ )
         {
             m_Normals *= -Vector3.forward;*

if (DoubleSided)
m_Normals[count + 1 + i] = Vector3.forward;
}

for (int i = 0; i < count; i++)
{
m_Triangles[i*3 ] = 0;
m_Triangles[i*3 + 1] = i + 1;
m_Triangles[i*3 + 2] = i + 2;
if (DoubleSided)
{
m_Triangles[(count + i) * 3] = count+1;
m_Triangles[(count + i) * 3 + 1] = count+1 +i + 1;
m_Triangles[(count + i) * 3 + 2] = count+1 +i + 2;
}
}
m_Triangles[count * 3 - 1] = 1;
if (DoubleSided)
m_Triangles[m_Triangles.Length - 1] = count + 1 + 1;

m_Mesh.Clear();
m_Mesh.vertices = m_Vertices;
m_Mesh.normals = m_Normals;
if (CreateUV)
m_Mesh.uv = m_UV;
m_Mesh.triangles = m_Triangles;

return m_Mesh;
}

void Update ()
{
if (AutoUpdate)
UpdateMesh();
}
}

[52012-example.jpg|52012]*
Thanks
*

Docs have good example

also

http://kobolds-keep.net/?p=33

Depending on the approach you want to take, you could potentially use a much more ambiguous setup. Namely, utilizing the GL class, you could have an empty object with vertex positions defined in a script. You’d then draw those using basic OpenGL commands, such as GL.Triangles. Then, when the vertices move, they’re being reconstructed regardless, so they’d move actively with just the vertex position changes in the script.

That said, I would recommend taking a look at this, this, this, and this to see examples on how it’s been handled if it’s the way you’re looking to approach it.

I still can’t workout quite what you’re code is doing, so I just wrote my own version so you can hopefully compare somewhat:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class meshCreate : MonoBehaviour {

	public float Width = 4f;
	public float Height = 2f;
	public float BorderWidth = 0.1f;
	public int CornerVertexCount = 5;
	public float CornerSize = 1f;
	Mesh goMesh;

	void Start(){

		//GameObject go = new GameObject("newObj", typeof(MeshFilter), typeof(MeshRenderer));
		goMesh = gameObject.GetComponent<MeshFilter>().mesh;
		//CreateMesh(Width, Height, CornerVertexCount, CornerSize, BorderWidth, goMesh);

	}

	void Update(){

		//GameObject go = new GameObject("newObj", typeof(MeshFilter), typeof(MeshRenderer));
		//goMesh = gameObject.GetComponent<MeshFilter>().mesh;
		CreateMesh(Width, Height, CornerVertexCount, CornerSize, BorderWidth, goMesh);

	}


	void CreateMesh(float w, float h, int c, float cornerSize, float borderWidth, Mesh nMesh){
		borderWidth = Mathf.Max(borderWidth, 0);
		bool hasBorder = borderWidth != 0;

		w -= 2*borderWidth;
		h -= 2*borderWidth;
		cornerSize -= borderWidth;

		w = Mathf.Max(w, 0);
		h = Mathf.Max(h, 0);

		cornerSize = Mathf.Clamp(cornerSize, 0, Mathf.Min(w, h));
		c = Mathf.Max(c, 2);

		List<Vector3> verts = new List<Vector3>();
		List<int> tris = new List<int>();

		///////////////////
		//	  ______
		//	 /	  |
		//	|	   |
		//	|_______|
		//
		///////////////////

		float halfW = w/2f;
		float halfH = h/2f;

		Vector3 offset = Vector3.zero; //new Vector3(halfW, halfH) add something like this if we want the 'centre' to be a corner

		/////////////////////////////////
		//Do the easy bit
		verts.Add(new Vector3(cornerSize-halfW, halfH) + offset); //0
		verts.Add(new Vector3(halfW, halfH) + offset); //1
		verts.Add(new Vector3(-halfW, halfH-cornerSize) + offset); //2
		verts.Add(new Vector3(cornerSize-halfW, halfH-cornerSize) + offset); //3
		verts.Add(new Vector3(halfW, halfH-cornerSize) + offset); //4
		verts.Add(new Vector3(-halfW, -halfH) + offset); //5
		verts.Add(new Vector3(cornerSize-halfW, -halfH) + offset); //6
		verts.Add(new Vector3(halfW, -halfH) + offset); //7

		tris.Add(0);
		tris.Add(1);
		tris.Add(3);

		tris.Add(1);
		tris.Add(4);
		tris.Add(3);

		tris.Add(2);
		tris.Add(3);
		tris.Add(5);

		tris.Add(3);
		tris.Add(6);
		tris.Add(5);

		tris.Add(3);
		tris.Add(4);
		tris.Add(6);

		tris.Add(4);
		tris.Add(7);
		tris.Add(6);
		/////////////////////////////////

		/////////////////////////////////
		//Not so easy corner bit

		//top is 0
		//left is 2
		//pivot is 3



		float dQ = 90f/(float)c;
		Vector3 left = new Vector3(-cornerSize, 0, 0);
		int lastVert = 2;

		

		for(int i = 1; i < c; i++){

			verts.Add(verts[3]+((Quaternion.Euler(0, 0, -i*dQ))*left));
			//lastVert = verts.Count-1;

			tris.Add(lastVert);
			tris.Add(verts.Count-1);
			tris.Add(3);

			lastVert = verts.Count-1;

		}

		//Debug.Log(verts.Count-1);
		
		tris.Add(lastVert);
		tris.Add(0);
		tris.Add(3);

		/////////////////////////////////
		//Add border
		/////////////////////////////////

		if(!hasBorder){
			SetMesh(verts.ToArray(), tris.ToArray(), nMesh);
			return;
		}


		//easy stuff
		w += 2*borderWidth;
		h += 2*borderWidth;
		cornerSize += borderWidth;
		halfW = w/2f;
		halfH = h/2f;

		verts.Add(new Vector3(cornerSize-halfW, halfH) + offset); //-1
		verts.Add(new Vector3(halfW-borderWidth, halfH) + offset); //
		verts.Add(new Vector3(halfW, halfH) + offset); //1
		verts.Add(new Vector3(halfW, halfH-borderWidth) + offset); //2
		verts.Add(new Vector3(halfW, borderWidth-halfH) + offset); //3
		verts.Add(new Vector3(halfW, -halfH) + offset); //4
		verts.Add(new Vector3(halfW-borderWidth, -halfH) + offset); //5
		verts.Add(new Vector3(borderWidth-halfW, -halfH) + offset); //6
		verts.Add(new Vector3(-halfW, -halfH) + offset); //7
		verts.Add(new Vector3(-halfW, borderWidth-halfH) + offset); //8
		verts.Add(new Vector3(-halfW, halfH-cornerSize) + offset); //9

		tris.Add(7+c);
		tris.Add(1);
		tris.Add(0);

		tris.Add(7+c);
		tris.Add(7+c+1);
		tris.Add(1);

		tris.Add(7+c+1);
		tris.Add(7+c+2);
		tris.Add(1);

		tris.Add(7+c+2);
		tris.Add(7+c+3);
		tris.Add(1);

		tris.Add(7+c+3);
		tris.Add(7);
		tris.Add(1);

		tris.Add(7+c+3);
		tris.Add(7+c+4);
		tris.Add(7);

		tris.Add(7+c+4);
		tris.Add(7+c+5);
		tris.Add(7);

		tris.Add(7+c+5);
		tris.Add(7+c+6);
		tris.Add(7);

		tris.Add(7+c+6);
		tris.Add(5);
		tris.Add(7);

		tris.Add(7+c+6);
		tris.Add(7+c+7);
		tris.Add(5);

		tris.Add(7+c+7);
		tris.Add(7+c+8);
		tris.Add(5);

		tris.Add(7+c+8);
		tris.Add(7+c+9);
		tris.Add(5);

		tris.Add(7+c+9);
		tris.Add(2);
		tris.Add(5);

		tris.Add(7+c+9);
		tris.Add(7+c+10);
		tris.Add(2);
		//tris.Add(7+c+5);
	

		//Difficult

		left = new Vector3(-cornerSize, 0, 0);
		int lastVertOut = 7+c+10;
		int lastVertIn = 2;

		for(int i = 1; i < c; i++){

			verts.Add(verts[3]+((Quaternion.Euler(0, 0, -i*dQ))*left));
			//lastVert = verts.Count-1;

			tris.Add(lastVertOut);
			tris.Add(verts.Count-1);
			tris.Add(lastVertIn);

			tris.Add(verts.Count-1);
			tris.Add(lastVertIn+1);
			tris.Add(lastVertIn);

			lastVertOut = verts.Count-1;
			lastVertIn = 7+i;

		}

		//Debug.Log(verts.Count-1);
		
		tris.Add(lastVertOut);
		tris.Add(7+c);
		tris.Add(lastVertIn);

		tris.Add(7+c);
		tris.Add(0);
		tris.Add(lastVertIn);







		nMesh.Clear();

		nMesh.vertices = verts.ToArray();
		nMesh.triangles = tris.ToArray();

		nMesh.RecalculateBounds();
		nMesh.RecalculateNormals();

		//return nMesh;
	}

	void SetMesh(Vector3[] v, int[] t, Mesh m){
		m.Clear();

		m.vertices = v;
		m.triangles = t;

		m.RecalculateBounds();
		m.RecalculateNormals();
	}


}

Scribe