Advertisement
dnnkeeper

Untitled

Sep 7th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ProceduralPlane : MonoBehaviour {
  5.  
  6.     public float h = 2.0f;
  7.     public float w = 2.0f;
  8.  
  9.     public int l = 3;
  10.  
  11.     public Mesh m;
  12.     // Use this for initialization
  13.     void Start () {
  14.         m = new Mesh();
  15.         m.vertices = new Vector3[]
  16.         {
  17.             -transform.right*w, //left
  18.             transform.right*w, //right
  19.  
  20.             -transform.right*w + transform.forward*h, //top left
  21.             transform.right*w + transform.forward*h, //top right
  22.         };
  23.         m.triangles = new int[]
  24.         {
  25.             0,2,1,1,2,3
  26.         };
  27.         m.RecalculateNormals();
  28.         GetComponent<MeshFilter>().mesh = m;
  29.  
  30.         for (int i = 0; i < l; i++)
  31.             AddQuad();
  32.     }
  33.  
  34.     void AddQuad()
  35.     {
  36.         int vindex = m.vertices.Length-1;
  37.         int tindex = m.triangles.Length-1;
  38.  
  39.         //Debug.Log(vindex+"="+m.vertices[vindex]);
  40.  
  41.         Vector3[] new_vertices = new Vector3[m.vertices.Length+2];
  42.         int[] new_triangles = new int[m.triangles.Length+6];
  43.  
  44.         m.vertices.CopyTo(new_vertices,0);
  45.         m.triangles.CopyTo(new_triangles,0);
  46.  
  47.         new_vertices[vindex+1] = m.vertices[vindex-1]+transform.forward*h;
  48.         new_vertices[vindex+2] = m.vertices[vindex]+transform.forward*h;
  49.  
  50.         new_triangles[tindex+1] = m.triangles[tindex-1];
  51.         new_triangles[tindex+2] = m.triangles[tindex]+1;
  52.         new_triangles[tindex+3] = m.triangles[tindex];
  53.  
  54.         new_triangles[tindex+4] = m.triangles[tindex];
  55.         new_triangles[tindex+5] = m.triangles[tindex]+1;
  56.         new_triangles[tindex+6] = m.triangles[tindex]+2;
  57.  
  58.         //Debug.Log(tindex+6+" triangle = "+(m.triangles[tindex]+2).ToString() );
  59.         //Debug.Log(tindex+6+" vertex = "+new_vertices[m.triangles[tindex]+2] );
  60.  
  61.         string t = "";
  62.         foreach(int i in new_triangles)
  63.             t += " "+i.ToString();
  64.         Debug.Log(t);
  65.  
  66.         t = "";
  67.         foreach(Vector3 v in new_vertices)
  68.             t += " "+v.ToString();
  69.         Debug.Log(t);
  70.  
  71.         m.vertices = new_vertices;
  72.         m.triangles = new_triangles;
  73.  
  74.         m.RecalculateNormals();
  75.  
  76.         /*Vector3[] normals = m.normals;
  77.         int x;
  78.         for (x = 0; x < normals.Length; x++)
  79.             normals[x] = Vector3.up;
  80.         m.normals = normals;
  81.         */
  82.  
  83.         GetComponent<MeshFilter>().mesh = m;
  84.     }
  85.  
  86.     // Update is called once per frame
  87.     void Update () {
  88.    
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement