mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-22 10:34:54 -05:00
Implements UrpGltfDefaultMaterialImporter
This commit is contained in:
@@ -1,27 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
/// <summary>
|
||||
/// Generate the descriptor of the glTF default material.
|
||||
///
|
||||
/// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#default-material
|
||||
/// </summary>
|
||||
public static class UrpGltfDefaultMaterialImporter
|
||||
public class UrpGltfDefaultMaterialImporter
|
||||
{
|
||||
public static MaterialDescriptor CreateParam()
|
||||
public Shader Shader { get; set; }
|
||||
|
||||
public UrpGltfDefaultMaterialImporter(Shader shader = null)
|
||||
{
|
||||
Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Lit");
|
||||
}
|
||||
|
||||
public MaterialDescriptor CreateParam(string materialName)
|
||||
{
|
||||
// FIXME
|
||||
return new MaterialDescriptor(
|
||||
"__default__",
|
||||
UrpGltfPbrMaterialImporter.Shader,
|
||||
default,
|
||||
string.IsNullOrEmpty(materialName) ? "__default__" : materialName,
|
||||
Shader,
|
||||
null,
|
||||
new Dictionary<string, TextureDescriptor>(),
|
||||
new Dictionary<string, float>(),
|
||||
new Dictionary<string, Color>(),
|
||||
new Dictionary<string, Vector4>(),
|
||||
new List<Action<Material>>()
|
||||
new List<Action<Material>> { GenerateDefaultMaterial }
|
||||
);
|
||||
}
|
||||
|
||||
public static void GenerateDefaultMaterial(Material dst)
|
||||
{
|
||||
var context = new UrpLitContext(dst);
|
||||
context.UnsafeEditMode = true;
|
||||
|
||||
context.SurfaceType = UrpLitSurfaceType.Opaque;
|
||||
context.IsAlphaClipEnabled = false;
|
||||
context.CullMode = CullMode.Back;
|
||||
|
||||
context.BaseColorSrgb = new Color(1, 1, 1, 1);
|
||||
context.BaseTexture = null;
|
||||
|
||||
context.WorkflowType = UrpLitWorkflowType.Metallic;
|
||||
context.SmoothnessTextureChannel = UrpLitSmoothnessMapChannel.SpecularMetallicAlpha;
|
||||
context.Metallic = 1f;
|
||||
context.Smoothness = 0f;
|
||||
context.MetallicGlossMap = null;
|
||||
|
||||
context.OcclusionTexture = null;
|
||||
|
||||
context.BumpMap = null;
|
||||
|
||||
context.IsEmissionEnabled = false;
|
||||
context.EmissionColorLinear = new Color(0, 0, 0, 0);
|
||||
context.EmissionTexture = null;
|
||||
|
||||
context.Validate();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,19 @@ namespace UniGLTF
|
||||
///
|
||||
/// see: https://github.com/Unity-Technologies/Graphics/blob/v7.5.3/com.unity.render-pipelines.universal/Editor/UniversalRenderPipelineMaterialUpgrader.cs#L354-L379
|
||||
/// </summary>
|
||||
public static class UrpGltfPbrMaterialImporter
|
||||
public class UrpGltfPbrMaterialImporter
|
||||
{
|
||||
public const string ShaderName = "Universal Render Pipeline/Lit";
|
||||
/// <summary>
|
||||
/// Universal Render Pipeline/Lit とプロパティやキーワードに互換があるカスタムシェーダに置換可能。
|
||||
/// </summary>
|
||||
public Shader Shader { get; set; }
|
||||
|
||||
public static Shader Shader => Shader.Find(ShaderName);
|
||||
public UrpGltfPbrMaterialImporter(Shader shader = null)
|
||||
{
|
||||
Shader = shader != null ? shader : Shader.Find("Universal Render Pipeline/Lit");
|
||||
}
|
||||
|
||||
public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
|
||||
public bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
|
||||
{
|
||||
if (i < 0 || i >= data.GLTF.materials.Count)
|
||||
{
|
||||
@@ -44,7 +50,6 @@ namespace UniGLTF
|
||||
|
||||
public static async Task GenerateMaterialAsync(GltfData data, glTFMaterial src, Material dst, GetTextureAsyncFunc getTextureAsync, IAwaitCaller awaitCaller)
|
||||
{
|
||||
dst.shader = Shader;
|
||||
var context = new UrpLitContext(dst);
|
||||
context.UnsafeEditMode = true;
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF
|
||||
@@ -10,30 +7,22 @@ namespace UniGLTF
|
||||
/// </summary>
|
||||
public sealed class UrpGltfMaterialDescriptorGenerator : IMaterialDescriptorGenerator
|
||||
{
|
||||
public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; }
|
||||
public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; }
|
||||
|
||||
public MaterialDescriptor Get(GltfData data, int i)
|
||||
{
|
||||
if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out var param)) return param;
|
||||
if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out param)) return param;
|
||||
// fallback
|
||||
if (PbrMaterialImporter.TryCreateParam(data, i, out param)) return param;
|
||||
|
||||
// NOTE: Fallback to default material
|
||||
if (Symbols.VRM_DEVELOP)
|
||||
{
|
||||
Debug.LogWarning($"material: {i} out of range. fallback");
|
||||
}
|
||||
|
||||
return new MaterialDescriptor(
|
||||
GltfMaterialImportUtils.ImportMaterialName(i, null),
|
||||
UrpGltfPbrMaterialImporter.Shader,
|
||||
null,
|
||||
new Dictionary<string, TextureDescriptor>(),
|
||||
new Dictionary<string, float>(),
|
||||
new Dictionary<string, Color>(),
|
||||
new Dictionary<string, Vector4>(),
|
||||
new Collection<Action<Material>>());
|
||||
return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null));
|
||||
}
|
||||
|
||||
public MaterialDescriptor GetGltfDefault()
|
||||
{
|
||||
return UrpGltfDefaultMaterialImporter.CreateParam();
|
||||
}
|
||||
public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,9 @@ namespace VRM
|
||||
{
|
||||
private readonly glTF_VRM_extensions _vrm;
|
||||
|
||||
public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; }
|
||||
public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; }
|
||||
|
||||
public UrpVrmMaterialDescriptorGenerator(glTF_VRM_extensions vrm)
|
||||
{
|
||||
_vrm = vrm;
|
||||
@@ -20,26 +23,16 @@ namespace VRM
|
||||
// unlit "UniUnlit" work in URP
|
||||
if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out var matDesc)) return matDesc;
|
||||
// pbr "Standard" to "Universal Render Pipeline/Lit"
|
||||
if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
|
||||
// fallback
|
||||
if (PbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
|
||||
|
||||
// NOTE: Fallback to default material
|
||||
if (Symbols.VRM_DEVELOP)
|
||||
{
|
||||
Debug.LogWarning($"material: {i} out of range. fallback");
|
||||
}
|
||||
return new MaterialDescriptor(
|
||||
GltfMaterialImportUtils.ImportMaterialName(i, null),
|
||||
UrpGltfPbrMaterialImporter.Shader,
|
||||
null,
|
||||
new Dictionary<string, TextureDescriptor>(),
|
||||
new Dictionary<string, float>(),
|
||||
new Dictionary<string, Color>(),
|
||||
new Dictionary<string, Vector4>(),
|
||||
new Action<Material>[]{});
|
||||
return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null));
|
||||
}
|
||||
|
||||
public MaterialDescriptor GetGltfDefault()
|
||||
{
|
||||
return UrpGltfDefaultMaterialImporter.CreateParam();
|
||||
}
|
||||
public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ namespace UniVRM10
|
||||
{
|
||||
public sealed class UrpVrm10MaterialDescriptorGenerator : IMaterialDescriptorGenerator
|
||||
{
|
||||
public UrpGltfPbrMaterialImporter PbrMaterialImporter { get; }
|
||||
public UrpGltfDefaultMaterialImporter DefaultMaterialImporter { get; }
|
||||
|
||||
public MaterialDescriptor Get(GltfData data, int i)
|
||||
{
|
||||
// mtoon
|
||||
@@ -14,24 +17,16 @@ namespace UniVRM10
|
||||
// unlit
|
||||
if (BuiltInGltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
|
||||
// pbr
|
||||
if (UrpGltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
|
||||
if (PbrMaterialImporter.TryCreateParam(data, i, out matDesc)) return matDesc;
|
||||
|
||||
// fallback
|
||||
Debug.LogWarning($"material: {i} out of range. fallback");
|
||||
return new MaterialDescriptor(
|
||||
GltfMaterialImportUtils.ImportMaterialName(i, null),
|
||||
UrpGltfPbrMaterialImporter.Shader,
|
||||
null,
|
||||
new Dictionary<string, TextureDescriptor>(),
|
||||
new Dictionary<string, float>(),
|
||||
new Dictionary<string, Color>(),
|
||||
new Dictionary<string, Vector4>(),
|
||||
new Action<Material>[]{});
|
||||
// NOTE: Fallback to default material
|
||||
if (Symbols.VRM_DEVELOP)
|
||||
{
|
||||
Debug.LogWarning($"material: {i} out of range. fallback");
|
||||
}
|
||||
return GetGltfDefault(GltfMaterialImportUtils.ImportMaterialName(i, null));
|
||||
}
|
||||
|
||||
public MaterialDescriptor GetGltfDefault()
|
||||
{
|
||||
return UrpGltfDefaultMaterialImporter.CreateParam();
|
||||
}
|
||||
public MaterialDescriptor GetGltfDefault(string materialName = null) => DefaultMaterialImporter.CreateParam(materialName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user