Add support for exporting prop, compatible with the new version, add the EnableRootMotion option (#143)

* update BillboardBuilder

* add a new enum value

* Add the EnableRootMotion option to support scenarios where the character is not within the camera view

* Add support for exporting prop

* update

* compatible with the new version 2

* update

---------

Co-authored-by: 左右看 <1918891542@qq.com>
This commit is contained in:
CroakFang
2025-09-28 18:10:18 +08:00
committed by GitHub
parent 2f2f06eadc
commit d66991c2ef
33 changed files with 6620 additions and 390 deletions
+126 -49
View File
@@ -4,59 +4,85 @@ using UnityEngine;
public class BillboardBuilder : MonoBehaviour
{
public static Mesh CreateSphereMesh(float radius, float uvRadius, Vector2 uvCenter, int longitude = 6, int latitude = 6)
//修改(Billboard的导出相关)
public static Mesh CreateSphereMesh(float radius, int subdiv, float uvRadius, Vector2 uvCenter)
{
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[(longitude + 1) * (latitude + 1)];
Vector2[] uvs = new Vector2[vertices.Length];
int[] triangles = new int[longitude * latitude * 6];
int vert = 0, tri = 0;
for (int lat = 0; lat <= latitude; lat++)
var t = (1f + Mathf.Sqrt(5f)) / 2f;
var baseVerts = new List<Vector3>
{
float a1 = Mathf.PI * lat / latitude;
float y = Mathf.Cos(a1);
float sin1 = Mathf.Sin(a1);
for (int lon = 0; lon <= longitude; lon++)
{
float a2 = 2 * Mathf.PI * lon / longitude;
float x = sin1 * Mathf.Cos(a2);
float z = sin1 * Mathf.Sin(a2);
vertices[vert] = new Vector3(x, y, z) * radius;
//uvs[vert] = new Vector2((float)lon / longitude, (float)lat / latitude);
Vector3 p = vertices[vert].normalized;
uvs[vert] = new Vector2(p.x, p.z) * uvRadius + uvCenter;
//uvs[vert] = new Vector2(p.x, p.z) * 0.5f + new Vector2(0.5f, 0.5f);
if (lat < latitude && lon < longitude)
{
int current = vert;
int next = vert + longitude + 1;
triangles[tri++] = current;
triangles[tri++] = current + 1;
triangles[tri++] = next;
triangles[tri++] = current + 1;
triangles[tri++] = next + 1;
triangles[tri++] = next;
}
vert++;
}
new Vector3(-1, t, 0), new Vector3( 1, t, 0), new Vector3(-1, -t, 0), new Vector3( 1, -t, 0),
new Vector3( 0, -1, t), new Vector3( 0, 1, t), new Vector3( 0, -1, -t), new Vector3( 0, 1, -t),
new Vector3( t, 0, -1), new Vector3( t, 0, 1), new Vector3(-t, 0, -1), new Vector3(-t, 0, 1),
};
var faces = new List<int[]>
{
new[]{0,11,5}, new[]{0,5,1}, new[]{0,1,7}, new[]{0,7,10}, new[]{0,10,11},
new[]{1,5,9}, new[]{5,11,4}, new[]{11,10,2}, new[]{10,7,6}, new[]{7,1,8},
new[]{3,9,4}, new[]{3,4,2}, new[]{3,2,6}, new[]{3,6,8}, new[]{3,8,9},
new[]{4,9,5}, new[]{2,4,11}, new[]{6,2,10}, new[]{8,6,7}, new[]{9,8,1}
};
for (int i = 0; i < baseVerts.Count; i++) baseVerts[i] = baseVerts[i].normalized;
var vertList = new List<Vector3>(baseVerts);
var triList = new List<int>();
foreach (var f in faces) { triList.Add(f[0]); triList.Add(f[1]); triList.Add(f[2]); }
var midpointCache = new Dictionary<long, int>();
int GetMidpoint(int a, int b)
{
long key = a < b ? ((long)a << 32) + b : ((long)b << 32) + a;
if (midpointCache.TryGetValue(key, out var idx)) return idx;
var m = (vertList[a] + vertList[b]).normalized;
idx = vertList.Count;
vertList.Add(m);
midpointCache[key] = idx;
return idx;
}
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals();
for (int s = 0; s < subdiv; s++)
{
var newTris = new List<int>(triList.Count * 4);
for (int i = 0; i < triList.Count; i += 3)
{
int v0 = triList[i], v1 = triList[i+1], v2 = triList[i+2];
int a = GetMidpoint(v0, v1);
int b = GetMidpoint(v1, v2);
int c = GetMidpoint(v2, v0);
newTris.AddRange(new[]{ v0, a, c, v1, b, a, v2, c, b, a, b, c });
}
triList = newTris;
}
Vector3[] vertices = new Vector3[vertList.Count];
Vector3[] normals = new Vector3[vertList.Count];
Vector2[] uvs = new Vector2[vertList.Count];
for (int i = 0; i < vertList.Count; i++)
{
Vector3 p = vertList[i];
vertices[i] = p * radius;
normals[i] = p;
uvs[i] = new Vector2(p.x, p.z) * uvRadius + uvCenter;
}
var mesh = new Mesh();
if (vertices.Length > 65535) mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.triangles = triList.ToArray();
mesh.RecalculateBounds();
return mesh;
}
public static void BuildBillboard(UmaContainerCharacter container)
{
int groupSize = 4;
@@ -119,16 +145,27 @@ public class BillboardBuilder : MonoBehaviour
// Create
Mesh sphereMesh;
//修改(Billboard的导出相关)
if (radius < 0.003)
{
sphereMesh = CreateSphereMesh(radius, uv_radius, uv_center);
sphereMesh = CreateSphereMesh(radius, 1, uv_radius, uv_center);
}
else
{
sphereMesh = CreateSphereMesh(radius, uv_radius, uv_center, 12, 9);
sphereMesh = CreateSphereMesh(radius, 2, uv_radius, uv_center);
}
int vertexOffset = finalVertices.Count;
foreach (Vector3 v in sphereMesh.vertices)
@@ -165,6 +202,14 @@ public class BillboardBuilder : MonoBehaviour
newSMR.material = s.sharedMaterial;
Debug.Log($"Generated {sphereCount} small spheres and combined them into a single mesh.");
//修改(Billboard的导出相关)
//ApplyBillboardUVOffset(s);
}
}
}
@@ -179,4 +224,36 @@ public class BillboardBuilder : MonoBehaviour
}
}
}
//修改(Billboard的导出相关)
private static void ApplyBillboardUVOffset(SkinnedMeshRenderer smr)
{
var mesh = smr.sharedMesh;
if (mesh == null || mesh.uv2 == null || mesh.uv2.Length == 0) return;
var newMesh = UnityEngine.Object.Instantiate(mesh);
var vertices = newMesh.vertices;
var uv2 = newMesh.uv2;
Matrix4x4 worldToLocal = smr.transform.worldToLocalMatrix;
Vector3 localXAxis = worldToLocal.MultiplyVector(Vector3.right).normalized;
Vector3 localYAxis = worldToLocal.MultiplyVector(Vector3.up).normalized;
for (int i = 0; i < vertices.Length; i++)
{
Vector2 offset = uv2[i];
vertices[i] += localXAxis * offset.x + localYAxis * offset.y;
}
newMesh.vertices = vertices;
newMesh.RecalculateNormals();
newMesh.RecalculateBounds();
smr.sharedMesh = newMesh;
}
}
+58 -8
View File
@@ -31,7 +31,7 @@ public class ModelExporter
BuildBillboard(container);
var textures = TextureExporter.ExportAllTexture(Path.GetDirectoryName(path), container.gameObject);
var model = ReadPMXModel(container.CharaEntry, container, textures);
var model = ReadPMXModel(container, textures, container.CharaEntry);
FileStream fileStream = new FileStream(path, FileMode.Create);
BinaryWriter writer = new BinaryWriter(fileStream);
@@ -46,15 +46,45 @@ public class ModelExporter
UmaViewerUI.Instance.ShowMessage($"PMX Save at {path}", UIMessageType.Success);
}
public static RawMMDModel ReadPMXModel(CharaEntry entry, UmaContainer container, string[] textures)
public static void ExportModel(UmaContainer container, string path)
{
var textures = TextureExporter.ExportAllTexture(Path.GetDirectoryName(path), container.gameObject);
var model = ReadPMXModel(container, textures);
FileStream fileStream = new FileStream(path, FileMode.Create);
BinaryWriter writer = new BinaryWriter(fileStream);
var config = new ModelConfig() { GlobalToonPath = "Toon" };
PMXWriter.Write(writer, model, config);
writer.Close();
fileStream.Close();
UmaViewerUI.Instance.ShowMessage($"PMX Save at {path}", UIMessageType.Success);
}
public static RawMMDModel ReadPMXModel(UmaContainer container, string[] textures, CharaEntry entry = null)
{
RawMMDModel model = new RawMMDModel();
model.Name = entry.Name;
model.NameEn = entry.GetName();
model.Description = model.DescriptionEn = $"{entry.GetName()}\n{modelInfo}";
if (entry != null)
{
model.Name = entry.Name;
model.NameEn = entry.GetName();
model.Description = model.DescriptionEn = $"{entry.GetName()}\n{modelInfo}";
}
else
{
model.Name = container.gameObject.name;
model.NameEn = container.gameObject.name;
model.Description = model.DescriptionEn = $"{container.gameObject.name}\n{modelInfo}";
}
//Rean Bones
var rootBone = container.transform.Find("Position");
if (rootBone == null)
{
rootBone = container.transform;
}
List<Transform> bones = new List<Transform>(rootBone.GetComponentsInChildren<Transform>());
bones.RemoveAll(o => o.name.Contains("Col_"));
@@ -275,7 +305,10 @@ public class ModelExporter
{
mesh = ((SkinnedMeshRenderer)renderer).sharedMesh;
}
if (!mesh.isReadable)
{
continue;
}
var materials = new List<Material>(renderer.sharedMaterials);
for (int i = 0; i < mesh.subMeshCount; i++)
{
@@ -293,8 +326,15 @@ public class ModelExporter
mat.DrawSelfShadow = true;
mat.EdgeColor = Color.black;
mat.EdgeSize = 0.4f;
var tex = model.TextureList.Find(t => t.TexturePath.Contains($"/{material.mainTexture.name}.png"));
mat.Texture = (tex ?? model.TextureList[0]);
if (material.HasProperty("_MainTex"))
{
var tex = model.TextureList.Find(t => t.TexturePath.Contains($"/{material.mainTexture.name}.png"));
mat.Texture = (tex ?? model.TextureList[0]);
}
else
{
mat.Texture = model.TextureList[0];
}
mat.MetaInfo = "";
part.BaseShift = baseShift + mesh.GetSubMesh(i).indexStart;
part.TriangleIndexNum = mesh.GetSubMesh(i).indexCount;
@@ -317,6 +357,11 @@ public class ModelExporter
var meshfilter = mr.GetComponent<MeshFilter>();
//Cache this data to avoid copying arrays.
var mesh = meshfilter.mesh;
if (!mesh.isReadable)
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
}
var vertices = mesh.vertices;
var normals = mesh.normals;
var uv = mesh.uv;
@@ -355,6 +400,11 @@ public class ModelExporter
else if (renderer is SkinnedMeshRenderer smr)
{
var mesh = smr.sharedMesh;
if (!mesh.isReadable)
{
UmaViewerUI.Instance.ShowMessage($"Export {mesh.name} Failed : Mesh is not readable", UIMessageType.Error);
continue;
}
var vertices = mesh.vertices;
var normals = mesh.normals;
var uv = mesh.uv;