Fixed loaded file export on global

Added workaround for isReadable error on scene export to .pmx
Fixed compilation on android
Incremented version
This commit is contained in:
katboi01
2025-10-04 00:27:12 +02:00
parent 5351f82f09
commit 3cb3239316
5 changed files with 98 additions and 33 deletions
+63 -6
View File
@@ -8,10 +8,10 @@ using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Rendering;
using static BillboardBuilder;
using static LibMMD.Model.Morph;
using static LibMMD.Model.SkinningOperator;
using static BillboardBuilder;
//using UnityGLTF;
@@ -359,8 +359,16 @@ public class ModelExporter
var mesh = meshfilter.mesh;
if (!mesh.isReadable)
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
UmaViewerUI.Instance.ShowMessage($"Mesh {mesh.name} was not readable. Attempting to export", UIMessageType.Warning);
try
{
mesh = MakeReadableMeshCopy(mesh);
}
catch
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
}
}
var vertices = mesh.vertices;
var normals = mesh.normals;
@@ -402,8 +410,16 @@ public class ModelExporter
var mesh = smr.sharedMesh;
if (!mesh.isReadable)
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
UmaViewerUI.Instance.ShowMessage($"Mesh {mesh.name} was not readable. Attempting to export", UIMessageType.Warning);
try
{
mesh = MakeReadableMeshCopy(mesh);
}
catch
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
}
}
var vertices = mesh.vertices;
var normals = mesh.normals;
@@ -498,4 +514,45 @@ public class ModelExporter
{
return bones.Contains(bone) ? bones.IndexOf(bone) : 0;
}
/// <summary> Credit to pohype: https://discussions.unity.com/t/reading-meshes-at-runtime-that-are-not-enabled-for-read-write/804189/7 </summary>
public static Mesh MakeReadableMeshCopy(Mesh nonReadableMesh)
{
Mesh meshCopy = new Mesh();
meshCopy.indexFormat = nonReadableMesh.indexFormat;
// Handle vertices
GraphicsBuffer verticesBuffer = nonReadableMesh.GetVertexBuffer(0);
int totalSize = verticesBuffer.stride * verticesBuffer.count;
byte[] data = new byte[totalSize];
verticesBuffer.GetData(data);
meshCopy.SetVertexBufferParams(nonReadableMesh.vertexCount, nonReadableMesh.GetVertexAttributes());
meshCopy.SetVertexBufferData(data, 0, 0, totalSize);
verticesBuffer.Release();
// Handle triangles
meshCopy.subMeshCount = nonReadableMesh.subMeshCount;
GraphicsBuffer indexesBuffer = nonReadableMesh.GetIndexBuffer();
int tot = indexesBuffer.stride * indexesBuffer.count;
byte[] indexesData = new byte[tot];
indexesBuffer.GetData(indexesData);
meshCopy.SetIndexBufferParams(indexesBuffer.count, nonReadableMesh.indexFormat);
meshCopy.SetIndexBufferData(indexesData, 0, 0, tot);
indexesBuffer.Release();
// Restore submesh structure
uint currentIndexOffset = 0;
for (int i = 0; i < meshCopy.subMeshCount; i++)
{
uint subMeshIndexCount = nonReadableMesh.GetIndexCount(i);
meshCopy.SetSubMesh(i, new SubMeshDescriptor((int)currentIndexOffset, (int)subMeshIndexCount));
currentIndexOffset += subMeshIndexCount;
}
// Recalculate normals and bounds
meshCopy.RecalculateNormals();
meshCopy.RecalculateBounds();
return meshCopy;
}
}