From 854d14af037d25defbb410d1f268bec592b848e7 Mon Sep 17 00:00:00 2001 From: jracek Date: Sat, 25 Jan 2025 20:46:56 +0100 Subject: [PATCH] Restored 16bpp support --- Runtime/PSXTexture2D.cs | 47 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/Runtime/PSXTexture2D.cs b/Runtime/PSXTexture2D.cs index c340f88..dec79e6 100644 --- a/Runtime/PSXTexture2D.cs +++ b/Runtime/PSXTexture2D.cs @@ -60,10 +60,13 @@ namespace PSXSplash.RuntimeCode public int Height { get; set; } public int[] Pixels { get; set; } public List ColorPalette = new List(); - public PSXBPP BitDepth; - + public PSXBPP BitDepth { get; set; } private int _maxColors; + // Used only for 16bpp + public ushort[] ImageData { get; set; } + + public static PSXTexture2D CreateFromTexture2D(Texture2D inputTexture, PSXBPP bitDepth, bool dither) { PSXTexture2D psxTex = new PSXTexture2D(); @@ -72,6 +75,19 @@ namespace PSXSplash.RuntimeCode 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++; + } + return psxTex; + } + psxTex._maxColors = (int)Mathf.Pow((int)bitDepth, 2); ImageQuantizer quantizer = new ImageQuantizer(); @@ -90,6 +106,27 @@ namespace PSXSplash.RuntimeCode 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 colors = new List(); for (int y = 0; y < Height; y++) @@ -113,6 +150,10 @@ namespace PSXSplash.RuntimeCode public Texture2D GenerateVramPreview() { + + if(BitDepth == PSXBPP.TEX_16BIT) { + return GeneratePreview(); + } int adjustedWidth = Width; @@ -152,7 +193,7 @@ namespace PSXSplash.RuntimeCode { int index = packedValues[i]; - float r = (index & 31) / 31.0f; + float r = (index & 31) / 31.0f; float g = ((index >> 5) & 31) / 31.0f; float b = ((index >> 10) & 31) / 31.0f;