Quantization window now handles 15bpp, Updated layout, Cluts are shorter if less centroids were found
This commit is contained in:
@@ -2,6 +2,8 @@ using UnityEditor;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using PSXSplash.RuntimeCode;
|
using PSXSplash.RuntimeCode;
|
||||||
using UnityEngine.Rendering;
|
using UnityEngine.Rendering;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEditor.PackageManager.UI;
|
||||||
|
|
||||||
public class QuantizedPreviewWindow : EditorWindow
|
public class QuantizedPreviewWindow : EditorWindow
|
||||||
{
|
{
|
||||||
@@ -14,10 +16,12 @@ public class QuantizedPreviewWindow : EditorWindow
|
|||||||
private int targetHeight = 128;
|
private int targetHeight = 128;
|
||||||
private int previewSize = 256;
|
private int previewSize = 256;
|
||||||
|
|
||||||
|
|
||||||
[MenuItem("Window/Quantized Preview")]
|
[MenuItem("Window/Quantized Preview")]
|
||||||
public static void ShowWindow()
|
public static void ShowWindow()
|
||||||
{
|
{
|
||||||
GetWindow<QuantizedPreviewWindow>("Quantized Preview");
|
QuantizedPreviewWindow win = GetWindow<QuantizedPreviewWindow>("Quantized Preview");
|
||||||
|
win.minSize = new Vector2(800, 700);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnGUI()
|
private void OnGUI()
|
||||||
@@ -29,31 +33,39 @@ public class QuantizedPreviewWindow : EditorWindow
|
|||||||
targetWidth = EditorGUILayout.IntField("Target Width", targetWidth);
|
targetWidth = EditorGUILayout.IntField("Target Width", targetWidth);
|
||||||
targetHeight = EditorGUILayout.IntField("Target Height", targetHeight);
|
targetHeight = EditorGUILayout.IntField("Target Height", targetHeight);
|
||||||
|
|
||||||
bpp = EditorGUILayout.IntPopup("Bits Per Pixel", bpp, new[] { "4 bpp", "8 bpp" }, new[] { 4, 8 });
|
bpp = EditorGUILayout.IntPopup("Bits Per Pixel", bpp, new[] { "4 bpp", "8 bpp", "15 bpp" }, new[] { 4, 8, 15 });
|
||||||
|
|
||||||
if (GUILayout.Button("Generate Quantized Preview") && originalTexture != null)
|
if (GUILayout.Button("Generate Quantized Preview") && originalTexture != null)
|
||||||
{
|
{
|
||||||
GenerateQuantizedPreview();
|
GenerateQuantizedPreview();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GUILayout.BeginHorizontal();
|
||||||
|
|
||||||
if (originalTexture != null)
|
if (originalTexture != null)
|
||||||
{
|
{
|
||||||
|
GUILayout.BeginVertical();
|
||||||
GUILayout.Label("Original Texture");
|
GUILayout.Label("Original Texture");
|
||||||
DrawTexturePreview(originalTexture, previewSize);
|
DrawTexturePreview(originalTexture, previewSize);
|
||||||
|
GUILayout.EndVertical();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resizedTexture != null)
|
if (resizedTexture != null)
|
||||||
{
|
{
|
||||||
|
GUILayout.BeginVertical();
|
||||||
GUILayout.Label("Resized Texture");
|
GUILayout.Label("Resized Texture");
|
||||||
DrawTexturePreview(resizedTexture, previewSize);
|
DrawTexturePreview(resizedTexture, previewSize);
|
||||||
|
GUILayout.EndVertical();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (quantizedTexture != null)
|
if (quantizedTexture != null)
|
||||||
{
|
{
|
||||||
|
GUILayout.BeginVertical();
|
||||||
GUILayout.Label("Quantized Texture");
|
GUILayout.Label("Quantized Texture");
|
||||||
DrawTexturePreview(quantizedTexture, previewSize);
|
DrawTexturePreview(quantizedTexture, previewSize);
|
||||||
|
GUILayout.EndVertical();
|
||||||
}
|
}
|
||||||
|
GUILayout.EndHorizontal();
|
||||||
if (clut != null)
|
if (clut != null)
|
||||||
{
|
{
|
||||||
GUILayout.Label("Color Lookup Table (CLUT)");
|
GUILayout.Label("Color Lookup Table (CLUT)");
|
||||||
@@ -65,6 +77,13 @@ public class QuantizedPreviewWindow : EditorWindow
|
|||||||
{
|
{
|
||||||
resizedTexture = ResizeTexture(originalTexture, targetWidth, targetHeight);
|
resizedTexture = ResizeTexture(originalTexture, targetWidth, targetHeight);
|
||||||
|
|
||||||
|
if (bpp == 15) // Handle 15bpp (R5G5B5) without CLUT
|
||||||
|
{
|
||||||
|
quantizedTexture = ConvertTo15Bpp(resizedTexture);
|
||||||
|
clut = null; // No CLUT for 15bpp
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
int maxColors = (int)Mathf.Pow(2, bpp);
|
int maxColors = (int)Mathf.Pow(2, bpp);
|
||||||
|
|
||||||
var (quantizedPixels, generatedClut) = ImageQuantizer.Quantize(resizedTexture, maxColors);
|
var (quantizedPixels, generatedClut) = ImageQuantizer.Quantize(resizedTexture, maxColors);
|
||||||
@@ -89,6 +108,34 @@ public class QuantizedPreviewWindow : EditorWindow
|
|||||||
|
|
||||||
clut = generatedClut;
|
clut = generatedClut;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Texture2D ConvertTo15Bpp(Texture2D source)
|
||||||
|
{
|
||||||
|
int width = source.width;
|
||||||
|
int height = source.height;
|
||||||
|
Texture2D convertedTexture = new Texture2D(width, height);
|
||||||
|
|
||||||
|
Color[] originalPixels = source.GetPixels();
|
||||||
|
Color[] convertedPixels = new Color[originalPixels.Length];
|
||||||
|
|
||||||
|
for (int i = 0; i < originalPixels.Length; i++)
|
||||||
|
{
|
||||||
|
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); // Maintain alpha channel
|
||||||
|
}
|
||||||
|
|
||||||
|
convertedTexture.SetPixels(convertedPixels);
|
||||||
|
convertedTexture.Apply();
|
||||||
|
|
||||||
|
return convertedTexture;
|
||||||
|
}
|
||||||
|
|
||||||
private void DrawTexturePreview(Texture2D texture, int size)
|
private void DrawTexturePreview(Texture2D texture, int size)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -82,8 +82,9 @@ public class ImageQuantizer
|
|||||||
pixelArray[x, y, 2] = centroidColor.z; // Blue
|
pixelArray[x, y, 2] = centroidColor.z; // Blue
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector3[,] clut = new Vector3[maxColors, 1];
|
int actualColors = centroids.Count;
|
||||||
for (int i = 0; i < centroids.Count; i++)
|
Vector3[,] clut = new Vector3[actualColors, 1];
|
||||||
|
for (int i = 0; i < actualColors; i++)
|
||||||
{
|
{
|
||||||
clut[i, 0] = centroids[i];
|
clut[i, 0] = centroids[i];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ namespace PSXSplash.RuntimeCode
|
|||||||
[Range(1, 256)]
|
[Range(1, 256)]
|
||||||
public int Height = 128;
|
public int Height = 128;
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: This just uses the quantization and doesn't store the result anywhere
|
||||||
|
// Maybe it should return the image and the clut back to the SceneExporter / The Editor code for only-texture export?
|
||||||
public void Export(GameObject gameObject)
|
public void Export(GameObject gameObject)
|
||||||
{
|
{
|
||||||
Debug.Log($"Export: {this}");
|
Debug.Log($"Export: {this}");
|
||||||
|
|||||||
Reference in New Issue
Block a user