Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class ProceduralPlane : MonoBehaviour {
- public float h = 2.0f;
- public float w = 2.0f;
- public int l = 3;
- public Mesh m;
- // Use this for initialization
- void Start () {
- m = new Mesh();
- m.vertices = new Vector3[]
- {
- -transform.right*w, //left
- transform.right*w, //right
- -transform.right*w + transform.forward*h, //top left
- transform.right*w + transform.forward*h, //top right
- };
- m.triangles = new int[]
- {
- 0,2,1,1,2,3
- };
- m.RecalculateNormals();
- GetComponent<MeshFilter>().mesh = m;
- for (int i = 0; i < l; i++)
- AddQuad();
- }
- void AddQuad()
- {
- int vindex = m.vertices.Length-1;
- int tindex = m.triangles.Length-1;
- //Debug.Log(vindex+"="+m.vertices[vindex]);
- Vector3[] new_vertices = new Vector3[m.vertices.Length+2];
- int[] new_triangles = new int[m.triangles.Length+6];
- m.vertices.CopyTo(new_vertices,0);
- m.triangles.CopyTo(new_triangles,0);
- new_vertices[vindex+1] = m.vertices[vindex-1]+transform.forward*h;
- new_vertices[vindex+2] = m.vertices[vindex]+transform.forward*h;
- new_triangles[tindex+1] = m.triangles[tindex-1];
- new_triangles[tindex+2] = m.triangles[tindex]+1;
- new_triangles[tindex+3] = m.triangles[tindex];
- new_triangles[tindex+4] = m.triangles[tindex];
- new_triangles[tindex+5] = m.triangles[tindex]+1;
- new_triangles[tindex+6] = m.triangles[tindex]+2;
- //Debug.Log(tindex+6+" triangle = "+(m.triangles[tindex]+2).ToString() );
- //Debug.Log(tindex+6+" vertex = "+new_vertices[m.triangles[tindex]+2] );
- string t = "";
- foreach(int i in new_triangles)
- t += " "+i.ToString();
- Debug.Log(t);
- t = "";
- foreach(Vector3 v in new_vertices)
- t += " "+v.ToString();
- Debug.Log(t);
- m.vertices = new_vertices;
- m.triangles = new_triangles;
- m.RecalculateNormals();
- /*Vector3[] normals = m.normals;
- int x;
- for (x = 0; x < normals.Length; x++)
- normals[x] = Vector3.up;
- m.normals = normals;
- */
- GetComponent<MeshFilter>().mesh = m;
- }
- // Update is called once per frame
- void Update () {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement