Added texture Quantization and a window to test it

This commit is contained in:
2025-01-14 00:44:45 +01:00
parent 4c108f2ad6
commit f0bc61dd92
9 changed files with 339 additions and 17 deletions

137
Runtime/ImageQuantizer.cs Normal file
View File

@@ -0,0 +1,137 @@
using System.Collections.Generic;
using UnityEngine;
public class ImageQuantizer
{
/// <summary>
/// Quantizes a texture and outputs a 3D pixel array.
/// </summary>
/// <param name="image">The input texture.</param>
/// <param name="maxColors">The maximum number of colors in the quantized image.</param>
/// <param name="maxIterations">The maximum number of iterations for the k-means algorithm.</param>
/// <returns>A tuple containing a 3D pixel array and the color lookup table.</returns>
public static (float[,,], Vector3[,]) Quantize(Texture2D image, int maxColors, int maxIterations = 10)
{
int width = image.width;
int height = image.height;
List<Vector3> centroids = InitializeCentroids(image, maxColors);
Color[] pixels = image.GetPixels();
Vector3[] pixelColors = new Vector3[pixels.Length];
for (int i = 0; i < pixels.Length; i++)
{
pixelColors[i] = new Vector3(pixels[i].r, pixels[i].g, pixels[i].b);
}
// Storage for pixel-to-centroid assignments
int[] assignments = new int[pixelColors.Length];
for (int iteration = 0; iteration < maxIterations; iteration++)
{
bool centroidsChanged = false;
// Step 1: Assign each pixel to the closest centroid
for (int i = 0; i < pixelColors.Length; i++)
{
int closestCentroid = GetClosestCentroid(pixelColors[i], centroids);
if (assignments[i] != closestCentroid)
{
assignments[i] = closestCentroid;
centroidsChanged = true;
}
}
// Step 2: Recalculate centroids
Vector3[] newCentroids = new Vector3[centroids.Count];
int[] centroidCounts = new int[centroids.Count];
for (int i = 0; i < assignments.Length; i++)
{
int centroidIndex = assignments[i];
newCentroids[centroidIndex] += pixelColors[i];
centroidCounts[centroidIndex]++;
}
for (int i = 0; i < centroids.Count; i++)
{
if (centroidCounts[i] > 0)
{
newCentroids[i] /= centroidCounts[i];
}
else
{
newCentroids[i] = RandomizeCentroid(image);
}
}
if (!centroidsChanged) break;
centroids = new List<Vector3>(newCentroids);
}
float[,,] pixelArray = new float[width, height, 3];
for (int i = 0; i < pixelColors.Length; i++)
{
int x = i % width;
int y = i / width;
Vector3 centroidColor = centroids[assignments[i]];
pixelArray[x, y, 0] = centroidColor.x; // Red
pixelArray[x, y, 1] = centroidColor.y; // Green
pixelArray[x, y, 2] = centroidColor.z; // Blue
}
Vector3[,] clut = new Vector3[maxColors, 1];
for (int i = 0; i < centroids.Count; i++)
{
clut[i, 0] = centroids[i];
}
return (pixelArray, clut);
}
private static List<Vector3> InitializeCentroids(Texture2D image, int maxColors)
{
List<Vector3> centroids = new List<Vector3>();
Color[] pixels = image.GetPixels();
HashSet<Vector3> uniqueColors = new HashSet<Vector3>();
foreach (Color pixel in pixels)
{
Vector3 color = new Vector3(pixel.r, pixel.g, pixel.b);
if (!uniqueColors.Contains(color))
{
uniqueColors.Add(color);
centroids.Add(color);
if (centroids.Count >= maxColors) break;
}
}
return centroids;
}
private static Vector3 RandomizeCentroid(Texture2D image)
{
Color randomPixel = image.GetPixel(Random.Range(0, image.width), Random.Range(0, image.height));
return new Vector3(randomPixel.r, randomPixel.g, randomPixel.b);
}
private static int GetClosestCentroid(Vector3 color, List<Vector3> centroids)
{
int closestCentroid = 0;
float minDistanceSq = float.MaxValue;
for (int i = 0; i < centroids.Count; i++)
{
float distanceSq = (color - centroids[i]).sqrMagnitude;
if (distanceSq < minDistanceSq)
{
minDistanceSq = distanceSq;
closestCentroid = i;
}
}
return closestCentroid;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 1291c85b333132b8392486949420d31a

View File

@@ -1,12 +1,15 @@
using UnityEngine;
namespace PSXSplash.RuntimeCode {
namespace PSXSplash.RuntimeCode
{
[System.Serializable]
public class PSXMesh {
public class PSXMesh
{
public bool TriangulateMesh = true;
public void Export() {
public void Export(GameObject gameObject)
{
Debug.Log($"Export: {this}");
}
}

View File

@@ -3,7 +3,7 @@ using UnityEngine.SceneManagement;
namespace PSXSplash.RuntimeCode
{
public class PSXSceneExporter : MonoBehaviour
public class PSXSceneExporter : MonoBehaviour
{
public void Export()
{

View File

@@ -1,22 +1,51 @@
using UnityEngine;
namespace PSXSplash.RuntimeCode {
public enum PSXTextureType {
TEX_4BPP,
namespace PSXSplash.RuntimeCode
{
public enum PSXTextureType
{
TEX_4BPP = 4,
TEX_8BPP,
TEX_8BPP = 8,
TEX16_BPP
TEX16_BPP = 16
}
[System.Serializable]
public class PSXTexture
{
public PSXTextureType TextureType;
public PSXTextureType TextureType = PSXTextureType.TEX_8BPP;
public bool Dithering = true;
public void Export() {
[Range(1, 256)]
public int Width = 128;
[Range(1, 256)]
public int Height = 128;
public void Export(GameObject gameObject)
{
Debug.Log($"Export: {this}");
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
if (meshRenderer != null)
{
Texture texture = meshRenderer.material.mainTexture;
if (texture is Texture2D)
{
Texture2D originalTexture = (Texture2D)texture;
Texture2D newTexture = new Texture2D(originalTexture.width, originalTexture.height, originalTexture.format, false);
newTexture.SetPixels(originalTexture.GetPixels());
newTexture.Apply();
Debug.Log((int)TextureType);
newTexture.Reinitialize(Width, Height, UnityEngine.Experimental.Rendering.GraphicsFormat.R8G8B8_UInt, false);
var (quantizedPixels, clut) = ImageQuantizer.Quantize(originalTexture, (int)TextureType);
}
}
}
}
}