287 lines
9.0 KiB
C#
287 lines
9.0 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using static PSXSplash.RuntimeCode.TextureQuantizer;
|
|
|
|
|
|
namespace PSXSplash.RuntimeCode
|
|
{
|
|
|
|
/// <summary>
|
|
/// Represents the bit depth of a PSX texture.
|
|
/// </summary>
|
|
public enum PSXBPP
|
|
{
|
|
TEX_4BIT = 4,
|
|
TEX_8BIT = 8,
|
|
TEX_16BIT = 15
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents a pixel in VRAM with RGB components and a semi-transparency flag.
|
|
/// </summary>
|
|
public struct VRAMPixel
|
|
{
|
|
private ushort r; // 0-4 bits
|
|
private ushort g; // 5-9 bits
|
|
private ushort b; // 10-14 bits
|
|
|
|
/// <summary>
|
|
/// Red component (0-4 bits).
|
|
/// </summary>
|
|
public ushort R
|
|
{
|
|
get => r;
|
|
set => r = (ushort)(value & 0b11111);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Green component (0-4 bits).
|
|
/// </summary>
|
|
public ushort G
|
|
{
|
|
get => g;
|
|
set => g = (ushort)(value & 0b11111);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Blue component (0-4 bits).
|
|
/// </summary>
|
|
public ushort B
|
|
{
|
|
get => b;
|
|
set => b = (ushort)(value & 0b11111);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the pixel is semi-transparent (15th bit).
|
|
/// </summary>
|
|
public bool SemiTransparent { get; set; } // 15th bit
|
|
|
|
|
|
/// <summary>
|
|
/// Packs the RGB components and semi-transparency flag into a single ushort value.
|
|
/// </summary>
|
|
/// <returns>The packed ushort value.</returns>
|
|
public ushort Pack()
|
|
{
|
|
return (ushort)((r << 11) | (g << 6) | (b << 1) | (SemiTransparent ? 1 : 0));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Unpacks the RGB components and semi-transparency flag from a packed ushort value.
|
|
/// </summary>
|
|
/// <param name="packedValue">The packed ushort value.</param>
|
|
public void Unpack(ushort packedValue)
|
|
{
|
|
r = (ushort)((packedValue >> 11) & 0b11111);
|
|
g = (ushort)((packedValue >> 6) & 0b11111);
|
|
b = (ushort)((packedValue >> 1) & 0b11111);
|
|
SemiTransparent = (packedValue & 0b1) != 0;
|
|
}
|
|
|
|
public Color GetUnityColor() {
|
|
return new Color(R / 31.0f, G / 31.0f, B / 31.0f);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Represents a PSX texture with various bit depths and provides methods to create and manipulate the texture.
|
|
/// </summary>
|
|
public class PSXTexture2D
|
|
{
|
|
public int Width { get; set; }
|
|
public int QuantizedWidth { get; set; }
|
|
public int Height { get; set; }
|
|
public int[] PixelIndices { get; set; }
|
|
public List<VRAMPixel> ColorPalette = new List<VRAMPixel>();
|
|
public PSXBPP BitDepth { get; set; }
|
|
|
|
public Texture2D OriginalTexture;
|
|
|
|
// Within supertexture
|
|
public int PackingX;
|
|
public int PackingY;
|
|
|
|
public int TexpageNum;
|
|
|
|
// Absolute positioning
|
|
public int ClutPackingX;
|
|
public int ClutPackingY;
|
|
|
|
private int _maxColors;
|
|
|
|
// Used only for 16bpp
|
|
public ushort[] ImageData { get; set; }
|
|
|
|
/// <summary>
|
|
/// Creates a PSX texture from a given Texture2D with the specified bit depth.
|
|
/// </summary>
|
|
/// <param name="inputTexture">The input Texture2D.</param>
|
|
/// <param name="bitDepth">The desired bit depth for the PSX texture.</param>
|
|
/// <returns>The created PSXTexture2D.</returns>
|
|
public static PSXTexture2D CreateFromTexture2D(Texture2D inputTexture, PSXBPP bitDepth)
|
|
{
|
|
PSXTexture2D psxTex = new PSXTexture2D();
|
|
|
|
psxTex.Width = inputTexture.width;
|
|
psxTex.QuantizedWidth = bitDepth == PSXBPP.TEX_4BIT ? inputTexture.width / 4 :
|
|
bitDepth == PSXBPP.TEX_8BIT ? inputTexture.width / 2 :
|
|
inputTexture.width;
|
|
psxTex.Height = inputTexture.height;
|
|
|
|
psxTex.BitDepth = bitDepth;
|
|
|
|
|
|
if (bitDepth == PSXBPP.TEX_16BIT)
|
|
{
|
|
psxTex.ImageData = new ushort[inputTexture.width * inputTexture.height];
|
|
int i = 0;
|
|
foreach (Color pixel in inputTexture.GetPixels())
|
|
{
|
|
VRAMPixel vramPixel = new VRAMPixel { R = (ushort)(pixel.r * 31), G = (ushort)(pixel.g * 31), B = (ushort)(pixel.b * 31) };
|
|
psxTex.ImageData[i] = vramPixel.Pack();
|
|
i++;
|
|
}
|
|
psxTex.ColorPalette = null;
|
|
return psxTex;
|
|
}
|
|
|
|
psxTex._maxColors = (int)Mathf.Pow((int)bitDepth, 2);
|
|
|
|
QuantizedResult result = Quantize(inputTexture, psxTex._maxColors);
|
|
|
|
foreach (Vector3 color in result.Palette)
|
|
{
|
|
Color pixel = new Color(color.x, color.y, color.z);
|
|
VRAMPixel vramPixel = new VRAMPixel { R = (ushort)(pixel.r * 31), G = (ushort)(pixel.g * 31), B = (ushort)(pixel.b * 31) };
|
|
psxTex.ColorPalette.Add(vramPixel);
|
|
}
|
|
|
|
|
|
psxTex.PixelIndices = new int[psxTex.Width * psxTex.Height];
|
|
for (int x = 0; x < psxTex.Width; x++)
|
|
{
|
|
for (int y = 0; y < psxTex.Height; y++)
|
|
{
|
|
psxTex.PixelIndices[x + y * psxTex.Width] = result.Indices[x, y];
|
|
}
|
|
}
|
|
|
|
|
|
return psxTex;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Generates a preview Texture2D from the PSX texture.
|
|
/// </summary>
|
|
/// <returns>The generated preview Texture2D.</returns>
|
|
public Texture2D GeneratePreview()
|
|
{
|
|
Texture2D tex = new Texture2D(Width, Height);
|
|
if (BitDepth == PSXBPP.TEX_16BIT)
|
|
{
|
|
Color[] colors16 = new Color[Width * Height];
|
|
// An instance for the Unpack method
|
|
VRAMPixel pixel = new VRAMPixel();
|
|
|
|
for (int i = 0; i < ImageData.Length; i++)
|
|
{
|
|
ushort packedValue = ImageData[i];
|
|
pixel.Unpack(packedValue);
|
|
float r = pixel.R / 31f;
|
|
float g = pixel.G / 31f;
|
|
float b = pixel.B / 31f;
|
|
|
|
colors16[i] = new Color(r, g, b);
|
|
}
|
|
tex.SetPixels(colors16);
|
|
tex.Apply();
|
|
return tex;
|
|
}
|
|
|
|
|
|
List<Color> colors = new List<Color>();
|
|
for (int y = 0; y < Height; y++)
|
|
{
|
|
for (int x = 0; x < Width; x++)
|
|
{
|
|
int pixel = PixelIndices[y * Width + x];
|
|
VRAMPixel color = ColorPalette[pixel];
|
|
|
|
float r = color.R / 31f;
|
|
float g = color.G / 31f;
|
|
float b = color.B / 31f;
|
|
|
|
colors.Add(new Color(r, g, b));
|
|
}
|
|
}
|
|
tex.SetPixels(colors.ToArray());
|
|
tex.Apply();
|
|
return tex;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates a VRAM preview Texture2D from the PSX texture.
|
|
/// </summary>
|
|
/// <returns>The generated VRAM preview Texture2D.</returns>
|
|
public Texture2D GenerateVramPreview()
|
|
{
|
|
|
|
if (BitDepth == PSXBPP.TEX_16BIT)
|
|
{
|
|
return GeneratePreview();
|
|
}
|
|
|
|
int adjustedWidth = Width;
|
|
|
|
if (BitDepth == PSXBPP.TEX_4BIT)
|
|
{
|
|
adjustedWidth = Mathf.CeilToInt(Width / 4f);
|
|
}
|
|
else if (BitDepth == PSXBPP.TEX_8BIT)
|
|
{
|
|
adjustedWidth = Mathf.CeilToInt(Width / 2f);
|
|
}
|
|
|
|
Texture2D vramTexture = new Texture2D(adjustedWidth, Height);
|
|
|
|
List<ushort> packedValues = new List<ushort>();
|
|
|
|
if (BitDepth == PSXBPP.TEX_4BIT)
|
|
{
|
|
for (int i = 0; i < PixelIndices.Length; i += 4)
|
|
{
|
|
ushort packed = (ushort)((PixelIndices[i] << 12) | (PixelIndices[i + 1] << 8) | (PixelIndices[i + 2] << 4) | PixelIndices[i + 3]);
|
|
packedValues.Add(packed);
|
|
}
|
|
}
|
|
else if (BitDepth == PSXBPP.TEX_8BIT)
|
|
{
|
|
for (int i = 0; i < PixelIndices.Length; i += 2)
|
|
{
|
|
ushort packed = (ushort)((PixelIndices[i] << 8) | PixelIndices[i + 1]);
|
|
packedValues.Add(packed);
|
|
}
|
|
}
|
|
|
|
|
|
List<Color> colors = new List<Color>();
|
|
for (int i = 0; i < packedValues.Count; i++)
|
|
{
|
|
int index = packedValues[i];
|
|
|
|
float r = (index & 31) / 31.0f;
|
|
float g = ((index >> 5) & 31) / 31.0f;
|
|
float b = ((index >> 10) & 31) / 31.0f;
|
|
|
|
colors.Add(new Color(r, g, b));
|
|
}
|
|
vramTexture.SetPixels(colors.ToArray());
|
|
vramTexture.Apply();
|
|
|
|
return vramTexture;
|
|
|
|
}
|
|
}
|
|
} |