61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using MessagePack;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
|
|
namespace XED.Manage
|
|
{
|
|
[MessagePackObject]
|
|
public class SerializableMesh
|
|
{
|
|
[Key(0)]
|
|
public SerializableVector3[] vertices;
|
|
[Key(1)]
|
|
public SerializableVector3[] normals;
|
|
[Key(2)]
|
|
public SerializableVector4[] tangents;
|
|
[Key(3)]
|
|
public SerializableVector2[] uv;
|
|
//[Key("Triangles")]
|
|
//public int[] triangles;
|
|
[Key(4)]
|
|
public SubmeshData[] submeshes;
|
|
public void SetData(Mesh mesh)
|
|
{
|
|
vertices = SerializableVector3.FromVector3Array(mesh.vertices);
|
|
normals = SerializableVector3.FromVector3Array(mesh.normals);
|
|
tangents = SerializableVector4.FromVector4Array(mesh.tangents);
|
|
uv = SerializableVector2.FromVector2Array(mesh.uv);
|
|
//triangles = mesh.triangles;
|
|
List<SubmeshData> submeshList = new List<SubmeshData>();
|
|
for (int i = 0; i < mesh.subMeshCount; i++)
|
|
{
|
|
SubmeshData submesh = new SubmeshData
|
|
{
|
|
materialIndex = i,
|
|
triangles = mesh.GetTriangles(i)
|
|
};
|
|
submeshList.Add(submesh);
|
|
}
|
|
submeshes = submeshList.ToArray();
|
|
}
|
|
public Mesh ToMesh()
|
|
{
|
|
Mesh newMesh = new Mesh();
|
|
newMesh.indexFormat = vertices.Length > 65535 ? IndexFormat.UInt32 : IndexFormat.UInt16;
|
|
newMesh.vertices = SerializableVector3.ToVector3Array(vertices);
|
|
newMesh.normals = SerializableVector3.ToVector3Array(normals);
|
|
newMesh.tangents = SerializableVector4.ToVector4Array(tangents);
|
|
newMesh.uv = SerializableVector2.ToVector2Array(uv);
|
|
//newMesh.triangles = triangles;
|
|
newMesh.subMeshCount = submeshes.Length;
|
|
for (int i = 0; i < submeshes.Length; i++)
|
|
{
|
|
newMesh.SetTriangles(submeshes[i].triangles, i);
|
|
}
|
|
newMesh.RecalculateBounds();
|
|
return newMesh;
|
|
}
|
|
}
|
|
|
|
} |