Added navmesh exporting

This commit is contained in:
2025-04-03 21:42:26 +02:00
parent fa747ad786
commit 9037937d3c
7 changed files with 142 additions and 14 deletions

62
Runtime/PSXNavMesh.cs Normal file
View File

@@ -0,0 +1,62 @@
using UnityEngine;
using Unity.AI.Navigation;
using UnityEngine.AI;
using System.Collections.Generic;
namespace SplashEdit.RuntimeCode
{
public struct PSXNavMeshTri
{
public PSXNavmeshVertex v0, v1, v2;
}
public struct PSXNavmeshVertex
{
public short vx, vy, vz;
}
[RequireComponent(typeof(NavMeshSurface))]
public class PSXNavMesh : MonoBehaviour
{
[HideInInspector]
public List<PSXNavMeshTri> Navmesh { get; set; }
public void CreateNavmesh(float GTEScaling)
{
Navmesh = new List<PSXNavMeshTri>();
NavMeshSurface navMeshSurface = GetComponent<NavMeshSurface>();
navMeshSurface.BuildNavMesh();
NavMeshTriangulation triangulation = NavMesh.CalculateTriangulation();
int[] triangles = triangulation.indices;
Vector3[] vertices = triangulation.vertices;
for (int i = 0; i < triangles.Length; i += 3)
{
int vid0 = triangles[i];
int vid1 = triangles[i + 1];
int vid2 = triangles[i + 2];
PSXNavMeshTri tri = new PSXNavMeshTri();
tri.v0.vx = PSXTrig.ConvertCoordinateToPSX(vertices[vid0].x, GTEScaling);
tri.v0.vy = PSXTrig.ConvertCoordinateToPSX(-vertices[vid0].y, GTEScaling);
tri.v0.vz = PSXTrig.ConvertCoordinateToPSX(vertices[vid0].z, GTEScaling);
tri.v1.vx = PSXTrig.ConvertCoordinateToPSX(vertices[vid1].x, GTEScaling);
tri.v1.vy = PSXTrig.ConvertCoordinateToPSX(-vertices[vid1].y, GTEScaling);
tri.v1.vz = PSXTrig.ConvertCoordinateToPSX(vertices[vid1].z, GTEScaling);
tri.v2.vx = PSXTrig.ConvertCoordinateToPSX(vertices[vid2].x, GTEScaling);
tri.v2.vy = PSXTrig.ConvertCoordinateToPSX(-vertices[vid2].y, GTEScaling);
tri.v2.vz = PSXTrig.ConvertCoordinateToPSX(vertices[vid2].z, GTEScaling);
Navmesh.Add(tri);
}
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6a2f8d45e1591de1e945b3b7bdfb123b

View File

@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.AI;
namespace SplashEdit.RuntimeCode namespace SplashEdit.RuntimeCode
{ {
@@ -16,6 +17,7 @@ namespace SplashEdit.RuntimeCode
private PSXObjectExporter[] _exporters; private PSXObjectExporter[] _exporters;
private TextureAtlas[] _atlases; private TextureAtlas[] _atlases;
private PSXNavMesh[] _navmeshes;
private PSXData _psxData; private PSXData _psxData;
@@ -34,6 +36,13 @@ namespace SplashEdit.RuntimeCode
exp.CreatePSXTextures2D(); exp.CreatePSXTextures2D();
exp.CreatePSXMesh(GTEScaling); exp.CreatePSXMesh(GTEScaling);
} }
_navmeshes = FindObjectsByType<PSXNavMesh>(FindObjectsSortMode.None);
foreach (PSXNavMesh navmesh in _navmeshes)
{
navmesh.CreateNavmesh(GTEScaling);
}
PackTextures(); PackTextures();
ExportFile(); ExportFile();
} }
@@ -73,6 +82,10 @@ namespace SplashEdit.RuntimeCode
List<long> clutOffsetPlaceholderPositions = new List<long>(); List<long> clutOffsetPlaceholderPositions = new List<long>();
List<long> clutDataOffsets = new List<long>(); List<long> clutDataOffsets = new List<long>();
// Lists for navmesh data offsets.
List<long> navmeshOffsetPlaceholderPositions = new List<long>();
List<long> navmeshDataOffsets = new List<long>();
int clutCount = 0; int clutCount = 0;
// Cluts // Cluts
@@ -94,9 +107,10 @@ namespace SplashEdit.RuntimeCode
writer.Write('P'); // 1 byte writer.Write('P'); // 1 byte
writer.Write((ushort)1); // 2 bytes - version writer.Write((ushort)1); // 2 bytes - version
writer.Write((ushort)_exporters.Length); // 2 bytes writer.Write((ushort)_exporters.Length); // 2 bytes
writer.Write((ushort)_navmeshes.Length);
writer.Write((ushort)_atlases.Length); // 2 bytes writer.Write((ushort)_atlases.Length); // 2 bytes
writer.Write((ushort)clutCount); // 2 bytes writer.Write((ushort)clutCount); // 2 bytes
for (int i = 0; i < 3; i++) writer.Write((ushort)0); for (int i = 0; i < 2; i++) writer.Write((ushort)0);
// GameObject section (exporters) // GameObject section (exporters)
foreach (PSXObjectExporter exporter in _exporters) foreach (PSXObjectExporter exporter in _exporters)
@@ -125,6 +139,17 @@ namespace SplashEdit.RuntimeCode
writer.Write((ushort)0); writer.Write((ushort)0);
} }
// Navmesh metadata section
foreach (PSXNavMesh navmesh in _navmeshes)
{
// Write placeholder for navmesh raw data offset.
navmeshOffsetPlaceholderPositions.Add(writer.BaseStream.Position);
writer.Write((int)0); // 4-byte placeholder for navmesh data offset.
writer.Write((ushort)navmesh.Navmesh.Count);
writer.Write((ushort)0);
}
// Atlas metadata section // Atlas metadata section
foreach (TextureAtlas atlas in _atlases) foreach (TextureAtlas atlas in _atlases)
{ {
@@ -228,6 +253,27 @@ namespace SplashEdit.RuntimeCode
} }
} }
foreach (PSXNavMesh navmesh in _navmeshes) {
AlignToFourBytes(writer);
long navmeshDataOffset = writer.BaseStream.Position;
navmeshDataOffsets.Add(navmeshDataOffset);
foreach(PSXNavMeshTri tri in navmesh.Navmesh) {
writer.Write((ushort) tri.v0.vx);
writer.Write((ushort) tri.v0.vy);
writer.Write((ushort) tri.v0.vz);
writer.Write((ushort) tri.v1.vx);
writer.Write((ushort) tri.v1.vy);
writer.Write((ushort) tri.v1.vz);
writer.Write((ushort) tri.v2.vx);
writer.Write((ushort) tri.v2.vy);
writer.Write((ushort) tri.v2.vz);
}
}
// Atlas data section: Write raw texture data for each atlas. // Atlas data section: Write raw texture data for each atlas.
foreach (TextureAtlas atlas in _atlases) foreach (TextureAtlas atlas in _atlases)
{ {
@@ -257,7 +303,8 @@ namespace SplashEdit.RuntimeCode
long clutDataOffset = writer.BaseStream.Position; long clutDataOffset = writer.BaseStream.Position;
clutDataOffsets.Add(clutDataOffset); clutDataOffsets.Add(clutDataOffset);
foreach(VRAMPixel color in texture.ColorPalette) { foreach (VRAMPixel color in texture.ColorPalette)
{
writer.Write((ushort)color.Pack()); writer.Write((ushort)color.Pack());
} }
} }
@@ -279,6 +326,21 @@ namespace SplashEdit.RuntimeCode
Debug.LogError("Mismatch between metadata mesh offset placeholders and mesh data blocks!"); Debug.LogError("Mismatch between metadata mesh offset placeholders and mesh data blocks!");
} }
// Backfill the navmesh offsets into the metadata section.
if (navmeshOffsetPlaceholderPositions.Count == navmeshDataOffsets.Count)
{
for (int i = 0; i < navmeshOffsetPlaceholderPositions.Count; i++)
{
writer.Seek((int)navmeshOffsetPlaceholderPositions[i], SeekOrigin.Begin);
writer.Write((int)navmeshDataOffsets[i]);
}
}
else
{
Debug.LogError("Mismatch between metadata mesh offset placeholders and mesh data blocks!");
}
// Backfill the atlas data offsets into the metadata section. // Backfill the atlas data offsets into the metadata section.
if (atlasOffsetPlaceholderPositions.Count == atlasDataOffsets.Count) if (atlasOffsetPlaceholderPositions.Count == atlasDataOffsets.Count)
{ {

View File

@@ -1,7 +1,5 @@
using System.Runtime.InteropServices;
using UnityEditor; using UnityEditor;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEditor;
using UnityEngine; using UnityEngine;
namespace SplashEdit.RuntimeCode namespace SplashEdit.RuntimeCode

View File

@@ -1,7 +1,9 @@
{ {
"name": "net.psxsplash.splashedit.Runtime", "name": "net.psxsplash.splashedit.Runtime",
"rootNamespace": "", "rootNamespace": "",
"references": [], "references": [
"GUID:8c4dd21966739024fbd72155091d199e"
],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],
"allowUnsafeCode": false, "allowUnsafeCode": false,

View File

@@ -3,6 +3,8 @@
"version": "1.0.0", "version": "1.0.0",
"displayName": "Splashedit", "displayName": "Splashedit",
"description": "A toolkit for PSX asset management within Unity", "description": "A toolkit for PSX asset management within Unity",
"unity": "6000.0" "unity": "6000.0",
"dependencies": {
"com.unity.ai.navigation": "2.0.6"
}
} }

Binary file not shown.