void Start() { // some sample quad planes of various sizes and descriptions { var mqp = new QuadPlaneDef(2, 2); GameObject go = MakeQuadPlane.Create(mqp); go.transform.position = new Vector3(-3, 0, 0); go.GetComponent <MeshRenderer>().material = mtl; } { var mqp = new QuadPlaneDef(4, 10, new Vector3(3.0f, 1.0f, 4.0f)); // we'll back this one's texture coordinates a bit tighter mqp.UVScale = Vector2.one * 3.0f; GameObject go = MakeQuadPlane.Create(mqp); go.transform.position = new Vector3(0, 0, 0); go.GetComponent <MeshRenderer>().material = mtl; } { var mqp = new QuadPlaneDef(40, 80, new Vector3(3.2f, 0.15f, 6.2f)); // we'll make this one "interesting" with intermingling sine waves mqp.HeightFunction = (v2) => { // these constants control how fast the // sine wave repeats in each axis return(Mathf.Sin(v2.x * 5.0f) + Mathf.Sin(v2.x * 2.0f + v2.y * 15.0f)); }; GameObject go = MakeQuadPlane.Create(mqp); go.transform.position = new Vector3(4, 0, 0); go.GetComponent <MeshRenderer>().material = mtl; } }
public static GameObject Create(QuadPlaneDef qpd) { GameObject go = new GameObject("MakeQuadPlane:" + qpd.ToString()); Mesh mesh = new Mesh(); using (var vh = new VertexHelper()) { for (int j = 0; j < qpd.zCells; j++) { for (int i = 0; i < qpd.xCells; i++) { Vector2 pos = new Vector2( (i * qpd.WorldSize.x) / (qpd.xCells - 1), (j * qpd.WorldSize.z) / (qpd.zCells - 1)); float height = 0.0f; if (qpd.HeightFunction != null) { height = qpd.HeightFunction(pos); } height *= qpd.WorldSize.y; UIVertex vtx = new UIVertex(); vtx.position = new Vector3(pos.x, height, pos.y); vtx.uv0 = new Vector2( (i / (qpd.xCells - 1.0f)) * qpd.UVScale.x, (j / (qpd.zCells - 1.0f)) * qpd.UVScale.y); vh.AddVert(vtx); } } for (int j = 0; j < qpd.zCells - 1; j++) { for (int i = 0; i < qpd.xCells - 1; i++) { int n = j * qpd.xCells + i; vh.AddTriangle(n, n + qpd.xCells, n + 1); vh.AddTriangle(n + 1, n + qpd.xCells, n + qpd.xCells + 1); } } vh.FillMesh(mesh); } mesh.RecalculateBounds(); mesh.RecalculateNormals(); MeshFilter mf = go.AddComponent <MeshFilter>(); mf.mesh = mesh; MeshRenderer mr = go.AddComponent <MeshRenderer>(); return(go); }