Tanoda
MeshInfo.cs
Go to the documentation of this file.
1using System.Collections;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using NaughtyAttributes;
6using UnityEngine;
8
9[RequireComponent(typeof(MeshFilter))]
10[RequireComponent(typeof(MeshCollider))] // because of controling gameobject after changing its shape (rotating and draging with mouse)
11public class MeshInfo : MonoBehaviour
12{
13 private Mesh m_Mesh; // a reference to attached mesh of MeshFilter
14
15 private List<Vector3> m_Vertices; // a copy of m_Mesh.vertices.ToList()
16
18
19 void Start()
20 {
21 if (m_MiniCubePrefab == null)
22 {
23 m_MiniCubePrefab = GameObject.Find("ModelEditorRoot").transform.Find("Store").transform.Find("Vertex Editor").GetComponent<VertexEditor>();
24 m_MiniCubePrefab.gameObject.SetActive(true);
25 GameObject.Find("Store")?.SetActive(false);
26 }
27 m_Mesh = GetComponent<MeshFilter>().mesh;
28
29 // make ready to edit mesh
30 m_Vertices = GetMeshVertices();
31 GenerateVerticesEditorObjects();
32 }
33
39 public void SetVertex(List<int> vertexIndex, Vector3 position)
40 {
41 for (int i = 0; i < vertexIndex.Count; i++)
42 m_Vertices[vertexIndex[i]] = position;
43
44 ModelEditorLogicManager.instance.SetVertex(vertexIndex, position);
45
46 m_Mesh.vertices = m_Vertices.ToArray();
47 m_Mesh.RecalculateNormals();
48 }
49
50 private List<Vector3> GetMeshVertices()
51 {
52 return m_Mesh.vertices.ToList();
53 }
54
55 // needs to fix
56 private void GenerateVerticesEditorObjects()
57 {
58 // tip: we don't need to have any reference to instantiated mini cubes
59 // because the cube is independent itself
60
61 // if mesh has no vertices, it will cause an error here. we should check it here,
62 // not in anywhere else (because maybe forget to check)
63 if (m_Mesh.vertices.Length < 1)
64 return;
65
66 // i need have a reference to entire generated VertexEditors till the end of this method's scope
67 List<VertexEditor> vertexEditors = new List<VertexEditor>();
68
69 // the first one is going to created always
70 vertexEditors.Add(Instantiate(m_MiniCubePrefab, transform));
71 vertexEditors[0].transform.localPosition = m_Vertices[0];
72 vertexEditors[0].AddVertexIndex(0);
73
74 for (int i = 1; i < m_Vertices.Count; i++)
75 {
76 int index = GetVertexPositionIndex(m_Vertices[i], vertexEditors);
77
78 if (index >= 0)
79 {
80 vertexEditors[index].AddVertexIndex(i);
81 }
82 else
83 {
84 vertexEditors.Add(Instantiate(m_MiniCubePrefab, transform));
85
86 int last = vertexEditors.Count - 1;
87 vertexEditors[last].transform.localPosition = m_Vertices[i];
88 vertexEditors[last].AddVertexIndex(i);
89 }
90 }
91 }
92
99 private int GetVertexPositionIndex(Vector3 vertexPosition, List<VertexEditor> list)
100 {
101 for (int i = 0; i < list.Count; i++)
102 if (list[i].transform.localPosition == vertexPosition)
103 return i;
104
105 return -1; // it means the search didn't found any result
106 }
107
108 [Button]
109 private void ExportFBX()
110 {var allVE = FindObjectsOfType<VertexEditor>();
111 foreach (var ve in allVE)
112 {
113 ve.gameObject.SetActive(false);
114 }
115 FBXExporter.ExportGameObjToFBX(gameObject, Path.Combine(Application.streamingAssetsPath, "..\\..\\", "teszt.fbx"), true, true);
116 foreach (var ve in allVE)
117 {
118 ve.gameObject.SetActive(true);
119 }
120 }
121
122 public void TestSave()
123 {
124 var allVE = FindObjectsOfType<VertexEditor>();
125 foreach (var ve in allVE)
126 {
127 ve.gameObject.SetActive(false);
128 }
130 foreach (var ve in allVE)
131 {
132 ve.gameObject.SetActive(true);
133 }
134 }
135}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
VertexEditor m_MiniCubePrefab
Definition: MeshInfo.cs:17
void SetVertex(List< int > vertexIndex, Vector3 position)
After moving a VertexEditor, it should change all vertices position
Definition: MeshInfo.cs:39
void TestSave()
Definition: MeshInfo.cs:122
static ModelEditorLogicManager instance
void SetVertex(List< int > vertexIndex, Vector3 position)
static bool ExportGameObjToFBX(GameObject gameObj, string newPath, bool copyMaterials=false, bool copyTextures=false)
Definition: FBXExporter.cs:41
static string ExportGameObjToFBXString(GameObject gameObj)
Definition: FBXExporter.cs:90