| | 1 | | using UnityEngine; |
| | 2 | | using UnityEditor; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | |
|
| | 6 | | #if UNITY_EDITOR |
| | 7 | | public class GenerateCone : EditorWindow { |
| 0 | 8 | | private int sides = 16; |
| 0 | 9 | | private float baseRadius = 1f; |
| 0 | 10 | | private float height = 2f; |
| | 11 | |
|
| | 12 | | [MenuItem("GameObject/3D Object/Cone", false, 10)] |
| 0 | 13 | | static void CreateCone() { |
| | 14 | | GameObject cone; |
| 0 | 15 | | GameObject selectedObject = Selection.activeGameObject; |
| | 16 | |
|
| 0 | 17 | | if (selectedObject != null) { |
| | 18 | | // Create as child of selected object |
| 0 | 19 | | cone = new GameObject("Cone"); |
| 0 | 20 | | cone.transform.SetParent(selectedObject.transform, false); |
| 0 | 21 | | } else { |
| | 22 | | // Create as new root object |
| 0 | 23 | | cone = new GameObject("Cone"); |
| 0 | 24 | | } |
| | 25 | |
|
| 0 | 26 | | cone.AddComponent<MeshFilter>(); |
| 0 | 27 | | cone.AddComponent<MeshRenderer>(); |
| 0 | 28 | | Undo.RegisterCreatedObjectUndo(cone, "Create Cone"); |
| | 29 | |
|
| 0 | 30 | | var window = ScriptableObject.CreateInstance<GenerateCone>(); |
| 0 | 31 | | window.GenerateConeObject(cone); |
| | 32 | |
|
| 0 | 33 | | Selection.activeGameObject = cone; |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | void GenerateConeObject(GameObject cone) { |
| 0 | 37 | | Mesh mesh = |
| | 38 | | CreateConeMesh("ConeMesh", sides, Vector3.zero, Quaternion.identity, baseRadius, height); |
| | 39 | |
|
| | 40 | | // Save the mesh as an asset |
| 0 | 41 | | string path = "Assets/Meshes"; |
| 0 | 42 | | if (!AssetDatabase.IsValidFolder(path)) { |
| 0 | 43 | | AssetDatabase.CreateFolder("Assets", "Meshes"); |
| 0 | 44 | | } |
| 0 | 45 | | string assetPath = AssetDatabase.GenerateUniqueAssetPath(path + "/ConeMesh.asset"); |
| 0 | 46 | | AssetDatabase.CreateAsset(mesh, assetPath); |
| 0 | 47 | | AssetDatabase.SaveAssets(); |
| | 48 | |
|
| | 49 | | // Assign the mesh to the MeshFilter |
| 0 | 50 | | cone.GetComponent<MeshFilter>().sharedMesh = mesh; |
| 0 | 51 | | cone.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Standard")); |
| 0 | 52 | | } |
| | 53 | |
|
| 0 | 54 | | Vector2[] GetBasePoints(int vertices, float radius) { |
| | 55 | | const float TAU = 2f * Mathf.PI; |
| 0 | 56 | | var pts = new Vector2[vertices]; |
| 0 | 57 | | var step = TAU / vertices; // angular step between two vertices |
| 0 | 58 | | for (int i = 0; i < vertices; i++) { |
| 0 | 59 | | pts[i] = radius * Trig(i * step); // convert polar coordinate to cartesian space |
| 0 | 60 | | } |
| 0 | 61 | | return pts; |
| 0 | 62 | | } |
| | 63 | |
|
| 0 | 64 | | static Vector2 Trig(float rad) => new Vector2(Mathf.Cos(rad), Mathf.Sin(rad)); |
| | 65 | |
|
| 0 | 66 | | Vector3[] BuildConeVertices(Vector2[] baseVerts, float coneHeight) { |
| 0 | 67 | | if (baseVerts == null || baseVerts.Length < 3) |
| 0 | 68 | | throw new InvalidOperationException("Requires at least 3 base vertices."); |
| 0 | 69 | | var verts = new Vector3[baseVerts.Length + 1]; |
| 0 | 70 | | verts[0] = new Vector3(0f, coneHeight, 0f); |
| 0 | 71 | | for (int i = 0; i < baseVerts.Length; i++) { |
| 0 | 72 | | verts[i + 1] = new Vector3(baseVerts[i].x, 0f, baseVerts[i].y); |
| 0 | 73 | | } |
| 0 | 74 | | return verts; |
| 0 | 75 | | } |
| | 76 | |
|
| 0 | 77 | | void ConstructCone(Vector3[] coneVerts, List<Vector3> finalVerts, List<int> triangles) { |
| 0 | 78 | | if (coneVerts == null || coneVerts.Length < 4) |
| 0 | 79 | | throw new InvalidOperationException("Requires at least 4 vertices."); |
| 0 | 80 | | if (finalVerts == null || triangles == null) |
| 0 | 81 | | throw new ArgumentNullException(); |
| | 82 | |
|
| 0 | 83 | | finalVerts.Clear(); |
| 0 | 84 | | triangles.Clear(); |
| | 85 | |
|
| 0 | 86 | | var rimVertices = coneVerts.Length - 1; |
| | 87 | |
|
| | 88 | | // Side faces |
| 0 | 89 | | for (int i = 1; i <= rimVertices; i++) { |
| 0 | 90 | | int a = i, b = i < rimVertices ? i + 1 : 1; |
| 0 | 91 | | AddTriangle(coneVerts[0], coneVerts[b], coneVerts[a]); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | // Base face |
| 0 | 95 | | for (int i = 1; i < rimVertices - 1; i++) { |
| 0 | 96 | | AddTriangle(coneVerts[1], coneVerts[i + 1], coneVerts[i + 2]); |
| 0 | 97 | | } |
| | 98 | |
|
| 0 | 99 | | void AddTriangle(Vector3 t1, Vector3 t2, Vector3 t3) { |
| 0 | 100 | | finalVerts.Add(t1); |
| 0 | 101 | | finalVerts.Add(t2); |
| 0 | 102 | | finalVerts.Add(t3); |
| 0 | 103 | | triangles.Add(finalVerts.Count - 3); |
| 0 | 104 | | triangles.Add(finalVerts.Count - 2); |
| 0 | 105 | | triangles.Add(finalVerts.Count - 1); |
| 0 | 106 | | } |
| 0 | 107 | | } |
| | 108 | | Mesh CreateConeMesh(string name, int sides, Vector3 apex, Quaternion rotation, float baseRadius, |
| 0 | 109 | | float height) { |
| 0 | 110 | | var baseVerts = GetBasePoints(sides, baseRadius); |
| 0 | 111 | | var coneVerts = BuildConeVertices(baseVerts, height); |
| | 112 | |
|
| 0 | 113 | | var verts = new List<Vector3>(); |
| 0 | 114 | | var tris = new List<int>(); |
| 0 | 115 | | ConstructCone(coneVerts, verts, tris); |
| | 116 | |
|
| 0 | 117 | | for (int i = 0; i < verts.Count; i++) { |
| 0 | 118 | | verts[i] = rotation * (verts[i] - coneVerts[0]); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | // Recenter the cone |
| 0 | 122 | | Vector3 center = CalculateCenter(verts); |
| 0 | 123 | | for (int i = 0; i < verts.Count; i++) { |
| 0 | 124 | | verts[i] = verts[i] - center + apex; |
| 0 | 125 | | } |
| | 126 | |
|
| 0 | 127 | | Mesh mesh = new Mesh(); |
| 0 | 128 | | mesh.name = name; |
| 0 | 129 | | mesh.SetVertices(verts); |
| 0 | 130 | | mesh.SetTriangles(tris.ToArray(), 0); |
| 0 | 131 | | mesh.RecalculateNormals(); |
| | 132 | |
|
| 0 | 133 | | return mesh; |
| 0 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | Vector3 CalculateCenter(List<Vector3> vertices) { |
| 0 | 137 | | Vector3 sum = Vector3.zero; |
| 0 | 138 | | foreach (Vector3 vert in vertices) { |
| 0 | 139 | | sum += vert; |
| 0 | 140 | | } |
| 0 | 141 | | return sum / vertices.Count; |
| 0 | 142 | | } |
| | 143 | | } |
| | 144 | | #endif |