1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| using Sirenix.OdinInspector.Editor; using Sirenix.OdinInspector; using UnityEngine; using UnityEditor;
namespace NKG.ProjectS { public class MipmapGeneratorEditor : OdinEditorWindow { [LabelText("使用说明")] public string wikipediaURL = "整理自:https://zhuanlan.zhihu.com/p/415919803"; public const int maxMipLimit = 7; [InfoBox("mipmax为64,mipmin为8192,对应值为0-7")] [LabelText("Mip等级数量")][Range(0, maxMipLimit)] public int maxMipLevel = 7;
public Texture2D[] fillrateSourceTextures; public Color[] fillrateSourceColors;
[FolderPath] public string targetSavePath;
[Button("创建")] void Create() { int levelCount = maxMipLevel + 1; if (fillrateSourceTextures == null || fillrateSourceTextures.Length != levelCount) { fillrateSourceTextures = new Texture2D[maxMipLimit + 1]; fillrateSourceTextures[0] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/8K.png"); fillrateSourceTextures[1] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/4K.png"); fillrateSourceTextures[2] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/2K.png"); fillrateSourceTextures[3] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/1K.png"); fillrateSourceTextures[4] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/512.png"); fillrateSourceTextures[5] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/256.png"); fillrateSourceTextures[6] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/128.png"); fillrateSourceTextures[7] = AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.nkg.mipmapvis/64.png"); }
if (fillrateSourceColors == null || fillrateSourceColors.Length != levelCount) { fillrateSourceColors = new Color[maxMipLimit + 1]; fillrateSourceColors[0] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[1] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[2] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[3] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[4] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[5] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[6] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); fillrateSourceColors[7] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1f); }
int resolution = Mathf.FloorToInt(Mathf.Pow(2, maxMipLevel)) * 64; Texture2D texture = new Texture2D(resolution, resolution, TextureFormat.RGBA32, levelCount, true);
for (int i = 0; i < maxMipLevel + 1; i++) { int width = resolution >> i; int height = resolution >> i;
Texture2D sourcePatternTexture = fillrateSourceTextures[i + maxMipLimit - maxMipLevel]; int sourcePatternTextureWidth = sourcePatternTexture.width; int sourcePatternTextureHeight = sourcePatternTexture.height; Color fillColor = fillrateSourceColors[i + maxMipLimit - maxMipLevel];
Color[] texCol = sourcePatternTexture.GetPixels(0, 0, sourcePatternTextureWidth, sourcePatternTextureHeight);
for (int p = 0; p < texCol.Length; ++p) { var col = texCol[p]; col *= fillColor; texCol[p] = col; }
int copyStepX = width / sourcePatternTextureWidth; int copyStepY = height / sourcePatternTextureHeight;
for (int x = 0; x < copyStepX; ++x) { for (int y = 0; y < copyStepY; ++y) { texture.SetPixels(x * sourcePatternTextureWidth, y * sourcePatternTextureHeight, sourcePatternTextureWidth, sourcePatternTextureHeight, texCol, i); } } }
texture.Apply(false); AssetDatabase.CreateAsset(texture, string.IsNullOrEmpty(targetSavePath) ? $"Packages/com.nkg.mipmapvis/Generated/MipmapForCheck.asset" : $"{targetSavePath}/MipmapForCheck.asset"); AssetDatabase.Refresh(); EditorUtility.CompressTexture(AssetDatabase.LoadAssetAtPath<Texture2D>( string.IsNullOrEmpty(targetSavePath) ? $"Packages/com.nkg.mipmapvis/Generated/MipmapForCheck.asset" : $"{targetSavePath}/MipmapForCheck.asset"), TextureFormat.BC7, TextureCompressionQuality.Best); AssetDatabase.Refresh(); }
[MonKey.Command("Mipmap Generator", Help = "生成一张用于纹理合理性检测的多Mipmap的纹理", Category = "Tool")] public static void ShowWindow() { GetWindow(typeof(MipmapGeneratorEditor)); } } }
|