CombinedResult.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. namespace LunarCatsStudio.SuperCombiner
  6. {
  7. /// <summary>
  8. /// This class stores all the relevant data of a combined session
  9. /// </summary>
  10. public class CombinedResult : ScriptableObject
  11. {
  12. // Editor display parameters
  13. public bool _showCombinedMaterials = false;
  14. public bool _showCombinedMeshes = false;
  15. public bool _showLogs = false;
  16. // Logs
  17. public string _logs;
  18. public List<String> _warningMessages = new List<string>();
  19. // The list of combined _material
  20. public List<CombinedMaterial> _combinedMaterials = new List<CombinedMaterial>();
  21. /// <summary>
  22. /// The list of dictionnaries of original materials to combine. There is one dicionnary by _combinedIndex.
  23. /// Key = _material's instanceID
  24. /// </summary>
  25. public List<Dictionary<int, MaterialToCombine>> _originalMaterialList = new List<Dictionary<int, MaterialToCombine>>();
  26. /// <summary>
  27. /// Dictionnary of original and reference _material's instanceID for each _combinedIndex. The reference _material is the one used and copied to create the combined _material
  28. /// </summary>
  29. public Dictionary<int, int> _originalReferenceMaterial = new Dictionary<int, int>();
  30. /// <summary>
  31. /// List of generated combined GameObjects for meshes
  32. /// </summary>
  33. public List<List<GameObject>> _combinedGameObjectFromMeshList = new List<List<GameObject>>();
  34. /// <summary>
  35. /// List of generated combined GameObjects for skinnedMeshes
  36. /// </summary>
  37. public List<List<GameObject>> _combinedGameObjectFromSkinnedMeshList = new List<List<GameObject>>();
  38. /// <summary>
  39. /// The list of mesh results
  40. /// </summary>
  41. public List<MeshCombined> _meshResults = new List<MeshCombined>();
  42. /// <summary>
  43. /// The number of original materials combined
  44. /// </summary>
  45. public int _materialCombinedCount;
  46. /// <summary>
  47. /// The number of combined _material created. If multimaterial is used, more than one _material should be created
  48. /// </summary>
  49. public int _combinedMaterialCount;
  50. /// <summary>
  51. /// The number of original meshes combined
  52. /// </summary>
  53. public int _meshesCombinedCount;
  54. /// <summary>
  55. /// The number of skinnedMeshes combined
  56. /// </summary>
  57. public int _skinnedMeshesCombinedCount;
  58. /// <summary>
  59. /// The number of vertex in combined mesh
  60. /// </summary>
  61. public int _totalVertexCount;
  62. /// <summary>
  63. /// The number of submeshes
  64. /// </summary>
  65. public int _subMeshCount;
  66. /// <summary>
  67. /// The _duration of the process
  68. /// </summary>
  69. public TimeSpan _duration;
  70. /// <summary>
  71. /// Clear all combine data
  72. /// </summary>
  73. public void Clear()
  74. {
  75. if (_originalMaterialList != null)
  76. {
  77. for (int i = 0; i < _originalMaterialList.Count; i++)
  78. {
  79. _originalMaterialList[i].Clear();
  80. }
  81. _originalMaterialList.Clear();
  82. }
  83. _originalReferenceMaterial.Clear();
  84. _materialCombinedCount = 0;
  85. _combinedMaterials.Clear();
  86. _combinedMaterialCount = 0;
  87. _meshesCombinedCount = 0;
  88. _skinnedMeshesCombinedCount = 0;
  89. _totalVertexCount = 0;
  90. _subMeshCount = 0;
  91. _meshResults.Clear();
  92. _combinedGameObjectFromMeshList.Clear();
  93. _combinedGameObjectFromSkinnedMeshList.Clear();
  94. _logs = " ";
  95. _warningMessages.Clear();
  96. }
  97. /// <summary>
  98. /// Add a new warning message
  99. /// </summary>
  100. /// <param name="message"></param>
  101. public void AddWarningMessage(String message)
  102. {
  103. _warningMessages.Add(message);
  104. }
  105. /// <summary>
  106. /// Sets a combined _material
  107. /// </summary>
  108. /// <param name="mat"></param>
  109. /// <param name="combinedIndex"></param>
  110. /// <param name="isOriginal"></param>
  111. public void SetCombinedMaterial(Material mat, int combinedIndex, bool isOriginal)
  112. {
  113. if (combinedIndex < _combinedMaterials.Count)
  114. {
  115. _combinedMaterials[combinedIndex].material = mat;
  116. if (!isOriginal)
  117. {
  118. _combinedMaterials[combinedIndex].material.name += "_" + _combinedMaterialCount;
  119. }
  120. _combinedMaterials[combinedIndex].displayedIndex = _combinedMaterialCount;
  121. _combinedMaterials[combinedIndex].isOriginalMaterial = isOriginal;
  122. }
  123. _combinedMaterialCount++;
  124. }
  125. /// <summary>
  126. /// Add a new combined _material to the list
  127. /// </summary>
  128. public void AddNewCombinedMaterial()
  129. {
  130. _combinedMaterials.Add(new CombinedMaterial());
  131. }
  132. /// <summary>
  133. /// Adds a new _material to be combined
  134. /// </summary>
  135. /// <param name="mat"></param>
  136. /// <param name="combinedIndex">Index of the multi material group</param>
  137. public void AddMaterialToCombine(MaterialToCombine mat, int combinedIndex)
  138. {
  139. if (!_originalReferenceMaterial.ContainsKey(combinedIndex))
  140. {
  141. _originalReferenceMaterial.Add(combinedIndex, mat._material.GetInstanceID());
  142. }
  143. mat._index = _originalMaterialList[combinedIndex].Count;
  144. _originalMaterialList[combinedIndex].Add(mat._material.GetInstanceID(), mat);
  145. //Logger.Instance.AddLog("SuperCombiner", "Adding new _material to combine: " + mat._material.name + " at _combinedIndex " + _combinedIndex);
  146. }
  147. /// <summary>
  148. /// Add a new CombinedMesh
  149. /// </summary>
  150. /// <param name="combinedMesh"></param>
  151. /// <param name="combineInstanceID"></param>
  152. /// <param name="combinedIndex"></param>
  153. public void AddCombinedMesh(Mesh combinedMesh, CombineInstanceID combineInstanceID, int combinedIndex)
  154. {
  155. MeshCombined meshResult = new MeshCombined();
  156. int vertexIndex = 0;
  157. int triangleIndex = 0;
  158. for (int i = 0; i < combineInstanceID._combineInstances.Count; i++)
  159. {
  160. if (!meshResult.instanceIds.Contains(combineInstanceID._instancesID[i]))
  161. {
  162. vertexIndex += combineInstanceID._combineInstances[i].mesh.vertexCount;
  163. triangleIndex += combineInstanceID._combineInstances[i].mesh.triangles.Length;
  164. meshResult.names.Add(combineInstanceID._names[i]);
  165. meshResult.instanceIds.Add(combineInstanceID._instancesID[i]);
  166. meshResult.indexes.Add(new CombineInstanceIndexes(combineInstanceID._combineInstances[i].mesh, vertexIndex, triangleIndex));
  167. }
  168. }
  169. _meshResults.Add(meshResult);
  170. }
  171. /// <summary>
  172. /// Returns the _index of a given _material in the list
  173. /// </summary>
  174. /// <param name="matToFind"></param>
  175. /// <param name="combinedIndex"></param>
  176. /// <returns></returns>
  177. public int FindCorrespondingMaterialIndex(Material matToFind, int combinedIndex)
  178. {
  179. if (combinedIndex < _originalMaterialList.Count)
  180. {
  181. if (_originalMaterialList[combinedIndex].ContainsKey(matToFind.GetInstanceID()))
  182. {
  183. return _originalMaterialList[combinedIndex][matToFind.GetInstanceID()]._index;
  184. }
  185. }
  186. Logger.Instance.AddLog("SuperCombiner", "Material " + matToFind + " was not found in list " + combinedIndex, Logger.LogLevel.LOG_WARNING);
  187. return 0;
  188. }
  189. /// <summary>
  190. /// Get the combined _material associated to the source _material in parameter
  191. /// </summary>
  192. /// <param name="sourceMaterial"></param>
  193. /// <returns></returns>
  194. public Material GetCombinedMaterial(Material sourceMaterial)
  195. {
  196. for (int i = 0; i < _originalMaterialList.Count; i++)
  197. {
  198. if (_originalMaterialList[i].ContainsKey(sourceMaterial.GetInstanceID()))
  199. {
  200. return _combinedMaterials[i].material;
  201. }
  202. }
  203. Logger.Instance.AddLog("SuperCombiner", "Could not find combined _material associated with " + sourceMaterial.name, Logger.LogLevel.LOG_WARNING);
  204. return null;
  205. }
  206. public List<Material> GetCombinedMaterials()
  207. {
  208. List<Material> materials = new List<Material>();
  209. for (int i = 0; i < _combinedMaterials.Count; i++)
  210. {
  211. if (_combinedMaterials[i].material != null)
  212. {
  213. materials.Add(_combinedMaterials[i].material);
  214. }
  215. }
  216. return materials;
  217. }
  218. /// <summary>
  219. /// Gets the combined _index of the given _material
  220. /// </summary>
  221. /// <returns>The combined _index.</returns>
  222. /// <param name="sourceMaterial">Material.</param>
  223. public int GetCombinedIndex(Material sourceMaterial)
  224. {
  225. for (int i = 0; i < _originalMaterialList.Count; i++)
  226. {
  227. if (_originalMaterialList[i].ContainsKey(sourceMaterial.GetInstanceID()))
  228. {
  229. return i;
  230. }
  231. }
  232. Logger.Instance.AddLog("SuperCombiner", "Could not find combined _material associated with " + sourceMaterial.name, Logger.LogLevel.LOG_WARNING);
  233. return 0;
  234. }
  235. /// <summary>
  236. /// Return the number of combined _material (the total number of multi material group possible)
  237. /// </summary>
  238. /// <returns></returns>
  239. public int GetCombinedIndexCount()
  240. {
  241. return _originalMaterialList.Count;
  242. }
  243. /// <summary>
  244. ///
  245. /// </summary>
  246. /// <returns></returns>
  247. public int GetMaterialGroupCount()
  248. {
  249. int count = 0;
  250. foreach (Dictionary<int, MaterialToCombine> dict in _originalMaterialList)
  251. {
  252. if (dict.Count > 0)
  253. {
  254. count++;
  255. }
  256. }
  257. return count;
  258. }
  259. }
  260. }