improve code readability

This commit is contained in:
aliaksei.kalosha
2025-04-01 22:34:30 +02:00
parent a42cce4ee2
commit fedf013ee9
5 changed files with 49 additions and 69 deletions

View File

@@ -12,8 +12,9 @@ namespace SplashEdit.EditorCode
{
public class VRAMEditorWindow : EditorWindow
{
private const int VramWidth = 1024;
private const int VramHeight = 512;
private int VramWidth => VRAMPacker.VramWidth;
private int VramHeight => VRAMPacker.VramHeight;
private static readonly Vector2 MinSize = new Vector2(800, 600);
private List<ProhibitedArea> prohibitedAreas = new List<ProhibitedArea>();
private Vector2 scrollPosition;

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Diagnostics;
namespace SplashEdit.RuntimeCode
{
@@ -116,11 +115,24 @@ namespace SplashEdit.RuntimeCode
}
// Get mesh data arrays.
mesh.RecalculateNormals();
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;// Assuming this function recalculates normals
Vector3[] normals = mesh.normals;
Vector3[] smoothNormals = RecalculateSmoothNormals(mesh);
Vector2[] uv = mesh.uv;
PSXVertex convertData(int index)
{
// Scale the vertex based on world scale.
Vector3 v = Vector3.Scale(vertices[index], transform.lossyScale);
// Transform the vertex to world space.
Vector3 wv = transform.TransformPoint(vertices[index]);
// Transform the normals to world space.
Vector3 wn = transform.TransformDirection(smoothNormals[index]).normalized;
// Compute lighting for each vertex (this can be a custom function).
Color c = PSXLightingBaker.ComputeLighting(wv, wn);
// Convert vertex to PSX format, including fixed-point conversion and shading.
return ConvertToPSXVertex(v, GTEScaling, normals[index], uv[index], psxTexture?.Width, psxTexture?.Height, c);
}
// Iterate through the triangles of the submesh.
for (int i = 0; i < submeshTriangles.Length; i += 3)
{
@@ -135,46 +147,8 @@ namespace SplashEdit.RuntimeCode
(vid1, vid2) = (vid2, vid1);
}
// Scale the vertices based on world scale.
Vector3 v0 = Vector3.Scale(vertices[vid0], transform.lossyScale);
Vector3 v1 = Vector3.Scale(vertices[vid1], transform.lossyScale);
Vector3 v2 = Vector3.Scale(vertices[vid2], transform.lossyScale);
// Transform the vertices to world space.
Vector3 wv0 = transform.TransformPoint(vertices[vid0]);
Vector3 wv1 = transform.TransformPoint(vertices[vid1]);
Vector3 wv2 = transform.TransformPoint(vertices[vid2]);
// Transform the normals to world space.
Vector3 wn0 = transform.TransformDirection(smoothNormals[vid0]).normalized;
Vector3 wn1 = transform.TransformDirection(smoothNormals[vid1]).normalized;
Vector3 wn2 = transform.TransformDirection(smoothNormals[vid2]).normalized;
// Compute lighting for each vertex (this can be a custom function).
Color cv0 = PSXLightingBaker.ComputeLighting(wv0, wn0);
Color cv1 = PSXLightingBaker.ComputeLighting(wv1, wn1);
Color cv2 = PSXLightingBaker.ComputeLighting(wv2, wn2);
// Convert vertices to PSX format, including fixed-point conversion and shading.
PSXVertex psxV0 = ConvertToPSXVertex(v0, GTEScaling, normals[vid0], uv[vid0], psxTexture?.Width ?? 0, psxTexture?.Height ?? 0);
PSXVertex psxV1 = ConvertToPSXVertex(v1, GTEScaling, normals[vid1], uv[vid1], psxTexture?.Width ?? 0, psxTexture?.Height ?? 0);
PSXVertex psxV2 = ConvertToPSXVertex(v2, GTEScaling, normals[vid2], uv[vid2], psxTexture?.Width ?? 0, psxTexture?.Height ?? 0);
// Apply lighting to the colors.
psxV0.r = (byte)Mathf.Clamp(cv0.r * 255, 0, 255);
psxV0.g = (byte)Mathf.Clamp(cv0.g * 255, 0, 255);
psxV0.b = (byte)Mathf.Clamp(cv0.b * 255, 0, 255);
psxV1.r = (byte)Mathf.Clamp(cv1.r * 255, 0, 255);
psxV1.g = (byte)Mathf.Clamp(cv1.g * 255, 0, 255);
psxV1.b = (byte)Mathf.Clamp(cv1.b * 255, 0, 255);
psxV2.r = (byte)Mathf.Clamp(cv2.r * 255, 0, 255);
psxV2.g = (byte)Mathf.Clamp(cv2.g * 255, 0, 255);
psxV2.b = (byte)Mathf.Clamp(cv2.b * 255, 0, 255);
// Add the constructed triangle to the mesh.
psxMesh.Triangles.Add(new Tri { v0 = psxV0, v1 = psxV1, v2 = psxV2, Texture = psxTexture });
psxMesh.Triangles.Add(new Tri { v0 = convertData(vid0), v1 = convertData(vid0), v2 = convertData(vid0), Texture = psxTexture });
}
}
@@ -192,29 +166,30 @@ namespace SplashEdit.RuntimeCode
/// <param name="textureWidth">Width of the texture for UV scaling.</param>
/// <param name="textureHeight">Height of the texture for UV scaling.</param>
/// <returns>A PSXVertex with converted coordinates, normals, UVs, and color.</returns>
private static PSXVertex ConvertToPSXVertex(Vector3 vertex, float GTEScaling, Vector3 normal, Vector2 uv, int textureWidth, int textureHeight)
private static PSXVertex ConvertToPSXVertex(Vector3 vertex, float GTEScaling, Vector3 normal, Vector2 uv, int? textureWidth, int? textureHeight, Color color)
{
static short clampPosition(float v) => (short)(Mathf.Clamp(v, -4f, 3.999f) * 4096);
static byte clamp0255(float v) => (byte)(Mathf.Clamp(v, 0, 255));
int width = textureWidth ?? 0;
int height = textureHeight ?? 0;
PSXVertex psxVertex = new PSXVertex
{
// Convert position to fixed-point, clamping values to a defined range.
vx = (short)PSXTrig.ConvertCoordinateToPSX(vertex.x, GTEScaling),
vy = (short)PSXTrig.ConvertCoordinateToPSX(-vertex.y, GTEScaling),
vz = (short)PSXTrig.ConvertCoordinateToPSX(vertex.z, GTEScaling),
vx = PSXTrig.ConvertCoordinateToPSX(vertex.x, GTEScaling),
vy = PSXTrig.ConvertCoordinateToPSX(-vertex.y, GTEScaling),
vz = PSXTrig.ConvertCoordinateToPSX(vertex.z, GTEScaling),
// Convert normals to fixed-point.
nx = (short)PSXTrig.ConvertCoordinateToPSX(normal.x),
ny = (short)PSXTrig.ConvertCoordinateToPSX(-normal.y),
nz = (short)PSXTrig.ConvertCoordinateToPSX(normal.z),
nx = PSXTrig.ConvertCoordinateToPSX(normal.x),
ny = PSXTrig.ConvertCoordinateToPSX(-normal.y),
nz = PSXTrig.ConvertCoordinateToPSX(normal.z),
// Map UV coordinates to a byte range after scaling based on texture dimensions.
u = (byte)Mathf.Clamp(uv.x * (textureWidth - 1), 0, 255),
v = (byte)Mathf.Clamp((1.0f - uv.y) * (textureHeight - 1), 0, 255),
u = (byte)Mathf.Clamp(uv.x * (width - 1), 0, 255),
v = (byte)Mathf.Clamp((1.0f - uv.y) * (height - 1), 0, 255),
// Convert the computed color to a byte range.
// Apply lighting to the colors.
r = Utils.Clamp0255(color.r),
g = Utils.Clamp0255(color.g),
b = Utils.Clamp0255(color.b),
};
return psxVertex;

View File

@@ -12,8 +12,8 @@ namespace SplashEdit.RuntimeCode
public PSXMesh Mesh { get; set; } // Stores the converted PlayStation-style mesh
public bool PreviewNormals = false;
public float normalPreviewLength = 0.5f; // Length of the normal lines
[SerializeField] private bool PreviewNormals = false;
[SerializeField] private float normalPreviewLength = 0.5f; // Length of the normal lines
private void OnDrawGizmos()
{
@@ -51,6 +51,7 @@ namespace SplashEdit.RuntimeCode
public void CreatePSXTextures2D()
{
Renderer renderer = GetComponent<Renderer>();
Textures.Clear();
if (renderer != null)
{
Material[] materials = renderer.sharedMaterials;

View File

@@ -30,8 +30,8 @@ namespace SplashEdit.RuntimeCode
private List<TextureAtlas> _finalizedAtlases = new List<TextureAtlas>(); // Atlases that have been successfully placed.
private List<Rect> _allocatedCLUTs = new List<Rect>(); // Allocated regions for CLUTs.
private const int VRAM_WIDTH = 1024;
private const int VRAM_HEIGHT = 512;
public static readonly int VramWidth = 1024;
public static readonly int VramHeight = 512;
private VRAMPixel[,] _vramPixels; // Simulated VRAM pixel data.
@@ -54,7 +54,7 @@ namespace SplashEdit.RuntimeCode
_reservedAreas.Add(framebuffers[0]);
_reservedAreas.Add(framebuffers[1]);
_vramPixels = new VRAMPixel[VRAM_WIDTH, VRAM_HEIGHT];
_vramPixels = new VRAMPixel[VramWidth, VramHeight];
}
/// <summary>
@@ -189,10 +189,10 @@ namespace SplashEdit.RuntimeCode
{
bool placed = false;
// Try every possible row (stepping by atlas height).
for (int y = 0; y <= VRAM_HEIGHT - TextureAtlas.Height; y += 256)
for (int y = 0; y <= VramHeight - TextureAtlas.Height; y += 256)
{
// Try every possible column (stepping by 64 pixels).
for (int x = 0; x <= VRAM_WIDTH - atlas.Width; x += 64)
for (int x = 0; x <= VramWidth - atlas.Width; x += 64)
{
// Only consider atlases that haven't been placed yet.
if (atlas.PositionX == 0 && atlas.PositionY == 0)
@@ -246,9 +246,9 @@ namespace SplashEdit.RuntimeCode
bool placed = false;
// Iterate over possible CLUT positions in VRAM.
for (ushort x = 0; x < VRAM_WIDTH; x += 16)
for (ushort x = 0; x < VramWidth; x += 16)
{
for (ushort y = 0; y <= VRAM_HEIGHT; y++)
for (ushort y = 0; y <= VramHeight; y++)
{
var candidate = new Rect(x, y, clutWidth, clutHeight);
if (IsPlacementValid(candidate))
@@ -312,8 +312,8 @@ namespace SplashEdit.RuntimeCode
private bool IsPlacementValid(Rect rect)
{
// Ensure the rectangle fits within VRAM boundaries.
if (rect.x + rect.width > VRAM_WIDTH) return false;
if (rect.y + rect.height > VRAM_HEIGHT) return false;
if (rect.x + rect.width > VramWidth) return false;
if (rect.y + rect.height > VramHeight) return false;
// Check for overlaps with existing atlases.
bool overlapsAtlas = _finalizedAtlases.Any(a => new Rect(a.PositionX, a.PositionY, a.Width, TextureAtlas.Height).Overlaps(rect));

View File

@@ -330,6 +330,9 @@ namespace SplashEdit.RuntimeCode
_ => throw new System.NotImplementedException(),
};
}
public static byte Clamp0255(float v) => (byte)(Mathf.Clamp(v, 0, 255));
}
}