Model 1: Cone
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshYuanZhui : MonoBehaviour { public float radius = 1f; // cone base radius public float height = 2f; // Cone height private void Awake() { CreateConeMesh(radius, height); } /// <summary> /// Create a cone Mesh /// </summary> /// <param name="radius">Conical base radius</param> /// <param name="height">Cone height</param> /// <returns>Mesh object</returns> private Mesh CreateConeMesh(float radius, float height) { var vertices = new List<Vector3>(); var indices = new List<int>(); vertices.Add(Vector3.zero); vertices.Add(Vector3.up * height); var temp = new List<Vector3>(); //bottom round surface for (var i = 0.0f; i < 360.0f; i + = 30) { var rad = Mathf.Deg2Rad * i; var x = radius * Mathf.Cos(rad); var z = radius * Mathf.Sin(rad); temp.Add(new Vector3(x, 0.0f, z)); } vertices.AddRange(temp); vertices.AddRange(temp); for (var i = 2; i <= 13; i + + ) { indices.Add(i); if (i < 13) { indices.Add(i + 1); } else { indices.Add(2); } indices.Add(0); } for (var i = 14; i <= 25; i + + ) { indices.Add(i); indices.Add(1); if (i < 25) { indices.Add(i + 1); } else { indices.Add(14); } } Mesh mesh = new Mesh(); mesh.SetVertices(vertices); mesh.SetTriangles(indices, 0); mesh.RecalculateNormals(); GetComponent<MeshFilter>().mesh = mesh; GetComponent<MeshCollider>().sharedMesh = mesh; Material mat = new Material(Shader.Find("Custom/NewSurfaceShader")); return mesh; } }
Model 2: Cylinder
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshYuanZhu : MonoBehaviour { public int num; // mesh renderer MeshRenderer meshRenderer; //grid filter MeshFilter meshFilter; // Used to store vertex data List<Vector3> verts; // Vertex list List<int> indices; // Serial number list private void Start() { verts = new List<Vector3>(); indices = new List<int>(); meshRenderer = GetComponent<MeshRenderer>(); meshFilter = GetComponent<MeshFilter>(); Generate(); } public void Generate() { // Fill in the vertex and serial number data in the list AddMeshData7(num);//Triangle //Create a mesh Mesh object using list data Mesh mesh = new Mesh(); mesh.SetVertices(verts); //mesh.vertices = verts.ToArray(); mesh.SetIndices(indices, MeshTopology.Triangles, 0); //mesh.triangles = indices.ToArray(); // Automatically calculate normals mesh.RecalculateNormals(); // Automatically calculate the overall boundary of the object mesh.RecalculateBounds(); //Assign the mesh object to the mesh filter and you're done. meshFilter.mesh = mesh; } void AddMeshData7(int n)//n is the number of triangles contained in the circle { int a = 360/n; verts.Add(new Vector3(0, 0, 0)); for (int i = 0; i < n; i + + ) //Above { verts.Add(new Vector3(Mathf.Sin(a * Mathf.Deg2Rad), 0, Mathf.Cos(a * Mathf.Deg2Rad))); a + = 360/n; } verts.Add(new Vector3(0, -2, 0)); for (int i = 1; i < n + 1; i + + ) //below { verts.Add(verts[i] + new Vector3(0, -2, 0)); } Debug.Log("verts.Count: " + verts.Count); for (int i = 0; i < n; i + + ) //Draw the above { indices.Add(0); indices.Add(i + 1); if (i + 1 >= n) { indices.Add(1); } else { indices.Add(i + 2); } } for (int i = n + 1; i < 2 * n + 1; i + + ) //Draw the following { Debug.Log("i " + i); indices.Add(n + 1); if (i + 2 >= 2 * n + 2) { indices.Add(n + 2); } else { indices.Add(i + 2); } indices.Add(i + 1); } int oldcount = verts.Count; Debug.Log(oldcount); for (int i = 0; i < oldcount; i + + ) { verts.Add(verts[i]); } for (int i = oldcount + 1; i <= oldcount + n; i + + ) //Draw the side { indices.Add(i); indices.Add(i + n + 1); if (i + 1 >= n + oldcount) { indices.Add(oldcount + 1); } else { indices.Add(i + 1); Debug.Log(verts[i + 1]); } indices.Add(i + n + 1); if (i + n + 2 > 2 * n + 1 + oldcount) { indices.Add(oldcount + n + 2); indices.Add(oldcount + 1); } else { indices.Add(i + n + 2); indices.Add(i + 1); } } } }
Model three: trapezoid
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshTiXing : MonoBehaviour { void Start() { Mesh mesh = new Mesh(); List<Vector3> vers = new List<Vector3>(); vers.Add(new Vector3(-5, 0, -5)); vers.Add(new Vector3(5, 0, -5)); vers.Add(new Vector3(5, 0, 5)); vers.Add(new Vector3(-5, 0, 5)); vers.Add(new Vector3(-2, 3, -2)); vers.Add(new Vector3(2, 3, -2)); vers.Add(new Vector3(2, 3, 2)); vers.Add(new Vector3(-2, 3, 2)); List<int> tris = new List<int>(); tris.Add(0); tris.Add(2); tris.Add(1); tris.Add(0); tris.Add(3); tris.Add(2); tris.Add(5); tris.Add(7); tris.Add(4); tris.Add(5); tris.Add(6); tris.Add(7); tris.Add(4); tris.Add(3); tris.Add(0); tris.Add(4); tris.Add(7); tris.Add(3); tris.Add(6); tris.Add(1); tris.Add(2); tris.Add(6); tris.Add(5); tris.Add(1); tris.Add(5); tris.Add(0); tris.Add(1); tris.Add(5); tris.Add(4); tris.Add(0); tris.Add(7); tris.Add(2); tris.Add(3); tris.Add(7); tris.Add(6); tris.Add(2); mesh.vertices = vers.ToArray(); mesh.triangles = tris.ToArray(); GetComponent<MeshFilter>().mesh = mesh; } }
Model 4: Round ball
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MeshSphere : MonoBehaviour { public MeshFilter meshFilter; public int num=100; public float r = 5; // Start is called before the first frame update void Start() { Mesh mesh=new Mesh(); meshFilter=gameObject.GetComponent<MeshFilter>(); List<Vector3> vector3s = new List<Vector3>(); float Nums = (2 * Mathf.PI) / num; vector3s.Add(new Vector3(0, 0, 0)); for (int i = 0; i < num; i + + ) { float x = Mathf.Sin(i * Nums) * r; float y = Mathf.Cos(i * Nums) * r; vector3s.Add(new Vector3(x, y, 0)); } mesh.vertices = vector3s.ToArray(); List<int> ints = new List<int>(); for (int i = 0; i < num; i + + ) { if(i==0) { ints.Add(0); ints.Add(num); ints.Add(1); } else { ints.Add(0); ints.Add(i); ints.Add(i + 1); } } mesh.triangles = ints.ToArray(); meshFilter.mesh = mesh; } // Update is called once per frame void Update() { } }