MB3_TextureCombinerPackerMeshBakerFast.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace DigitalOpus.MB.Core
  6. {
  7. internal class MB3_TextureCombinerPackerMeshBakerFast : MB_ITextureCombinerPacker
  8. {
  9. public 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. {
  16. Debug.Assert(!data.OnlyOneTextureInAtlasReuseTextures());
  17. //MB3_TextureCombinerPackerRoot.MakeProceduralTexturesReadable(progressInfo, result, data, combiner, textureEditorMethods, LOG_LEVEL);
  18. yield break;
  19. }
  20. public virtual AtlasPackingResult[] CalculateAtlasRectangles(MB3_TextureCombinerPipeline.TexturePipelineData data, bool doMultiAtlas, MB2_LogLevel LOG_LEVEL)
  21. {
  22. Debug.Assert(!data.OnlyOneTextureInAtlasReuseTextures());
  23. return MB3_TextureCombinerPackerRoot.CalculateAtlasRectanglesStatic(data, doMultiAtlas, LOG_LEVEL);
  24. }
  25. public IEnumerator CreateAtlases(ProgressUpdateDelegate progressInfo,
  26. MB3_TextureCombinerPipeline.TexturePipelineData data, MB3_TextureCombiner combiner,
  27. AtlasPackingResult packedAtlasRects,
  28. Texture2D[] atlases, MB2_EditorMethodsInterface textureEditorMethods,
  29. MB2_LogLevel LOG_LEVEL)
  30. {
  31. Debug.Assert(!data.OnlyOneTextureInAtlasReuseTextures());
  32. Rect[] uvRects = packedAtlasRects.rects;
  33. int atlasSizeX = packedAtlasRects.atlasX;
  34. int atlasSizeY = packedAtlasRects.atlasY;
  35. if (LOG_LEVEL >= MB2_LogLevel.debug) Debug.Log("Generated atlas will be " + atlasSizeX + "x" + atlasSizeY);
  36. //create a game object
  37. GameObject renderAtlasesGO = null;
  38. try
  39. {
  40. renderAtlasesGO = new GameObject("MBrenderAtlasesGO");
  41. MB3_AtlasPackerRenderTexture atlasRenderTexture = renderAtlasesGO.AddComponent<MB3_AtlasPackerRenderTexture>();
  42. renderAtlasesGO.AddComponent<Camera>();
  43. if (data._considerNonTextureProperties && LOG_LEVEL >= MB2_LogLevel.warn) Debug.LogWarning("Blend Non-Texture Properties has limited functionality when used with Mesh Baker Texture Packer Fast.");
  44. for (int propIdx = 0; propIdx < data.numAtlases; propIdx++)
  45. {
  46. Texture2D atlas = null;
  47. if (!MB3_TextureCombinerPipeline._ShouldWeCreateAtlasForThisProperty(propIdx, data._considerNonTextureProperties, data.allTexturesAreNullAndSameColor))
  48. {
  49. atlas = null;
  50. if (LOG_LEVEL >= MB2_LogLevel.debug) Debug.Log("Not creating atlas for " + data.texPropertyNames[propIdx].name + " because textures are null and default value parameters are the same.");
  51. }
  52. else
  53. {
  54. GC.Collect();
  55. MB3_TextureCombinerPackerRoot.CreateTemporaryTexturesForAtlas(data.distinctMaterialTextures, combiner, propIdx, data);
  56. if (progressInfo != null) progressInfo("Creating Atlas '" + data.texPropertyNames[propIdx].name + "'", .01f);
  57. // ===========
  58. // configure it
  59. if (LOG_LEVEL >= MB2_LogLevel.debug) Debug.Log("About to render " + data.texPropertyNames[propIdx].name + " isNormal=" + data.texPropertyNames[propIdx].isNormalMap);
  60. atlasRenderTexture.LOG_LEVEL = LOG_LEVEL;
  61. atlasRenderTexture.width = atlasSizeX;
  62. atlasRenderTexture.height = atlasSizeY;
  63. atlasRenderTexture.padding = data._atlasPadding;
  64. atlasRenderTexture.rects = uvRects;
  65. atlasRenderTexture.textureSets = data.distinctMaterialTextures;
  66. atlasRenderTexture.indexOfTexSetToRender = propIdx;
  67. atlasRenderTexture.texPropertyName = data.texPropertyNames[propIdx];
  68. atlasRenderTexture.isNormalMap = data.texPropertyNames[propIdx].isNormalMap;
  69. atlasRenderTexture.fixOutOfBoundsUVs = data._fixOutOfBoundsUVs;
  70. atlasRenderTexture.considerNonTextureProperties = data._considerNonTextureProperties;
  71. atlasRenderTexture.resultMaterialTextureBlender = data.nonTexturePropertyBlender;
  72. // call render on it
  73. atlas = atlasRenderTexture.OnRenderAtlas(combiner);
  74. // destroy it
  75. // =============
  76. if (LOG_LEVEL >= MB2_LogLevel.debug) Debug.Log("Saving atlas " + data.texPropertyNames[propIdx].name + " w=" + atlas.width + " h=" + atlas.height + " id=" + atlas.GetInstanceID());
  77. }
  78. atlases[propIdx] = atlas;
  79. if (progressInfo != null) progressInfo("Saving atlas: '" + data.texPropertyNames[propIdx].name + "'", .04f);
  80. if (data.resultType == MB2_TextureBakeResults.ResultType.atlas)
  81. {
  82. if (data._saveAtlasesAsAssets && textureEditorMethods != null)
  83. {
  84. textureEditorMethods.SaveAtlasToAssetDatabase(atlases[propIdx], data.texPropertyNames[propIdx], propIdx, data.resultMaterial);
  85. }
  86. else
  87. {
  88. data.resultMaterial.SetTexture(data.texPropertyNames[propIdx].name, atlases[propIdx]);
  89. }
  90. data.resultMaterial.SetTextureOffset(data.texPropertyNames[propIdx].name, Vector2.zero);
  91. data.resultMaterial.SetTextureScale(data.texPropertyNames[propIdx].name, Vector2.one);
  92. }
  93. combiner._destroyTemporaryTextures(data.texPropertyNames[propIdx].name); // need to save atlases before doing this
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. //Debug.LogError(ex);
  99. Debug.LogException(ex);
  100. }
  101. finally
  102. {
  103. if (renderAtlasesGO != null)
  104. {
  105. MB_Utility.Destroy(renderAtlasesGO);
  106. }
  107. }
  108. yield break;
  109. }
  110. }
  111. }