MB3_TextureCombinerMerging.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. public class MB3_TextureCombinerMerging
  8. {
  9. public static bool DO_INTEGRITY_CHECKS = false;
  10. private bool _HasBeenInitialized = false;
  11. public static Rect BuildTransformMeshUV2AtlasRect(
  12. bool considerMeshUVs,
  13. Rect _atlasRect,
  14. Rect _obUVRect,
  15. Rect _sourceMaterialTiling,
  16. Rect _encapsulatingRect)
  17. {
  18. DRect atlasRect = new DRect(_atlasRect);
  19. DRect obUVRect;
  20. if (considerMeshUVs)
  21. {
  22. obUVRect = new DRect(_obUVRect); //this is the uvRect in src mesh
  23. }
  24. else
  25. {
  26. obUVRect = new DRect(0.0, 0.0, 1.0, 1.0);
  27. }
  28. DRect sourceMaterialTiling = new DRect(_sourceMaterialTiling);
  29. DRect encapsulatingRectMatAndUVTiling = new DRect(_encapsulatingRect);
  30. DRect encapsulatingRectMatAndUVTilingInverse = MB3_UVTransformUtility.InverseTransform(ref encapsulatingRectMatAndUVTiling);
  31. DRect toNormalizedUVs = MB3_UVTransformUtility.InverseTransform(ref obUVRect);
  32. DRect meshFullSamplingRect = MB3_UVTransformUtility.CombineTransforms(ref obUVRect, ref sourceMaterialTiling);
  33. DRect shiftToFitInEncapsulating = MB3_UVTransformUtility.GetShiftTransformToFitBinA(ref encapsulatingRectMatAndUVTiling, ref meshFullSamplingRect);
  34. meshFullSamplingRect = MB3_UVTransformUtility.CombineTransforms(ref meshFullSamplingRect, ref shiftToFitInEncapsulating);
  35. //transform between full sample rect and encapsulating rect
  36. DRect relativeTrans = MB3_UVTransformUtility.CombineTransforms(ref meshFullSamplingRect, ref encapsulatingRectMatAndUVTilingInverse);
  37. // [transform] = [toNormalizedUVs][relativeTrans][uvSubRectInAtlas]
  38. DRect trans = MB3_UVTransformUtility.CombineTransforms(ref toNormalizedUVs, ref relativeTrans);
  39. trans = MB3_UVTransformUtility.CombineTransforms(ref trans, ref atlasRect);
  40. Rect rr = trans.GetRect();
  41. return rr;
  42. }
  43. bool _considerNonTextureProperties = false;
  44. MB3_TextureCombinerNonTextureProperties resultMaterialTextureBlender;
  45. bool fixOutOfBoundsUVs = true;
  46. public MB2_LogLevel LOG_LEVEL = MB2_LogLevel.info;
  47. private static bool LOG_LEVEL_TRACE_MERGE_MAT_SUBRECTS = false;
  48. public MB3_TextureCombinerMerging(bool considerNonTextureProps, MB3_TextureCombinerNonTextureProperties resultMaterialTexBlender, bool fixObUVs, MB2_LogLevel logLevel)
  49. {
  50. LOG_LEVEL = logLevel;
  51. _considerNonTextureProperties = considerNonTextureProps;
  52. resultMaterialTextureBlender = resultMaterialTexBlender;
  53. fixOutOfBoundsUVs = fixObUVs;
  54. }
  55. public void MergeOverlappingDistinctMaterialTexturesAndCalcMaterialSubrects(List<MB_TexSet> distinctMaterialTextures)
  56. {
  57. if (LOG_LEVEL >= MB2_LogLevel.debug)
  58. {
  59. Debug.Log("MergeOverlappingDistinctMaterialTexturesAndCalcMaterialSubrects num atlas rects" + distinctMaterialTextures.Count);
  60. }
  61. int numMerged = 0;
  62. // IMPORTANT: Note that the verts stored in the mesh are NOT Normalized UV Coords. They are normalized * [UVTrans]. To get normalized UV
  63. // coords we must multiply them by [invUVTrans]. Need to do this to the verts in the mesh before we do any transforms with them.
  64. // Also check that all textures use same tiling. This is a prerequisite for merging.
  65. // Mark MB3_TexSet that are mergable (allTexturesUseSameMatTiling)
  66. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  67. {
  68. MB_TexSet tx = distinctMaterialTextures[i];
  69. int idxOfFirstNotNull = -1;
  70. bool allAreSame = true;
  71. DRect firstRect = new DRect();
  72. for (int propIdx = 0; propIdx < tx.ts.Length; propIdx++)
  73. {
  74. if (idxOfFirstNotNull != -1)
  75. {
  76. if (!tx.ts[propIdx].isNull && firstRect != tx.ts[propIdx].matTilingRect)
  77. {
  78. allAreSame = false;
  79. }
  80. }
  81. else if (!tx.ts[propIdx].isNull)
  82. {
  83. idxOfFirstNotNull = propIdx;
  84. firstRect = tx.ts[propIdx].matTilingRect;
  85. }
  86. }
  87. if (LOG_LEVEL >= MB2_LogLevel.debug || LOG_LEVEL_TRACE_MERGE_MAT_SUBRECTS == true)
  88. {
  89. if (allAreSame)
  90. {
  91. Debug.LogFormat("TextureSet {0} allTexturesUseSameMatTiling = {1}", i, allAreSame);
  92. }
  93. else
  94. {
  95. Debug.Log(string.Format("Textures in material(s) do not all use the same material tiling. This set of textures will not be considered for merge: {0} ", tx.GetDescription()));
  96. }
  97. }
  98. if (allAreSame)
  99. {
  100. tx.SetAllTexturesUseSameMatTilingTrue();
  101. }
  102. }
  103. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  104. {
  105. MB_TexSet tx = distinctMaterialTextures[i];
  106. for (int matIdx = 0; matIdx < tx.matsAndGOs.mats.Count; matIdx++)
  107. {
  108. if (tx.matsAndGOs.gos.Count > 0) {
  109. tx.matsAndGOs.mats[matIdx].objName = tx.matsAndGOs.gos[0].name;
  110. } else if (tx.ts[0] != null) {
  111. tx.matsAndGOs.mats[matIdx].objName = string.Format("[objWithTx:{0} atlasBlock:{1} matIdx{2}]",tx.ts[0].GetTexName(),i,matIdx);
  112. } else {
  113. tx.matsAndGOs.mats[matIdx].objName = string.Format("[objWithTx:{0} atlasBlock:{1} matIdx{2}]", "Unknown", i, matIdx);
  114. }
  115. }
  116. tx.CalcInitialFullSamplingRects(fixOutOfBoundsUVs);
  117. tx.CalcMatAndUVSamplingRects();
  118. }
  119. _HasBeenInitialized = true;
  120. // need to calculate the srcSampleRect for the complete tiling in the atlas
  121. // for each material need to know what the subrect would be in the atlas if material UVRect was 0,0,1,1 and Merged uvRect was full tiling
  122. List<int> MarkedForDeletion = new List<int>();
  123. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  124. {
  125. MB_TexSet tx2 = distinctMaterialTextures[i];
  126. for (int j = i + 1; j < distinctMaterialTextures.Count; j++)
  127. {
  128. MB_TexSet tx1 = distinctMaterialTextures[j];
  129. if (tx1.AllTexturesAreSameForMerge(tx2, _considerNonTextureProperties, resultMaterialTextureBlender))
  130. {
  131. double accumulatedAreaCombined = 0f;
  132. double accumulatedAreaNotCombined = 0f;
  133. DRect encapsulatingRectMerged = new DRect();
  134. int idxOfFirstNotNull = -1;
  135. for (int propIdx = 0; propIdx < tx2.ts.Length; propIdx++)
  136. {
  137. if (!tx2.ts[propIdx].isNull)
  138. {
  139. if (idxOfFirstNotNull == -1) idxOfFirstNotNull = propIdx;
  140. }
  141. }
  142. DRect encapsulatingRect1 = new DRect();
  143. DRect encapsulatingRect2 = new DRect();
  144. if (idxOfFirstNotNull != -1)
  145. {
  146. // only in here if all properties use the same tiling so don't need to worry about which propIdx we are dealing with
  147. //Get the rect that encapsulates all material and UV tiling for materials and meshes in tx1
  148. encapsulatingRect1 = tx1.matsAndGOs.mats[0].samplingRectMatAndUVTiling;
  149. for (int matIdx = 1; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  150. {
  151. DRect tmpSsamplingRectMatAndUVTilingTx1 = tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling;
  152. encapsulatingRect1 = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect1, ref tmpSsamplingRectMatAndUVTilingTx1);
  153. }
  154. //same for tx2
  155. encapsulatingRect2 = tx2.matsAndGOs.mats[0].samplingRectMatAndUVTiling;
  156. for (int matIdx = 1; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  157. {
  158. DRect tmpSsamplingRectMatAndUVTilingTx2 = tx2.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling;
  159. encapsulatingRect2 = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect2, ref tmpSsamplingRectMatAndUVTilingTx2);
  160. }
  161. encapsulatingRectMerged = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect1, ref encapsulatingRect2);
  162. accumulatedAreaCombined += encapsulatingRectMerged.width * encapsulatingRectMerged.height;
  163. accumulatedAreaNotCombined += encapsulatingRect1.width * encapsulatingRect1.height + encapsulatingRect2.width * encapsulatingRect2.height;
  164. }
  165. else
  166. {
  167. encapsulatingRectMerged = new DRect(0f, 0f, 1f, 1f);
  168. }
  169. //the distinct material textures may overlap.
  170. //if the area of these rectangles combined is less than the sum of these areas of these rectangles then merge these distinctMaterialTextures
  171. if (accumulatedAreaCombined < accumulatedAreaNotCombined)
  172. {
  173. // merge tx2 into tx1
  174. numMerged++;
  175. StringBuilder sb = null;
  176. if (LOG_LEVEL >= MB2_LogLevel.info)
  177. {
  178. sb = new StringBuilder();
  179. sb.AppendFormat("About To Merge:\n TextureSet1 {0}\n TextureSet2 {1}\n", tx1.GetDescription(), tx2.GetDescription());
  180. if (LOG_LEVEL >= MB2_LogLevel.trace)
  181. {
  182. for (int matIdx = 0; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  183. {
  184. sb.AppendFormat("tx1 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  185. tx1.matsAndGOs.mats[matIdx].mat, tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx1.ts[0].GetEncapsulatingSamplingRect());
  186. }
  187. for (int matIdx = 0; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  188. {
  189. sb.AppendFormat("tx2 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  190. tx2.matsAndGOs.mats[matIdx].mat, tx2.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx2.ts[0].GetEncapsulatingSamplingRect());
  191. }
  192. }
  193. }
  194. //copy game objects over
  195. for (int k = 0; k < tx2.matsAndGOs.gos.Count; k++)
  196. {
  197. if (!tx1.matsAndGOs.gos.Contains(tx2.matsAndGOs.gos[k]))
  198. {
  199. tx1.matsAndGOs.gos.Add(tx2.matsAndGOs.gos[k]);
  200. }
  201. }
  202. //copy materials over from tx2 to tx1
  203. for (int matIdx = 0; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  204. {
  205. tx1.matsAndGOs.mats.Add(tx2.matsAndGOs.mats[matIdx]);
  206. }
  207. tx1.SetEncapsulatingSamplingRectWhenMergingTexSets(encapsulatingRectMerged);
  208. if (!MarkedForDeletion.Contains(i))
  209. {
  210. MarkedForDeletion.Add(i);
  211. }
  212. if (LOG_LEVEL >= MB2_LogLevel.debug)
  213. {
  214. if (LOG_LEVEL >= MB2_LogLevel.trace)
  215. {
  216. sb.AppendFormat("=== After Merge TextureSet {0}\n", tx1.GetDescription());
  217. for (int matIdx = 0; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  218. {
  219. sb.AppendFormat("tx1 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  220. tx1.matsAndGOs.mats[matIdx].mat, tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx1.ts[0].GetEncapsulatingSamplingRect());
  221. }
  222. //Integrity check that sampling rects fit into enapsulating rects
  223. if (DO_INTEGRITY_CHECKS)
  224. {
  225. if (DO_INTEGRITY_CHECKS) { DoIntegrityCheckMergedEncapsulatingSamplingRects(distinctMaterialTextures); }
  226. }
  227. }
  228. Debug.Log(sb.ToString());
  229. }
  230. break;
  231. }
  232. else
  233. {
  234. if (LOG_LEVEL >= MB2_LogLevel.debug)
  235. {
  236. Debug.Log(string.Format("Considered merging {0} and {1} but there was not enough overlap. It is more efficient to bake these to separate rectangles.",
  237. tx1.GetDescription() + encapsulatingRect1,
  238. tx2.GetDescription() + encapsulatingRect2));
  239. }
  240. }
  241. }
  242. }
  243. }
  244. //remove distinctMaterialTextures that were merged
  245. for (int j = MarkedForDeletion.Count - 1; j >= 0; j--)
  246. {
  247. distinctMaterialTextures.RemoveAt(MarkedForDeletion[j]);
  248. }
  249. MarkedForDeletion.Clear();
  250. if (LOG_LEVEL >= MB2_LogLevel.debug)
  251. {
  252. Debug.Log(string.Format("MergeOverlappingDistinctMaterialTexturesAndCalcMaterialSubrects complete merged {0} now have {1}", numMerged, distinctMaterialTextures.Count));
  253. }
  254. if (DO_INTEGRITY_CHECKS) { DoIntegrityCheckMergedEncapsulatingSamplingRects(distinctMaterialTextures); }
  255. }
  256. // This should only be called after regular merge so that rects have been correctly setup.
  257. public void MergeDistinctMaterialTexturesThatWouldExceedMaxAtlasSizeAndCalcMaterialSubrects(List<MB_TexSet> distinctMaterialTextures, int maxAtlasSize)
  258. {
  259. if (LOG_LEVEL >= MB2_LogLevel.debug)
  260. {
  261. Debug.Log("MergeDistinctMaterialTexturesThatWouldExceedMaxAtlasSizeAndCalcMaterialSubrects num atlas rects" + distinctMaterialTextures.Count);
  262. }
  263. Debug.Assert(_HasBeenInitialized, "MergeOverlappingDistinctMaterialTexturesAndCalcMaterialSubrects must be called before MergeDistinctMaterialTexturesThatWouldExceedMaxAtlasSizeAndCalcMaterialSubrects");
  264. int numMerged = 0;
  265. List<int> MarkedForDeletion = new List<int>();
  266. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  267. {
  268. MB_TexSet tx2 = distinctMaterialTextures[i];
  269. for (int j = i + 1; j < distinctMaterialTextures.Count; j++)
  270. {
  271. MB_TexSet tx1 = distinctMaterialTextures[j];
  272. if (tx1.AllTexturesAreSameForMerge(tx2, _considerNonTextureProperties, resultMaterialTextureBlender))
  273. {
  274. //Check if the size of the rect in the atlas would be greater than max atlas size.
  275. DRect encapsulatingRectMerged = new DRect();
  276. int idxOfFirstNotNull = -1;
  277. for (int propIdx = 0; propIdx < tx2.ts.Length; propIdx++)
  278. {
  279. if (!tx2.ts[propIdx].isNull)
  280. {
  281. if (idxOfFirstNotNull == -1) idxOfFirstNotNull = propIdx;
  282. }
  283. }
  284. DRect encapsulatingRect1 = new DRect();
  285. DRect encapsulatingRect2 = new DRect();
  286. if (idxOfFirstNotNull != -1)
  287. {
  288. // only in here if all properties use the same tiling so don't need to worry about which propIdx we are dealing with
  289. //Get the rect that encapsulates all material and UV tiling for materials and meshes in tx1
  290. encapsulatingRect1 = tx1.matsAndGOs.mats[0].samplingRectMatAndUVTiling;
  291. for (int matIdx = 1; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  292. {
  293. DRect tmpSsamplingRectMatAndUVTilingTx1 = tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling;
  294. encapsulatingRect1 = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect1, ref tmpSsamplingRectMatAndUVTilingTx1);
  295. }
  296. //same for tx2
  297. encapsulatingRect2 = tx2.matsAndGOs.mats[0].samplingRectMatAndUVTiling;
  298. for (int matIdx = 1; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  299. {
  300. DRect tmpSsamplingRectMatAndUVTilingTx2 = tx2.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling;
  301. encapsulatingRect2 = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect2, ref tmpSsamplingRectMatAndUVTilingTx2);
  302. }
  303. encapsulatingRectMerged = MB3_UVTransformUtility.GetEncapsulatingRectShifted(ref encapsulatingRect1, ref encapsulatingRect2);
  304. }
  305. else
  306. {
  307. encapsulatingRectMerged = new DRect(0f, 0f, 1f, 1f);
  308. }
  309. Vector2 maxHeightWidth = tx1.GetMaxRawTextureHeightWidth();
  310. if (encapsulatingRectMerged.width * maxHeightWidth.x > maxAtlasSize ||
  311. encapsulatingRectMerged.height * maxHeightWidth.y > maxAtlasSize)
  312. {
  313. // merge tx2 into tx1
  314. numMerged++;
  315. StringBuilder sb = null;
  316. if (LOG_LEVEL >= MB2_LogLevel.info)
  317. {
  318. sb = new StringBuilder();
  319. sb.AppendFormat("About To Merge:\n TextureSet1 {0}\n TextureSet2 {1}\n", tx1.GetDescription(), tx2.GetDescription());
  320. if (LOG_LEVEL >= MB2_LogLevel.trace)
  321. {
  322. for (int matIdx = 0; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  323. {
  324. sb.AppendFormat("tx1 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  325. tx1.matsAndGOs.mats[matIdx].mat, tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx1.ts[0].GetEncapsulatingSamplingRect());
  326. }
  327. for (int matIdx = 0; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  328. {
  329. sb.AppendFormat("tx2 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  330. tx2.matsAndGOs.mats[matIdx].mat, tx2.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx2.ts[0].GetEncapsulatingSamplingRect());
  331. }
  332. }
  333. }
  334. //copy game objects over
  335. for (int k = 0; k < tx2.matsAndGOs.gos.Count; k++)
  336. {
  337. if (!tx1.matsAndGOs.gos.Contains(tx2.matsAndGOs.gos[k]))
  338. {
  339. tx1.matsAndGOs.gos.Add(tx2.matsAndGOs.gos[k]);
  340. }
  341. }
  342. //copy materials over from tx2 to tx1
  343. for (int matIdx = 0; matIdx < tx2.matsAndGOs.mats.Count; matIdx++)
  344. {
  345. tx1.matsAndGOs.mats.Add(tx2.matsAndGOs.mats[matIdx]);
  346. }
  347. tx1.SetEncapsulatingSamplingRectWhenMergingTexSets(encapsulatingRectMerged);
  348. if (!MarkedForDeletion.Contains(i))
  349. {
  350. MarkedForDeletion.Add(i);
  351. }
  352. if (LOG_LEVEL >= MB2_LogLevel.debug)
  353. {
  354. if (LOG_LEVEL >= MB2_LogLevel.trace)
  355. {
  356. sb.AppendFormat("=== After Merge TextureSet {0}\n", tx1.GetDescription());
  357. for (int matIdx = 0; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  358. {
  359. sb.AppendFormat("tx1 Mat {0} matAndMeshUVRect {1} fullSamplingRect {2}\n",
  360. tx1.matsAndGOs.mats[matIdx].mat, tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling, tx1.ts[0].GetEncapsulatingSamplingRect());
  361. }
  362. //Integrity check that sampling rects fit into enapsulating rects
  363. if (DO_INTEGRITY_CHECKS)
  364. {
  365. if (DO_INTEGRITY_CHECKS) { DoIntegrityCheckMergedEncapsulatingSamplingRects(distinctMaterialTextures); }
  366. }
  367. }
  368. Debug.Log(sb.ToString());
  369. }
  370. break;
  371. }
  372. else
  373. {
  374. if (LOG_LEVEL >= MB2_LogLevel.debug)
  375. {
  376. Debug.Log(string.Format("Considered merging {0} and {1} but there was not enough overlap. It is more efficient to bake these to separate rectangles.",
  377. tx1.GetDescription() + encapsulatingRect1,
  378. tx2.GetDescription() + encapsulatingRect2));
  379. }
  380. }
  381. }
  382. }
  383. }
  384. //remove distinctMaterialTextures that were merged
  385. for (int j = MarkedForDeletion.Count - 1; j >= 0; j--)
  386. {
  387. distinctMaterialTextures.RemoveAt(MarkedForDeletion[j]);
  388. }
  389. MarkedForDeletion.Clear();
  390. if (LOG_LEVEL >= MB2_LogLevel.debug)
  391. {
  392. Debug.Log(string.Format("MergeDistinctMaterialTexturesThatWouldExceedMaxAtlasSizeAndCalcMaterialSubrects complete merged {0} now have {1}", numMerged, distinctMaterialTextures.Count));
  393. }
  394. if (DO_INTEGRITY_CHECKS) { DoIntegrityCheckMergedEncapsulatingSamplingRects(distinctMaterialTextures); }
  395. }
  396. public void DoIntegrityCheckMergedEncapsulatingSamplingRects(List<MB_TexSet> distinctMaterialTextures)
  397. {
  398. if (DO_INTEGRITY_CHECKS)
  399. {
  400. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  401. {
  402. MB_TexSet tx1 = distinctMaterialTextures[i];
  403. if (!tx1.allTexturesUseSameMatTiling)
  404. {
  405. continue;
  406. }
  407. for (int matIdx = 0; matIdx < tx1.matsAndGOs.mats.Count; matIdx++)
  408. {
  409. MatAndTransformToMerged mat = tx1.matsAndGOs.mats[matIdx];
  410. DRect uvR = mat.obUVRectIfTilingSame;
  411. DRect matR = mat.materialTiling;
  412. if (!MB2_TextureBakeResults.IsMeshAndMaterialRectEnclosedByAtlasRect(tx1.tilingTreatment, uvR.GetRect(), matR.GetRect(), tx1.ts[0].GetEncapsulatingSamplingRect().GetRect(),MB2_LogLevel.info))
  413. {
  414. Debug.LogErrorFormat("mesh " + tx1.matsAndGOs.mats[matIdx].objName + "\n" +
  415. " uv=" + uvR + "\n" +
  416. " mat=" + matR.GetRect().ToString("f5") + "\n" +
  417. " samplingRect=" + tx1.matsAndGOs.mats[matIdx].samplingRectMatAndUVTiling.GetRect().ToString("f4") + "\n" +
  418. " encapsulatingRect " + tx1.ts[0].GetEncapsulatingSamplingRect().GetRect().ToString("f4") + "\n");
  419. Debug.LogErrorFormat(string.Format("Integrity check failed. " + tx1.matsAndGOs.mats[matIdx].objName + " Encapsulating sampling rect failed to contain potentialRect\n"));
  420. MB2_TextureBakeResults.IsMeshAndMaterialRectEnclosedByAtlasRect(tx1.tilingTreatment, uvR.GetRect(), matR.GetRect(), tx1.ts[0].GetEncapsulatingSamplingRect().GetRect(), MB2_LogLevel.trace);
  421. Debug.Assert(false);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. }