using System.Collections.Generic; using UnityEngine; [AddComponentMenu("MegaShapes/Page Mesh")] [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] public class MegaMeshPage : MonoBehaviour { public float Width = 1f; public float Length = 1.41f; public float Height = 0.1f; public int WidthSegs = 10; public int LengthSegs = 10; public int HeightSegs = 1; public bool genUVs = true; public float rotate; public bool PivotBase; public bool PivotEdge = true; public bool tangents; public bool optimize; [ContextMenu("Help")] public void Help() { Application.OpenURL("http://www.west-racing.com/mf/?page_id=853"); } private void Reset() { Rebuild(); } public void Rebuild() { MeshFilter component = GetComponent(); if (!(component != null)) { return; } Mesh mesh = component.sharedMesh; if (mesh == null) { mesh = (component.sharedMesh = new Mesh()); } if (mesh != null) { BuildMesh(mesh); MegaModifyObject component2 = GetComponent(); if (component2 != null) { component2.MeshUpdated(); } } } private static void MakeQuad1(List f, int a, int b, int c, int d) { f.Add(a); f.Add(b); f.Add(c); f.Add(c); f.Add(d); f.Add(a); } private int MaxComponent(Vector3 v) { if (Mathf.Abs(v.x) > Mathf.Abs(v.y)) { if (Mathf.Abs(v.x) > Mathf.Abs(v.z)) { return 0; } return 2; } if (Mathf.Abs(v.y) > Mathf.Abs(v.z)) { return 1; } return 2; } private void BuildMesh(Mesh mesh) { Width = Mathf.Clamp(Width, 0f, float.MaxValue); Length = Mathf.Clamp(Length, 0f, float.MaxValue); Height = Mathf.Clamp(Height, 0f, float.MaxValue); LengthSegs = Mathf.Clamp(LengthSegs, 1, 200); HeightSegs = Mathf.Clamp(HeightSegs, 1, 200); WidthSegs = Mathf.Clamp(WidthSegs, 1, 200); Vector3 vector = new Vector3(Width, Height, Length) / 2f; Vector3 vector2 = -vector; if (PivotBase) { vector2.y = 0f; vector.y = Height; } if (PivotEdge) { vector2.x = 0f; vector.x = Width; } float num = Width / (float)WidthSegs; float num2 = Height / (float)HeightSegs; float num3 = Length / (float)LengthSegs; Vector3 item = vector2; List list = new List(); List list2 = new List(); List list3 = new List(); List list4 = new List(); List list5 = new List(); Vector2 zero = Vector2.zero; if (Width > 0f && Length > 0f) { Matrix4x4 matrix4x = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, rotate, 0f), Vector3.one); Vector3 point = Vector3.zero; item.y = vector.y; for (int i = 0; i <= LengthSegs; i++) { item.x = vector2.x; for (int j = 0; j <= WidthSegs; j++) { list.Add(item); if (genUVs) { zero.x = (item.x - vector2.x) / Width; zero.y = (item.z + vector.z) / Length; point.x = zero.x - 0.5f; point.y = 0f; point.z = zero.y - 0.5f; point = matrix4x.MultiplyPoint3x4(point); zero.x = 0.5f + point.x; zero.y = 0.5f + point.z; list2.Add(zero); } item.x += num; } item.z += num3; } for (int k = 0; k < LengthSegs; k++) { int num4 = k * (WidthSegs + 1); for (int l = 0; l < WidthSegs; l++) { MakeQuad1(list3, num4, num4 + WidthSegs + 1, num4 + WidthSegs + 2, num4 + 1); num4++; } } int count = list.Count; item.y = vector2.y; item.z = vector2.z; for (int m = 0; m <= LengthSegs; m++) { item.x = vector2.x; for (int n = 0; n <= WidthSegs; n++) { list.Add(item); if (genUVs) { zero.x = 1f - (item.x + vector.x) / Width; zero.y = (item.z + vector.z) / Length; point.x = zero.x - 0.5f; point.y = 0f; point.z = zero.y - 0.5f; point = matrix4x.MultiplyPoint3x4(point); zero.x = 0.5f + point.x; zero.y = 0.5f + point.z; list2.Add(zero); } item.x += num; } item.z += num3; } for (int num5 = 0; num5 < LengthSegs; num5++) { int num6 = num5 * (WidthSegs + 1) + count; for (int num7 = 0; num7 < WidthSegs; num7++) { MakeQuad1(list4, num6, num6 + 1, num6 + WidthSegs + 2, num6 + WidthSegs + 1); num6++; } } } if (Width > 0f && Height > 0f) { int count2 = list.Count; item.z = vector2.z; item.y = vector2.y; for (int num8 = 0; num8 <= HeightSegs; num8++) { item.x = vector2.x; for (int num9 = 0; num9 <= WidthSegs; num9++) { list.Add(item); if (genUVs) { zero.x = (item.x + vector.x) / Width; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.x += num; } item.y += num2; } for (int num10 = 0; num10 < HeightSegs; num10++) { int num11 = num10 * (WidthSegs + 1) + count2; for (int num12 = 0; num12 < WidthSegs; num12++) { MakeQuad1(list5, num11, num11 + WidthSegs + 1, num11 + WidthSegs + 2, num11 + 1); num11++; } } count2 = list.Count; item.z = vector.z; item.y = vector2.y; for (int num13 = 0; num13 <= HeightSegs; num13++) { item.x = vector2.x; for (int num14 = 0; num14 <= WidthSegs; num14++) { list.Add(item); if (genUVs) { zero.x = (item.x + vector.x) / Width; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.x += num; } item.y += num2; } for (int num15 = 0; num15 < HeightSegs; num15++) { int num16 = num15 * (WidthSegs + 1) + count2; for (int num17 = 0; num17 < WidthSegs; num17++) { MakeQuad1(list5, num16, num16 + 1, num16 + WidthSegs + 2, num16 + WidthSegs + 1); num16++; } } } if (Length > 0f && Height > 0f) { int count3 = list.Count; item.x = vector.x; item.y = vector2.y; for (int num18 = 0; num18 <= HeightSegs; num18++) { item.z = vector2.z; for (int num19 = 0; num19 <= LengthSegs; num19++) { list.Add(item); if (genUVs) { zero.x = (item.z + vector.z) / Length; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.z += num3; } item.y += num2; } for (int num20 = 0; num20 < HeightSegs; num20++) { int num21 = num20 * (LengthSegs + 1) + count3; for (int num22 = 0; num22 < LengthSegs; num22++) { MakeQuad1(list5, num21, num21 + LengthSegs + 1, num21 + LengthSegs + 2, num21 + 1); num21++; } } count3 = list.Count; item.x = vector2.x; item.y = vector2.y; for (int num23 = 0; num23 <= HeightSegs; num23++) { item.z = vector2.z; for (int num24 = 0; num24 <= LengthSegs; num24++) { list.Add(item); if (genUVs) { zero.x = (item.z + vector.z) / Length; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.z += num3; } item.y += num2; } for (int num25 = 0; num25 < HeightSegs; num25++) { int num26 = num25 * (LengthSegs + 1) + count3; for (int num27 = 0; num27 < LengthSegs; num27++) { MakeQuad1(list5, num26, num26 + 1, num26 + LengthSegs + 2, num26 + LengthSegs + 1); num26++; } } } mesh.Clear(); mesh.subMeshCount = 3; mesh.vertices = list.ToArray(); mesh.uv = list2.ToArray(); mesh.SetTriangles(list3.ToArray(), 0); mesh.SetTriangles(list4.ToArray(), 1); mesh.SetTriangles(list5.ToArray(), 2); mesh.RecalculateNormals(); if (tangents) { MegaUtils.BuildTangents(mesh); } if (optimize) { } mesh.RecalculateBounds(); } private void BuildMeshOld(Mesh mesh) { Width = Mathf.Clamp(Width, 0f, float.MaxValue); Length = Mathf.Clamp(Length, 0f, float.MaxValue); Height = Mathf.Clamp(Height, 0f, float.MaxValue); LengthSegs = Mathf.Clamp(LengthSegs, 1, 200); HeightSegs = Mathf.Clamp(HeightSegs, 1, 200); WidthSegs = Mathf.Clamp(WidthSegs, 1, 200); Vector3 vector = new Vector3(Width, Height, Length) / 2f; Vector3 vector2 = -vector; if (PivotBase) { vector2.y = 0f; vector.y = Height; } if (PivotEdge) { vector2.x = 0f; vector.x = Width; } float num = Width / (float)WidthSegs; float num2 = Height / (float)HeightSegs; float num3 = Length / (float)LengthSegs; Vector3 item = vector2; List list = new List(); List list2 = new List(); List list3 = new List(); List list4 = new List(); List list5 = new List(); Vector2 zero = Vector2.zero; if (Width > 0f && Length > 0f) { item.y = vector.y; for (int i = 0; i <= LengthSegs; i++) { item.x = vector2.x; for (int j = 0; j <= WidthSegs; j++) { list.Add(item); if (genUVs) { zero.x = (item.x + vector.x) / Width; zero.y = (item.z + vector.z) / Length; list2.Add(zero); } item.x += num; } item.z += num3; } for (int k = 0; k < LengthSegs; k++) { int num4 = k * (WidthSegs + 1); for (int l = 0; l < WidthSegs; l++) { MakeQuad1(list3, num4, num4 + WidthSegs + 1, num4 + WidthSegs + 2, num4 + 1); num4++; } } int count = list.Count; item.y = vector2.y; item.z = vector2.z; for (int m = 0; m <= LengthSegs; m++) { item.x = vector2.x; for (int n = 0; n <= WidthSegs; n++) { list.Add(item); if (genUVs) { zero.x = 1f - (item.x + vector.x) / Width; zero.y = (item.z + vector.z) / Length; list2.Add(zero); } item.x += num; } item.z += num3; } for (int num5 = 0; num5 < LengthSegs; num5++) { int num6 = num5 * (WidthSegs + 1) + count; for (int num7 = 0; num7 < WidthSegs; num7++) { MakeQuad1(list4, num6, num6 + 1, num6 + WidthSegs + 2, num6 + WidthSegs + 1); num6++; } } } if (Width > 0f && Height > 0f) { int count2 = list.Count; item.z = vector2.z; item.y = vector2.y; for (int num8 = 0; num8 <= HeightSegs; num8++) { item.x = vector2.x; for (int num9 = 0; num9 <= WidthSegs; num9++) { list.Add(item); if (genUVs) { zero.x = (item.x + vector.x) / Width; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.x += num; } item.y += num2; } for (int num10 = 0; num10 < HeightSegs; num10++) { int num11 = num10 * (WidthSegs + 1) + count2; for (int num12 = 0; num12 < WidthSegs; num12++) { MakeQuad1(list5, num11, num11 + WidthSegs + 1, num11 + WidthSegs + 2, num11 + 1); num11++; } } count2 = list.Count; item.z = vector.z; item.y = vector2.y; for (int num13 = 0; num13 <= HeightSegs; num13++) { item.x = vector2.x; for (int num14 = 0; num14 <= WidthSegs; num14++) { list.Add(item); if (genUVs) { zero.x = (item.x + vector.x) / Width; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.x += num; } item.y += num2; } for (int num15 = 0; num15 < HeightSegs; num15++) { int num16 = num15 * (WidthSegs + 1) + count2; for (int num17 = 0; num17 < WidthSegs; num17++) { MakeQuad1(list5, num16, num16 + 1, num16 + WidthSegs + 2, num16 + WidthSegs + 1); num16++; } } } if (Length > 0f && Height > 0f) { int count3 = list.Count; item.x = vector.x; item.y = vector2.y; for (int num18 = 0; num18 <= HeightSegs; num18++) { item.z = vector2.z; for (int num19 = 0; num19 <= LengthSegs; num19++) { list.Add(item); if (genUVs) { zero.x = (item.z + vector.z) / Length; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.z += num3; } item.y += num2; } for (int num20 = 0; num20 < HeightSegs; num20++) { int num21 = num20 * (LengthSegs + 1) + count3; for (int num22 = 0; num22 < LengthSegs; num22++) { MakeQuad1(list5, num21, num21 + LengthSegs + 1, num21 + LengthSegs + 2, num21 + 1); num21++; } } count3 = list.Count; item.x = vector2.x; item.y = vector2.y; for (int num23 = 0; num23 <= HeightSegs; num23++) { item.z = vector2.z; for (int num24 = 0; num24 <= LengthSegs; num24++) { list.Add(item); if (genUVs) { zero.x = (item.z + vector.z) / Length; zero.y = (item.y + vector.y) / Height; list2.Add(zero); } item.z += num3; } item.y += num2; } for (int num25 = 0; num25 < HeightSegs; num25++) { int num26 = num25 * (LengthSegs + 1) + count3; for (int num27 = 0; num27 < LengthSegs; num27++) { MakeQuad1(list5, num26, num26 + 1, num26 + LengthSegs + 2, num26 + LengthSegs + 1); num26++; } } } mesh.Clear(); mesh.subMeshCount = 3; mesh.vertices = list.ToArray(); mesh.uv = list2.ToArray(); mesh.SetTriangles(list3.ToArray(), 0); mesh.SetTriangles(list4.ToArray(), 1); mesh.SetTriangles(list5.ToArray(), 2); mesh.RecalculateNormals(); if (tangents) { MegaUtils.BuildTangents(mesh); } if (optimize) { } mesh.RecalculateBounds(); } }