From f5d40b4d850dd091212cffdea855dd87679cc562 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 29 Jul 2024 21:59:14 +0900 Subject: [PATCH 1/3] Refactor about detecting the RenderPipeline --- .../MaterialDescriptorGeneratorUtility.cs | 16 ++++++++++++++++ .../MaterialDescriptorGeneratorUtility.cs.meta} | 0 ...iptorUtility.cs => RenderPipelineUtility.cs} | 17 +++-------------- .../IO/MaterialIO/RenderPipelineUtility.cs.meta | 3 +++ ...=> VrmMaterialDescriptorGeneratorUtility.cs} | 2 +- ...mMaterialDescriptorGeneratorUtility.cs.meta} | 0 ...rialDescriptorGeneratorDescriptorUtility.cs} | 2 +- ...escriptorGeneratorDescriptorUtility.cs.meta} | 0 8 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{RenderPipelineMaterialDescriptorUtility.cs.meta => Import/MaterialDescriptorGeneratorUtility.cs.meta} (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{RenderPipelineMaterialDescriptorUtility.cs => RenderPipelineUtility.cs} (57%) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs.meta rename Assets/VRM/Runtime/IO/MaterialIO/{VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs => VrmMaterialDescriptorGeneratorUtility.cs} (86%) rename Assets/VRM/Runtime/IO/MaterialIO/{VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs.meta => VrmMaterialDescriptorGeneratorUtility.cs.meta} (100%) rename Assets/VRM10/Runtime/IO/Material/URP/Import/{VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs => Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs} (86%) rename Assets/VRM10/Runtime/IO/Material/URP/Import/{VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs.meta => Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta} (100%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs new file mode 100644 index 000000000..dee8cba3d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs @@ -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(), + }; + } + } +} + diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineMaterialDescriptorUtility.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineMaterialDescriptorUtility.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialDescriptorGeneratorUtility.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineMaterialDescriptorUtility.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs similarity index 57% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineMaterialDescriptorUtility.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs index 927e6f6e6..76ecc0547 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineMaterialDescriptorUtility.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs @@ -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(), - }; - } } -} - +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs.meta new file mode 100644 index 000000000..2f54eddfe --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/RenderPipelineUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 0fd29db9c49145cd9f56f92e6153c8db +timeCreated: 1722257774 \ No newline at end of file diff --git a/Assets/VRM/Runtime/IO/MaterialIO/VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialDescriptorGeneratorUtility.cs similarity index 86% rename from Assets/VRM/Runtime/IO/MaterialIO/VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs rename to Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialDescriptorGeneratorUtility.cs index 282e4d8c3..f13e9e92a 100644 --- a/Assets/VRM/Runtime/IO/MaterialIO/VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs +++ b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialDescriptorGeneratorUtility.cs @@ -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), diff --git a/Assets/VRM/Runtime/IO/MaterialIO/VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs.meta b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialDescriptorGeneratorUtility.cs.meta similarity index 100% rename from Assets/VRM/Runtime/IO/MaterialIO/VRMRenderPipelineMaterialDescriptorGeneratorUtility.cs.meta rename to Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialDescriptorGeneratorUtility.cs.meta diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs b/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs similarity index 86% rename from Assets/VRM10/Runtime/IO/Material/URP/Import/VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs rename to Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs index 9bd072165..bb3000f29 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Import/VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs +++ b/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs @@ -6,7 +6,7 @@ namespace UniVRM10 { public static IMaterialDescriptorGenerator GetValidVrm10MaterialDescriptorGenerator() { - return MaterialDescriptorGeneratorUtility.GetRenderPipelineType() switch + return RenderPipelineUtility.GetRenderPipelineType() switch { RenderPipelineTypes.UniversalRenderPipeline => new UrpVrm10MaterialDescriptorGenerator(), RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInVrm10MaterialDescriptorGenerator(), diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs.meta b/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta similarity index 100% rename from Assets/VRM10/Runtime/IO/Material/URP/Import/VRM10RenderPipelineMaterialDescriptorGeneratorUtility.cs.meta rename to Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta From 23f387a7c8310683c9dad9596498f489bcf8e383 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 29 Jul 2024 22:28:35 +0900 Subject: [PATCH 2/3] add RenderPipeline considering to gltf/vrm/vrm10 exporters --- .../UniGLTF/ExportDialog/GltfExportWindow.cs | 7 ++-- .../Export/MaterialExporterUtility.cs | 15 ++++++++ .../Export/MaterialExporterUtility.cs.meta | 3 ++ .../Runtime/UniGLTF/IO/gltfExporter.cs | 32 +++++++++------- .../UniGLTF/EditorTextureSerializerTests.cs | 4 +- Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 10 ++--- Assets/VRM/Editor/Format/VRMEditorExporter.cs | 5 ++- .../MaterialIO/VrmMaterialExporterUtility.cs | 17 +++++++++ .../VrmMaterialExporterUtility.cs.meta | 3 ++ Assets/VRM/Runtime/IO/VRMExporter.cs | 24 +++++++----- Assets/VRM10/Editor/Vrm10ExportDialog.cs | 6 ++- ...rm10MaterialDescriptorGeneratorUtility.cs} | 2 +- ...aterialDescriptorGeneratorUtility.cs.meta} | 0 .../Material/Vrm10MaterialExporterUtility.cs | 17 +++++++++ .../Vrm10MaterialExporterUtility.cs.meta | 3 ++ Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 38 +++++++++++-------- Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 2 +- .../VRM10/Runtime/IO/VrmAnimationExporter.cs | 2 +- Assets/VRM10/Tests/ApiSampleTests.cs | 2 +- .../VRM10RuntimeExporter.cs | 9 +++-- 21 files changed, 143 insertions(+), 60 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs.meta create mode 100644 Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs create mode 100644 Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs.meta rename Assets/VRM10/Runtime/IO/Material/{URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs => Vrm10MaterialDescriptorGeneratorUtility.cs} (87%) rename Assets/VRM10/Runtime/IO/Material/{URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta => Vrm10MaterialDescriptorGeneratorUtility.cs.meta} (100%) create mode 100644 Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs create mode 100644 Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs index b05c56076..75a9686b4 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs @@ -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) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs new file mode 100644 index 000000000..449e9f735 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs @@ -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(), + }; + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs.meta new file mode 100644 index 000000000..9a2cbdb44 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ce72b16c8b374add909ab887c14a0f44 +timeCreated: 1722257666 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs index 15d8cd9af..57d423dd1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/gltfExporter.cs @@ -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 progress = null, - IAnimationExporter animationExporter = null) + public gltfExporter( + ExportingGltfData data, + GltfExportSettings settings, + IProgress 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); diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs index 3766f9d4f..dbc1349e9 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs @@ -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); diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs index 7231079b6..58e0f7e76 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs @@ -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(); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index 5b918f109..99ad080f6 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -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(); } diff --git a/Assets/VRM/Editor/Format/VRMEditorExporter.cs b/Assets/VRM/Editor/Format/VRMEditorExporter.cs index 427da420d..c4489d7a4 100644 --- a/Assets/VRM/Editor/Format/VRMEditorExporter.cs +++ b/Assets/VRM/Editor/Format/VRMEditorExporter.cs @@ -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); diff --git a/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs new file mode 100644 index 000000000..9c220c65c --- /dev/null +++ b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs @@ -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(), + }; + } + } +} \ No newline at end of file diff --git a/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs.meta b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs.meta new file mode 100644 index 000000000..cb4b69cfe --- /dev/null +++ b/Assets/VRM/Runtime/IO/MaterialIO/VrmMaterialExporterUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c195ef8334f647febbfc517181117548 +timeCreated: 1722258234 \ No newline at end of file diff --git a/Assets/VRM/Runtime/IO/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs index f1f5d02b2..64e77cb80 100644 --- a/Assets/VRM/Runtime/IO/VRMExporter.cs +++ b/Assets/VRM/Runtime/IO/VRMExporter.cs @@ -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); diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs index 64b8a6e7a..8c463b5ec 100644 --- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs +++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs @@ -311,7 +311,11 @@ 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, + Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), + new EditorTextureSerializer() + ); var option = new VrmLib.ExportArgs { sparse = m_settings.MorphTargetUseSparse, diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialDescriptorGeneratorUtility.cs similarity index 87% rename from Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs rename to Assets/VRM10/Runtime/IO/Material/Vrm10MaterialDescriptorGeneratorUtility.cs index bb3000f29..d45c4741d 100644 --- a/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialDescriptorGeneratorUtility.cs @@ -2,7 +2,7 @@ using UniGLTF; namespace UniVRM10 { - public static class Vrm10MaterialDescriptorGeneratorDescriptorUtility + public static class Vrm10MaterialDescriptorGeneratorUtility { public static IMaterialDescriptorGenerator GetValidVrm10MaterialDescriptorGenerator() { diff --git a/Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialDescriptorGeneratorUtility.cs.meta similarity index 100% rename from Assets/VRM10/Runtime/IO/Material/URP/Import/Vrm10MaterialDescriptorGeneratorDescriptorUtility.cs.meta rename to Assets/VRM10/Runtime/IO/Material/Vrm10MaterialDescriptorGeneratorUtility.cs.meta diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs new file mode 100644 index 000000000..5f8e61397 --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs @@ -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(), + }; + } + } +} \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs.meta b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs.meta new file mode 100644 index 000000000..753c36dfc --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MaterialExporterUtility.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6f58deb399404ebd81647dcf52ad7929 +timeCreated: 1722258791 \ No newline at end of file diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 79bdfc6e9..903ba4901 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -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, + ITextureSerializer textureSerializer + ) { - m_settings = settings; - - if (textureSerializer == null) - { - throw new ArgumentException(nameof(textureSerializer)); - } + m_settings = settings ?? throw new ArgumentException(nameof(settings)); + m_materialExporter = materialExporter ?? throw new ArgumentException(nameof(materialExporter)); + m_textureSerializer = textureSerializer ?? throw new ArgumentException(nameof(textureSerializer)); + 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 ExportMaterials(Model model, ITextureExporter textureExporter, GltfExportSettings settings) + static IEnumerable 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 /// /// /// - 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,12 @@ 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 ?? Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), + textureSerializer ?? new RuntimeTextureSerializer() + ); + var option = new VrmLib.ExportArgs { }; diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index 96918786d..a9447fea7 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -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) diff --git a/Assets/VRM10/Runtime/IO/VrmAnimationExporter.cs b/Assets/VRM10/Runtime/IO/VrmAnimationExporter.cs index 4cda49aa5..d61369b64 100644 --- a/Assets/VRM10/Runtime/IO/VrmAnimationExporter.cs +++ b/Assets/VRM10/Runtime/IO/VrmAnimationExporter.cs @@ -80,7 +80,7 @@ namespace UniVRM10 public void Export(Action addFrames) { - base.Export(new RuntimeTextureSerializer()); + base.Export(); addFrames(this); diff --git a/Assets/VRM10/Tests/ApiSampleTests.cs b/Assets/VRM10/Tests/ApiSampleTests.cs index 326a2eee3..cc50aec15 100644 --- a/Assets/VRM10/Tests/ApiSampleTests.cs +++ b/Assets/VRM10/Tests/ApiSampleTests.cs @@ -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"); } diff --git a/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs b/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs index 76c5ebc2c..5accf8ca8 100644 --- a/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs +++ b/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs @@ -142,10 +142,11 @@ 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(), + Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), + new RuntimeTextureSerializer() + ); exporter.Export(root, model, converter, new VrmLib.ExportArgs { }, meta); From 78a99fb9e966774439299126a50e92f6f33eb404 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 29 Jul 2024 22:33:51 +0900 Subject: [PATCH 3/3] use default arguments --- Assets/VRM10/Editor/Vrm10ExportDialog.cs | 3 +-- Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 15 +++++---------- .../VRM10RuntimeExporter.cs | 6 +----- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs index 8c463b5ec..a2c9195aa 100644 --- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs +++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs @@ -313,8 +313,7 @@ namespace UniVRM10 // export vrm-1.0 var exporter = new Vrm10Exporter( m_settings.MeshExportSettings, - Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), - new EditorTextureSerializer() + textureSerializer: new EditorTextureSerializer() ); var option = new VrmLib.ExportArgs { diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index 903ba4901..864c1e6ff 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -29,13 +29,13 @@ namespace UniVRM10 public Vrm10Exporter( GltfExportSettings settings, - IMaterialExporter materialExporter, - ITextureSerializer textureSerializer + IMaterialExporter materialExporter = null, + ITextureSerializer textureSerializer = null ) { m_settings = settings ?? throw new ArgumentException(nameof(settings)); - m_materialExporter = materialExporter ?? throw new ArgumentException(nameof(materialExporter)); - m_textureSerializer = textureSerializer ?? throw new ArgumentException(nameof(textureSerializer)); + 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); @@ -931,12 +931,7 @@ namespace UniVRM10 model.ConvertCoordinate(VrmLib.Coordinates.Vrm1); // Model と go から VRM-1.0 にExport - var exporter10 = new Vrm10Exporter( - new GltfExportSettings(), - materialExporter ?? Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), - textureSerializer ?? new RuntimeTextureSerializer() - ); - + var exporter10 = new Vrm10Exporter(new GltfExportSettings(), materialExporter, textureSerializer); var option = new VrmLib.ExportArgs { }; diff --git a/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs b/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs index 5accf8ca8..f76da8c6b 100644 --- a/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs +++ b/Assets/VRM10_Samples/VRM10RuntimeExporterSample/VRM10RuntimeExporter.cs @@ -142,11 +142,7 @@ namespace UniVRM10.RuntimeExporterSample model.ConvertCoordinate(VrmLib.Coordinates.Vrm1, ignoreVrm: false); // export vrm-1.0 - var exporter = new Vrm10Exporter( - new GltfExportSettings(), - Vrm10MaterialExporterUtility.GetValidVrm10MaterialExporter(), - new RuntimeTextureSerializer() - ); + var exporter = new Vrm10Exporter(new GltfExportSettings()); exporter.Export(root, model, converter, new VrmLib.ExportArgs { }, meta);