Add URP/Lit material handler class

This commit is contained in:
Masataka SUMI 2024-07-30 22:41:58 +09:00
parent cdfe7c9622
commit 072dcba638
15 changed files with 291 additions and 1 deletions

View File

@ -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));

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4c2edd284a744a13bf888a4b29fac93a
timeCreated: 1722269836

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4a35a0639a7f4eb4b2c5fb8225803ce3
timeCreated: 1722270899

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2100c40775e3446bb05f63f32820930a
timeCreated: 1722270911

View File

@ -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);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c15ac3fb36ff4e7d8ec271e2cf1fb263
timeCreated: 1722270619

View File

@ -0,0 +1,8 @@
namespace UniGLTF
{
public enum UrpLitSmoothnessMapChannel
{
SpecularMetallicAlpha = 0,
AlbedoAlpha = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5669057b7c2d45de9167a335f222c61d
timeCreated: 1722344104

View File

@ -0,0 +1,8 @@
namespace UniGLTF
{
public enum UrpLitSurfaceType
{
Opaque = 0,
Transparent = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 404bf44062364d41a91735181e6ead33
timeCreated: 1722270974

View File

@ -0,0 +1,8 @@
namespace UniGLTF
{
public enum UrpLitWorkflowType
{
Specular = 0,
Metallic = 1,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a76aba2bef494654948abea5eef21fac
timeCreated: 1722271263

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f0ed4e9ca99a4447896accb69a065c2e
timeCreated: 1722343812

View File

@ -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);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: cc1b44f784074898b268882d75b86e23
timeCreated: 1722343798