mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-07-19 00:31:52 -05:00
Implements UrpUniUnlitMaterialExporter
This commit is contained in:
parent
eea8b6937e
commit
182fdbe8b0
|
|
@ -0,0 +1,74 @@
|
|||
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 (src.shader != Shader) throw new ArgumentException(nameof(src));
|
||||
if (textureExporter == null) throw new ArgumentNullException(nameof(textureExporter));
|
||||
|
||||
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 (Exception e)
|
||||
{
|
||||
Debug.LogException(e);
|
||||
dst = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5b17f3928c684fc1a9beda10797b0e76
|
||||
timeCreated: 1722613258
|
||||
80
Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs
Normal file
80
Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace UniGLTF.UniUnlit
|
||||
{
|
||||
public class UniUnlitContext
|
||||
{
|
||||
public Material Material { get; }
|
||||
public bool UnsafeEditMode { get; set; } = false;
|
||||
|
||||
public UniUnlitContext(Material material)
|
||||
{
|
||||
Material = material;
|
||||
}
|
||||
|
||||
public UniUnlitRenderMode RenderMode
|
||||
{
|
||||
get => UniUnlitUtil.GetRenderMode(Material);
|
||||
set
|
||||
{
|
||||
UniUnlitUtil.SetRenderMode(Material, value);
|
||||
if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material, isRenderModeChangedByUser: true);
|
||||
}
|
||||
}
|
||||
|
||||
public UniUnlitCullMode CullMode
|
||||
{
|
||||
get => UniUnlitUtil.GetCullMode(Material);
|
||||
set
|
||||
{
|
||||
UniUnlitUtil.SetCullMode(Material, value);
|
||||
if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material);
|
||||
}
|
||||
}
|
||||
|
||||
public UniUnlitVertexColorBlendOp VColBlendMode
|
||||
{
|
||||
get => UniUnlitUtil.GetVColBlendMode(Material);
|
||||
set
|
||||
{
|
||||
UniUnlitUtil.SetVColBlendMode(Material, value);
|
||||
if (!UnsafeEditMode) UniUnlitUtil.ValidateProperties(Material);
|
||||
}
|
||||
}
|
||||
|
||||
public Color MainColorSrgb
|
||||
{
|
||||
get => Material.GetColor(UniUnlitUtil.PropNameColor);
|
||||
set => Material.SetColor(UniUnlitUtil.PropNameColor, value);
|
||||
}
|
||||
|
||||
public Texture MainTexture
|
||||
{
|
||||
get => Material.GetTexture(UniUnlitUtil.PropNameMainTex);
|
||||
set => Material.SetTexture(UniUnlitUtil.PropNameMainTex, value);
|
||||
}
|
||||
|
||||
public Vector2 MainTextureOffset
|
||||
{
|
||||
get => Material.GetTextureOffset(UniUnlitUtil.PropNameMainTex);
|
||||
set => Material.SetTextureOffset(UniUnlitUtil.PropNameMainTex, value);
|
||||
}
|
||||
|
||||
public Vector2 MainTextureScale
|
||||
{
|
||||
get => Material.GetTextureScale(UniUnlitUtil.PropNameMainTex);
|
||||
set => Material.SetTextureScale(UniUnlitUtil.PropNameMainTex, value);
|
||||
}
|
||||
|
||||
public float Cutoff
|
||||
{
|
||||
get => Material.GetFloat(UniUnlitUtil.PropNameCutoff);
|
||||
set => Material.SetFloat(UniUnlitUtil.PropNameCutoff, value);
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
UniUnlitUtil.ValidateProperties(Material, isRenderModeChangedByUser: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs.meta
Normal file
3
Assets/UniGLTF/UniUnlit/Runtime/UniUnlitContext.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 57963fbf465047bf83ed829f8a0022d3
|
||||
timeCreated: 1722613449
|
||||
Loading…
Reference in New Issue
Block a user