116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using SplashEdit.RuntimeCode;
|
|
using UnityEngine;
|
|
using UnityEngine.Serialization;
|
|
|
|
namespace SplashEdit.RuntimeCode
|
|
{
|
|
public enum PSXCollisionType
|
|
{
|
|
None = 0,
|
|
Solid = 1,
|
|
Trigger = 2,
|
|
Platform = 3
|
|
}
|
|
|
|
[RequireComponent(typeof(Renderer))]
|
|
public class PSXObjectExporter : MonoBehaviour, IPSXExportable
|
|
{
|
|
public LuaFile LuaFile => luaFile;
|
|
|
|
[FormerlySerializedAs("IsActive")]
|
|
[SerializeField] private bool isActive = true;
|
|
public bool IsActive => isActive;
|
|
|
|
public List<PSXTexture2D> Textures { get; set; } = new List<PSXTexture2D>();
|
|
public PSXMesh Mesh { get; protected set; }
|
|
|
|
[FormerlySerializedAs("BitDepth")]
|
|
[SerializeField] private PSXBPP bitDepth = PSXBPP.TEX_8BIT;
|
|
[SerializeField] private LuaFile luaFile;
|
|
|
|
[SerializeField] private PSXCollisionType collisionType = PSXCollisionType.None;
|
|
[SerializeField] private bool staticCollider = true;
|
|
[SerializeField] private bool exportCollisionMesh = false;
|
|
[SerializeField] private Mesh customCollisionMesh;
|
|
[Range(1, 8)]
|
|
[SerializeField] private int collisionLayer = 1;
|
|
|
|
[SerializeField] private bool generateNavigation = false;
|
|
|
|
public PSXBPP BitDepth => bitDepth;
|
|
public PSXCollisionType CollisionType => collisionType;
|
|
public bool StaticCollider => staticCollider;
|
|
public bool ExportCollisionMesh => exportCollisionMesh;
|
|
public Mesh CustomCollisionMesh => customCollisionMesh;
|
|
public int CollisionLayer => collisionLayer;
|
|
public bool GenerateNavigation => generateNavigation;
|
|
|
|
private readonly Dictionary<(int, PSXBPP), PSXTexture2D> cache = new();
|
|
|
|
public void CreatePSXTextures2D()
|
|
{
|
|
Renderer renderer = GetComponent<Renderer>();
|
|
Textures.Clear();
|
|
if (renderer == null) return;
|
|
|
|
Material[] materials = renderer.sharedMaterials;
|
|
foreach (Material mat in materials)
|
|
{
|
|
if (mat == null || mat.mainTexture == null) continue;
|
|
|
|
Texture mainTexture = mat.mainTexture;
|
|
Texture2D tex2D = mainTexture is Texture2D existing
|
|
? existing
|
|
: ConvertToTexture2D(mainTexture);
|
|
|
|
if (tex2D == null) continue;
|
|
|
|
if (cache.TryGetValue((tex2D.GetInstanceID(), bitDepth), out var cached))
|
|
{
|
|
Textures.Add(cached);
|
|
}
|
|
else
|
|
{
|
|
var tex = PSXTexture2D.CreateFromTexture2D(tex2D, bitDepth);
|
|
tex.OriginalTexture = tex2D;
|
|
cache.Add((tex2D.GetInstanceID(), bitDepth), tex);
|
|
Textures.Add(tex);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static Texture2D ConvertToTexture2D(Texture src)
|
|
{
|
|
Texture2D texture2D = new Texture2D(src.width, src.height, TextureFormat.RGBA32, false);
|
|
|
|
RenderTexture currentActiveRT = RenderTexture.active;
|
|
RenderTexture.active = src as RenderTexture;
|
|
|
|
texture2D.ReadPixels(new Rect(0, 0, src.width, src.height), 0, 0);
|
|
texture2D.Apply();
|
|
|
|
RenderTexture.active = currentActiveRT;
|
|
|
|
return texture2D;
|
|
}
|
|
|
|
public PSXTexture2D GetTexture(int index)
|
|
{
|
|
if (index >= 0 && index < Textures.Count)
|
|
{
|
|
return Textures[index];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void CreatePSXMesh(float GTEScaling)
|
|
{
|
|
Renderer renderer = GetComponent<Renderer>();
|
|
if (renderer != null)
|
|
{
|
|
Mesh = PSXMesh.CreateFromUnityRenderer(renderer, GTEScaling, transform, Textures);
|
|
}
|
|
}
|
|
}
|
|
} |