mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-23 03:16:12 -05:00
110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using System;
|
|
using VrmLib;
|
|
|
|
namespace UniVRM10
|
|
{
|
|
public static class IExporterExtensions
|
|
{
|
|
public static byte[] Export(this Vrm10Exporter exporter, Model m, ExportArgs option)
|
|
{
|
|
exporter.ExportAsset(m);
|
|
|
|
///
|
|
/// 必要な容量を先に確保
|
|
/// (sparseは考慮してないので大きめ)
|
|
///
|
|
{
|
|
var reserveBytes = 0;
|
|
// image
|
|
foreach (var image in m.Images)
|
|
{
|
|
reserveBytes += image.Bytes.Count;
|
|
}
|
|
// mesh
|
|
foreach (var g in m.MeshGroups)
|
|
{
|
|
foreach (var mesh in g.Meshes)
|
|
{
|
|
// 頂点バッファ
|
|
reserveBytes += mesh.IndexBuffer.ByteLength;
|
|
foreach (var kv in mesh.VertexBuffer)
|
|
{
|
|
reserveBytes += kv.Value.ByteLength;
|
|
}
|
|
// morph
|
|
foreach (var morph in mesh.MorphTargets)
|
|
{
|
|
foreach (var kv in morph.VertexBuffer)
|
|
{
|
|
reserveBytes += kv.Value.ByteLength;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
exporter.Reserve(reserveBytes);
|
|
}
|
|
|
|
exporter.ExportImageAndTextures(m.Images, m.Textures);
|
|
|
|
// material
|
|
foreach (var src in m.Materials)
|
|
{
|
|
if (src is MToonMaterial mtoon)
|
|
{
|
|
exporter.ExportMaterialMToon(src, mtoon, m.Textures);
|
|
}
|
|
else if (src is UnlitMaterial unlit)
|
|
{
|
|
exporter.ExportMaterialUnlit(src, unlit, m.Textures);
|
|
}
|
|
else if (src is PBRMaterial pbr)
|
|
{
|
|
exporter.ExportMaterialPBR(src, pbr, m.Textures);
|
|
}
|
|
else
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
|
|
// mesh
|
|
exporter.ExportMeshes(m.MeshGroups, m.Materials, option);
|
|
|
|
// node
|
|
exporter.ExportNodes(m.Root, m.Nodes, m.MeshGroups, option);
|
|
|
|
// animation
|
|
exporter.ExportAnimations(m.Animations, m.Nodes, option);
|
|
|
|
if (option.vrm)
|
|
{
|
|
ExportVrm(exporter, m);
|
|
}
|
|
|
|
return exporter.ToBytes();
|
|
}
|
|
|
|
static void ExportVrm(Vrm10Exporter exporter, Model m)
|
|
{
|
|
if (m.Vrm == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
exporter.ExportVrmMeta(m.Vrm, m.Textures);
|
|
|
|
exporter.ExportVrmHumanoid(m.GetBoneMap(), m.Nodes);
|
|
|
|
exporter.ExportVrmMaterialProperties(m.Materials, m.Textures);
|
|
|
|
exporter.ExportVrmExpression(m.Vrm.ExpressionManager, m.MeshGroups, m.Materials, m.Nodes);
|
|
|
|
exporter.ExportVrmSpringBone(m.Vrm.SpringBone, m.Nodes);
|
|
|
|
exporter.ExportVrmFirstPersonAndLookAt(m.Vrm.FirstPerson, m.Vrm.LookAt, m.MeshGroups, m.Nodes);
|
|
|
|
exporter.ExportVrmEnd();
|
|
}
|
|
|
|
}
|
|
} |