diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs index 449e9f735..3ddfca93a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/MaterialExporterUtility.cs @@ -6,7 +6,7 @@ { return RenderPipelineUtility.GetRenderPipelineType() switch { - RenderPipelineTypes.UniversalRenderPipeline => throw new System.NotImplementedException(), + RenderPipelineTypes.UniversalRenderPipeline => new UrpGltfMaterialExporter(), RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInGltfMaterialExporter(), _ => new BuiltInGltfMaterialExporter(), }; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials.meta new file mode 100644 index 000000000..4fe19bda2 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 70546771aa234567b5d7c4430548ac0f +timeCreated: 1722269920 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs new file mode 100644 index 000000000..54f34e7f3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs @@ -0,0 +1,99 @@ +using System; +using UnityEngine; + +namespace UniGLTF +{ + public class UrpLitMaterialExporter + { + public string TargetShaderName { get; } + + /// + /// "Universal Render Pipeline/Lit" シェーダのマテリアルをエクスポートする。 + /// + /// targetShaderName に、プロパティに互換性がある他のシェーダを指定することもできる。 + /// + public UrpLitMaterialExporter(string targetShaderName = null) + { + TargetShaderName = string.IsNullOrEmpty(targetShaderName) + ? "Universal Render Pipeline/Lit" + : targetShaderName; + } + + public bool TryExportMaterial(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + { + try + { + if (src == null) throw new ArgumentNullException(nameof(src)); + if (src.shader.name != TargetShaderName) throw new ArgumentException(nameof(src)); + if (textureExporter == null) throw new ArgumentNullException(nameof(textureExporter)); + + dst = new glTFMaterial + { + name = src.name, + pbrMetallicRoughness = new glTFPbrMetallicRoughness(), + }; + + var context = new UrpLitContext(src); + if (!Validate(context, out var errorMessage)) + { + Debug.LogError(errorMessage, src); + throw new UniGLTFNotSupportedException(errorMessage); + } + + ExportBaseColor(context, dst, textureExporter); + + return true; + } + catch (Exception) + { + dst = default; + return false; + } + } + + public static void ExportBaseColor(UrpLitContext context, glTFMaterial dst, ITextureExporter textureExporter) + { + dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); + if (context.BaseTexture != null) + { + var needsAlpha = context.SurfaceType != UrpLitSurfaceType.Opaque; + var index = textureExporter.RegisterExportingAsSRgb(context.BaseTexture, needsAlpha); + if (index >= 0) + { + dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo + { + index = index, + texCoord = 0, + }; + GltfMaterialExportUtils.ExportTextureTransform( + context.BaseTextureOffset, + context.BaseTextureScale, + dst.pbrMetallicRoughness.baseColorTexture + ); + } + } + } + + public static bool Validate(UrpLitContext context, out string errorMessage) + { + if (context.WorkflowType != UrpLitWorkflowType.Metallic) + { + errorMessage = "Specular workflow is not supported."; + return false; + } + + if (context.OcclusionTexture != null) + { + Debug.LogWarning("Occlusion texture is not supported."); + } + + if (context.ParallaxTexture != null) + { + Debug.LogWarning("Parallax texture is not supported."); + } + + errorMessage = null; + return true; + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs.meta new file mode 100644 index 000000000..37020197d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpLitMaterialExporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8832f807c30a420d95aeaee15a1fbff7 +timeCreated: 1722269933 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs new file mode 100644 index 000000000..01391fe22 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace UniGLTF +{ + public class UrpGltfMaterialExporter : IMaterialExporter + { + private readonly UrpLitMaterialExporter _litExporter = new(); + + public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings) + { + glTFMaterial dst; + + if (_litExporter.TryExportMaterial(m, textureExporter, out dst)) return dst; + + return dst; + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs.meta new file mode 100644 index 000000000..64d75fa33 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/UrpGltfMaterialExporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 943c27f3f6054f25b4c0bf7e4cd50eff +timeCreated: 1722269861 \ No newline at end of file