Vrm10Exporter.ExportMeta

This commit is contained in:
ousttrue 2021-04-08 16:21:19 +09:00
parent 45503b4b31
commit 4bf461a226
12 changed files with 91 additions and 65 deletions

View File

@ -156,5 +156,6 @@ namespace UniGLTF
}
return self.Slice(start, self.Count - start);
}
}
}

View File

@ -302,7 +302,7 @@ namespace UniVRM10
try
{
var model = new UniVRM10.RuntimeVrmConverter().ToModelFrom10(root, Meta ? Meta : m_tmpMeta);
var model = new UniVRM10.RuntimeVrmConverter().ToModelFrom10(root);
// if (MeshUtility.Validators.HumanoidValidator.HasRotationOrScale(root))
// {
@ -318,9 +318,9 @@ namespace UniVRM10
// var exportedBytes = GetGlb(model);
// export vrm-1.0
var exporter = new UniVRM10.Vrm10Exporter();
var exporter = new UniVRM10.Vrm10Exporter(AssetTextureUtil.IsTextureEditorAsset);
var option = new VrmLib.ExportArgs();
exporter.Export(model, option, AssetTextureUtil.IsTextureEditorAsset);
exporter.Export(root, model, option, Meta ? Meta : m_tmpMeta);
var exportedBytes = exporter.Storage.ToBytes();

View File

@ -7,12 +7,12 @@ namespace UniVRM10
public static byte[] ToGlb(this VrmLib.Model model)
{
// export vrm-1.0
var exporter10 = new Vrm10Exporter();
var exporter10 = new Vrm10Exporter(_ => false);
var option = new VrmLib.ExportArgs
{
// vrm = false
};
exporter10.Export(model, option, _ => false);
exporter10.Export(null, model, option);
var glb10 = UniGLTF.Glb.Parse(exporter10.Storage.ToBytes());
return glb10.ToBytes();
}

View File

@ -20,20 +20,10 @@ namespace UniVRM10
/// <summary>
/// metaObject が null のときは、root から取得する
/// </summary>
public VrmLib.Model ToModelFrom10(GameObject root, VRM10MetaObject metaObject = null)
public VrmLib.Model ToModelFrom10(GameObject root)
{
Model = new VrmLib.Model(VrmLib.Coordinates.Unity);
if (metaObject is null)
{
var vrmController = root.GetComponent<VRM10Controller>();
if (vrmController is null || vrmController.Meta is null)
{
throw new NullReferenceException("metaObject is null");
}
metaObject = vrmController.Meta;
}
ToGlbModel(root);
// humanoid

View File

@ -9,13 +9,15 @@ using VRMShaders;
namespace UniVRM10
{
public class Vrm10Exporter
public class Vrm10Exporter : IDisposable
{
public readonly Vrm10Storage Storage = new Vrm10Storage();
public readonly string VrmExtensionName = "VRMC_vrm";
public Vrm10Exporter()
TextureExporter m_textureExporter;
public Vrm10Exporter(Func<Texture, bool> useAsset)
{
Storage.Gltf.extensionsUsed.Add(glTF_KHR_materials_unlit.ExtensionName);
Storage.Gltf.extensionsUsed.Add(glTF_KHR_texture_transform.ExtensionName);
@ -28,6 +30,13 @@ namespace UniVRM10
{
});
m_textureExporter = new TextureExporter(useAsset);
}
public void Dispose()
{
m_textureExporter.Dispose();
}
public void ExportAsset(Model model)
@ -109,9 +118,9 @@ namespace UniVRM10
});
}
public void Export(Model m, ExportArgs option, Func<Texture, bool> useAsset)
public void Export(GameObject root, Model model, ExportArgs option, VRM10MetaObject metaObject = null)
{
ExportAsset(m);
ExportAsset(model);
///
/// 必要な容量を先に確保
@ -120,7 +129,7 @@ namespace UniVRM10
{
var reserveBytes = 0;
// mesh
foreach (var g in m.MeshGroups)
foreach (var g in model.MeshGroups)
{
foreach (var mesh in g.Meshes)
{
@ -144,32 +153,49 @@ namespace UniVRM10
}
// mesh
ExportMeshes(m.MeshGroups, m.Materials, option);
ExportMeshes(model.MeshGroups, model.Materials, option);
// node
ExportNodes(m.Root, m.Nodes, m.MeshGroups, option);
ExportNodes(model.Root, model.Nodes, model.MeshGroups, option);
// material
var textureExporter = new TextureExporter(useAsset);
var materialExporter = new Vrm10MaterialExporter();
foreach (Material material in m.Materials)
foreach (Material material in model.Materials)
{
var glTFMaterial = materialExporter.ExportMaterial(material, textureExporter);
var glTFMaterial = materialExporter.ExportMaterial(material, m_textureExporter);
Storage.Gltf.materials.Add(glTFMaterial);
}
var (vrm, thumbnailTextureIndex) = ExportVrm(root, model, metaObject);
// Extension で Texture が増える場合があるので最後に呼ぶ
for (int i = 0; i < textureExporter.Exported.Count; ++i)
for (int i = 0; i < m_textureExporter.Exported.Count; ++i)
{
var unityTexture = textureExporter.Exported[i];
var unityTexture = m_textureExporter.Exported[i];
Storage.Gltf.PushGltfTexture(0, unityTexture);
}
ExportVrm(m);
if (thumbnailTextureIndex.HasValue)
{
vrm.Meta.ThumbnailImage = Storage.Gltf.textures[thumbnailTextureIndex.Value].source;
}
UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrm);
}
void ExportVrm(Model model)
(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm, int?) ExportVrm(GameObject root, Model model, VRM10MetaObject meta)
{
if (meta is null)
{
var vrmController = root.GetComponent<VRM10Controller>();
if (vrmController is null || vrmController.Meta is null)
{
throw new NullReferenceException("metaObject is null");
}
meta = vrmController.Meta;
}
var vrm = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm
{
Humanoid = new UniGLTF.Extensions.VRMC_vrm.Humanoid
@ -188,6 +214,7 @@ namespace UniVRM10
};
ExportHumanoid(vrm, model);
var thumbnailTextureIndex = ExportMeta(vrm, meta);
// meta
@ -197,7 +224,33 @@ namespace UniVRM10
// firstPerson
UniGLTF.Extensions.VRMC_vrm.GltfSerializer.SerializeTo(ref Storage.Gltf.extensions, vrm);
return (vrm, thumbnailTextureIndex);
}
int? ExportMeta(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, VRM10MetaObject meta)
{
vrm.Meta.Name = meta.Name;
vrm.Meta.Version = meta.Version;
vrm.Meta.Authors = meta.Authors.ToList();
vrm.Meta.CopyrightInformation = meta.CopyrightInformation;
vrm.Meta.ContactInformation = meta.ContactInformation;
vrm.Meta.References = meta.References.ToList();
// vrm.Meta.ThirdPartyLicenses =
vrm.Meta.AvatarPermission = meta.AllowedUser;
vrm.Meta.AllowExcessivelyViolentUsage = meta.ViolentUsage;
vrm.Meta.AllowExcessivelySexualUsage = meta.SexualUsage;
vrm.Meta.CommercialUsage = meta.CommercialUsage;
vrm.Meta.AllowPoliticalOrReligiousUsage = meta.PoliticalOrReligiousUsage;
vrm.Meta.CreditNotation = meta.CreditNotation;
vrm.Meta.AllowRedistribution = meta.Redistribution;
vrm.Meta.Modification = meta.ModificationLicense;
vrm.Meta.OtherLicenseUrl = meta.OtherLicenseUrl;
int? thumbnailTextureIndex = default;
if (meta.Thumbnail != null)
{
thumbnailTextureIndex = m_textureExporter.ExportSRGB(meta.Thumbnail);
}
return thumbnailTextureIndex;
}
void ExportHumanoid(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, Model model)

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using UniGLTF;
using UniJSON;
using VrmLib;
@ -36,6 +37,8 @@ namespace UniVRM10
{
new UniGLTF.ArrayByteBuffer()
};
Gltf.AddBuffer(Buffers[0]);
}
/// <summary>

View File

@ -22,12 +22,12 @@ public static class ExportDebugUtil
public static string GetJsonString(VrmLib.Model model)
{
// export vrm-1.0
var exporter10 = new Vrm10Exporter();
var exporter10 = new Vrm10Exporter(_ => false);
var option = new VrmLib.ExportArgs
{
// vrm = false
};
exporter10.Export(model, option, _ => false);
exporter10.Export(null, model, option);
var glbBytes10 = exporter10.Storage.ToBytes();
var glb10 = UniGLTF.Glb.Parse(glbBytes10);
return System.Text.Encoding.UTF8.GetString(glb10.Json.Bytes.Array, glb10.Json.Bytes.Offset, glb10.Json.Bytes.Count);

View File

@ -50,7 +50,7 @@ namespace UniVRM10.Test
private Model ToVrmModel(GameObject root)
{
var exporter = new UniVRM10.RuntimeVrmConverter();
var model = exporter.ToModelFrom10(root, root.GetComponent<VRM10Controller>().Meta);
var model = exporter.ToModelFrom10(root);
model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);
return model;

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using UniGLTF;
namespace VrmLib
{

View File

@ -1,29 +0,0 @@
using System;
namespace VrmLib
{
public static class ArraySegmentExtensions
{
public static ArraySegment<T> Slice<T>(this ArraySegment<T> self, int start, int length)
{
if (start + length > self.Count)
{
throw new ArgumentOutOfRangeException();
}
return new ArraySegment<T>(
self.Array,
self.Offset + start,
length
);
}
public static ArraySegment<T> Slice<T>(this ArraySegment<T> self, int start)
{
if (start > self.Count)
{
throw new ArgumentOutOfRangeException();
}
return self.Slice(start, self.Count - start);
}
}
}

View File

@ -1,6 +1,8 @@
{
"name": "VrmLib",
"references": [],
"references": [
"UniGLTF"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -9,7 +9,7 @@ namespace VRMShaders
/// glTF にエクスポートする Texture2D を蓄えて index を確定させる。
/// Exporter の最後でまとめて Texture2D から bytes 列を得て出力する。
/// </summary>
public class TextureExporter
public class TextureExporter : IDisposable
{
Func<Texture, bool> m_useAsset;
@ -18,6 +18,11 @@ namespace VRMShaders
m_useAsset = useAsset;
}
public void Dispose()
{
// TODO: export 用にコピー・変換したテクスチャーをここで解放したい
}
public enum ConvertTypes
{
// 無変換