VoxelFrameAnimationObjectCore.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using UnityEditor;
  4. using System;
  5. using System.Linq;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. namespace VoxelImporter
  10. {
  11. public class VoxelFrameAnimationObjectCore : VoxelBaseCore
  12. {
  13. public VoxelFrameAnimationObjectCore(VoxelBase target) : base(target)
  14. {
  15. voxelObject = target as VoxelFrameAnimationObject;
  16. }
  17. public VoxelFrameAnimationObject voxelObject { get; protected set; }
  18. #region AtlasRects
  19. protected Rect[] atlasRects;
  20. protected AtlasRectTable atlasRectTable;
  21. #endregion
  22. #region Chunk
  23. protected class ChunkData
  24. {
  25. public int voxelBegin;
  26. public int voxelEnd;
  27. public VoxelData.ChunkArea area;
  28. public VoxelData.FaceAreaTable faceAreaTable;
  29. }
  30. protected List<ChunkData> chunkDataList;
  31. #endregion
  32. #region CreateVoxel
  33. public override bool ReCreate()
  34. {
  35. ClearFramesIcon();
  36. #region Path
  37. if (voxelObject.frames != null)
  38. {
  39. for (int i = 0; i < voxelObject.frames.Count; i++)
  40. {
  41. if (voxelObject.frames[i].voxelFileObject != null)
  42. {
  43. var assetPath = AssetDatabase.GetAssetPath(voxelObject.frames[i].voxelFileObject);
  44. if (!string.IsNullOrEmpty(assetPath))
  45. {
  46. var fullPath = Application.dataPath + assetPath.Remove(0, "Assets".Length);
  47. voxelObject.frames[i].voxelFilePath = fullPath;
  48. }
  49. }
  50. }
  51. }
  52. #endregion
  53. var result = base.ReCreate();
  54. if (voxelObject.frames.Count > 0 && voxelObject.edit_frameIndex < 0)
  55. {
  56. voxelObject.edit_frameIndex = 0;
  57. SetCurrentMesh();
  58. }
  59. return result;
  60. }
  61. public override void Reset(string path, UnityEngine.Object obj)
  62. {
  63. base.Reset(path, obj);
  64. voxelObject.frames = new List<VoxelFrameAnimationObject.FrameData>();
  65. bool done = false;
  66. if (obj != null && obj is Texture2D)
  67. {
  68. var assetPath = AssetDatabase.GetAssetPath(obj);
  69. if (!string.IsNullOrEmpty(assetPath))
  70. {
  71. var sprites = AssetDatabase.LoadAllAssetsAtPath(assetPath);
  72. TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter;
  73. if (importer != null && importer.spriteImportMode == SpriteImportMode.Multiple)
  74. {
  75. for (int i = 0; i < sprites.Length; i++)
  76. {
  77. if (sprites[i] is Sprite)
  78. voxelObject.frames.Add(new VoxelFrameAnimationObject.FrameData() { voxelFilePath = path, voxelFileObject = sprites[i], name = voxelObject.Edit_GetUniqueFrameName(sprites[i].name) });
  79. }
  80. done = true;
  81. }
  82. }
  83. }
  84. else if(GetFileType(path) == VoxelBase.FileType.vox)
  85. {
  86. var subCount = GetVoxelFileSubCount(path);
  87. if (subCount > 1)
  88. {
  89. for (int i = 0; i < subCount; i++)
  90. {
  91. voxelObject.frames.Add(new VoxelFrameAnimationObject.FrameData() { voxelFilePath = path, voxelFileObject = obj, voxelFileSubIndex = i, name = voxelObject.Edit_GetUniqueFrameName(string.Format("{0}_{1}", Path.GetFileNameWithoutExtension(path), i)) });
  92. }
  93. done = true;
  94. }
  95. }
  96. if(!done)
  97. {
  98. voxelObject.frames.Add(new VoxelFrameAnimationObject.FrameData() { voxelFilePath = path, voxelFileObject = obj, name = voxelObject.Edit_GetUniqueFrameName(Path.GetFileNameWithoutExtension(path)) });
  99. }
  100. voxelObject.edit_frameIndex = 0;
  101. }
  102. public override bool IsVoxelFileExists()
  103. {
  104. var fileExists = true;
  105. if (voxelObject.frames != null && voxelObject.frames.Count > 0)
  106. {
  107. for (int i = 0; i < voxelObject.frames.Count; i++)
  108. {
  109. bool tmp = false;
  110. if (!string.IsNullOrEmpty(voxelObject.frames[i].voxelFilePath))
  111. {
  112. tmp = File.Exists(voxelObject.frames[i].voxelFilePath);
  113. }
  114. if (!tmp)
  115. {
  116. if (voxelObject.frames[i].voxelFileObject != null && AssetDatabase.Contains(voxelObject.frames[i].voxelFileObject))
  117. {
  118. tmp = true;
  119. }
  120. }
  121. if (!tmp)
  122. fileExists = false;
  123. }
  124. }
  125. else
  126. {
  127. fileExists = false;
  128. }
  129. return fileExists;
  130. }
  131. public override bool ReadyVoxelData(bool forceReload = false)
  132. {
  133. if (forceReload)
  134. {
  135. voxelBase.voxelData = null;
  136. for (int i = 0; i < voxelObject.frames.Count; i++)
  137. {
  138. voxelObject.frames[i].voxelData = null;
  139. }
  140. }
  141. if (voxelBase.voxelData == null)
  142. {
  143. return LoadVoxelData();
  144. }
  145. return true;
  146. }
  147. protected override bool LoadVoxelData()
  148. {
  149. bool result = true;
  150. if(voxelObject.frames != null && voxelObject.frames.Count > 0)
  151. {
  152. for (int i = 0; i < voxelObject.frames.Count; i++)
  153. {
  154. voxelObject.voxelFilePath = voxelObject.frames[i].voxelFilePath;
  155. voxelObject.voxelFileObject = voxelObject.frames[i].voxelFileObject;
  156. voxelObject.voxelFileSubIndex = voxelObject.frames[i].voxelFileSubIndex;
  157. voxelObject.fileType = voxelObject.frames[i].fileType;
  158. voxelObject.localOffset = voxelObject.frames[i].localOffset;
  159. voxelObject.voxelData = voxelObject.frames[i].voxelData;
  160. voxelObject.voxelDataCreatedVoxelFileTimeTicks = voxelObject.frames[i].voxelDataCreatedVoxelFileTimeTicks;
  161. voxelObject.disableData = voxelObject.frames[i].disableData;
  162. voxelObject.materialData = voxelObject.frames[i].materialData;
  163. voxelObject.materialIndexes = voxelObject.frames[i].materialIndexes;
  164. if (!base.LoadVoxelData())
  165. result = false;
  166. voxelObject.frames[i].voxelFilePath = voxelObject.voxelFilePath;
  167. voxelObject.frames[i].voxelFileObject = voxelObject.voxelFileObject;
  168. voxelObject.frames[i].voxelFileSubIndex = voxelObject.voxelFileSubIndex;
  169. voxelObject.frames[i].fileType = voxelObject.fileType;
  170. voxelObject.frames[i].localOffset = voxelObject.localOffset;
  171. voxelObject.frames[i].voxelData = voxelObject.voxelData;
  172. voxelObject.frames[i].voxelDataCreatedVoxelFileTimeTicks = voxelObject.voxelDataCreatedVoxelFileTimeTicks;
  173. voxelObject.frames[i].disableData = voxelObject.disableData;
  174. voxelObject.frames[i].materialData = voxelObject.materialData;
  175. voxelObject.frames[i].materialIndexes = voxelObject.materialIndexes;
  176. }
  177. voxelObject.voxelFilePath = voxelObject.frames[0].voxelFilePath;
  178. voxelObject.voxelFileObject = voxelObject.frames[0].voxelFileObject;
  179. voxelObject.voxelFileSubIndex = voxelObject.frames[0].voxelFileSubIndex;
  180. voxelObject.fileType = voxelObject.frames[0].fileType;
  181. voxelObject.localOffset = voxelObject.frames[0].localOffset;
  182. voxelObject.voxelData = voxelObject.frames[0].voxelData;
  183. voxelObject.voxelDataCreatedVoxelFileTimeTicks = voxelObject.frames[0].voxelDataCreatedVoxelFileTimeTicks;
  184. voxelObject.disableData = voxelObject.frames[0].disableData;
  185. voxelObject.materialData = voxelObject.frames[0].materialData;
  186. voxelObject.materialIndexes = voxelObject.frames[0].materialIndexes;
  187. }
  188. else
  189. {
  190. result = false;
  191. }
  192. return result;
  193. }
  194. public override string GetDefaultPath()
  195. {
  196. var path = base.GetDefaultPath();
  197. if (voxelObject != null)
  198. {
  199. if (voxelObject.materials != null)
  200. {
  201. for (int i = 0; i < voxelObject.materials.Count; i++)
  202. {
  203. if (AssetDatabase.Contains(voxelObject.materials[i]))
  204. {
  205. var assetPath = AssetDatabase.GetAssetPath(voxelObject.materials[i]);
  206. if (!string.IsNullOrEmpty(assetPath))
  207. {
  208. path = Path.GetDirectoryName(assetPath);
  209. }
  210. }
  211. }
  212. }
  213. if (voxelObject.atlasTexture != null && AssetDatabase.Contains(voxelObject.atlasTexture))
  214. {
  215. var assetPath = AssetDatabase.GetAssetPath(voxelObject.atlasTexture);
  216. if (!string.IsNullOrEmpty(assetPath))
  217. {
  218. path = Path.GetDirectoryName(assetPath);
  219. }
  220. }
  221. if (voxelObject.frames != null)
  222. {
  223. for (int i = 0; i < voxelObject.frames.Count; i++)
  224. {
  225. if (voxelObject.frames[i].mesh != null && AssetDatabase.Contains(voxelObject.frames[i].mesh))
  226. {
  227. var assetPath = AssetDatabase.GetAssetPath(voxelObject.frames[i].mesh);
  228. if (!string.IsNullOrEmpty(assetPath))
  229. {
  230. path = Path.GetDirectoryName(assetPath);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. return path;
  237. }
  238. #endregion
  239. #region CreateMesh
  240. protected override bool CreateMesh()
  241. {
  242. base.CreateMesh();
  243. #region ProgressBar
  244. const float MaxProgressCount = 8f;
  245. float ProgressCount = 0;
  246. Action<string> DisplayProgressBar = (info) =>
  247. {
  248. if (voxelData.voxels.Length > 10000)
  249. EditorUtility.DisplayProgressBar("Create Mesh...", string.Format("{0} / {1}", ProgressCount, MaxProgressCount), (ProgressCount++ / MaxProgressCount));
  250. };
  251. #endregion
  252. DisplayProgressBar("");
  253. #region Combine VoxelData
  254. {
  255. voxelBase.voxelData = new VoxelData();
  256. voxelBase.voxelData.chunkTable = new DataTable3<IntVector3>(voxelBase.voxelData.voxelSize.x, voxelBase.voxelData.voxelSize.y, voxelBase.voxelData.voxelSize.z);
  257. chunkDataList = new List<ChunkData>(voxelObject.frames.Count);
  258. List<VoxelData.Voxel> voxels = new List<VoxelData.Voxel>();
  259. IntVector3 voxelSize = IntVector3.zero;
  260. Dictionary<Color, int> paletteTable = new Dictionary<Color, int>();
  261. int offset = 0;
  262. for (int i = 0; i < voxelObject.frames.Count; i++)
  263. {
  264. chunkDataList.Add(new ChunkData());
  265. chunkDataList[i].voxelBegin = voxels.Count;
  266. for (int j = 0; j < voxelObject.frames[i].voxelData.voxels.Length; j++)
  267. {
  268. var voxel = voxelObject.frames[i].voxelData.voxels[j];
  269. var color = voxelObject.frames[i].voxelData.palettes[voxel.palette];
  270. if (!paletteTable.ContainsKey(color))
  271. paletteTable.Add(color, paletteTable.Count);
  272. voxel.palette = paletteTable[color];
  273. voxel.z += offset;
  274. voxels.Add(voxel);
  275. voxelBase.voxelData.chunkTable.Set(voxel.position, new IntVector3(i, 0, 0));
  276. }
  277. chunkDataList[i].voxelEnd = voxels.Count;
  278. chunkDataList[i].area = new VoxelData.ChunkArea() { min = new IntVector3(0, 0, offset), max = new IntVector3(voxelObject.frames[i].voxelData.voxelSize.x, voxelObject.frames[i].voxelData.voxelSize.y, offset + voxelObject.frames[i].voxelData.voxelSize.z) };
  279. voxelSize = IntVector3.Max(voxelSize, new IntVector3(voxelObject.frames[i].voxelData.voxelSize.x, voxelObject.frames[i].voxelData.voxelSize.y, offset + voxelObject.frames[i].voxelData.voxelSize.z));
  280. offset += voxelObject.frames[i].voxelData.voxelSize.z + 1;
  281. }
  282. #region Create
  283. voxelBase.localOffset = Vector3.zero;
  284. voxelBase.fileType = VoxelBase.FileType.vox;
  285. voxelBase.voxelData.voxels = voxels.ToArray();
  286. voxelBase.voxelData.palettes = new Color[paletteTable.Count];
  287. foreach (var pair in paletteTable)
  288. voxelBase.voxelData.palettes[pair.Value] = pair.Key;
  289. voxelBase.voxelData.voxelSize = voxelSize;
  290. voxelBase.voxelData.CreateVoxelTable();
  291. UpdateVisibleFlags();
  292. #endregion
  293. }
  294. #endregion
  295. DisplayProgressBar("");
  296. #region Combine DisableData
  297. {
  298. #region Erase
  299. for (int i = 0; i < voxelObject.frames.Count; i++)
  300. {
  301. if (voxelObject.frames[i].disableData == null) continue;
  302. List<IntVector3> removeList = new List<IntVector3>();
  303. voxelObject.frames[i].disableData.AllAction((pos, face) =>
  304. {
  305. if (voxelObject.frames[i].voxelData.VoxelTableContains(pos) < 0)
  306. {
  307. removeList.Add(pos);
  308. }
  309. });
  310. for (int k = 0; k < removeList.Count; k++)
  311. {
  312. voxelObject.frames[i].disableData.RemoveDisable(removeList[k]);
  313. }
  314. }
  315. #endregion
  316. for (int i = 0; i < voxelObject.frames.Count; i++)
  317. {
  318. if (voxelObject.frames[i].disableData == null)
  319. voxelObject.frames[i].disableData = new DisableData();
  320. }
  321. {
  322. voxelObject.disableData = new DisableData();
  323. for (int j = 0; j < voxelObject.frames.Count; j++)
  324. {
  325. if (voxelObject.frames[j].disableData == null) continue;
  326. voxelObject.frames[j].disableData.AllAction((pos, face) =>
  327. {
  328. voxelObject.disableData.SetDisable(chunkDataList[j].area.min + pos, face);
  329. });
  330. }
  331. }
  332. }
  333. #endregion
  334. DisplayProgressBar("");
  335. #region Combine MaterialData
  336. {
  337. #region Erase
  338. for (int i = 0; i < voxelObject.frames.Count; i++)
  339. {
  340. if (voxelObject.frames[i].materialData == null) continue;
  341. for (int j = 0; j < voxelObject.frames[i].materialData.Count; j++)
  342. {
  343. List<IntVector3> removeList = new List<IntVector3>();
  344. voxelObject.frames[i].materialData[j].AllAction((pos) =>
  345. {
  346. if (voxelObject.frames[i].voxelData.VoxelTableContains(pos) < 0)
  347. {
  348. removeList.Add(pos);
  349. }
  350. });
  351. for (int k = 0; k < removeList.Count; k++)
  352. {
  353. voxelObject.frames[i].materialData[j].RemoveMaterial(removeList[k]);
  354. }
  355. }
  356. }
  357. #endregion
  358. voxelObject.materialData = new List<MaterialData>();
  359. int materialCount = 1;
  360. for (int i = 0; i < voxelObject.frames.Count; i++)
  361. {
  362. if (voxelObject.frames[i].materialData != null)
  363. materialCount = Math.Max(materialCount, voxelObject.frames[i].materialData.Count);
  364. }
  365. for (int i = 0; i < voxelObject.frames.Count; i++)
  366. {
  367. if (voxelObject.frames[i].materialData == null)
  368. voxelObject.frames[i].materialData = new List<MaterialData>();
  369. for (int j = voxelObject.frames[i].materialData.Count; j < materialCount; j++)
  370. {
  371. voxelObject.frames[i].materialData.Add(new MaterialData());
  372. }
  373. }
  374. for (int i = 0; i < materialCount; i++)
  375. {
  376. voxelObject.materialData.Add(new MaterialData());
  377. voxelObject.materialData[i].name = voxelObject.frames[0].materialData[i].name;
  378. voxelObject.materialData[i].transparent = voxelObject.frames[0].materialData[i].transparent;
  379. voxelObject.materialData[i].material = voxelObject.frames[0].materialData[i].material;
  380. for (int j = 0; j < voxelObject.frames.Count; j++)
  381. {
  382. if (voxelObject.frames[j].materialData[i] == null) continue;
  383. voxelObject.frames[j].materialData[i].AllAction((pos) =>
  384. {
  385. voxelObject.materialData[i].SetMaterial(chunkDataList[j].area.min + pos);
  386. });
  387. }
  388. }
  389. }
  390. #endregion
  391. DisplayProgressBar("");
  392. #region Material
  393. {
  394. if (voxelBase.materialData == null)
  395. voxelBase.materialData = new List<MaterialData>();
  396. if (voxelBase.materialData.Count == 0)
  397. voxelBase.materialData.Add(null);
  398. for (int i = 0; i < voxelBase.materialData.Count; i++)
  399. {
  400. if (voxelBase.materialData[i] == null)
  401. voxelBase.materialData[i] = new MaterialData();
  402. }
  403. if (voxelObject.materials == null)
  404. voxelObject.materials = new List<Material>();
  405. if (voxelObject.materials.Count < voxelObject.materialData.Count)
  406. {
  407. for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
  408. voxelObject.materials.Add(null);
  409. }
  410. else if (voxelObject.materials.Count > voxelObject.materialData.Count)
  411. {
  412. voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
  413. }
  414. }
  415. voxelBase.CreateMaterialIndexTable();
  416. #endregion
  417. CalcDataCreate(voxelBase.voxelData.voxels);
  418. #region CreateFaceAreaTable
  419. {
  420. for (int i = 0; i < chunkDataList.Count; i++)
  421. {
  422. VoxelData.Voxel[] voxels = new VoxelData.Voxel[chunkDataList[i].voxelEnd - chunkDataList[i].voxelBegin];
  423. Array.Copy(voxelBase.voxelData.voxels, chunkDataList[i].voxelBegin, voxels, 0, voxels.Length);
  424. chunkDataList[i].faceAreaTable = CreateFaceArea(voxels);
  425. }
  426. }
  427. #endregion
  428. #region CreateTexture
  429. {
  430. var tmpFaceAreaTable = new VoxelData.FaceAreaTable();
  431. for (int i = 0; i < chunkDataList.Count; i++)
  432. {
  433. tmpFaceAreaTable.Merge(chunkDataList[i].faceAreaTable);
  434. }
  435. {
  436. var atlasTextureTmp = voxelObject.atlasTexture;
  437. if (!CreateTexture(tmpFaceAreaTable, voxelBase.voxelData.palettes, ref atlasRectTable, ref atlasTextureTmp, ref atlasRects))
  438. {
  439. EditorUtility.ClearProgressBar();
  440. return false;
  441. }
  442. voxelObject.atlasTexture = atlasTextureTmp;
  443. {
  444. if (voxelObject.materialData == null)
  445. voxelObject.materialData = new List<MaterialData>();
  446. if (voxelObject.materialData.Count == 0)
  447. voxelObject.materialData.Add(null);
  448. for (int i = 0; i < voxelObject.materialData.Count; i++)
  449. {
  450. if (voxelObject.materialData[i] == null)
  451. voxelObject.materialData[i] = new MaterialData();
  452. }
  453. if (voxelObject.materials == null)
  454. voxelObject.materials = new List<Material>();
  455. if (voxelObject.materials.Count < voxelObject.materialData.Count)
  456. {
  457. for (int i = voxelObject.materials.Count; i < voxelObject.materialData.Count; i++)
  458. voxelObject.materials.Add(null);
  459. }
  460. else if (voxelObject.materials.Count > voxelObject.materialData.Count)
  461. {
  462. voxelObject.materials.RemoveRange(voxelObject.materialData.Count, voxelObject.materials.Count - voxelObject.materialData.Count);
  463. }
  464. for (int i = 0; i < voxelObject.materials.Count; i++)
  465. {
  466. if (voxelObject.materials[i] == null)
  467. voxelObject.materials[i] = new Material(Shader.Find("Standard"));
  468. if (!AssetDatabase.Contains(voxelObject.materials[i]))
  469. {
  470. AddObjectToPrefabAsset(voxelObject.materials[i], "mat", i);
  471. }
  472. }
  473. if (!AssetDatabase.Contains(voxelObject.atlasTexture))
  474. {
  475. AddObjectToPrefabAsset(voxelObject.atlasTexture, "tex");
  476. }
  477. }
  478. }
  479. }
  480. #endregion
  481. #region CreateMesh
  482. DisplayProgressBar("");
  483. if (voxelObject.importMode == VoxelBase.ImportMode.LowPoly)
  484. {
  485. int forward = 0;
  486. int up = 0;
  487. int right = 0;
  488. int left = 0;
  489. int down = 0;
  490. int back = 0;
  491. for (int i = 0; i < chunkDataList.Count; i++)
  492. {
  493. AtlasRectTable atlasRectTableTmp = new AtlasRectTable();
  494. {
  495. atlasRectTableTmp.forward = atlasRectTable.forward.GetRange(forward, chunkDataList[i].faceAreaTable.forward.Count);
  496. forward += chunkDataList[i].faceAreaTable.forward.Count;
  497. atlasRectTableTmp.up = atlasRectTable.up.GetRange(up, chunkDataList[i].faceAreaTable.up.Count);
  498. up += chunkDataList[i].faceAreaTable.up.Count;
  499. atlasRectTableTmp.right = atlasRectTable.right.GetRange(right, chunkDataList[i].faceAreaTable.right.Count);
  500. right += chunkDataList[i].faceAreaTable.right.Count;
  501. atlasRectTableTmp.left = atlasRectTable.left.GetRange(left, chunkDataList[i].faceAreaTable.left.Count);
  502. left += chunkDataList[i].faceAreaTable.left.Count;
  503. atlasRectTableTmp.down = atlasRectTable.down.GetRange(down, chunkDataList[i].faceAreaTable.down.Count);
  504. down += chunkDataList[i].faceAreaTable.down.Count;
  505. atlasRectTableTmp.back = atlasRectTable.back.GetRange(back, chunkDataList[i].faceAreaTable.back.Count);
  506. back += chunkDataList[i].faceAreaTable.back.Count;
  507. }
  508. var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
  509. voxelBase.localOffset = voxelObject.frames[i].localOffset;
  510. voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTableTmp, extraOffset, out voxelObject.frames[i].materialIndexes);
  511. if (!AssetDatabase.Contains(voxelObject.frames[i].mesh))
  512. {
  513. AddObjectToPrefabAsset(voxelObject.frames[i].mesh, string.Format("mesh_{0}", voxelObject.frames[i].name));
  514. }
  515. }
  516. }
  517. else
  518. {
  519. for (int i = 0; i < chunkDataList.Count; i++)
  520. {
  521. var extraOffset = new Vector3(0, 0f, -chunkDataList[i].area.min.z);
  522. voxelBase.localOffset = voxelObject.frames[i].localOffset;
  523. voxelObject.frames[i].mesh = CreateMeshOnly(voxelObject.frames[i].mesh, chunkDataList[i].faceAreaTable, voxelObject.atlasTexture, atlasRects, atlasRectTable, extraOffset, out voxelObject.frames[i].materialIndexes);
  524. if (!AssetDatabase.Contains(voxelObject.frames[i].mesh))
  525. {
  526. AddObjectToPrefabAsset(voxelObject.frames[i].mesh, string.Format("mesh_{0}", voxelObject.frames[i].name));
  527. }
  528. }
  529. }
  530. #endregion
  531. DisplayProgressBar("");
  532. if (voxelBase.generateLightmapUVs)
  533. {
  534. var param = voxelBase.GetLightmapParam();
  535. for (int i = 0; i < chunkDataList.Count; i++)
  536. {
  537. if (voxelObject.frames[i].mesh.uv.Length > 0)
  538. Unwrapping.GenerateSecondaryUVSet(voxelObject.frames[i].mesh, param);
  539. }
  540. }
  541. if (voxelBase.generateTangents)
  542. {
  543. for (int i = 0; i < chunkDataList.Count; i++)
  544. {
  545. voxelObject.frames[i].mesh.RecalculateTangents();
  546. }
  547. }
  548. DisplayProgressBar("");
  549. SetRendererCompornent();
  550. RefreshCheckerSave();
  551. EditorUtility.ClearProgressBar();
  552. voxelObject.Edit_SetFrameCurrentVoxelOtherData();
  553. return true;
  554. }
  555. protected override void CreateMeshAfter()
  556. {
  557. chunkDataList = null;
  558. base.CreateMeshAfter();
  559. }
  560. public override void SetRendererCompornent()
  561. {
  562. if (voxelBase.updateMaterialTexture)
  563. {
  564. if (voxelObject.materials != null)
  565. {
  566. for (int i = 0; i < voxelObject.materials.Count; i++)
  567. {
  568. if (voxelObject.materials[i] != null)
  569. {
  570. Undo.RecordObject(voxelObject.materials[i], "Inspector");
  571. voxelObject.materials[i].mainTexture = voxelObject.atlasTexture;
  572. }
  573. }
  574. }
  575. }
  576. SetCurrentMesh();
  577. }
  578. public void SetCurrentMesh()
  579. {
  580. Undo.RecordObject(voxelObject, "Inspector");
  581. if (!voxelObject.edit_frameEnable)
  582. voxelObject.mesh = null;
  583. else
  584. voxelObject.mesh = voxelObject.edit_currentFrame.mesh;
  585. {
  586. var meshFilter = voxelBase.GetComponent<MeshFilter>();
  587. if (meshFilter != null)
  588. {
  589. Undo.RecordObject(meshFilter, "Inspector");
  590. meshFilter.sharedMesh = voxelObject.mesh;
  591. }
  592. }
  593. if (voxelBase.updateMeshRendererMaterials)
  594. {
  595. var renderer = voxelBase.GetComponent<Renderer>();
  596. Undo.RecordObject(renderer, "Inspector");
  597. if (voxelObject.materials != null && voxelObject.edit_frameEnable)
  598. {
  599. Material[] tmps = new Material[voxelObject.edit_currentFrame.materialIndexes.Count];
  600. for (int i = 0; i < voxelObject.edit_currentFrame.materialIndexes.Count; i++)
  601. {
  602. tmps[i] = voxelObject.materials[voxelObject.edit_currentFrame.materialIndexes[i]];
  603. }
  604. voxelObject.Edit_SetPlayMaterials(tmps);
  605. renderer.sharedMaterials = tmps;
  606. }
  607. else
  608. {
  609. voxelObject.Edit_ClearPlayMaterials();
  610. renderer.sharedMaterial = null;
  611. }
  612. }
  613. if (voxelBase.loadFromVoxelFile && voxelBase.materialData != null)
  614. {
  615. if (voxelObject.materials != null && voxelObject.materials.Count == voxelBase.materialData.Count)
  616. {
  617. for (int i = 0; i < voxelObject.materials.Count; i++)
  618. {
  619. Undo.RecordObject(voxelObject.materials[i], "Inspector");
  620. SetMaterialData(voxelObject.materials[i], voxelBase.materialData[i]);
  621. }
  622. }
  623. }
  624. }
  625. public override Mesh[] Edit_CreateMesh(List<VoxelData.Voxel> voxels, List<Edit_VerticesInfo> dstList = null, bool combine = true)
  626. {
  627. return new Mesh[1] { Edit_CreateMeshOnly(voxels, null, dstList, combine) };
  628. }
  629. #endregion
  630. #region Preview & Icon
  631. public void ClearFramesIcon()
  632. {
  633. if (voxelObject.frames == null) return;
  634. for (int i = 0; i < voxelObject.frames.Count; i++)
  635. {
  636. if (voxelObject.frames[i] == null) continue;
  637. voxelObject.frames[i].icon = null;
  638. }
  639. }
  640. #endregion
  641. #region Undo
  642. protected override void RefreshCheckerCreate() { voxelObject.refreshChecker = new VoxelFrameAnimationObject.RefreshCheckerFrameAnimation(voxelObject); }
  643. #endregion
  644. }
  645. }