Skip to content

Commit

Permalink
Merge branch 'master' into Santarh-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue authored Aug 5, 2024
2 parents 4bcb7e7 + f7215a7 commit 6adf168
Show file tree
Hide file tree
Showing 42 changed files with 713 additions and 742 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ namespace UniGLTF
public class MaterialFactory : IResponsibilityForDestroyObjects
{
private readonly IReadOnlyDictionary<SubAssetKey, Material> m_externalMap;

/// <summary>
/// デフォルトマテリアルの MaterialDescriptor は IMaterialDescriptorGenerator の実装によって異なるので外から渡す
/// </summary>
private readonly SubAssetKey m_defaultMaterialKey = new SubAssetKey(typeof(Material), "__UNIGLTF__DEFAULT__MATERIAL__");
private readonly MaterialDescriptor m_defaultMaterialParams;
private readonly List<MaterialLoadInfo> m_materials = new List<MaterialLoadInfo>();

/// <summary>
/// gltfPritmitive.material が無い場合のデフォルトマテリアル
Expand All @@ -23,6 +21,9 @@ public class MaterialFactory : IResponsibilityForDestroyObjects
/// </summary>
private Material m_defaultMaterial;

public IReadOnlyList<MaterialLoadInfo> Materials => m_materials;


public MaterialFactory(IReadOnlyDictionary<SubAssetKey, Material> externalMaterialMap, MaterialDescriptor defaultMaterialParams)
{
m_externalMap = externalMaterialMap;
Expand All @@ -45,18 +46,6 @@ public MaterialLoadInfo(SubAssetKey key, Material asset, bool useExternal)
}
}

List<MaterialLoadInfo> m_materials = new List<MaterialLoadInfo>();
public IReadOnlyList<MaterialLoadInfo> Materials => m_materials;
void Remove(Material material)
{
var index = m_materials.FindIndex(x => x.Asset == material);
if (index >= 0)
{
m_materials.RemoveAt(index);

}
}

public void Dispose()
{
foreach (var x in m_materials)
Expand All @@ -67,6 +56,11 @@ public void Dispose()
UnityObjectDestroyer.DestroyRuntimeOrEditor(x.Asset);
}
}

if (m_defaultMaterial != null)
{
UnityObjectDestroyer.DestroyRuntimeOrEditor(m_defaultMaterial);
}
}

/// <summary>
Expand All @@ -90,6 +84,12 @@ public void TransferOwnership(TakeResponsibilityForDestroyObjectFunc take)
m_materials.Remove(x);
}
}

if (m_defaultMaterial != null)
{
take(m_defaultMaterialKey, m_defaultMaterial);
m_defaultMaterial = null;
}
}

public Material GetMaterial(int index)
Expand All @@ -101,6 +101,12 @@ public Material GetMaterial(int index)

public async Task<Material> GetDefaultMaterialAsync(IAwaitCaller awaitCaller)
{
if (m_externalMap.ContainsKey(m_defaultMaterialKey))
{
m_defaultMaterial = m_externalMap[m_defaultMaterialKey];
return m_externalMap[m_defaultMaterialKey];
}

if (m_defaultMaterial == null)
{
m_defaultMaterial = await LoadAsync(m_defaultMaterialParams, (_, _) => null, awaitCaller);
Expand Down Expand Up @@ -136,7 +142,8 @@ public async Task<Material> LoadAsync(MaterialDescriptor matDesc, GetTextureAsyn
if (texture != null)
{
material.SetTexture(kv.Key, texture);
SetTextureOffsetAndScale(material, kv.Key, kv.Value.Offset, kv.Value.Scale);
material.SetTextureOffset(kv.Key, kv.Value.Offset);
material.SetTextureScale(kv.Key, kv.Value.Scale);
}
}

Expand Down Expand Up @@ -174,11 +181,5 @@ public async Task<Material> LoadAsync(MaterialDescriptor matDesc, GetTextureAsyn

return material;
}

public static void SetTextureOffsetAndScale(Material material, string propertyName, Vector2 offset, Vector2 scale)
{
material.SetTextureOffset(propertyName, offset);
material.SetTextureScale(propertyName, scale);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using UnityEngine;

namespace UniGLTF
{
/// <summary>
/// フォールバック目的で最低限なにかをエクスポートする。
///
/// メインカラーとメインテクスチャをエクスポートする。
/// </summary>
public class UrpFallbackMaterialExporter
{
public glTFMaterial ExportMaterial(Material src, ITextureExporter textureExporter)
{
var dst = new glTFMaterial
{
name = src.name,
pbrMetallicRoughness = new glTFPbrMetallicRoughness(),
};

dst.pbrMetallicRoughness.baseColorFactor = src.color.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
var index = textureExporter.RegisterExportingAsSRgb(src.mainTexture, false);
if (index >= 0)
{
dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
index = index,
texCoord = 0,
};
GltfMaterialExportUtils.ExportTextureTransform(src.mainTextureOffset, src.mainTextureScale, dst.pbrMetallicRoughness.baseColorTexture);
}

return dst;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ namespace UniGLTF
{
public class UrpLitMaterialExporter
{
public string TargetShaderName { get; }
public Shader Shader { get; set; }

/// <summary>
/// "Universal Render Pipeline/Lit" シェーダのマテリアルをエクスポートする。
///
/// targetShaderName に、プロパティに互換性がある他のシェーダを指定することもできる。
/// プロパティに互換性がある他のシェーダを指定することもできる。
/// </summary>
public UrpLitMaterialExporter(string targetShaderName = null)
public UrpLitMaterialExporter(Shader shader = null)
{
TargetShaderName = string.IsNullOrEmpty(targetShaderName)
? "Universal Render Pipeline/Lit"
: targetShaderName;
Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Lit");
}

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));
if (src.shader != Shader || Shader == null) throw new UniGLTFShaderNotMatchedInternalException(src.shader);

dst = new glTFMaterial
{
Expand All @@ -54,14 +52,20 @@ public bool TryExportMaterial(Material src, ITextureExporter textureExporter, ou

return true;
}
catch (Exception)
catch (UniGLTFShaderNotMatchedInternalException)
{
dst = default;
return false;
}
catch (Exception e)
{
Debug.LogException(e);
dst = default;
return false;
}
}

public static void ExportSurfaceSettings(UrpLitContext context, glTFMaterial dst, ITextureExporter textureExporter)
public static void ExportSurfaceSettings(UrpBaseShaderContext context, glTFMaterial dst, ITextureExporter textureExporter)
{
dst.alphaMode = (context.SurfaceType, context.IsAlphaClipEnabled) switch
{
Expand All @@ -75,7 +79,7 @@ public static void ExportSurfaceSettings(UrpLitContext context, glTFMaterial dst
dst.doubleSided = context.CullMode != CullMode.Back; // NOTE: cull front not supported in glTF
}

public static void ExportBaseColor(UrpLitContext context, glTFMaterial dst, ITextureExporter textureExporter)
public static void ExportBaseColor(UrpBaseShaderContext context, glTFMaterial dst, ITextureExporter textureExporter)
{
dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
if (context.BaseTexture != null)
Expand Down Expand Up @@ -186,7 +190,7 @@ public void ExportEmission(UrpLitContext context, glTFMaterial dst, ITextureExpo
}
}

private static void ExportBaseTexTransform(UrpLitContext context, glTFTextureInfo dst)
private static void ExportBaseTexTransform(UrpBaseShaderContext context, glTFTextureInfo dst)
{
GltfMaterialExportUtils.ExportTextureTransform(
context.BaseTextureOffset,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using UniGLTF.UniUnlit;
using UnityEngine;

namespace UniGLTF
{
public class UrpUniUnlitMaterialExporter
{
public Shader Shader { get; set; }

/// <summary>
/// "UniGLTF/UniUnlit" シェーダのマテリアルをエクスポートする。
///
/// プロパティに互換性がある他のシェーダを指定することもできる。
/// </summary>
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 (textureExporter == null) throw new ArgumentNullException(nameof(textureExporter));
if (src.shader != Shader || Shader == null) throw new UniGLTFShaderNotMatchedInternalException(src.shader);

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 (UniGLTFShaderNotMatchedInternalException)
{
dst = default;
return false;
}
catch (Exception e)
{
Debug.LogException(e);
dst = default;
return false;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using UnityEngine;

namespace UniGLTF
{
public class UrpUnlitMaterialExporter
{
public Shader Shader { get; set; }

/// <summary>
/// "Universal Render Pipeline/Unlit" シェーダのマテリアルをエクスポートする。
///
/// プロパティに互換性がある他のシェーダを指定することもできる。
/// </summary>
public UrpUnlitMaterialExporter(Shader shader = null)
{
Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Unlit");
}

public bool TryExportMaterial(Material src, ITextureExporter textureExporter, out glTFMaterial dst)
{
try
{
if (src == null) throw new ArgumentNullException(nameof(src));
if (textureExporter == null) throw new ArgumentNullException(nameof(textureExporter));
if (src.shader != Shader || Shader == null) throw new UniGLTFShaderNotMatchedInternalException(src.shader);

dst = glTF_KHR_materials_unlit.CreateDefault();
dst.name = src.name;

var context = new UrpUnlitContext(src);
UrpLitMaterialExporter.ExportSurfaceSettings(context, dst, textureExporter);
UrpLitMaterialExporter.ExportBaseColor(context, dst, textureExporter);

return true;
}
catch (UniGLTFShaderNotMatchedInternalException)
{
dst = default;
return false;
}
catch (Exception e)
{
Debug.LogException(e);
dst = default;
return false;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ namespace UniGLTF
{
public class UrpGltfMaterialExporter : IMaterialExporter
{
private readonly UrpLitMaterialExporter _litExporter = new();
public UrpLitMaterialExporter UrpLitExporter { get; set; } = new();
public UrpUnlitMaterialExporter UrpUnlitExporter { get; set; } = new();
public UrpUniUnlitMaterialExporter UrpUniUnlitExporter { get; set; } = new();
public UrpFallbackMaterialExporter FallbackExporter { get; set; } = new();

public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings)
{
glTFMaterial dst;
if (UrpLitExporter.TryExportMaterial(m, textureExporter, out var dst)) return dst;
if (UrpUnlitExporter.TryExportMaterial(m, textureExporter, out dst)) return dst;
if (UrpUniUnlitExporter.TryExportMaterial(m, textureExporter, out dst)) return dst;

if (_litExporter.TryExportMaterial(m, textureExporter, out dst)) return dst;

return dst;
Debug.Log($"Material `{m.name}` fallbacks.");
return FallbackExporter.ExportMaterial(m, textureExporter);
}
}
}
Loading

0 comments on commit 6adf168

Please sign in to comment.