-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContainer.cs
262 lines (240 loc) · 9.13 KB
/
Container.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using MazeViewer.Maze;
using MazeViewer.UI;
using Unitilities;
using UnityEngine.Serialization;
using Logger = MazeViewer.UI.Logger;
namespace MazeViewer.Viewer
{
public class Container : MonoBehaviour
{
[SerializeField] private List<List<GameObject>> cellObjs;
[SerializeField] private CellFactory cellFactory = default;
[SerializeField] private EffectFactory effectFactory = default;
[SerializeField] private ProgressMgr progressMgr = default;
public float scale;
[FormerlySerializedAs("path")] public string mazePath = "";
public string resultPath = "";
public int maxMergedInOne = 256;
public Material wallMaterial;
[SerializeField] private Transform mergedMeshes = default;
[SerializeField] private GameObject rendererPrefeb = default;
[SerializeField] private PathDrawer pathDrawer = default;
private Vector3 size;
private Vector2Int exitPos;
private void CombineWithNewMesh(MeshFilter target, List<CombineInstance> instances)
{
var originalMesh = target.mesh;
target.mesh = new Mesh();
List<CombineInstance> toCombine = new List<CombineInstance>(instances);
CombineInstance tempInstance = new CombineInstance
{
mesh = originalMesh,
transform = target.transform.localToWorldMatrix
};
toCombine.Add(tempInstance);
target.mesh.CombineMeshes(toCombine.ToArray(), true, true);
}
IEnumerator MergeMesh()
{
var timeWatcher = System.Diagnostics.Stopwatch.StartNew();
int meshMerged = 0;
GameObject targetObj = Instantiate(rendererPrefeb, mergedMeshes);
var targetRenderer = targetObj.GetComponent<MeshRenderer>();
var targetFilter = targetObj.GetComponent<MeshFilter>();
var toAddInstances = new List<CombineInstance>();
targetRenderer.sharedMaterial = wallMaterial;
foreach(var row in cellObjs)
{
// get all walls in row
foreach(var obj in row)
{
if((obj.GetComponent<WallCell>()) != null)
{
CombineInstance tempInstance = new CombineInstance
{
mesh = obj.GetComponent<MeshFilter>().mesh,
transform = obj.transform.localToWorldMatrix
};
toAddInstances.Add(tempInstance);
}
}
meshMerged += toAddInstances.Count;
if(meshMerged < maxMergedInOne)
{
CombineWithNewMesh(targetFilter, toAddInstances);
}
else
{
// combine the instances in range
int inRangeCount = toAddInstances.Count - meshMerged + maxMergedInOne;
List<CombineInstance> instancesInRange =
new List<CombineInstance>(toAddInstances.GetRange(0, inRangeCount));
CombineWithNewMesh(targetFilter, instancesInRange);
// shift to new object, combine the overflowed part
targetObj = Instantiate(rendererPrefeb, mergedMeshes);
targetRenderer = targetObj.GetComponent<MeshRenderer>();
targetRenderer.sharedMaterial = wallMaterial;
targetFilter = targetObj.GetComponent<MeshFilter>();
toAddInstances.RemoveRange(0, inRangeCount);
CombineWithNewMesh(targetFilter, toAddInstances);
meshMerged = toAddInstances.Count;
}
toAddInstances.Clear();
// 超时则跳过这一帧
if(timeWatcher.ElapsedMilliseconds > 5)
{
timeWatcher.Restart();
yield return null;
}
}
Logger.Instance.PrintInfo("迷宫场景已加载完毕");
}
/// <summary>
/// 加载迷宫和搜索数据
/// </summary>
public void LoadAll()
{
if (!LoadMaze()) return;
LoadResult();
}
/// <summary>
/// 加载迷宫, 失败则返回false
/// </summary>
/// <returns></returns>
public bool LoadMaze()
{
List<List<MazeState>> maze = new List<List<MazeState>>();
try
{
maze = MazeIO.ReadFromFile(mazePath, out exitPos);
}
catch (FileNotFoundException)
{
Logger.Instance.PrintError($"无法打开路径为\"{mazePath}\"的迷宫文件!");
}
catch (Exception e)
{
Logger.Instance.PrintError($"加载迷宫时发生未知错误:{e}");
}
// return if empty
if (maze.Count < 1 || maze[0].Count < 1)
return false;
// convert all to cellObjs;
cellObjs = new List<List<GameObject>>();
for (var i = 0; i < maze.Count; i++)
{
cellObjs.Add(new List<GameObject>());
for (var j = 0; j < maze[0].Count; j++)
{
cellObjs[i].Add(cellFactory.GetCellObj(new Vector2Int(i, j), maze[i][j]));
cellObjs[i][j].transform.SetParent(transform);
var icell = cellObjs[i][j].GetComponent<ICellObj>();
icell.Init();
}
}
size = new Vector3(maze.Count, 0, maze[0].Count);
// transform.position = -size / 2;
// merge all walls
StartCoroutine(MergeMesh());
return true;
}
/// <summary>
/// 加载搜索结果
/// </summary>
public void LoadResult()
{
List<IRecovableOperation> operations = new List<IRecovableOperation>();
List<Vector2Int> way = new List<Vector2Int>();
int finalCost = 0;
Logger.Instance.PrintInfo("正在载入搜索数据...");
try
{
operations = MazeIO.ReadSearchDataFromFile(resultPath,
cellObjs.ConvertAll(row => row.ConvertAll(obj => obj.GetComponent<ICellObj>())), out way, out finalCost);
}
catch (FileNotFoundException)
{
Logger.Instance.PrintError($"无法打开路径为\"{resultPath}\"的搜索文件!");
}
catch (Exception)
{
Logger.Instance.PrintError("加载搜索数据时出现未知错误!");
}
operations.Add(new PathDrawOperation(way, pathDrawer));
progressMgr.LoadOperationChain(new OperationChain(operations, -1));
pathDrawer.HidePath();
StatusBar.Instance.SetFinalCost(finalCost);
Logger.Instance.PrintInfo("迷宫数据已加载");
}
/// <summary>
/// 仅清除搜索结果
/// </summary>
public void ClearResult()
{
progressMgr.BackToBegin();
EffectFactory.Instance.Initialize();
}
/// <summary>
/// 清空整个场景
/// </summary>
public void ClearAll()
{
ClearResult();
cellFactory.RecycleAll();
// 清除所有生成的网格
for(int i = 0; i < mergedMeshes.childCount; i++)
{
var tempObject = mergedMeshes.GetChild(i).gameObject;
tempObject.GetComponent<MeshFilter>().mesh = new Mesh();
// TODO: recycle gameObject
Destroy(tempObject);
}
Logger.Instance.PrintInfo("迷宫已初始化");
}
private void Update()
{
#if UNITY_EDITOR
if(Input.GetKey(KeyCode.Z))
#else
if((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKey(KeyCode.Z))
#endif
{
progressMgr.BackStep();
}
#if UNITY_EDITOR
if(Input.GetKey(KeyCode.Y))
#else
if((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && Input.GetKey(KeyCode.Y))
#endif
{
progressMgr.NextStep();
}
}
[ContextMenu("Redo")]
public void Redo()
{
progressMgr.BackStep();
}
[ContextMenu("Undo")]
public void Undo()
{
progressMgr.NextStep();
}
/// <summary>
/// 重载迷宫
/// <para>等价于先调用ClearMaze();后调用LoadMaze();</para>
/// </summary>
public void ReloadMaze()
{
ClearAll();
LoadAll();
}
public void UpdateMazePath(string path) => mazePath = path;
public void UpdateResultPath(string path) => resultPath = path;
}
}