Added texture export options
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.IO;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
@@ -7,7 +8,7 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
private Texture2D originalTexture;
|
||||
private Texture2D quantizedTexture;
|
||||
private Texture2D vramTexture; // New VRAM Texture
|
||||
private float[] clut; // Changed to 1D array
|
||||
private ushort[] clut; // Changed to 1D array
|
||||
private ushort[] indexedPixelData; // New field for indexed pixel data
|
||||
private int bpp = 4;
|
||||
private int targetWidth = 128;
|
||||
@@ -45,7 +46,7 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
{
|
||||
GUILayout.BeginVertical();
|
||||
GUILayout.Label("Original Texture");
|
||||
DrawTexturePreview(originalTexture, previewSize);
|
||||
DrawTexturePreview(originalTexture, previewSize, false);
|
||||
GUILayout.EndVertical();
|
||||
}
|
||||
|
||||
@@ -72,26 +73,78 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
GUILayout.Label("Color Lookup Table (CLUT)");
|
||||
DrawCLUT();
|
||||
}
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
if (indexedPixelData != null)
|
||||
{
|
||||
if (GUILayout.Button("Export pixel data"))
|
||||
{
|
||||
string path = EditorUtility.SaveFilePanel(
|
||||
"Save pixel data",
|
||||
"",
|
||||
"pixel_data",
|
||||
"bin"
|
||||
);
|
||||
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
|
||||
using (BinaryWriter writer = new BinaryWriter(fileStream))
|
||||
{
|
||||
foreach (ushort value in indexedPixelData)
|
||||
{
|
||||
writer.Write(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (clut != null)
|
||||
{
|
||||
if (GUILayout.Button("Export clut data"))
|
||||
{
|
||||
string path = EditorUtility.SaveFilePanel(
|
||||
"Save clut data",
|
||||
"",
|
||||
"clut_data",
|
||||
"bin"
|
||||
);
|
||||
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
|
||||
using (BinaryWriter writer = new BinaryWriter(fileStream))
|
||||
{
|
||||
foreach (ushort value in clut)
|
||||
{
|
||||
writer.Write(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void GenerateQuantizedPreview()
|
||||
{
|
||||
Texture2D resizedTexture = ResizeTexture(originalTexture, targetWidth, targetHeight);
|
||||
|
||||
if (bpp == 16)
|
||||
if (bpp == 16)
|
||||
{
|
||||
quantizedTexture = ConvertTo16Bpp(resizedTexture);
|
||||
clut = null;
|
||||
clut = null;
|
||||
vramTexture = resizedTexture;
|
||||
}
|
||||
else
|
||||
{
|
||||
var (indexedPixels, generatedClut) = ImageQuantizer.Quantize(resizedTexture, bpp, maxKMeans);
|
||||
|
||||
indexedPixelData = indexedPixels;
|
||||
indexedPixelData = indexedPixels;
|
||||
clut = generatedClut;
|
||||
|
||||
int pixelSize = bpp == 4 ? 4 : bpp == 8 ? 2 : 1;
|
||||
int pixelSize = bpp == 4 ? 4 : bpp == 8 ? 2 : 1;
|
||||
quantizedTexture = new Texture2D(resizedTexture.width, resizedTexture.height);
|
||||
Color[] quantizedColors = new Color[resizedTexture.width * resizedTexture.height];
|
||||
|
||||
@@ -110,18 +163,23 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
else if (pixelSize == 2)
|
||||
{
|
||||
int packedValue = indexedPixelData[pixelIndex];
|
||||
index = (packedValue >> ((x % 2) * 8)) & 0xFF;
|
||||
index = (packedValue >> ((x % 2) * 8)) & 0xFF;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
index = indexedPixelData[pixelIndex];
|
||||
}
|
||||
|
||||
|
||||
Vector3 color = new Vector3(clut[index * 3], clut[index * 3 + 1], clut[index * 3 + 2]);
|
||||
|
||||
Vector3 color = new Vector3(
|
||||
(clut[index] & 31) / 31.0f, // Red: bits 0–4
|
||||
((clut[index] >> 5) & 31) / 31.0f, // Green: bits 5–9
|
||||
((clut[index] >> 10) & 31) / 31.0f // Blue: bits 10–14
|
||||
);
|
||||
quantizedColors[y * resizedTexture.width + x] = new Color(color.x, color.y, color.z);
|
||||
|
||||
|
||||
if ((x % pixelSize) == (pixelSize - 1))
|
||||
{
|
||||
pixelIndex++;
|
||||
@@ -145,11 +203,11 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
|
||||
if (bpp == 4)
|
||||
{
|
||||
adjustedWidth = Mathf.CeilToInt(width / 4f);
|
||||
adjustedWidth = Mathf.CeilToInt(width / 4f);
|
||||
}
|
||||
else if (bpp == 8)
|
||||
{
|
||||
adjustedWidth = Mathf.CeilToInt(width / 2f);
|
||||
adjustedWidth = Mathf.CeilToInt(width / 2f);
|
||||
}
|
||||
|
||||
Texture2D vramTexture = new Texture2D(adjustedWidth, height);
|
||||
@@ -160,9 +218,9 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
{
|
||||
int index = indexedData[i];
|
||||
|
||||
float r = Mathf.Floor((index >> 11) & 31) / 31.0f;
|
||||
float g = Mathf.Floor((index >> 5) & 31) / 31.0f;
|
||||
float b = Mathf.Floor(index & 31) / 31.0f;
|
||||
float r = (index & 31) / 31.0f; // Red: bits 0–4
|
||||
float g = ((index >> 5) & 31) / 31.0f; // Green: bits 5–9
|
||||
float b = ((index >> 10) & 31) / 31.0f; // Blue: bits 10–14
|
||||
|
||||
vramColors[i] = new Color(r, g, b);
|
||||
}
|
||||
@@ -174,6 +232,7 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Texture2D ConvertTo16Bpp(Texture2D source)
|
||||
{
|
||||
int width = source.width;
|
||||
@@ -187,12 +246,11 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
{
|
||||
Color pixel = originalPixels[i];
|
||||
|
||||
// Convert to 5 bits per channel (R5G5B5)
|
||||
float r = Mathf.Floor(pixel.r * 31) / 31.0f; // 5 bits for red
|
||||
float g = Mathf.Floor(pixel.g * 31) / 31.0f; // 5 bits for green
|
||||
float b = Mathf.Floor(pixel.b * 31) / 31.0f; // 5 bits for blue
|
||||
|
||||
convertedPixels[i] = new Color(r, g, b, pixel.a);
|
||||
convertedPixels[i] = new Color(r, g, b, pixel.a);
|
||||
}
|
||||
|
||||
convertedTexture.SetPixels(convertedPixels);
|
||||
@@ -201,10 +259,37 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
return convertedTexture;
|
||||
}
|
||||
|
||||
private void DrawTexturePreview(Texture2D texture, int size)
|
||||
private void DrawTexturePreview(Texture2D texture, int size, bool flipY = true)
|
||||
{
|
||||
Rect rect = GUILayoutUtility.GetRect(size, size, GUILayout.ExpandWidth(false));
|
||||
EditorGUI.DrawPreviewTexture(rect, texture, null, ScaleMode.ScaleToFit, 0, 0, ColorWriteMask.All);
|
||||
|
||||
// Flip the texture on the Y-axis
|
||||
Texture2D displayedTexture = flipY ? FlipTextureY(texture) : texture;
|
||||
EditorGUI.DrawPreviewTexture(rect, displayedTexture, null, ScaleMode.ScaleToFit, 0, 0, ColorWriteMask.All);
|
||||
}
|
||||
|
||||
private Texture2D FlipTextureY(Texture2D texture)
|
||||
{
|
||||
Color[] originalPixels = texture.GetPixels();
|
||||
Color[] flippedPixels = new Color[originalPixels.Length];
|
||||
|
||||
int width = texture.width;
|
||||
int height = texture.height;
|
||||
|
||||
// Flip the pixels on the Y-axis
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
flippedPixels[(height - y - 1) * width + x] = originalPixels[y * width + x];
|
||||
}
|
||||
}
|
||||
|
||||
Texture2D flippedTexture = new Texture2D(width, height);
|
||||
flippedTexture.SetPixels(flippedPixels);
|
||||
flippedTexture.Apply();
|
||||
|
||||
return flippedTexture;
|
||||
}
|
||||
|
||||
private void DrawCLUT()
|
||||
@@ -216,7 +301,7 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
int totalColors = clut.Length / 3;
|
||||
int totalColors = clut.Length;
|
||||
int totalRows = Mathf.CeilToInt((float)totalColors / maxColorsPerRow);
|
||||
|
||||
for (int row = 0; row < totalRows; row++)
|
||||
@@ -228,7 +313,13 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
for (int col = 0; col < colorsInRow; col++)
|
||||
{
|
||||
int index = row * maxColorsPerRow + col;
|
||||
Vector3 color = new Vector3(clut[index * 3], clut[index * 3 + 1], clut[index * 3 + 2]);
|
||||
|
||||
Vector3 color = new Vector3(
|
||||
(clut[index] & 31) / 31.0f, // Red: bits 0–4
|
||||
((clut[index] >> 5) & 31) / 31.0f, // Green: bits 5–9
|
||||
((clut[index] >> 10) & 31) / 31.0f // Blue: bits 10–14
|
||||
);
|
||||
|
||||
Rect rect = GUILayoutUtility.GetRect(swatchSize, swatchSize, GUILayout.ExpandWidth(false));
|
||||
EditorGUI.DrawRect(rect, new Color(color.x, color.y, color.z));
|
||||
}
|
||||
@@ -237,6 +328,7 @@ public class QuantizedPreviewWindow : EditorWindow
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight)
|
||||
{
|
||||
RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
|
||||
|
||||
Reference in New Issue
Block a user