diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs new file mode 100644 index 000000000..5a34299dd --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs @@ -0,0 +1,74 @@ +using System; +using UniGLTF.UniUnlit; +using UnityEngine; + +namespace UniGLTF +{ + public class UrpUniUnlitMaterialExporter + { + public Shader Shader { get; set; } + + /// + /// "UniGLTF/UniUnlit" シェーダのマテリアルをエクスポートする。 + /// + /// プロパティに互換性がある他のシェーダを指定することもできる。 + /// + public UrpUniUnlitMaterialExporter(Shader shader = null) + { + Shader = shader != null ? shader : Shader.Find("UniGLTF/UniUnlit"); + } + + public bool TryExportMaterial(Material src, ITextureExporter textureExporter, out glTFMaterial dst) + { + try + { + if (src == null) throw new ArgumentNullException(nameof(src)); + if (src.shader != Shader) throw new ArgumentException(nameof(src)); + if (textureExporter == null) throw new ArgumentNullException(nameof(textureExporter)); + + dst = glTF_KHR_materials_unlit.CreateDefault(); + dst.name = src.name; + + var context = new UniUnlitContext(src); + + dst.alphaMode = context.RenderMode switch + { + UniUnlitRenderMode.Opaque => glTFBlendMode.OPAQUE.ToString(), + UniUnlitRenderMode.Cutout => glTFBlendMode.MASK.ToString(), + UniUnlitRenderMode.Transparent => glTFBlendMode.BLEND.ToString(), + _ => throw new ArgumentOutOfRangeException(), + }; + dst.alphaCutoff = context.Cutoff; + dst.doubleSided = context.CullMode != UniUnlitCullMode.Back; + + dst.pbrMetallicRoughness.baseColorFactor = context.MainColorSrgb + .ToFloat4(ColorSpace.sRGB, ColorSpace.Linear); + if (context.MainTexture != null) + { + var needsAlpha = context.RenderMode != UniUnlitRenderMode.Opaque; + var index = textureExporter.RegisterExportingAsSRgb(context.MainTexture, needsAlpha); + if (index >= 0) + { + dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo + { + index = index, + texCoord = 0, + }; + GltfMaterialExportUtils.ExportTextureTransform( + context.MainTextureOffset, + context.MainTextureScale, + dst.pbrMetallicRoughness.baseColorTexture); + } + } + + return true; + } + catch (Exception e) + { + Debug.LogException(e); + dst = default; + return false; + } + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs.meta new file mode 100644 index 000000000..1c986fd4f --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export/Materials/UrpUniUnlitMaterialExporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5b17f3928c684fc1a9beda10797b0e76 +timeCreated: 1722613258 \ No newline at end of file diff --git a/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs b/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs new file mode 100644 index 000000000..5f3573aba --- /dev/null +++ b/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs @@ -0,0 +1,80 @@ +using UnityEngine; + +namespace UniGLTF.UniUnlit +{ + public class UniUnlitContext + { + public Material Material { get; } + public bool UnsafeEditMode { get; set; } = false; + + public UniUnlitContext(Material material) + { + Material = material; + } + + public UniUnlitRenderMode RenderMode + { + get => UniUnlitUtil.GetRenderMode(Material); + set + { + UniUnlitUtil.SetRenderMode(Material, value); + if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material, isRenderModeChangedByUser: true); + } + } + + public UniUnlitCullMode CullMode + { + get => UniUnlitUtil.GetCullMode(Material); + set + { + UniUnlitUtil.SetCullMode(Material, value); + if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material); + } + } + + public UniUnlitVertexColorBlendOp VColBlendMode + { + get => UniUnlitUtil.GetVColBlendMode(Material); + set + { + UniUnlitUtil.SetVColBlendMode(Material, value); + if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material); + } + } + + public Color MainColorSrgb + { + get => Material.GetColor(UniUnlitUtil.PropNameColor); + set => Material.SetColor(UniUnlitUtil.PropNameColor, value); + } + + public Texture MainTexture + { + get => Material.GetTexture(UniUnlitUtil.PropNameMainTex); + set => Material.SetTexture(UniUnlitUtil.PropNameMainTex, value); + } + + public Vector2 MainTextureOffset + { + get => Material.GetTextureOffset(UniUnlitUtil.PropNameMainTex); + set => Material.SetTextureOffset(UniUnlitUtil.PropNameMainTex, value); + } + + public Vector2 MainTextureScale + { + get => Material.GetTextureScale(UniUnlitUtil.PropNameMainTex); + set => Material.SetTextureScale(UniUnlitUtil.PropNameMainTex, value); + } + + public float Cutoff + { + get => Material.GetFloat(UniUnlitUtil.PropNameCutoff); + set => Material.SetFloat(UniUnlitUtil.PropNameCutoff, value); + } + + public void Validate() + { + UniUnlitUtil.ValidateProperties(Material, isRenderModeChangedByUser: true); + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs.meta b/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs.meta new file mode 100644 index 000000000..77c89e884 --- /dev/null +++ b/Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 57963fbf465047bf83ed829f8a0022d3 +timeCreated: 1722613449 \ No newline at end of file