Added VRAM preview, changed the quantization output so it resables what we wish to export

This commit is contained in:
2025-01-14 14:29:52 +01:00
parent 65e52940ec
commit 242d6ce94c
2 changed files with 144 additions and 49 deletions

View File

@@ -1,22 +1,20 @@
using UnityEditor;
using UnityEngine;
using PSXSplash.RuntimeCode;
using UnityEngine.Rendering;
using System.Threading.Tasks;
using UnityEditor.PackageManager.UI;
public class QuantizedPreviewWindow : EditorWindow
{
private Texture2D originalTexture;
private Texture2D resizedTexture;
private Texture2D quantizedTexture;
private Vector3[,] clut;
private Texture2D vramTexture; // New VRAM Texture
private float[] clut; // Changed to 1D array
private ushort[] indexedPixelData; // New field for indexed pixel data
private int bpp = 4;
private int targetWidth = 128;
private int targetHeight = 128;
private int maxKMeans = 100;
private int previewSize = 256;
[MenuItem("Window/Quantized Preview")]
public static void ShowWindow()
{
@@ -33,7 +31,8 @@ public class QuantizedPreviewWindow : EditorWindow
targetWidth = EditorGUILayout.IntField("Target Width", targetWidth);
targetHeight = EditorGUILayout.IntField("Target Height", targetHeight);
bpp = EditorGUILayout.IntPopup("Bits Per Pixel", bpp, new[] { "4 bpp", "8 bpp", "15 bpp" }, new[] { 4, 8, 15 });
bpp = EditorGUILayout.IntPopup("Bits Per Pixel", bpp, new[] { "4 bpp", "8 bpp", "16 bpp" }, new[] { 4, 8, 16 });
maxKMeans = EditorGUILayout.IntField("Max K-Means", maxKMeans);
if (GUILayout.Button("Generate Quantized Preview") && originalTexture != null)
{
@@ -50,11 +49,11 @@ public class QuantizedPreviewWindow : EditorWindow
GUILayout.EndVertical();
}
if (resizedTexture != null)
if (vramTexture != null)
{
GUILayout.BeginVertical();
GUILayout.Label("Resized Texture");
DrawTexturePreview(resizedTexture, previewSize);
GUILayout.Label("VRAM View (Indexed Data as 16bpp)");
DrawTexturePreview(vramTexture, previewSize);
GUILayout.EndVertical();
}
@@ -65,7 +64,9 @@ public class QuantizedPreviewWindow : EditorWindow
DrawTexturePreview(quantizedTexture, previewSize);
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
if (clut != null)
{
GUILayout.Label("Color Lookup Table (CLUT)");
@@ -75,42 +76,105 @@ public class QuantizedPreviewWindow : EditorWindow
private void GenerateQuantizedPreview()
{
resizedTexture = ResizeTexture(originalTexture, targetWidth, targetHeight);
Texture2D resizedTexture = ResizeTexture(originalTexture, targetWidth, targetHeight);
if (bpp == 15) // Handle 15bpp (R5G5B5) without CLUT
if (bpp == 16)
{
quantizedTexture = ConvertTo15Bpp(resizedTexture);
clut = null; // No CLUT for 15bpp
quantizedTexture = ConvertTo16Bpp(resizedTexture);
clut = null;
vramTexture = resizedTexture;
}
else
{
int maxColors = (int)Mathf.Pow(2, bpp);
var (indexedPixels, generatedClut) = ImageQuantizer.Quantize(resizedTexture, bpp, maxKMeans);
var (quantizedPixels, generatedClut) = ImageQuantizer.Quantize(resizedTexture, maxColors);
indexedPixelData = indexedPixels;
clut = generatedClut;
int pixelSize = bpp == 4 ? 4 : bpp == 8 ? 2 : 1;
quantizedTexture = new Texture2D(resizedTexture.width, resizedTexture.height);
Color[] quantizedColors = new Color[resizedTexture.width * resizedTexture.height];
int pixelIndex = 0;
for (int y = 0; y < resizedTexture.height; y++)
{
for (int x = 0; x < resizedTexture.width; x++)
{
quantizedColors[y * resizedTexture.width + x] = new Color(
quantizedPixels[x, y, 0],
quantizedPixels[x, y, 1],
quantizedPixels[x, y, 2]
);
int index = 0;
if (pixelSize == 4)
{
int packedValue = indexedPixelData[pixelIndex];
index = (packedValue >> ((x % 4) * 4)) & 0xF;
}
else if (pixelSize == 2)
{
int packedValue = indexedPixelData[pixelIndex];
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]);
quantizedColors[y * resizedTexture.width + x] = new Color(color.x, color.y, color.z);
if ((x % pixelSize) == (pixelSize - 1))
{
pixelIndex++;
}
}
}
quantizedTexture.SetPixels(quantizedColors);
quantizedTexture.Apply();
clut = generatedClut;
vramTexture = CreateVramTexture(resizedTexture.width, resizedTexture.height, indexedPixelData);
}
}
private Texture2D ConvertTo15Bpp(Texture2D source)
private Texture2D CreateVramTexture(int width, int height, ushort[] indexedData)
{
int adjustedWidth = width;
if (bpp == 4)
{
adjustedWidth = Mathf.CeilToInt(width / 4f);
}
else if (bpp == 8)
{
adjustedWidth = Mathf.CeilToInt(width / 2f);
}
Texture2D vramTexture = new Texture2D(adjustedWidth, height);
Color[] vramColors = new Color[adjustedWidth * height];
for (int i = 0; i < indexedData.Length; i++)
{
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;
vramColors[i] = new Color(r, g, b);
}
vramTexture.SetPixels(vramColors);
vramTexture.Apply();
return vramTexture;
}
private Texture2D ConvertTo16Bpp(Texture2D source)
{
int width = source.width;
int height = source.height;
@@ -128,7 +192,7 @@ public class QuantizedPreviewWindow : EditorWindow
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); // Maintain alpha channel
convertedPixels[i] = new Color(r, g, b, pixel.a);
}
convertedTexture.SetPixels(convertedPixels);
@@ -152,7 +216,7 @@ public class QuantizedPreviewWindow : EditorWindow
GUILayout.Space(10);
int totalColors = clut.GetLength(0);
int totalColors = clut.Length / 3;
int totalRows = Mathf.CeilToInt((float)totalColors / maxColorsPerRow);
for (int row = 0; row < totalRows; row++)
@@ -164,7 +228,7 @@ public class QuantizedPreviewWindow : EditorWindow
for (int col = 0; col < colorsInRow; col++)
{
int index = row * maxColorsPerRow + col;
Vector3 color = clut[index, 0];
Vector3 color = new Vector3(clut[index * 3], clut[index * 3 + 1], clut[index * 3 + 2]);
Rect rect = GUILayoutUtility.GetRect(swatchSize, swatchSize, GUILayout.ExpandWidth(false));
EditorGUI.DrawRect(rect, new Color(color.x, color.y, color.z));
}
@@ -173,7 +237,6 @@ public class QuantizedPreviewWindow : EditorWindow
}
}
private Texture2D ResizeTexture(Texture2D source, int newWidth, int newHeight)
{
RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);