Added navmesh exporting
This commit is contained in:
62
Runtime/PSXNavMesh.cs
Normal file
62
Runtime/PSXNavMesh.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Runtime/PSXNavMesh.cs.meta
Normal file
2
Runtime/PSXNavMesh.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a2f8d45e1591de1e945b3b7bdfb123b
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
namespace SplashEdit.RuntimeCode
|
||||
{
|
||||
@@ -16,6 +17,7 @@ namespace SplashEdit.RuntimeCode
|
||||
|
||||
private PSXObjectExporter[] _exporters;
|
||||
private TextureAtlas[] _atlases;
|
||||
private PSXNavMesh[] _navmeshes;
|
||||
|
||||
private PSXData _psxData;
|
||||
|
||||
@@ -34,6 +36,13 @@ namespace SplashEdit.RuntimeCode
|
||||
exp.CreatePSXTextures2D();
|
||||
exp.CreatePSXMesh(GTEScaling);
|
||||
}
|
||||
|
||||
_navmeshes = FindObjectsByType<PSXNavMesh>(FindObjectsSortMode.None);
|
||||
foreach (PSXNavMesh navmesh in _navmeshes)
|
||||
{
|
||||
navmesh.CreateNavmesh(GTEScaling);
|
||||
}
|
||||
|
||||
PackTextures();
|
||||
ExportFile();
|
||||
}
|
||||
@@ -73,6 +82,10 @@ namespace SplashEdit.RuntimeCode
|
||||
List<long> clutOffsetPlaceholderPositions = 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;
|
||||
|
||||
// Cluts
|
||||
@@ -94,9 +107,10 @@ namespace SplashEdit.RuntimeCode
|
||||
writer.Write('P'); // 1 byte
|
||||
writer.Write((ushort)1); // 2 bytes - version
|
||||
writer.Write((ushort)_exporters.Length); // 2 bytes
|
||||
writer.Write((ushort)_navmeshes.Length);
|
||||
writer.Write((ushort)_atlases.Length); // 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)
|
||||
foreach (PSXObjectExporter exporter in _exporters)
|
||||
@@ -122,7 +136,18 @@ namespace SplashEdit.RuntimeCode
|
||||
writer.Write((int)rotationMatrix[2, 2]);
|
||||
|
||||
writer.Write((ushort)exporter.Mesh.Triangles.Count);
|
||||
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
|
||||
@@ -150,7 +175,7 @@ namespace SplashEdit.RuntimeCode
|
||||
writer.Write((ushort)texture.ClutPackingX); // 2 bytes
|
||||
writer.Write((ushort)texture.ClutPackingY); // 2 bytes
|
||||
writer.Write((ushort)texture.ColorPalette.Count); // 2 bytes
|
||||
writer.Write((ushort) 0); // 2 bytes
|
||||
writer.Write((ushort)0); // 2 bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
foreach (TextureAtlas atlas in _atlases)
|
||||
{
|
||||
@@ -257,8 +303,9 @@ namespace SplashEdit.RuntimeCode
|
||||
long clutDataOffset = writer.BaseStream.Position;
|
||||
clutDataOffsets.Add(clutDataOffset);
|
||||
|
||||
foreach(VRAMPixel color in texture.ColorPalette) {
|
||||
writer.Write((ushort) color.Pack());
|
||||
foreach (VRAMPixel color in texture.ColorPalette)
|
||||
{
|
||||
writer.Write((ushort)color.Pack());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,6 +326,21 @@ namespace SplashEdit.RuntimeCode
|
||||
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.
|
||||
if (atlasOffsetPlaceholderPositions.Count == atlasDataOffsets.Count)
|
||||
{
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using UnityEditor;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SplashEdit.RuntimeCode
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"name": "net.psxsplash.splashedit.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [],
|
||||
"references": [
|
||||
"GUID:8c4dd21966739024fbd72155091d199e"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
|
||||
14
package.json
14
package.json
@@ -1,8 +1,10 @@
|
||||
{
|
||||
"name": "net.psxsplash.splashedit",
|
||||
"version": "1.0.0",
|
||||
"displayName": "Splashedit",
|
||||
"description": "A toolkit for PSX asset management within Unity",
|
||||
"unity": "6000.0"
|
||||
"name": "net.psxsplash.splashedit",
|
||||
"version": "1.0.0",
|
||||
"displayName": "Splashedit",
|
||||
"description": "A toolkit for PSX asset management within Unity",
|
||||
"unity": "6000.0",
|
||||
"dependencies": {
|
||||
"com.unity.ai.navigation": "2.0.6"
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user