MB3_ITextureCombinerPacker.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. internal interface MB_ITextureCombinerPacker
  8. {
  9. IEnumerator ConvertTexturesToReadableFormats(ProgressUpdateDelegate progressInfo,
  10. MB3_TextureCombiner.CombineTexturesIntoAtlasesCoroutineResult result,
  11. MB3_TextureCombinerPipeline.TexturePipelineData data,
  12. MB3_TextureCombiner combiner,
  13. MB2_EditorMethodsInterface textureEditorMethods,
  14. MB2_LogLevel LOG_LEVEL);
  15. AtlasPackingResult[] CalculateAtlasRectangles(MB3_TextureCombinerPipeline.TexturePipelineData data, bool doMultiAtlas, MB2_LogLevel LOG_LEVEL);
  16. IEnumerator CreateAtlases(ProgressUpdateDelegate progressInfo,
  17. MB3_TextureCombinerPipeline.TexturePipelineData data, MB3_TextureCombiner combiner,
  18. AtlasPackingResult packedAtlasRects,
  19. Texture2D[] atlases, MB2_EditorMethodsInterface textureEditorMethods,
  20. MB2_LogLevel LOG_LEVEL);
  21. }
  22. internal abstract class MB3_TextureCombinerPackerRoot : MB_ITextureCombinerPacker
  23. {
  24. internal static void CreateTemporaryTexturesForAtlas(List<MB_TexSet> distinctMaterialTextures, MB3_TextureCombiner combiner, int propIdx, MB3_TextureCombinerPipeline.TexturePipelineData data)
  25. {
  26. for (int texSetIdx = 0; texSetIdx < data.distinctMaterialTextures.Count; texSetIdx++)
  27. {
  28. MB_TexSet txs = data.distinctMaterialTextures[texSetIdx];
  29. MeshBakerMaterialTexture matTex = txs.ts[propIdx];
  30. if (matTex.isNull)
  31. {
  32. //create a small 16 x 16 texture to use in the atlas
  33. Color col = data.nonTexturePropertyBlender.GetColorForTemporaryTexture(txs.matsAndGOs.mats[0].mat, data.texPropertyNames[propIdx]);
  34. txs.CreateColoredTexToReplaceNull(data.texPropertyNames[propIdx].name, propIdx, data._fixOutOfBoundsUVs, combiner, col);
  35. }
  36. }
  37. }
  38. public static AtlasPackingResult[] CalculateAtlasRectanglesStatic(MB3_TextureCombinerPipeline.TexturePipelineData data, bool doMultiAtlas, MB2_LogLevel LOG_LEVEL)
  39. {
  40. List<Vector2> imageSizes = new List<Vector2>();
  41. for (int i = 0; i < data.distinctMaterialTextures.Count; i++)
  42. {
  43. imageSizes.Add(new Vector2(data.distinctMaterialTextures[i].idealWidth, data.distinctMaterialTextures[i].idealHeight));
  44. }
  45. MB2_TexturePacker tp = MB3_TextureCombinerPipeline.CreateTexturePacker(data._packingAlgorithm);
  46. tp.atlasMustBePowerOfTwo = data._meshBakerTexturePackerForcePowerOfTwo;
  47. List<AtlasPadding> paddings = new List<AtlasPadding>();
  48. for (int i = 0; i < imageSizes.Count; i++)
  49. {
  50. AtlasPadding padding = new AtlasPadding();
  51. padding.topBottom = data._atlasPadding;
  52. padding.leftRight = data._atlasPadding;
  53. if (data._packingAlgorithm == MB2_PackingAlgorithmEnum.MeshBakerTexturePacker_Horizontal) padding.leftRight = 0;
  54. if (data._packingAlgorithm == MB2_PackingAlgorithmEnum.MeshBakerTexturePacker_Vertical) padding.topBottom = 0;
  55. paddings.Add(padding);
  56. }
  57. return tp.GetRects(imageSizes, paddings, data._maxAtlasWidth, data._maxAtlasHeight, doMultiAtlas);
  58. }
  59. public static void MakeProceduralTexturesReadable(ProgressUpdateDelegate progressInfo,
  60. MB3_TextureCombiner.CombineTexturesIntoAtlasesCoroutineResult result,
  61. MB3_TextureCombinerPipeline.TexturePipelineData data,
  62. MB3_TextureCombiner combiner,
  63. MB2_EditorMethodsInterface textureEditorMethods,
  64. MB2_LogLevel LOG_LEVEL)
  65. {
  66. //Debug.LogError("TODO this should be done as close to textures being used as possible due to memory issues.");
  67. //make procedural materials readable
  68. /*
  69. for (int i = 0; i < combiner._proceduralMaterials.Count; i++)
  70. {
  71. if (!combiner._proceduralMaterials[i].proceduralMat.isReadable)
  72. {
  73. combiner._proceduralMaterials[i].originalIsReadableVal = combiner._proceduralMaterials[i].proceduralMat.isReadable;
  74. combiner._proceduralMaterials[i].proceduralMat.isReadable = true;
  75. //textureEditorMethods.AddProceduralMaterialFormat(_proceduralMaterials[i].proceduralMat);
  76. combiner._proceduralMaterials[i].proceduralMat.RebuildTexturesImmediately();
  77. }
  78. }
  79. //convert procedural textures to RAW format
  80. for (int i = 0; i < distinctMaterialTextures.Count; i++)
  81. {
  82. for (int j = 0; j < texPropertyNames.Count; j++)
  83. {
  84. if (distinctMaterialTextures[i].ts[j].IsProceduralTexture())
  85. {
  86. if (LOG_LEVEL >= MB2_LogLevel.debug) Debug.Log("Converting procedural texture to Textur2D:" + distinctMaterialTextures[i].ts[j].GetTexName() + " property:" + texPropertyNames[i]);
  87. Texture2D txx = distinctMaterialTextures[i].ts[j].ConvertProceduralToTexture2D(_temporaryTextures);
  88. distinctMaterialTextures[i].ts[j].t = txx;
  89. }
  90. }
  91. }
  92. */
  93. }
  94. public virtual IEnumerator ConvertTexturesToReadableFormats(ProgressUpdateDelegate progressInfo,
  95. MB3_TextureCombiner.CombineTexturesIntoAtlasesCoroutineResult result,
  96. MB3_TextureCombinerPipeline.TexturePipelineData data,
  97. MB3_TextureCombiner combiner,
  98. MB2_EditorMethodsInterface textureEditorMethods,
  99. MB2_LogLevel LOG_LEVEL)
  100. {
  101. Debug.Assert(!data.OnlyOneTextureInAtlasReuseTextures());
  102. //MakeProceduralTexturesReadable(progressInfo, result, data, combiner, textureEditorMethods, LOG_LEVEL);
  103. for (int i = 0; i < data.distinctMaterialTextures.Count; i++)
  104. {
  105. for (int j = 0; j < data.texPropertyNames.Count; j++)
  106. {
  107. MeshBakerMaterialTexture ts = data.distinctMaterialTextures[i].ts[j];
  108. if (!ts.isNull)
  109. {
  110. if (textureEditorMethods != null)
  111. {
  112. Texture tx = ts.GetTexture2D();
  113. TextureFormat format = TextureFormat.RGBA32;
  114. if (progressInfo != null) progressInfo(String.Format("Convert texture {0} to readable format ", tx), .5f);
  115. textureEditorMethods.AddTextureFormat((Texture2D)tx, format, data.texPropertyNames[j].isNormalMap);
  116. }
  117. }
  118. }
  119. }
  120. yield break;
  121. }
  122. public virtual AtlasPackingResult[] CalculateAtlasRectangles(MB3_TextureCombinerPipeline.TexturePipelineData data, bool doMultiAtlas, MB2_LogLevel LOG_LEVEL)
  123. {
  124. return CalculateAtlasRectanglesStatic(data, doMultiAtlas, LOG_LEVEL);
  125. }
  126. public abstract IEnumerator CreateAtlases(ProgressUpdateDelegate progressInfo,
  127. MB3_TextureCombinerPipeline.TexturePipelineData data, MB3_TextureCombiner combiner,
  128. AtlasPackingResult packedAtlasRects,
  129. Texture2D[] atlases, MB2_EditorMethodsInterface textureEditorMethods,
  130. MB2_LogLevel LOG_LEVEL);
  131. }
  132. }