Merge pull request #2390 from Santarh/exporterBranching

gltf/vrm/vrm10 exporters consider RenderPipelines
This commit is contained in:
Masataka SUMI 2024-07-30 15:41:15 +09:00 committed by GitHub
commit 0a2ea72817
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 157 additions and 76 deletions

View File

@ -109,11 +109,12 @@ namespace UniGLTF
{
var data = new ExportingGltfData();
using (var exporter = new gltfExporter(data, Settings,
progress: new EditorProgress(),
animationExporter: new EditorAnimationExporter()))
progress: new EditorProgress(),
animationExporter: new EditorAnimationExporter(),
textureSerializer: new EditorTextureSerializer()))
{
exporter.Prepare(State.ExportRoot);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
}
if (isGlb)

View File

@ -0,0 +1,15 @@
namespace UniGLTF
{
public static class MaterialExporterUtility
{
public static IMaterialExporter GetValidGltfMaterialExporter()
{
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => throw new System.NotImplementedException(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInGltfMaterialExporter(),
_ => new BuiltInGltfMaterialExporter(),
};
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ce72b16c8b374add909ab887c14a0f44
timeCreated: 1722257666

View File

@ -0,0 +1,16 @@
namespace UniGLTF
{
public static class MaterialDescriptorGeneratorUtility
{
public static IMaterialDescriptorGenerator GetValidGltfMaterialDescriptorGenerator()
{
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => new UrpGltfMaterialDescriptorGenerator(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInGltfMaterialDescriptorGenerator(),
_ => new BuiltInGltfMaterialDescriptorGenerator(),
};
}
}
}

View File

@ -1,8 +1,8 @@
using UnityEngine.Rendering;
using UnityEngine.Rendering;
namespace UniGLTF
{
public static class MaterialDescriptorGeneratorUtility
public static class RenderPipelineUtility
{
public static RenderPipelineTypes GetRenderPipelineType()
{
@ -25,16 +25,5 @@ namespace UniGLTF
return RenderPipelineTypes.Unknown;
}
public static IMaterialDescriptorGenerator GetValidGltfMaterialDescriptorGenerator()
{
return GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => new UrpGltfMaterialDescriptorGenerator(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInGltfMaterialDescriptorGenerator(),
_ => new BuiltInGltfMaterialDescriptorGenerator(),
};
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0fd29db9c49145cd9f56f92e6153c8db
timeCreated: 1722257774

View File

@ -45,11 +45,6 @@ namespace UniGLTF
private set;
}
protected virtual IMaterialExporter CreateMaterialExporter()
{
return new BuiltInGltfMaterialExporter();
}
protected ITextureExporter TextureExporter => _textureExporter;
private TextureExporter _textureExporter;
@ -66,10 +61,18 @@ namespace UniGLTF
m_progress.Report(new ExportProgress("gltfExporter", msg, progress));
}
IAnimationExporter m_animationExporter;
private readonly IAnimationExporter m_animationExporter;
private readonly IMaterialExporter m_materialExporter;
private readonly ITextureSerializer m_textureSerializer;
public gltfExporter(ExportingGltfData data, GltfExportSettings settings, IProgress<ExportProgress> progress = null,
IAnimationExporter animationExporter = null)
public gltfExporter(
ExportingGltfData data,
GltfExportSettings settings,
IProgress<ExportProgress> progress = null,
IAnimationExporter animationExporter = null,
IMaterialExporter materialExporter = null,
ITextureSerializer textureSerializer = null
)
{
_data = data;
@ -87,6 +90,8 @@ namespace UniGLTF
}
m_animationExporter = animationExporter;
m_materialExporter = materialExporter ?? MaterialExporterUtility.GetValidGltfMaterialExporter();
m_textureSerializer = textureSerializer ?? new RuntimeTextureSerializer();
}
GameObject m_tmpParent = null;
@ -224,7 +229,7 @@ namespace UniGLTF
// do nothing
}
public virtual void Export(ITextureSerializer textureSerializer)
public virtual void Export()
{
if (m_settings.FreezeMesh)
{
@ -249,10 +254,9 @@ namespace UniGLTF
ReportProgress("Materials and Textures", 0.2f);
Materials = uniqueUnityMeshes.GetUniqueMaterials().ToList();
_textureExporter = new TextureExporter(textureSerializer);
_textureExporter = new TextureExporter(m_textureSerializer);
var materialExporter = CreateMaterialExporter();
_gltf.materials = Materials.Select(x => materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList();
_gltf.materials = Materials.Select(x => m_materialExporter.ExportMaterial(x, TextureExporter, m_settings)).ToList();
#endregion
#region Meshes
@ -346,14 +350,14 @@ namespace UniGLTF
m_animationExporter.Export(_data, Copy, Nodes);
}
ExportExtensions(textureSerializer);
ExportExtensions(m_textureSerializer);
// Extension で Texture が増える場合があるので最後に呼ぶ
var exported = _textureExporter.Export();
for (var exportedTextureIdx = 0; exportedTextureIdx < exported.Count; ++exportedTextureIdx)
{
var (unityTexture, colorSpace) = exported[exportedTextureIdx];
GltfTextureExporter.PushGltfTexture(_data, unityTexture, colorSpace, textureSerializer);
GltfTextureExporter.PushGltfTexture(_data, unityTexture, colorSpace, m_textureSerializer);
}
FixName(_gltf);

View File

@ -125,10 +125,10 @@ namespace UniGLTF
ExportOnlyBlendShapePosition = false,
UseSparseAccessorForMorphTarget = false,
DivideVertexBuffer = false,
}))
}, textureSerializer: new EditorTextureSerializer()))
{
exporter.Prepare(root);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
}
var gltf = data.Gltf;
Assert.AreEqual(1, gltf.images.Count);

View File

@ -68,7 +68,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(root);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
}
return data.ToGlbBytes();
}

View File

@ -105,7 +105,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(go);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
// remove empty buffer
data.Gltf.buffers.Clear();
@ -357,7 +357,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(CreateSimpleScene());
exporter.Export(new EditorTextureSerializer());
exporter.Export();
}
var expected = data.Gltf.ToJson().ParseAsJson();
@ -592,7 +592,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(go);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
json = gltf.ToJson();
}
@ -670,7 +670,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(go);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
json = gltf.ToJson();
}
@ -733,7 +733,7 @@ namespace UniGLTF
using (var exporter = new gltfExporter(data, new GltfExportSettings()))
{
exporter.Prepare(root);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
json = gltf.ToJson();
}

View File

@ -226,10 +226,11 @@ namespace VRM
var data = new UniGLTF.ExportingGltfData();
var gltfExportSettings = settings.GltfExportSettings;
using (var exporter = new VRMExporter(data, gltfExportSettings,
settings.KeepAnimation ? new EditorAnimationExporter() : null))
animationExporter: settings.KeepAnimation ? new EditorAnimationExporter() : null,
textureSerializer: new EditorTextureSerializer()))
{
exporter.Prepare(target);
exporter.Export(new EditorTextureSerializer());
exporter.Export();
}
var bytes = data.ToGlbBytes();
Debug.LogFormat("Export elapsed {0}", sw.Elapsed);

View File

@ -6,7 +6,7 @@ namespace VRM
{
public static IMaterialDescriptorGenerator GetValidVrmMaterialDescriptorGenerator(glTF_VRM_extensions vrm)
{
return MaterialDescriptorGeneratorUtility.GetRenderPipelineType() switch
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => new UrpVrmMaterialDescriptorGenerator(vrm),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInVrmMaterialDescriptorGenerator(vrm),

View File

@ -0,0 +1,17 @@
using UniGLTF;
namespace VRM
{
public static class VrmMaterialExporterUtility
{
public static IMaterialExporter GetValidVrmMaterialExporter()
{
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => throw new System.NotImplementedException(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInVrmMaterialExporter(),
_ => new BuiltInVrmMaterialExporter(),
};
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c195ef8334f647febbfc517181117548
timeCreated: 1722258234

View File

@ -14,18 +14,29 @@ namespace VRM
public static ExportingGltfData Export(GltfExportSettings configuration, GameObject go, ITextureSerializer textureSerializer)
{
var data = new ExportingGltfData();
using (var exporter = new VRMExporter(data, configuration))
using (var exporter = new VRMExporter(data, configuration, textureSerializer: textureSerializer))
{
exporter.Prepare(go);
exporter.Export(textureSerializer);
exporter.Export();
}
return data;
}
public readonly VRM.glTF_VRM_extensions VRM = new glTF_VRM_extensions();
public VRMExporter(ExportingGltfData data, GltfExportSettings exportSettings, IAnimationExporter animationExporter = null) : base(
data, exportSettings, animationExporter: animationExporter)
public VRMExporter(
ExportingGltfData data,
GltfExportSettings exportSettings,
IAnimationExporter animationExporter = null,
IMaterialExporter materialExporter = null,
ITextureSerializer textureSerializer = null
) : base(
data,
exportSettings,
animationExporter: animationExporter,
materialExporter: materialExporter ?? VrmMaterialExporterUtility.GetValidVrmMaterialExporter(),
textureSerializer: textureSerializer
)
{
if (exportSettings == null)
{
@ -40,11 +51,6 @@ namespace VRM
_gltf.extensionsUsed.Add(glTF_VRM_extensions.ExtensionName);
}
protected override IMaterialExporter CreateMaterialExporter()
{
return new BuiltInVrmMaterialExporter();
}
public override void ExportExtensions(ITextureSerializer textureSerializer)
{
var getBone = UniHumanoid.Humanoid.Get_GetBoneTransform(Copy);

View File

@ -311,7 +311,10 @@ namespace UniVRM10
model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);
// export vrm-1.0
var exporter = new UniVRM10.Vrm10Exporter(new EditorTextureSerializer(), m_settings.MeshExportSettings);
var exporter = new Vrm10Exporter(
m_settings.MeshExportSettings,
textureSerializer: new EditorTextureSerializer()
);
var option = new VrmLib.ExportArgs
{
sparse = m_settings.MorphTargetUseSparse,

View File

@ -2,11 +2,11 @@ using UniGLTF;
namespace UniVRM10
{
public static class Vrm10MaterialDescriptorGeneratorDescriptorUtility
public static class Vrm10MaterialDescriptorGeneratorUtility
{
public static IMaterialDescriptorGenerator GetValidVrm10MaterialDescriptorGenerator()
{
return MaterialDescriptorGeneratorUtility.GetRenderPipelineType() switch
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => new UrpVrm10MaterialDescriptorGenerator(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInVrm10MaterialDescriptorGenerator(),

View File

@ -0,0 +1,17 @@
using UniGLTF;
namespace UniVRM10
{
public static class Vrm10MaterialExporterUtility
{
public static IMaterialExporter GetValidVrm10MaterialExporter()
{
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => throw new System.NotImplementedException(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInVrm10MaterialExporter(),
_ => new BuiltInVrm10MaterialExporter(),
};
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6f58deb399404ebd81647dcf52ad7929
timeCreated: 1722258791

View File

@ -21,19 +21,22 @@ namespace UniVRM10
public readonly string VrmExtensionName = "VRMC_vrm";
IMaterialExporter m_materialExporter;
ITextureSerializer m_textureSerializer;
TextureExporter m_textureExporter;
GltfExportSettings m_settings;
public Vrm10Exporter(ITextureSerializer textureSerializer, GltfExportSettings settings)
public Vrm10Exporter(
GltfExportSettings settings,
IMaterialExporter materialExporter = null,
ITextureSerializer textureSerializer = null
)
{
m_settings = settings;
if (textureSerializer == null)
{
throw new ArgumentException(nameof(textureSerializer));
}
m_settings = settings ?? throw new ArgumentException(nameof(settings));
m_materialExporter = materialExporter ?? Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter();
m_textureSerializer = textureSerializer ?? new RuntimeTextureSerializer();
m_textureExporter = new TextureExporter(m_textureSerializer);
Storage.Gltf.extensionsUsed.Add(glTF_KHR_texture_transform.ExtensionName);
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionName);
@ -41,9 +44,6 @@ namespace UniVRM10
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon.ExtensionName);
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_springBone.VRMC_springBone.ExtensionName);
Storage.Gltf.extensionsUsed.Add(UniGLTF.Extensions.VRMC_node_constraint.VRMC_node_constraint.ExtensionName);
m_textureSerializer = textureSerializer;
m_textureExporter = new TextureExporter(m_textureSerializer);
}
public void Dispose()
@ -169,9 +169,8 @@ namespace UniVRM10
return reserveBytes;
}
static IEnumerable<glTFMaterial> ExportMaterials(Model model, ITextureExporter textureExporter, GltfExportSettings settings)
static IEnumerable<glTFMaterial> ExportMaterials(Model model, IMaterialExporter materialExporter, ITextureExporter textureExporter, GltfExportSettings settings)
{
var materialExporter = new BuiltInVrm10MaterialExporter();
foreach (Material material in model.Materials)
{
yield return materialExporter.ExportMaterial(material, textureExporter, settings);
@ -184,7 +183,7 @@ namespace UniVRM10
Storage.Reserve(CalcReserveBytes(model));
foreach (var material in ExportMaterials(model, m_textureExporter, m_settings))
foreach (var material in ExportMaterials(model, m_materialExporter, m_textureExporter, m_settings))
{
Storage.Gltf.materials.Add(material);
}
@ -916,7 +915,11 @@ namespace UniVRM10
/// <param name="go"></param>
/// <param name="getTextureBytes"></param>
/// <returns></returns>
public static byte[] Export(GameObject go, ITextureSerializer textureSerializer = null, VRM10ObjectMeta vrmMeta = null)
public static byte[] Export(
GameObject go,
IMaterialExporter materialExporter = null,
ITextureSerializer textureSerializer = null,
VRM10ObjectMeta vrmMeta = null)
{
using (var arrayManager = new NativeArrayManager())
{
@ -928,7 +931,7 @@ namespace UniVRM10
model.ConvertCoordinate(VrmLib.Coordinates.Vrm1);
// Model と go から VRM-1.0 にExport
var exporter10 = new Vrm10Exporter(textureSerializer ?? new RuntimeTextureSerializer(), new GltfExportSettings());
var exporter10 = new Vrm10Exporter(new GltfExportSettings(), materialExporter, textureSerializer);
var option = new VrmLib.ExportArgs
{
};

View File

@ -41,7 +41,7 @@ namespace UniVRM10
m_useControlRig = useControlRig;
TextureDescriptorGenerator = new Vrm10TextureDescriptorGenerator(Data);
MaterialDescriptorGenerator = materialGenerator ?? Vrm10MaterialDescriptorGeneratorDescriptorUtility.GetValidVrm10MaterialDescriptorGenerator();
MaterialDescriptorGenerator = materialGenerator ?? Vrm10MaterialDescriptorGeneratorUtility.GetValidVrm10MaterialDescriptorGenerator();
m_externalMap = externalObjectMap;
if (m_externalMap == null)

View File

@ -80,7 +80,7 @@ namespace UniVRM10
public void Export(Action<VrmAnimationExporter> addFrames)
{
base.Export(new RuntimeTextureSerializer());
base.Export();
addFrames(this);

View File

@ -46,7 +46,7 @@ namespace UniVRM10.Test
Debug.Log(go);
// export
var vrmBytes = Vrm10Exporter.Export(go, new EditorTextureSerializer());
var vrmBytes = Vrm10Exporter.Export(go, textureSerializer: new EditorTextureSerializer());
Debug.Log($"export {vrmBytes.Length} bytes");
}

View File

@ -142,10 +142,7 @@ namespace UniVRM10.RuntimeExporterSample
model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false);
// export vrm-1.0
var exporter = new UniVRM10.Vrm10Exporter(new RuntimeTextureSerializer(), new GltfExportSettings
{
});
var exporter = new Vrm10Exporter(new GltfExportSettings());
exporter.Export(root, model, converter, new VrmLib.ExportArgs
{
}, meta);