Texture and clut packing, All important information starts to join in one place for final export

This commit is contained in:
2025-03-11 12:32:22 +01:00
parent 10c2186966
commit 00c94d732b
8 changed files with 333 additions and 90 deletions

View File

@@ -1,70 +0,0 @@
using UnityEngine;
using UnityEditor;
using PSXSplash.RuntimeCode;
using System.IO;
namespace PSXSplash.EditorCode
{
[CustomEditor(typeof(PSXObjectExporter))]
public class PSXObjectExporterEditor : Editor
{
public override void OnInspectorGUI()
{
/*
PSXObjectExporter comp = (PSXObjectExporter)target;
serializedObject.Update();
EditorGUILayout.BeginVertical("box");
EditorGUILayout.PropertyField(serializedObject.FindProperty("Mesh"));
if (GUILayout.Button("Export mesh"))
{
comp.Mesh.Export(comp.gameObject);
GUIUtility.ExitGUI();
}
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical("box");
EditorGUILayout.PropertyField(serializedObject.FindProperty("Texture"));
if (GUILayout.Button("Export texture"))
{
ushort[] textureData = comp.Texture.ExportTexture(comp.gameObject);
string path = EditorUtility.SaveFilePanel(
"Save texture data",
"",
"texture_data",
"bin"
);
if (!string.IsNullOrEmpty(path))
{
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
using (BinaryWriter writer = new BinaryWriter(fileStream))
{
foreach (ushort value in textureData)
{
writer.Write(value);
}
}
Hodně string path = EditorUtility.SaveFilePanel(
"Save clut data",
"",
"clut_data",
"bin"
);
if (!string.IsNullOrEmpty(path))
{
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
using (BinaryWriter writer = new BinaryWriter(fileStream))
{
foreach (ushort value in clutData)
{
writer.Write(value);
}Hodně plyModifiedProperties();
*/
}
}
}

View File

@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 89b9844e599dd374098ec6168f0de0b0

View File

@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using PSXSplash.RuntimeCode;
using Unity.Collections;
using UnityEditor;
@@ -17,7 +19,6 @@ public class VRAMEditorWindow : EditorWindow
private const int VramWidth = 1024;
private const int VramHeight = 512;
private VRAMPixel[,] vramPixels = new VRAMPixel[VramWidth, VramHeight];
private List<ProhibitedArea> prohibitedAreas = new List<ProhibitedArea>();
private Vector2 scrollPosition;
private Texture2D vramImage;
@@ -44,16 +45,98 @@ public class VRAMEditorWindow : EditorWindow
}
private void OnGUI()
public static void PasteTexture(Texture2D baseTexture, Texture2D overlayTexture, int posX, int posY)
{
vramImage = new Texture2D(VramWidth, VramHeight);
if (baseTexture == null || overlayTexture == null)
{
Debug.LogError("Textures cannot be null!");
return;
}
// Get pixels from the overlay texture
Color[] overlayPixels = overlayTexture.GetPixels();
Color[] basePixels = baseTexture.GetPixels();
int baseWidth = baseTexture.width;
int baseHeight = baseTexture.height;
int overlayWidth = overlayTexture.width;
int overlayHeight = overlayTexture.height;
// Loop through the overlay texture and paste it onto the base texture
for (int y = 0; y < overlayHeight; y++)
{
for (int x = 0; x < overlayWidth; x++)
{
int baseX = posX + x;
int baseY = posY + y;
// Ensure we are within bounds of the base texture
if (baseX >= 0 && baseX < baseWidth && baseY >= 0 && baseY < baseHeight)
{
int baseIndex = baseY * baseWidth + baseX;
int overlayIndex = y * overlayWidth + x;
// Blend or replace pixel (simple overwrite in this case)
basePixels[baseIndex] = overlayPixels[overlayIndex];
}
}
}
// Apply the modified pixels back to the base texture
baseTexture.SetPixels(basePixels);
baseTexture.Apply();
}
private void PackTextures()
{
vramImage = new Texture2D(VramWidth, VramHeight);
NativeArray<Color32> blackPixels = new NativeArray<Color32>(VramWidth * VramHeight, Allocator.Temp);
vramImage.SetPixelData(blackPixels, 0);
vramImage.Apply();
blackPixels.Dispose();
PSXObjectExporter[] objects = FindObjectsByType<PSXObjectExporter>(FindObjectsSortMode.None);
foreach (PSXObjectExporter exp in objects)
{
exp.CreatePSXTexture2D();
}
List<Rect> dontPackAreas = new List<Rect>();
foreach (ProhibitedArea area in prohibitedAreas)
{
dontPackAreas.Add(new Rect(area.X, area.Y, area.Width, area.Height));
}
Rect buffer1 = new Rect(0, 0, selectedResolution.x, selectedResolution.y);
Rect buffer2 = verticalLayout ? new Rect(0, 256, selectedResolution.x, selectedResolution.y)
: new Rect(selectedResolution.x, 0, selectedResolution.x, selectedResolution.y);
dontPackAreas.Add(buffer1);
dontPackAreas.Add(buffer2);
VRAMPacker tp = new VRAMPacker(dontPackAreas);
var packed = tp.PackTexturesIntoVRAM(objects);
foreach (TextureAtlas ta in packed.atlases)
{
foreach (PSXTexture2D texture in ta.ContainedTextures)
{
Debug.Log($"Packing {texture} at: x:{ta.PositionX + texture.PackingX} y:{ta.PositionY + texture.PackingY}");
PasteTexture(vramImage, texture.GenerateVramPreview(), ta.PositionX + texture.PackingX, ta.PositionY + texture.PackingY);
Debug.Log($"Texpage: {texture.TexpageNum} Offset:({texture.PackingX},{texture.PackingY})");
for(int i = 0; i < texture.ColorPalette.Count; i++) {
vramImage.SetPixel(texture.ClutPackingX+i, texture.ClutPackingY, texture.ColorPalette[i].GetUnityColor());
}
vramImage.Apply();
}
}
}
private void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
GUILayout.Label("VRAM Editor", EditorStyles.boldLabel);
@@ -61,19 +144,22 @@ public class VRAMEditorWindow : EditorWindow
selectedResolution = resolutions[EditorGUILayout.Popup("Resolution", System.Array.IndexOf(resolutions, selectedResolution),
new string[] { "256x240", "256x480", "320x240", "320x480", "368x240", "368x480", "512x240", "512x480", "640x240", "640x480" })];
bool canDBHorizontal = selectedResolution[0]*2 <= 1024;
bool canDBVertical = selectedResolution[1]*2 <= 512;
bool canDBHorizontal = selectedResolution[0] * 2 <= 1024;
bool canDBVertical = selectedResolution[1] * 2 <= 512;
if(canDBHorizontal || canDBVertical) {
if (canDBHorizontal || canDBVertical)
{
dualBuffering = EditorGUILayout.Toggle("Dual Buffering", dualBuffering);
}
else {dualBuffering = false;}
if(canDBVertical && canDBHorizontal) {
else { dualBuffering = false; }
if (canDBVertical && canDBHorizontal)
{
verticalLayout = EditorGUILayout.Toggle("Vertical", verticalLayout);
}
else if (canDBVertical) {verticalLayout = true;}
else {
else if (canDBVertical) { verticalLayout = true; }
else
{
verticalLayout = false;
}
@@ -106,6 +192,13 @@ public class VRAMEditorWindow : EditorWindow
{
prohibitedAreas.Add(new ProhibitedArea());
}
// New "Pack Textures" Button
if (GUILayout.Button("Pack Textures"))
{
PackTextures();
}
GUILayout.EndVertical();
Rect vramRect = GUILayoutUtility.GetRect(VramWidth, VramHeight, GUILayout.ExpandWidth(false));
@@ -124,7 +217,8 @@ public class VRAMEditorWindow : EditorWindow
GUI.Label(new Rect(buffer2.center.x - 40, buffer2.center.y - 10, 80, 20), "Framebuffer B", EditorStyles.boldLabel);
}
foreach(ProhibitedArea area in prohibitedAreas) {
foreach (ProhibitedArea area in prohibitedAreas)
{
Rect areaRect = new Rect(vramRect.x + area.X, vramRect.y + area.Y, area.Width, area.Height);
EditorGUI.DrawRect(areaRect, prohibitedColor);
}