Can export baseColor & texture of URP/Lit

This commit is contained in:
Masataka SUMI
2024-07-30 22:47:39 +09:00
parent 072dcba638
commit dc6f3605a2
6 changed files with 127 additions and 1 deletions

View File

@@ -6,7 +6,7 @@
{
return RenderPipelineUtility.GetRenderPipelineType() switch
{
RenderPipelineTypes.UniversalRenderPipeline => throw new System.NotImplementedException(),
RenderPipelineTypes.UniversalRenderPipeline => new UrpGltfMaterialExporter(),
RenderPipelineTypes.BuiltinRenderPipeline => new BuiltInGltfMaterialExporter(),
_ => new BuiltInGltfMaterialExporter(),
};

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 70546771aa234567b5d7c4430548ac0f
timeCreated: 1722269920

View File

@@ -0,0 +1,99 @@
using System;
using UnityEngine;
namespace UniGLTF
{
public class UrpLitMaterialExporter
{
public string TargetShaderName { get; }
/// <summary>
/// "Universal Render Pipeline/Lit" シェーダのマテリアルをエクスポートする。
///
/// targetShaderName に、プロパティに互換性がある他のシェーダを指定することもできる。
/// </summary>
public UrpLitMaterialExporter(string targetShaderName = null)
{
TargetShaderName = string.IsNullOrEmpty(targetShaderName)
? "Universal Render Pipeline/Lit"
: targetShaderName;
}
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));
dst = new glTFMaterial
{
name = src.name,
pbrMetallicRoughness = new glTFPbrMetallicRoughness(),
};
var context = new UrpLitContext(src);
if (!Validate(context, out var errorMessage))
{
Debug.LogError(errorMessage, src);
throw new UniGLTFNotSupportedException(errorMessage);
}
ExportBaseColor(context, dst, textureExporter);
return true;
}
catch (Exception)
{
dst = default;
return false;
}
}
public static void ExportBaseColor(UrpLitContext context, glTFMaterial dst, ITextureExporter textureExporter)
{
dst.pbrMetallicRoughness.baseColorFactor = context.BaseColorSrgb.ToFloat4(ColorSpace.sRGB, ColorSpace.Linear);
if (context.BaseTexture != null)
{
var needsAlpha = context.SurfaceType != UrpLitSurfaceType.Opaque;
var index = textureExporter.RegisterExportingAsSRgb(context.BaseTexture, needsAlpha);
if (index >= 0)
{
dst.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo
{
index = index,
texCoord = 0,
};
GltfMaterialExportUtils.ExportTextureTransform(
context.BaseTextureOffset,
context.BaseTextureScale,
dst.pbrMetallicRoughness.baseColorTexture
);
}
}
}
public static bool Validate(UrpLitContext context, out string errorMessage)
{
if (context.WorkflowType != UrpLitWorkflowType.Metallic)
{
errorMessage = "Specular workflow is not supported.";
return false;
}
if (context.OcclusionTexture != null)
{
Debug.LogWarning("Occlusion texture is not supported.");
}
if (context.ParallaxTexture != null)
{
Debug.LogWarning("Parallax texture is not supported.");
}
errorMessage = null;
return true;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8832f807c30a420d95aeaee15a1fbff7
timeCreated: 1722269933

View File

@@ -0,0 +1,18 @@
using UnityEngine;
namespace UniGLTF
{
public class UrpGltfMaterialExporter : IMaterialExporter
{
private readonly UrpLitMaterialExporter _litExporter = new();
public glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter, GltfExportSettings settings)
{
glTFMaterial dst;
if (_litExporter.TryExportMaterial(m, textureExporter, out dst)) return dst;
return dst;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 943c27f3f6054f25b4c0bf7e4cd50eff
timeCreated: 1722269861