code refactoring and improvement

add more "Unity" way of handling static object
add RequireComponent to ensure that object have mesh and texture
add some handling of empty texture on objects
removed repetition from code
set all formatting to be the same
This commit is contained in:
aliaksei.kalosha
2025-03-18 01:11:45 +01:00
parent 3e914ac71f
commit e2a9c13fe5
4 changed files with 153 additions and 166 deletions

View File

@@ -2,16 +2,14 @@ using UnityEngine;
namespace SplashEdit.RuntimeCode
{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(Renderer))]
public class PSXObjectExporter : MonoBehaviour
{
public PSXBPP BitDepth = PSXBPP.TEX_8BIT; // Defines the bit depth of the texture (e.g., 4BPP, 8BPP)
public bool MeshIsStatic = true; // Determines if the mesh is static, affecting how it's processed. Non-static meshes don't export correctly as of now.
[HideInInspector]
public PSXTexture2D Texture; // Stores the converted PlayStation-style texture
[HideInInspector]
public PSXMesh Mesh; // Stores the converted PlayStation-style mesh
public PSXTexture2D Texture { get; set; } // Stores the converted PlayStation-style texture
public PSXMesh Mesh { get; set; } // Stores the converted PlayStation-style mesh
/// <summary>
/// Converts the object's material texture into a PlayStation-compatible texture.
@@ -19,11 +17,22 @@ namespace SplashEdit.RuntimeCode
public void CreatePSXTexture2D()
{
Renderer renderer = GetComponent<Renderer>();
if (renderer != null && renderer.sharedMaterial != null && renderer.sharedMaterial.mainTexture is Texture2D texture)
if (renderer.sharedMaterial != null && renderer.sharedMaterial.mainTexture is Texture2D texture)
{
Texture = PSXTexture2D.CreateFromTexture2D(texture, BitDepth);
Texture.OriginalTexture = texture; // Stores reference to the original texture
}
else
{
//TODO: Better handle object with default texture
Texture = new PSXTexture2D()
{
BitDepth = BitDepth,
Width = 0,
Height = 0,
};
Texture.OriginalTexture = null;
}
}
/// <summary>
@@ -31,19 +40,16 @@ namespace SplashEdit.RuntimeCode
/// </summary>
public void CreatePSXMesh()
{
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
if (meshFilter != null)
MeshFilter meshFilter = GetComponent<MeshFilter>();
if (gameObject.isStatic)
{
if (MeshIsStatic)
{
// Static meshes take object transformation into account
Mesh = PSXMesh.CreateFromUnityMesh(meshFilter.sharedMesh, Texture.Width, Texture.Height, transform);
}
else
{
// Dynamic meshes do not consider object transformation
Mesh = PSXMesh.CreateFromUnityMesh(meshFilter.sharedMesh, Texture.Width, Texture.Height);
}
// Static meshes take object transformation into account
Mesh = PSXMesh.CreateFromUnityMesh(meshFilter.sharedMesh, Texture.Width, Texture.Height, transform);
}
else
{
// Dynamic meshes do not consider object transformation
Mesh = PSXMesh.CreateFromUnityMesh(meshFilter.sharedMesh, Texture.Width, Texture.Height);
}
}
}