-
Notifications
You must be signed in to change notification settings - Fork 699
[FEATURE] Procedural Texture2D/Sprite Generation #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2bfd45
Update for Texture2D/Sprite Generation
Scriptwonder 4767f4b
Texture Size Set
Scriptwonder 9993766
Add image input
Scriptwonder d672968
Update to release direct error with large tex2d
Scriptwonder 0c16699
Fix for AI advice
Scriptwonder c7b56ba
Update on action fetch line
Scriptwonder 5dd1568
Merge branch 'CoplayDev:beta' into texture2d/sprite
Scriptwonder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Newtonsoft.Json.Linq; | ||
| using UnityEngine; | ||
|
|
||
| namespace MCPForUnity.Editor.Helpers | ||
| { | ||
| public static class TextureOps | ||
| { | ||
| public static byte[] EncodeTexture(Texture2D texture, string assetPath) | ||
| { | ||
| if (texture == null) | ||
| return null; | ||
|
|
||
| string extension = Path.GetExtension(assetPath); | ||
| if (string.IsNullOrEmpty(extension)) | ||
| { | ||
| McpLog.Warn($"[TextureOps] No file extension for '{assetPath}', defaulting to PNG."); | ||
| return texture.EncodeToPNG(); | ||
| } | ||
|
|
||
| switch (extension.ToLowerInvariant()) | ||
| { | ||
| case ".png": | ||
| return texture.EncodeToPNG(); | ||
| case ".jpg": | ||
| case ".jpeg": | ||
| return texture.EncodeToJPG(); | ||
| default: | ||
| McpLog.Warn($"[TextureOps] Unsupported extension '{extension}' for '{assetPath}', defaulting to PNG."); | ||
| return texture.EncodeToPNG(); | ||
| } | ||
| } | ||
|
|
||
| public static void FillTexture(Texture2D texture, Color32 color) | ||
| { | ||
| if (texture == null) | ||
| return; | ||
|
|
||
| Color32[] pixels = new Color32[texture.width * texture.height]; | ||
| for (int i = 0; i < pixels.Length; i++) | ||
| { | ||
| pixels[i] = color; | ||
| } | ||
| texture.SetPixels32(pixels); | ||
| } | ||
|
|
||
| public static Color32 ParseColor32(JArray colorArray) | ||
| { | ||
| if (colorArray == null || colorArray.Count < 3) | ||
| return new Color32(255, 255, 255, 255); | ||
|
|
||
| byte r = (byte)Mathf.Clamp(colorArray[0].ToObject<int>(), 0, 255); | ||
| byte g = (byte)Mathf.Clamp(colorArray[1].ToObject<int>(), 0, 255); | ||
| byte b = (byte)Mathf.Clamp(colorArray[2].ToObject<int>(), 0, 255); | ||
| byte a = colorArray.Count > 3 ? (byte)Mathf.Clamp(colorArray[3].ToObject<int>(), 0, 255) : (byte)255; | ||
|
|
||
| return new Color32(r, g, b, a); | ||
| } | ||
|
|
||
| public static List<Color32> ParsePalette(JArray paletteArray) | ||
| { | ||
| if (paletteArray == null) | ||
| return null; | ||
|
|
||
| List<Color32> palette = new List<Color32>(); | ||
| foreach (var item in paletteArray) | ||
| { | ||
| if (item is JArray colorArray) | ||
| { | ||
| palette.Add(ParseColor32(colorArray)); | ||
| } | ||
| } | ||
| return palette.Count > 0 ? palette : null; | ||
| } | ||
|
|
||
| public static void ApplyPixelData(Texture2D texture, JToken pixelsToken, int width, int height) | ||
| { | ||
| ApplyPixelDataToRegion(texture, pixelsToken, 0, 0, width, height); | ||
| } | ||
|
|
||
| public static void ApplyPixelDataToRegion(Texture2D texture, JToken pixelsToken, int offsetX, int offsetY, int regionWidth, int regionHeight) | ||
| { | ||
| if (texture == null || pixelsToken == null) | ||
| return; | ||
|
|
||
| int textureWidth = texture.width; | ||
| int textureHeight = texture.height; | ||
|
|
||
| if (pixelsToken is JArray pixelArray) | ||
| { | ||
| int index = 0; | ||
| for (int y = 0; y < regionHeight && index < pixelArray.Count; y++) | ||
| { | ||
| for (int x = 0; x < regionWidth && index < pixelArray.Count; x++) | ||
| { | ||
| var pixelColor = pixelArray[index] as JArray; | ||
| if (pixelColor != null) | ||
| { | ||
| int px = offsetX + x; | ||
| int py = offsetY + y; | ||
| if (px >= 0 && px < textureWidth && py >= 0 && py < textureHeight) | ||
| { | ||
| texture.SetPixel(px, py, ParseColor32(pixelColor)); | ||
| } | ||
| } | ||
| index++; | ||
| } | ||
| } | ||
|
|
||
| int expectedCount = regionWidth * regionHeight; | ||
| if (pixelArray.Count != expectedCount) | ||
| { | ||
| McpLog.Warn($"[TextureOps] Pixel array size mismatch: expected {expectedCount} entries, got {pixelArray.Count}"); | ||
| } | ||
| } | ||
| else if (pixelsToken.Type == JTokenType.String) | ||
| { | ||
| string pixelString = pixelsToken.ToString(); | ||
| string base64 = pixelString.StartsWith("base64:") ? pixelString.Substring(7) : pixelString; | ||
| if (!pixelString.StartsWith("base64:")) | ||
| { | ||
| McpLog.Warn("[TextureOps] Base64 pixel data missing 'base64:' prefix; attempting to decode."); | ||
| } | ||
|
|
||
| byte[] rawData = Convert.FromBase64String(base64); | ||
|
|
||
| // Assume RGBA32 format: 4 bytes per pixel | ||
| int expectedBytes = regionWidth * regionHeight * 4; | ||
| if (rawData.Length == expectedBytes) | ||
| { | ||
| int pixelIndex = 0; | ||
| for (int y = 0; y < regionHeight; y++) | ||
| { | ||
| for (int x = 0; x < regionWidth; x++) | ||
| { | ||
| int px = offsetX + x; | ||
| int py = offsetY + y; | ||
| if (px >= 0 && px < textureWidth && py >= 0 && py < textureHeight) | ||
| { | ||
| int byteIndex = pixelIndex * 4; | ||
| Color32 color = new Color32( | ||
| rawData[byteIndex], | ||
| rawData[byteIndex + 1], | ||
| rawData[byteIndex + 2], | ||
| rawData[byteIndex + 3] | ||
| ); | ||
| texture.SetPixel(px, py, color); | ||
| } | ||
| pixelIndex++; | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| McpLog.Warn($"[TextureOps] Base64 data size mismatch: expected {expectedBytes} bytes, got {rawData.Length}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This foreach loop implicitly filters its target sequence - consider filtering the sequence explicitly using '.Where(...)'.