From a8e5f7cc4a4dba2c903c06c4bffa555cb812c071 Mon Sep 17 00:00:00 2001 From: "aliaksei.kalosha" Date: Sat, 12 Apr 2025 00:56:17 +0200 Subject: [PATCH] add Texture Cache on PSXObjectExporter --- Runtime/PSXObjectExporter.cs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/Runtime/PSXObjectExporter.cs b/Runtime/PSXObjectExporter.cs index 8cd3fda..3d40a5c 100644 --- a/Runtime/PSXObjectExporter.cs +++ b/Runtime/PSXObjectExporter.cs @@ -1,30 +1,32 @@ using System.Collections.Generic; using UnityEngine; +using UnityEngine.Serialization; namespace SplashEdit.RuntimeCode { [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 List Textures { get; set; } = new List(); // Stores the converted PlayStation-style texture public PSXMesh Mesh { get; set; } // Stores the converted PlayStation-style mesh - - - [SerializeField] private bool PreviewNormals = false; + [Header("Export Settings")] + [FormerlySerializedAs("BitDepth")] + [SerializeField] private PSXBPP bitDepth = PSXBPP.TEX_8BIT; // Defines the bit depth of the texture (e.g., 4BPP, 8BPP) + [Header("Gizmo Settings")] + [FormerlySerializedAs("PreviewNormals")] + [SerializeField] private bool previewNormals = false; [SerializeField] private float normalPreviewLength = 0.5f; // Length of the normal lines + private readonly Dictionary<(int, PSXBPP), PSXTexture2D> cache = new(); + private void OnDrawGizmos() { - - if (PreviewNormals) + if (previewNormals) { MeshFilter filter = GetComponent(); if (filter != null) { - Mesh mesh = filter.sharedMesh; Vector3[] vertices = mesh.vertices; Vector3[] normals = mesh.normals; @@ -76,8 +78,17 @@ namespace SplashEdit.RuntimeCode if (tex2D != null) { - PSXTexture2D tex = PSXTexture2D.CreateFromTexture2D(tex2D, BitDepth); - tex.OriginalTexture = tex2D; // Store reference to the original texture + PSXTexture2D tex; + if (cache.ContainsKey((tex2D.GetInstanceID(), bitDepth))) + { + tex = cache[(tex2D.GetInstanceID(), bitDepth)]; + } + else + { + tex = PSXTexture2D.CreateFromTexture2D(tex2D, bitDepth); + tex.OriginalTexture = tex2D; // Store reference to the original texture + cache.Add((tex2D.GetInstanceID(), bitDepth), tex); + } Textures.Add(tex); } }