Vertex.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class Vertex : MonoBehaviour
  6. {
  7. public int meshVertexIndex = -1;
  8. public bool isSelected;
  9. public List<Vertex> neighbours;
  10. public Vertex assignNeighbour;
  11. [EasyButtons.Button]
  12. void bindSelected(){
  13. bindSelected();
  14. }
  15. [EasyButtons.Button]
  16. void addNeighbour(){
  17. AddNeighbour();
  18. }
  19. [MenuItem("Vertex/bind_selected #b")]
  20. static void bind_selected()
  21. {
  22. List<Vertex> selectedVerticies = new List<Vertex>();
  23. foreach (Object obj in Selection.objects)
  24. {
  25. try
  26. {
  27. selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
  28. }
  29. catch
  30. {
  31. }
  32. }
  33. foreach (Vertex vertex in selectedVerticies)
  34. {
  35. foreach (Vertex newNeighbour in selectedVerticies)
  36. {
  37. if (newNeighbour == vertex) { continue; }
  38. if (!vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Add(newNeighbour); }
  39. if (!newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Add(vertex); }
  40. }
  41. }
  42. }
  43. [MenuItem("Vertex/unbind_selected #u")]
  44. static void unbind_selected()
  45. {
  46. List<Vertex> selectedVerticies = new List<Vertex>();
  47. foreach (Object obj in Selection.objects)
  48. {
  49. try
  50. {
  51. selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
  52. }
  53. catch
  54. {
  55. }
  56. }
  57. foreach (Vertex vertex in selectedVerticies)
  58. {
  59. foreach (Vertex newNeighbour in selectedVerticies)
  60. {
  61. if (newNeighbour == vertex) { continue; }
  62. if (vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Remove(newNeighbour); }
  63. if (newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Remove(vertex); }
  64. }
  65. }
  66. }
  67. [MenuItem("Vertex/new_face #m")]
  68. static void newFace()
  69. {
  70. List<Vertex> selectedVerticies = new List<Vertex>();
  71. foreach (Object obj in Selection.objects)
  72. {
  73. try
  74. {
  75. selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
  76. }
  77. catch
  78. {
  79. }
  80. }
  81. if(selectedVerticies.Count < 3){Debug.LogError("Not enough verticies to make a face!");}
  82. MeshFilter meshFilter = selectedVerticies[0].transform.parent.GetComponent<MeshFilter>();
  83. if(meshFilter==null){
  84. meshFilter = selectedVerticies[0].transform.parent.gameObject.AddComponent<MeshFilter>();
  85. }
  86. MeshRenderer meshRenderer = selectedVerticies[0].transform.parent.GetComponent<MeshRenderer>();
  87. if(meshRenderer==null){
  88. meshRenderer = selectedVerticies[0].transform.parent.gameObject.AddComponent<MeshRenderer>();
  89. }
  90. // Mesh mesh = new Mesh();
  91. // mesh.vertices = new Vector3[selectedVerticies.Count];
  92. // for(int i =0; i < selectedVerticies.Count; i++){
  93. // mesh.vertices[i] = selectedVerticies[i].transform.position;
  94. // }
  95. // meshFilter.sharedMesh = mesh;
  96. // meshFilter.sharedMesh.RecalculateBounds();
  97. // meshFilter.sharedMesh.RecalculateNormals();
  98. meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
  99. Mesh mesh = new Mesh();
  100. Vector3[] vertices = new Vector3[4];
  101. for(int i =0; i < 4; i++){
  102. vertices[i] = selectedVerticies[i].transform.position;
  103. selectedVerticies[i].meshVertexIndex=i;
  104. }
  105. mesh.vertices = vertices;
  106. int[] tris = new int[6]
  107. {
  108. // lower left triangle
  109. 0, 2, 1,
  110. // upper right triangle
  111. 2, 3, 1
  112. };
  113. mesh.triangles = tris;
  114. Vector3[] normals = new Vector3[4]
  115. {
  116. -Vector3.forward,
  117. -Vector3.forward,
  118. -Vector3.forward,
  119. -Vector3.forward
  120. };
  121. mesh.normals = normals;
  122. Vector2[] uv = new Vector2[4]
  123. {
  124. new Vector2(0, 0),
  125. new Vector2(1, 0),
  126. new Vector2(0, 1),
  127. new Vector2(1, 1)
  128. };
  129. mesh.uv = uv;
  130. meshFilter.mesh = mesh;
  131. }
  132. [MenuItem("Vertex/add_neighbour #e")]
  133. static void AddNeighbour()
  134. {
  135. List<Vertex> selectedVerticies = new List<Vertex>();
  136. foreach (Object obj in Selection.objects)
  137. {
  138. Debug.Log(obj.name);
  139. try
  140. {
  141. selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
  142. }
  143. catch
  144. {
  145. }
  146. }
  147. if (selectedVerticies.Count == 1)
  148. {
  149. GameObject go = Instantiate(selectedVerticies[0].gameObject, selectedVerticies[0].transform.position, Quaternion.identity);
  150. if (selectedVerticies[0].neighbours == null) { selectedVerticies[0].neighbours = new List<Vertex>(); }
  151. selectedVerticies[0].neighbours.Add(go.GetComponent<Vertex>());
  152. go.GetComponent<Vertex>().neighbours = new List<Vertex>();
  153. go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[0]);
  154. go.transform.parent = selectedVerticies[0].transform.parent;
  155. go.transform.name = selectedVerticies[0].transform.name;
  156. UnityEditor.Selection.activeGameObject = go;
  157. }
  158. else if (selectedVerticies.Count > 1)
  159. {
  160. Object[] selections = new Object[selectedVerticies.Count];
  161. for (int i = 0; i < selectedVerticies.Count; i++)
  162. {
  163. GameObject go = Instantiate(selectedVerticies[i].gameObject, selectedVerticies[i].transform.position, Quaternion.identity);
  164. if (selectedVerticies[i].neighbours == null) { selectedVerticies[i].neighbours = new List<Vertex>(); }
  165. selectedVerticies[i].neighbours.Add(go.GetComponent<Vertex>());
  166. go.GetComponent<Vertex>().neighbours = new List<Vertex>();
  167. go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[i]);
  168. go.transform.parent = selectedVerticies[i].transform.parent;
  169. go.transform.name = selectedVerticies[i].transform.name;
  170. selections[i] = go;
  171. }
  172. Selection.objects = selections;
  173. }
  174. else
  175. {
  176. Debug.LogError("Please select single vertex to add new neighbour!");
  177. }
  178. }
  179. void Start()
  180. {
  181. }
  182. // Update is called once per frame
  183. void Update()
  184. {
  185. }
  186. void OnDrawGizmos()
  187. {
  188. isSelected = false;
  189. foreach (Object obj in UnityEditor.Selection.objects)
  190. {
  191. if ((GameObject)obj == gameObject)
  192. {
  193. isSelected = true;
  194. break;
  195. }
  196. }
  197. Gizmos.color = (isSelected) ? Color.green : Color.red;
  198. Gizmos.DrawSphere(transform.position, 0.1f);
  199. foreach (Vertex neighbour in neighbours)
  200. {
  201. if (neighbour == null)
  202. {
  203. neighbours.Remove(neighbour);
  204. }
  205. if (!neighbour.neighbours.Contains(this))
  206. {
  207. neighbour.neighbours.Add(this);
  208. }
  209. Gizmos.color = (isSelected || neighbour.isSelected) ? Color.green : Color.red; Gizmos.DrawLine(transform.position, neighbour.transform.position);
  210. }
  211. MeshFilter meshFilter = transform.parent.GetComponent<MeshFilter>();
  212. if(meshFilter!=null && meshVertexIndex >=0){
  213. Vector3[] vertices = meshFilter.mesh.vertices;
  214. for(int i =0; i < vertices.Length; i++){
  215. if(i == meshVertexIndex){
  216. vertices[i] = transform.position;
  217. }
  218. }
  219. meshFilter.mesh.vertices = vertices;
  220. meshFilter.sharedMesh.RecalculateNormals();
  221. // Debug.Log("Changing mesh");
  222. }
  223. }
  224. void OnValidate()
  225. {
  226. if (assignNeighbour != null)
  227. {
  228. if (!neighbours.Contains(assignNeighbour)) { neighbours.Add(assignNeighbour); }
  229. if (!assignNeighbour.neighbours.Contains(this)) { assignNeighbour.neighbours.Add(this); }
  230. assignNeighbour = null;
  231. }
  232. }
  233. }