123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class Vertex : MonoBehaviour
- {
- public int meshVertexIndex = -1;
- public bool isSelected;
- public List<Vertex> neighbours;
- public Vertex assignNeighbour;
- [EasyButtons.Button]
- void bindSelected(){
- bindSelected();
- }
- [EasyButtons.Button]
- void addNeighbour(){
- AddNeighbour();
- }
- [MenuItem("Vertex/bind_selected #b")]
- static void bind_selected()
- {
- List<Vertex> selectedVerticies = new List<Vertex>();
- foreach (Object obj in Selection.objects)
- {
- try
- {
- selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
- }
- catch
- {
- }
- }
- foreach (Vertex vertex in selectedVerticies)
- {
- foreach (Vertex newNeighbour in selectedVerticies)
- {
- if (newNeighbour == vertex) { continue; }
- if (!vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Add(newNeighbour); }
- if (!newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Add(vertex); }
- }
- }
- }
- [MenuItem("Vertex/unbind_selected #u")]
- static void unbind_selected()
- {
- List<Vertex> selectedVerticies = new List<Vertex>();
- foreach (Object obj in Selection.objects)
- {
- try
- {
- selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
- }
- catch
- {
- }
- }
- foreach (Vertex vertex in selectedVerticies)
- {
- foreach (Vertex newNeighbour in selectedVerticies)
- {
- if (newNeighbour == vertex) { continue; }
- if (vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Remove(newNeighbour); }
- if (newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Remove(vertex); }
- }
- }
- }
- [MenuItem("Vertex/new_face #m")]
- static void newFace()
- {
- List<Vertex> selectedVerticies = new List<Vertex>();
- foreach (Object obj in Selection.objects)
- {
- try
- {
- selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
- }
- catch
- {
- }
- }
- if(selectedVerticies.Count < 3){Debug.LogError("Not enough verticies to make a face!");}
- MeshFilter meshFilter = selectedVerticies[0].transform.parent.GetComponent<MeshFilter>();
- if(meshFilter==null){
- meshFilter = selectedVerticies[0].transform.parent.gameObject.AddComponent<MeshFilter>();
- }
- MeshRenderer meshRenderer = selectedVerticies[0].transform.parent.GetComponent<MeshRenderer>();
- if(meshRenderer==null){
- meshRenderer = selectedVerticies[0].transform.parent.gameObject.AddComponent<MeshRenderer>();
- }
- // Mesh mesh = new Mesh();
- // mesh.vertices = new Vector3[selectedVerticies.Count];
- // for(int i =0; i < selectedVerticies.Count; i++){
- // mesh.vertices[i] = selectedVerticies[i].transform.position;
- // }
- // meshFilter.sharedMesh = mesh;
- // meshFilter.sharedMesh.RecalculateBounds();
- // meshFilter.sharedMesh.RecalculateNormals();
- meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
- Mesh mesh = new Mesh();
- Vector3[] vertices = new Vector3[4];
- for(int i =0; i < 4; i++){
- vertices[i] = selectedVerticies[i].transform.position;
- selectedVerticies[i].meshVertexIndex=i;
- }
- mesh.vertices = vertices;
- int[] tris = new int[6]
- {
- // lower left triangle
- 0, 2, 1,
- // upper right triangle
- 2, 3, 1
- };
- mesh.triangles = tris;
- Vector3[] normals = new Vector3[4]
- {
- -Vector3.forward,
- -Vector3.forward,
- -Vector3.forward,
- -Vector3.forward
- };
- mesh.normals = normals;
- Vector2[] uv = new Vector2[4]
- {
- new Vector2(0, 0),
- new Vector2(1, 0),
- new Vector2(0, 1),
- new Vector2(1, 1)
- };
- mesh.uv = uv;
- meshFilter.mesh = mesh;
- }
- [MenuItem("Vertex/add_neighbour #e")]
- static void AddNeighbour()
- {
- List<Vertex> selectedVerticies = new List<Vertex>();
- foreach (Object obj in Selection.objects)
- {
- Debug.Log(obj.name);
- try
- {
- selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
- }
- catch
- {
- }
- }
- if (selectedVerticies.Count == 1)
- {
- GameObject go = Instantiate(selectedVerticies[0].gameObject, selectedVerticies[0].transform.position, Quaternion.identity);
- if (selectedVerticies[0].neighbours == null) { selectedVerticies[0].neighbours = new List<Vertex>(); }
- selectedVerticies[0].neighbours.Add(go.GetComponent<Vertex>());
- go.GetComponent<Vertex>().neighbours = new List<Vertex>();
- go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[0]);
- go.transform.parent = selectedVerticies[0].transform.parent;
- go.transform.name = selectedVerticies[0].transform.name;
- UnityEditor.Selection.activeGameObject = go;
- }
- else if (selectedVerticies.Count > 1)
- {
- Object[] selections = new Object[selectedVerticies.Count];
- for (int i = 0; i < selectedVerticies.Count; i++)
- {
- GameObject go = Instantiate(selectedVerticies[i].gameObject, selectedVerticies[i].transform.position, Quaternion.identity);
- if (selectedVerticies[i].neighbours == null) { selectedVerticies[i].neighbours = new List<Vertex>(); }
- selectedVerticies[i].neighbours.Add(go.GetComponent<Vertex>());
- go.GetComponent<Vertex>().neighbours = new List<Vertex>();
- go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[i]);
- go.transform.parent = selectedVerticies[i].transform.parent;
- go.transform.name = selectedVerticies[i].transform.name;
- selections[i] = go;
- }
- Selection.objects = selections;
- }
- else
- {
- Debug.LogError("Please select single vertex to add new neighbour!");
- }
- }
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- void OnDrawGizmos()
- {
- isSelected = false;
- foreach (Object obj in UnityEditor.Selection.objects)
- {
- if ((GameObject)obj == gameObject)
- {
- isSelected = true;
- break;
- }
- }
- Gizmos.color = (isSelected) ? Color.green : Color.red;
- Gizmos.DrawSphere(transform.position, 0.1f);
- foreach (Vertex neighbour in neighbours)
- {
- if (neighbour == null)
- {
- neighbours.Remove(neighbour);
- }
- if (!neighbour.neighbours.Contains(this))
- {
- neighbour.neighbours.Add(this);
- }
- Gizmos.color = (isSelected || neighbour.isSelected) ? Color.green : Color.red; Gizmos.DrawLine(transform.position, neighbour.transform.position);
- }
- MeshFilter meshFilter = transform.parent.GetComponent<MeshFilter>();
- if(meshFilter!=null && meshVertexIndex >=0){
- Vector3[] vertices = meshFilter.mesh.vertices;
- for(int i =0; i < vertices.Length; i++){
- if(i == meshVertexIndex){
- vertices[i] = transform.position;
- }
- }
- meshFilter.mesh.vertices = vertices;
- meshFilter.sharedMesh.RecalculateNormals();
- // Debug.Log("Changing mesh");
- }
- }
- void OnValidate()
- {
- if (assignNeighbour != null)
- {
- if (!neighbours.Contains(assignNeighbour)) { neighbours.Add(assignNeighbour); }
- if (!assignNeighbour.neighbours.Contains(this)) { assignNeighbour.neighbours.Add(this); }
- assignNeighbour = null;
- }
- }
- }
|