Somewhat fixed ui

This commit is contained in:
Jan Racek
2026-03-25 17:14:22 +01:00
parent 8914ba35cc
commit 5fffcea6cf
5 changed files with 507 additions and 226 deletions

View File

@@ -13,7 +13,7 @@ namespace SplashEdit.RuntimeCode
/// </summary>
public static class PSXUIExporter
{
/// <summary>
/// <summary>
/// Collect all PSXCanvas components and their child UI elements,
/// converting RectTransform coordinates to PS1 pixel space.
/// Also collects and deduplicates custom fonts.
@@ -49,28 +49,60 @@ namespace SplashEdit.RuntimeCode
}
}
// Build font data with VRAM positions
// Font textures go at x=960 (same column as system font), stacking upward from y=464
// System font: (960, 464)-(1023, 511) → 64 wide, 48 tall
// Build font data with VRAM positions.
// Each font gets its own texture page to avoid V-coordinate overflow.
// Font textures go at x=960:
// Font 1: y=0 (page 15,0) - 256px available
// Font 2: y=256 (page 15,1) - 208px available (system font at y=464)
// Font 3: not supported (would need different VRAM column)
// System font: (960, 464) in page (15,1), occupies y=464-511.
List<PSXFontData> fontDataList = new List<PSXFontData>();
ushort fontVramY = 0; // start from top of VRAM at x=960
ushort[] fontPageStarts = { 0, 256 }; // one per texture page
int fontPageIndex = 0;
foreach (PSXFontAsset fa in uniqueFonts)
{
byte[] pixelData = fa.ConvertTo4BPP();
if (pixelData == null) continue;
// Read advance widths directly from the font asset.
// These were computed during bitmap generation from the exact same
// CharacterInfo used to render the glyphs - guaranteed to match.
byte[] advances = fa.AdvanceWidths;
if (advances == null || advances.Length < 96)
{
Debug.LogWarning($"PSXUIExporter: Font '{fa.name}' has no stored advance widths. Using cell width as fallback.");
advances = new byte[96];
for (int i = 0; i < 96; i++) advances[i] = (byte)fa.GlyphWidth;
}
ushort texH = (ushort)fa.TextureHeight;
if (fontPageIndex >= fontPageStarts.Length)
{
Debug.LogError($"PSXUIExporter: Max 2 custom fonts supported (need separate texture pages). Skipping '{fa.name}'.");
continue;
}
ushort vramY = fontPageStarts[fontPageIndex];
int maxHeight = (fontPageIndex == 1) ? 208 : 256; // page 1 shares with system font
if (texH > maxHeight)
{
Debug.LogWarning($"PSXUIExporter: Font '{fa.name}' texture ({texH}px) exceeds page limit ({maxHeight}px). May be clipped.");
}
fontDataList.Add(new PSXFontData
{
Source = fa,
GlyphWidth = (byte)fa.GlyphWidth,
GlyphHeight = (byte)fa.GlyphHeight,
VramX = 960, // same column as system font (64 VRAM hwords for 256px 4bpp)
VramY = fontVramY,
VramX = 960,
VramY = vramY,
TextureHeight = texH,
PixelData = pixelData
PixelData = pixelData,
AdvanceWidths = advances
});
fontVramY += texH;
fontPageIndex++;
}
fonts = fontDataList.ToArray();