This commit is contained in:
Jan Racek
2026-03-24 13:00:54 +01:00
parent 53e993f58e
commit 4aa4e49424
145 changed files with 10853 additions and 2965 deletions

View File

@@ -1,16 +1,45 @@
using System.Collections.Generic;
using Splashedit.RuntimeCode;
using SplashEdit.RuntimeCode;
using UnityEngine;
using UnityEngine.Serialization;
namespace SplashEdit.RuntimeCode
{
/// <summary>
/// Collision type for PS1 runtime
/// </summary>
public enum PSXCollisionType
{
None = 0, // No collision
Solid = 1, // Solid collision - blocks movement
Trigger = 2, // Trigger - fires events but doesn't block
Platform = 3 // Platform - solid from above, passable from below
}
/// <summary>
/// Object behavior flags for PS1 runtime
/// </summary>
[System.Flags]
public enum PSXObjectFlags
{
None = 0,
Static = 1 << 0, // Object never moves (can be optimized)
Dynamic = 1 << 1, // Object can move
Visible = 1 << 2, // Object is rendered
CastsShadow = 1 << 3, // Object casts shadows (future)
ReceivesShadow = 1 << 4, // Object receives shadows (future)
Interactable = 1 << 5, // Player can interact with this
AlwaysRender = 1 << 6, // Skip frustum culling for this object
}
[RequireComponent(typeof(Renderer))]
public class PSXObjectExporter : MonoBehaviour
public class PSXObjectExporter : MonoBehaviour, IPSXExportable
{
public LuaFile LuaFile => luaFile;
public bool IsActive = true;
[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; }
@@ -20,22 +49,41 @@ namespace SplashEdit.RuntimeCode
[SerializeField] private PSXBPP bitDepth = PSXBPP.TEX_8BIT;
[SerializeField] private LuaFile luaFile;
[Header("BSP Settings")]
[SerializeField] private Mesh _modifiedMesh; // Mesh after BSP processing
[Header("Object Flags")]
[SerializeField] private PSXObjectFlags objectFlags = PSXObjectFlags.Static | PSXObjectFlags.Visible;
[Header("Collision Settings")]
[SerializeField] private PSXCollisionType collisionType = PSXCollisionType.None;
[SerializeField] private bool exportCollisionMesh = false;
[SerializeField] private Mesh customCollisionMesh; // Optional simplified collision mesh
[Tooltip("Layer mask for collision detection (1-8)")]
[Range(1, 8)]
[SerializeField] private int collisionLayer = 1;
[Header("Navigation")]
[Tooltip("Include this object's walkable surfaces in nav region generation")]
[SerializeField] private bool generateNavigation = false;
[Header("Gizmo Settings")]
[FormerlySerializedAs("PreviewNormals")]
[SerializeField] private bool previewNormals = false;
[SerializeField] private float normalPreviewLength = 0.5f;
[SerializeField] private bool showCollisionBounds = true;
// Public accessors for editor and export
public PSXBPP BitDepth => bitDepth;
public PSXCollisionType CollisionType => collisionType;
public bool ExportCollisionMesh => exportCollisionMesh;
public Mesh CustomCollisionMesh => customCollisionMesh;
public int CollisionLayer => collisionLayer;
public PSXObjectFlags ObjectFlags => objectFlags;
public bool GenerateNavigation => generateNavigation;
// For assigning texture from editor
public Texture2D texture;
private readonly Dictionary<(int, PSXBPP), PSXTexture2D> cache = new();
public Mesh ModifiedMesh
{
get => _modifiedMesh;
set => _modifiedMesh = value;
}
private void OnDrawGizmos()
{
if (previewNormals)
@@ -60,6 +108,48 @@ namespace SplashEdit.RuntimeCode
}
}
}
private void OnDrawGizmosSelected()
{
// Draw collision bounds when object is selected
if (showCollisionBounds && collisionType != PSXCollisionType.None)
{
MeshFilter filter = GetComponent<MeshFilter>();
Mesh collisionMesh = customCollisionMesh != null ? customCollisionMesh : (filter?.sharedMesh);
if (collisionMesh != null)
{
Bounds bounds = collisionMesh.bounds;
// Choose color based on collision type
switch (collisionType)
{
case PSXCollisionType.Solid:
Gizmos.color = new Color(1f, 0.3f, 0.3f, 0.5f); // Red
break;
case PSXCollisionType.Trigger:
Gizmos.color = new Color(0.3f, 1f, 0.3f, 0.5f); // Green
break;
case PSXCollisionType.Platform:
Gizmos.color = new Color(0.3f, 0.3f, 1f, 0.5f); // Blue
break;
}
// Draw AABB
Matrix4x4 oldMatrix = Gizmos.matrix;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(bounds.center, bounds.size);
// Draw filled with lower alpha
Color fillColor = Gizmos.color;
fillColor.a = 0.1f;
Gizmos.color = fillColor;
Gizmos.DrawCube(bounds.center, bounds.size);
Gizmos.matrix = oldMatrix;
}
}
}
public void CreatePSXTextures2D()
{
@@ -67,6 +157,24 @@ namespace SplashEdit.RuntimeCode
Textures.Clear();
if (renderer != null)
{
// If an override texture is set, use it for all submeshes
if (texture != null)
{
PSXTexture2D tex;
if (cache.ContainsKey((texture.GetInstanceID(), bitDepth)))
{
tex = cache[(texture.GetInstanceID(), bitDepth)];
}
else
{
tex = PSXTexture2D.CreateFromTexture2D(texture, bitDepth);
tex.OriginalTexture = texture;
cache.Add((texture.GetInstanceID(), bitDepth), tex);
}
Textures.Add(tex);
return;
}
Material[] materials = renderer.sharedMaterials;
foreach (Material mat in materials)
@@ -129,34 +237,12 @@ namespace SplashEdit.RuntimeCode
return null;
}
public void CreatePSXMesh(float GTEScaling, bool useBSP = false)
public void CreatePSXMesh(float GTEScaling)
{
Renderer renderer = GetComponent<Renderer>();
if (renderer != null)
{
if (useBSP && _modifiedMesh != null)
{
// Create a temporary GameObject with the modified mesh but same materials
GameObject tempGO = new GameObject("TempBSPMesh");
tempGO.transform.position = transform.position;
tempGO.transform.rotation = transform.rotation;
tempGO.transform.localScale = transform.localScale;
MeshFilter tempMF = tempGO.AddComponent<MeshFilter>();
tempMF.sharedMesh = _modifiedMesh;
MeshRenderer tempMR = tempGO.AddComponent<MeshRenderer>();
tempMR.sharedMaterials = renderer.sharedMaterials;
Mesh = PSXMesh.CreateFromUnityRenderer(tempMR, GTEScaling, transform, Textures);
// Clean up
GameObject.DestroyImmediate(tempGO);
}
else
{
Mesh = PSXMesh.CreateFromUnityRenderer(renderer, GTEScaling, transform, Textures);
}
Mesh = PSXMesh.CreateFromUnityRenderer(renderer, GTEScaling, transform, Textures);
}
}
}