diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/GltfMaterialExportUtils.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/GltfMaterialExportUtils.cs index 22da6e790..74b068a1d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/GltfMaterialExportUtils.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Export/GltfMaterialExportUtils.cs @@ -7,10 +7,19 @@ namespace UniGLTF { public static void ExportTextureTransform(Material src, glTFTextureInfo dstTextureInfo, string targetTextureName) { - if (dstTextureInfo != null && src.HasProperty(targetTextureName)) + if (src.HasProperty(targetTextureName)) { var offset = src.GetTextureOffset(targetTextureName); var scale = src.GetTextureScale(targetTextureName); + + ExportTextureTransform(offset, scale, dstTextureInfo); + } + } + + public static void ExportTextureTransform(Vector2 offset, Vector2 scale, glTFTextureInfo dstTextureInfo) + { + if (dstTextureInfo != null) + { (scale, offset) = TextureTransform.VerticalFlipScaleOffset(scale, offset); glTF_KHR_texture_transform.Serialize(dstTextureInfo, (offset.x, offset.y), (scale.x, scale.y)); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export.meta new file mode 100644 index 000000000..758ce0a7e --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/Export.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4c2edd284a744a13bf888a4b29fac93a +timeCreated: 1722269836 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil.meta new file mode 100644 index 000000000..349fc6a09 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4a35a0639a7f4eb4b2c5fb8225803ce3 +timeCreated: 1722270899 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit.meta new file mode 100644 index 000000000..fb73c7173 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2100c40775e3446bb05f63f32820930a +timeCreated: 1722270911 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs new file mode 100644 index 000000000..2d07220d9 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs @@ -0,0 +1,211 @@ +using UnityEngine; +using UnityEngine.Rendering; + +namespace UniGLTF +{ + public sealed class UrpLitContext + { + private readonly Material _mat; + + private static readonly int WorkflowMode = Shader.PropertyToID("_WorkflowMode"); + private static readonly int Surface = Shader.PropertyToID("_Surface"); + private static readonly int AlphaClip = Shader.PropertyToID("_AlphaClip"); + private static readonly int Cull = Shader.PropertyToID("_Cull"); + private static readonly int BaseColorProp = Shader.PropertyToID("_BaseColor"); + private static readonly int BaseMap = Shader.PropertyToID("_BaseMap"); + private static readonly int EmissionColor = Shader.PropertyToID("_EmissionColor"); + private static readonly int EmissionMap = Shader.PropertyToID("_EmissionMap"); + private static readonly int OcclusionMap = Shader.PropertyToID("_OcclusionMap"); + private static readonly int ParallaxMap = Shader.PropertyToID("_ParallaxMap"); + private static readonly int CutoffProp = Shader.PropertyToID("_Cutoff"); + private static readonly int SmoothnessProp = Shader.PropertyToID("_Smoothness"); + private static readonly int SmoothnessTextureChannel = Shader.PropertyToID("_SmoothnessTextureChannel"); + private static readonly int MetallicProp = Shader.PropertyToID("_Metallic"); + private static readonly int MetallicGlossMapProp = Shader.PropertyToID("_MetallicGlossMap"); + private static readonly int SpecColorProp = Shader.PropertyToID("_SpecColor"); + private static readonly int SpecGlossMapProp = Shader.PropertyToID("_SpecGlossMap"); + private static readonly int BumpScaleProp = Shader.PropertyToID("_BumpScale"); + private static readonly int BumpMapProp = Shader.PropertyToID("_BumpMap"); + private static readonly int EmissionEnabled = Shader.PropertyToID("_EmissionEnabled"); + + private static readonly string SpecularSetupKeyword = "_SPECULAR_SETUP"; + private static readonly string MetallicSpecGlossMapKeyword = "_METALLICSPECGLOSSMAP"; + private static readonly string SurfaceTypeTransparentKeyword = "_SURFACE_TYPE_TRANSPARENT"; + private static readonly string SmoothnessTextureAlbedoChannelAKeyword = "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A"; + private static readonly string NormalMapKeyword = "_NORMALMAP"; + private static readonly string EmissionKeyword = "_EMISSION"; + private static readonly string OcclusionMapKeyword = "_OCCLUSIONMAP"; + private static readonly string ParallaxMapKeyword = "_PARALLAXMAP"; + + public UrpLitContext(Material material) + { + _mat = material; + } + + public UrpLitWorkflowType WorkflowType + { + get => (UrpLitWorkflowType)_mat.GetFloat(WorkflowMode); + set + { + _mat.SetFloat(WorkflowMode, (float)value); + var isSpecularSetup = value == UrpLitWorkflowType.Specular; + _mat.SetKeyword(SpecularSetupKeyword, isSpecularSetup); + + var glossMapName = isSpecularSetup ? SpecGlossMapProp : MetallicGlossMapProp; + var hasGlossMap = _mat.GetTexture(glossMapName) != null; + _mat.SetKeyword(MetallicSpecGlossMapKeyword, hasGlossMap); + } + } + + public UrpLitSurfaceType SurfaceType + { + get => (UrpLitSurfaceType)_mat.GetFloat(Surface); + set + { + _mat.SetFloat(Surface, (float)value); + _mat.SetKeyword(SurfaceTypeTransparentKeyword, value != UrpLitSurfaceType.Opaque); + } + } + + public bool AlphaClipping + { + get => _mat.GetFloat(AlphaClip) >= 0.5f; + set => _mat.SetFloat(AlphaClip, value ? 1.0f : 0.0f); + } + + public CullMode CullMode + { + get => (CullMode)_mat.GetFloat(Cull); + set => _mat.SetFloat(Cull, (float)value); + } + + public Color BaseColorSrgb + { + get => _mat.GetColor(BaseColorProp); + set => _mat.SetColor(BaseColorProp, value); + } + + public Texture BaseTexture + { + get => _mat.GetTexture(BaseMap); + } + + public Vector2 BaseTextureOffset + { + get => _mat.GetTextureOffset(BaseMap); + } + + public Vector2 BaseTextureScale + { + get => _mat.GetTextureScale(BaseMap); + } + + public float Cutoff + { + get => _mat.GetFloat(CutoffProp); + set => _mat.SetFloat(CutoffProp, value); + } + + public float Smoothness + { + get => _mat.GetFloat(SmoothnessProp); + set => _mat.SetFloat(SmoothnessProp, value); + } + + public UrpLitSmoothnessMapChannel SmoothnessMapChannel + { + // NOTE: Float Prop 以外に条件があるので、Keyword から読み取った方が確実 + get => _mat.IsKeywordEnabled(SmoothnessTextureAlbedoChannelAKeyword) ? UrpLitSmoothnessMapChannel.AlbedoAlpha : UrpLitSmoothnessMapChannel.SpecularMetallicAlpha; + set + { + _mat.SetFloat(SmoothnessTextureChannel, (float)value); + var isOpaque = SurfaceType == UrpLitSurfaceType.Opaque; + _mat.SetKeyword(SmoothnessTextureAlbedoChannelAKeyword, isOpaque && value == UrpLitSmoothnessMapChannel.AlbedoAlpha); + } + } + + public float Metallic + { + // NOTE: Metallic ワークフロー専用 + get => _mat.GetFloat(MetallicProp); + set => _mat.SetFloat(MetallicProp, value); + } + + public Texture MetallicGlossMap + { + // NOTE: Metallic ワークフロー専用 + get => _mat.GetTexture(MetallicGlossMapProp); + set => _mat.SetTexture(MetallicGlossMapProp, value); + } + + public Color SpecColorSrgb + { + // NOTE: Specular ワークフロー専用 + get => _mat.GetColor(SpecColorProp); + set => _mat.SetColor(SpecColorProp, value); + } + + public Texture SpecGlossMap + { + // NOTE: Specular ワークフロー専用 + get => _mat.GetTexture(SpecGlossMapProp); + set => _mat.SetTexture(SpecGlossMapProp, value); + } + + public float BumpScale + { + get => _mat.GetFloat(BumpScaleProp); + set => _mat.SetFloat(BumpScaleProp, value); + } + + public Texture BumpMap + { + get => _mat.GetTexture(BumpMapProp); + set + { + _mat.SetTexture(BumpMapProp, value); + _mat.SetKeyword(NormalMapKeyword, value != null); + } + } + + public bool IsEmissionEnabled + { + get => _mat.IsKeywordEnabled(EmissionKeyword); + set + { + _mat.SetFloat(EmissionEnabled, value ? 1.0f : 0.0f); + _mat.SetKeyword(EmissionKeyword, value); + } + } + + public Color EmissionLinear + { + get => _mat.GetColor(EmissionColor); + } + + public Texture EmissionTexture + { + get => _mat.GetTexture(EmissionMap); + } + + public Texture OcclusionTexture + { + get => _mat.GetTexture(OcclusionMap); + set + { + _mat.SetTexture(OcclusionMap, value); + _mat.SetKeyword(OcclusionMapKeyword, value != null); + } + } + + public Texture ParallaxTexture + { + get => _mat.GetTexture(ParallaxMap); + set + { + _mat.SetTexture(ParallaxMap, value); + _mat.SetKeyword(ParallaxMapKeyword, value != null); + } + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs.meta new file mode 100644 index 000000000..ace7df81f --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitContext.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c15ac3fb36ff4e7d8ec271e2cf1fb263 +timeCreated: 1722270619 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs new file mode 100644 index 000000000..593f9f728 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs @@ -0,0 +1,8 @@ +namespace UniGLTF +{ + public enum UrpLitSmoothnessMapChannel + { + SpecularMetallicAlpha = 0, + AlbedoAlpha = 1, + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs.meta new file mode 100644 index 000000000..159bf2df7 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSmoothnessMapChannel.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5669057b7c2d45de9167a335f222c61d +timeCreated: 1722344104 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs new file mode 100644 index 000000000..ee85f82c4 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs @@ -0,0 +1,8 @@ +namespace UniGLTF +{ + public enum UrpLitSurfaceType + { + Opaque = 0, + Transparent = 1, + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs.meta new file mode 100644 index 000000000..e366b94aa --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitSurfaceType.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 404bf44062364d41a91735181e6ead33 +timeCreated: 1722270974 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs new file mode 100644 index 000000000..379d787b0 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs @@ -0,0 +1,8 @@ +namespace UniGLTF +{ + public enum UrpLitWorkflowType + { + Specular = 0, + Metallic = 1, + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs.meta new file mode 100644 index 000000000..065dd00c4 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/ShaderUtil/Lit/UrpLitWorkflowType.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a76aba2bef494654948abea5eef21fac +timeCreated: 1722271263 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils.meta new file mode 100644 index 000000000..64945a10f --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f0ed4e9ca99a4447896accb69a065c2e +timeCreated: 1722343812 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs new file mode 100644 index 000000000..b6d3136b7 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs @@ -0,0 +1,19 @@ +using UnityEngine; + +namespace UniGLTF +{ + internal static class InternalMaterialUtils + { + public static void SetKeyword(this Material material, string keyword, bool enabled) + { + if (enabled) + { + material.EnableKeyword(keyword); + } + else + { + material.DisableKeyword(keyword); + } + } + } +} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs.meta new file mode 100644 index 000000000..b004c841d --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Utils/InternalMaterialUtils.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cc1b44f784074898b268882d75b86e23 +timeCreated: 1722343798 \ No newline at end of file