duck

Unity procedural generation of quad strip

Apr 2nd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MakeMesh : MonoBehaviour {
  5.  
  6.    
  7.     MeshFilter filter;
  8.     MeshRenderer renderer;
  9.     Mesh mesh;
  10.     public Material material;
  11.    
  12.     // Use this for initialization
  13.     void Start () {
  14.    
  15.         filter = gameObject.AddComponent<MeshFilter>();
  16.            
  17.         renderer = gameObject.AddComponent<MeshRenderer>();
  18.        
  19.         mesh = new Mesh();
  20.        
  21.         filter.mesh = mesh;
  22.        
  23.        
  24.         int numQuads = 4000;
  25.         int numVerts = (numQuads+1) * 2;
  26.        
  27.         Vector3[] verts = new Vector3[ numVerts ];
  28.        
  29.         int v = 0;
  30.         for (int n=0; n<=numQuads; ++n)
  31.         {
  32.            
  33.             verts[v++] = new Vector3( -1, Mathf.Sin(n*.1f), n * 1 );
  34.             verts[v++] = new Vector3(  1, Mathf.Sin((n*.1f)+Mathf.PI), n * 1 );
  35.            
  36.         }
  37.        
  38.        
  39.         int numTris = numQuads*2;
  40.        
  41.         int[] tris = new int[ numTris * 3 ];
  42.        
  43.         int ti = 0;
  44.         for (int triNum=0; triNum<numTris; triNum += 2)
  45.         {
  46.            
  47.             tris[ti++] = triNum;
  48.             tris[ti++] = triNum+3;
  49.             tris[ti++] = triNum+1;
  50.            
  51.            
  52.             tris[ti++] = triNum;
  53.             tris[ti++] = triNum+2;
  54.             tris[ti++] = triNum+3;
  55.            
  56.            
  57.         }
  58.        
  59.        
  60.        
  61.         mesh.vertices = verts;
  62.         mesh.triangles = tris;
  63.         renderer.material = material;
  64.        
  65.         mesh.RecalculateNormals();
  66.        
  67.        
  68.     }
  69.    
  70.    
  71.    
  72.    
  73.     // Update is called once per frame
  74.     void Update () {
  75.    
  76.     }
  77.    
  78.    
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment