|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using Newtonsoft.Json.Linq; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace MCPForUnity.Editor.Helpers |
| 8 | +{ |
| 9 | + public static class TextureOps |
| 10 | + { |
| 11 | + public static byte[] EncodeTexture(Texture2D texture, string assetPath) |
| 12 | + { |
| 13 | + if (texture == null) |
| 14 | + return null; |
| 15 | + |
| 16 | + string extension = Path.GetExtension(assetPath); |
| 17 | + if (string.IsNullOrEmpty(extension)) |
| 18 | + { |
| 19 | + McpLog.Warn($"[TextureOps] No file extension for '{assetPath}', defaulting to PNG."); |
| 20 | + return texture.EncodeToPNG(); |
| 21 | + } |
| 22 | + |
| 23 | + switch (extension.ToLowerInvariant()) |
| 24 | + { |
| 25 | + case ".png": |
| 26 | + return texture.EncodeToPNG(); |
| 27 | + case ".jpg": |
| 28 | + case ".jpeg": |
| 29 | + return texture.EncodeToJPG(); |
| 30 | + default: |
| 31 | + McpLog.Warn($"[TextureOps] Unsupported extension '{extension}' for '{assetPath}', defaulting to PNG."); |
| 32 | + return texture.EncodeToPNG(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static void FillTexture(Texture2D texture, Color32 color) |
| 37 | + { |
| 38 | + if (texture == null) |
| 39 | + return; |
| 40 | + |
| 41 | + Color32[] pixels = new Color32[texture.width * texture.height]; |
| 42 | + for (int i = 0; i < pixels.Length; i++) |
| 43 | + { |
| 44 | + pixels[i] = color; |
| 45 | + } |
| 46 | + texture.SetPixels32(pixels); |
| 47 | + } |
| 48 | + |
| 49 | + public static Color32 ParseColor32(JArray colorArray) |
| 50 | + { |
| 51 | + if (colorArray == null || colorArray.Count < 3) |
| 52 | + return new Color32(255, 255, 255, 255); |
| 53 | + |
| 54 | + byte r = (byte)Mathf.Clamp(colorArray[0].ToObject<int>(), 0, 255); |
| 55 | + byte g = (byte)Mathf.Clamp(colorArray[1].ToObject<int>(), 0, 255); |
| 56 | + byte b = (byte)Mathf.Clamp(colorArray[2].ToObject<int>(), 0, 255); |
| 57 | + byte a = colorArray.Count > 3 ? (byte)Mathf.Clamp(colorArray[3].ToObject<int>(), 0, 255) : (byte)255; |
| 58 | + |
| 59 | + return new Color32(r, g, b, a); |
| 60 | + } |
| 61 | + |
| 62 | + public static List<Color32> ParsePalette(JArray paletteArray) |
| 63 | + { |
| 64 | + if (paletteArray == null) |
| 65 | + return null; |
| 66 | + |
| 67 | + List<Color32> palette = new List<Color32>(); |
| 68 | + foreach (var item in paletteArray) |
| 69 | + { |
| 70 | + if (item is JArray colorArray) |
| 71 | + { |
| 72 | + palette.Add(ParseColor32(colorArray)); |
| 73 | + } |
| 74 | + } |
| 75 | + return palette.Count > 0 ? palette : null; |
| 76 | + } |
| 77 | + |
| 78 | + public static void ApplyPixelData(Texture2D texture, JToken pixelsToken, int width, int height) |
| 79 | + { |
| 80 | + ApplyPixelDataToRegion(texture, pixelsToken, 0, 0, width, height); |
| 81 | + } |
| 82 | + |
| 83 | + public static void ApplyPixelDataToRegion(Texture2D texture, JToken pixelsToken, int offsetX, int offsetY, int regionWidth, int regionHeight) |
| 84 | + { |
| 85 | + if (texture == null || pixelsToken == null) |
| 86 | + return; |
| 87 | + |
| 88 | + int textureWidth = texture.width; |
| 89 | + int textureHeight = texture.height; |
| 90 | + |
| 91 | + if (pixelsToken is JArray pixelArray) |
| 92 | + { |
| 93 | + int index = 0; |
| 94 | + for (int y = 0; y < regionHeight && index < pixelArray.Count; y++) |
| 95 | + { |
| 96 | + for (int x = 0; x < regionWidth && index < pixelArray.Count; x++) |
| 97 | + { |
| 98 | + var pixelColor = pixelArray[index] as JArray; |
| 99 | + if (pixelColor != null) |
| 100 | + { |
| 101 | + int px = offsetX + x; |
| 102 | + int py = offsetY + y; |
| 103 | + if (px >= 0 && px < textureWidth && py >= 0 && py < textureHeight) |
| 104 | + { |
| 105 | + texture.SetPixel(px, py, ParseColor32(pixelColor)); |
| 106 | + } |
| 107 | + } |
| 108 | + index++; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + int expectedCount = regionWidth * regionHeight; |
| 113 | + if (pixelArray.Count != expectedCount) |
| 114 | + { |
| 115 | + McpLog.Warn($"[TextureOps] Pixel array size mismatch: expected {expectedCount} entries, got {pixelArray.Count}"); |
| 116 | + } |
| 117 | + } |
| 118 | + else if (pixelsToken.Type == JTokenType.String) |
| 119 | + { |
| 120 | + string pixelString = pixelsToken.ToString(); |
| 121 | + string base64 = pixelString.StartsWith("base64:") ? pixelString.Substring(7) : pixelString; |
| 122 | + if (!pixelString.StartsWith("base64:")) |
| 123 | + { |
| 124 | + McpLog.Warn("[TextureOps] Base64 pixel data missing 'base64:' prefix; attempting to decode."); |
| 125 | + } |
| 126 | + |
| 127 | + byte[] rawData = Convert.FromBase64String(base64); |
| 128 | + |
| 129 | + // Assume RGBA32 format: 4 bytes per pixel |
| 130 | + int expectedBytes = regionWidth * regionHeight * 4; |
| 131 | + if (rawData.Length == expectedBytes) |
| 132 | + { |
| 133 | + int pixelIndex = 0; |
| 134 | + for (int y = 0; y < regionHeight; y++) |
| 135 | + { |
| 136 | + for (int x = 0; x < regionWidth; x++) |
| 137 | + { |
| 138 | + int px = offsetX + x; |
| 139 | + int py = offsetY + y; |
| 140 | + if (px >= 0 && px < textureWidth && py >= 0 && py < textureHeight) |
| 141 | + { |
| 142 | + int byteIndex = pixelIndex * 4; |
| 143 | + Color32 color = new Color32( |
| 144 | + rawData[byteIndex], |
| 145 | + rawData[byteIndex + 1], |
| 146 | + rawData[byteIndex + 2], |
| 147 | + rawData[byteIndex + 3] |
| 148 | + ); |
| 149 | + texture.SetPixel(px, py, color); |
| 150 | + } |
| 151 | + pixelIndex++; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + McpLog.Warn($"[TextureOps] Base64 data size mismatch: expected {expectedBytes} bytes, got {rawData.Length}"); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments