Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VRM/UnlitTransparentZWrite の Import を修正 #1248

Merged
merged 2 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using UnityEngine;
using UniGLTF.UniUnlit;
using VRMShaders;
using ColorSpace = VRMShaders.ColorSpace;

namespace UniGLTF
{
public static class GltfUnlitMaterialImporter
{
public const string ShaderName = "UniGLTF/UniUnlit";

public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor matDesc)
{
if (i < 0 || i >= data.GLTF.materials.Count)
Expand All @@ -23,7 +21,7 @@ public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor m
return false;
}

matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), ShaderName);
matDesc = new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, src), UniUnlitUtil.ShaderName);

// texture
if (src.pbrMetallicRoughness.baseColorTexture != null)
Expand All @@ -46,41 +44,41 @@ public static bool TryCreateParam(GltfData data, int i, out MaterialDescriptor m
{
if (src.alphaMode == "OPAQUE")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
}
else if (src.alphaMode == "BLEND")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent);
}
else if (src.alphaMode == "MASK")
{
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout);
material.SetFloat("_Cutoff", src.alphaCutoff);
}
else
{
// default OPAQUE
UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
UniUnlitUtil.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque);
}

// culling
if (src.doubleSided)
{
UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
UniUnlitUtil.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off);
}
else
{
UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back);
UniUnlitUtil.SetCullMode(material, UniUnlit.UniUnlitCullMode.Back);
}

// VColor
var hasVertexColor = data.GLTF.MaterialHasVertexColor(i);
if (hasVertexColor)
{
UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
UniUnlitUtil.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply);
}

UniUnlit.Utils.ValidateProperties(material, true);
UniUnlitUtil.ValidateProperties(material, true);
});

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static bool IsUnlit(string shaderName)
case "Unlit/Texture":
case "Unlit/Transparent":
case "Unlit/Transparent Cutout":
case "UniGLTF/UniUnlit":
case UniUnlit.UniUnlitUtil.ShaderName:
return true;

default:
Expand All @@ -244,7 +244,7 @@ protected virtual glTFMaterial CreateMaterial(Material m)
case "Unlit/Transparent Cutout":
return Export_UnlitCutout(m);

case "UniGLTF/UniUnlit":
case UniUnlit.UniUnlitUtil.ShaderName:
return Export_UniUnlit(m);

default:
Expand Down Expand Up @@ -285,7 +285,7 @@ private glTFMaterial Export_UniUnlit(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();

var renderMode = UniUnlit.Utils.GetRenderMode(m);
var renderMode = UniUnlit.UniUnlitUtil.GetRenderMode(m);
if (renderMode == UniUnlitRenderMode.Opaque)
{
material.alphaMode = glTFBlendMode.OPAQUE.ToString();
Expand All @@ -304,7 +304,7 @@ private glTFMaterial Export_UniUnlit(Material m)
material.alphaMode = glTFBlendMode.OPAQUE.ToString();
}

var cullMode = UniUnlit.Utils.GetCullMode(m);
var cullMode = UniUnlit.UniUnlitUtil.GetCullMode(m);
if (cullMode == UniUnlitCullMode.Off)
{
material.doubleSided = true;
Expand Down
4 changes: 2 additions & 2 deletions Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/VertexColorState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ static bool MaterialUseVertexColor(Material m)
{
return false;
}
if (m.shader.name != UniGLTF.UniUnlit.Utils.ShaderName)
if (m.shader.name != UniGLTF.UniUnlit.UniUnlitUtil.ShaderName)
{
return false;
}
if (UniGLTF.UniUnlit.Utils.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply)
if (UniGLTF.UniUnlit.UniUnlitUtil.GetVColBlendMode(m) != UniGLTF.UniUnlit.UniUnlitVertexColorBlendOp.Multiply)
{
return false;
}
Expand Down
45 changes: 27 additions & 18 deletions Assets/VRM/Runtime/IO/VRMMaterialDescriptorGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using UniGLTF;
using UniGLTF;
using UnityEngine;
using VRMShaders;

Expand All @@ -16,24 +14,35 @@ public VRMMaterialDescriptorGenerator(glTF_VRM_extensions vrm)

public MaterialDescriptor Get(GltfData data, int i)
{
MaterialDescriptor matDesc;

// legacy "VRM/UnlitTransparentZWrite"
if (VRMZWriteMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで分岐

{
return matDesc;
}

// mtoon
if (!VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out MaterialDescriptor matDesc))
if (VRMMToonMaterialImporter.TryCreateParam(data, m_vrm, i, out matDesc))
{
// unlit
if (!GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc))
{
// pbr
if (!GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc))
{
// fallback
#if VRM_DEVELOP
Debug.LogWarning($"material: {i} out of range. fallback");
#endif
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
}
}
return matDesc;
}
return matDesc;

// unlit
if (GltfUnlitMaterialImporter.TryCreateParam(data, i, out matDesc))
{
return matDesc;
}

// pbr
if (GltfPbrMaterialImporter.TryCreateParam(data, i, out matDesc))
{
return matDesc;
}

// fallback
Debug.LogWarning($"fallback");
return new MaterialDescriptor(GltfMaterialDescriptorGenerator.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName);
}
}
}
87 changes: 87 additions & 0 deletions Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using UniGLTF;
using UnityEngine;
using VRMShaders;

namespace VRM
{
public static class VRMZWriteMaterialImporter
{
public const string ShaderName = "VRM/UnlitTransparentZWrite";

public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc)
{
if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0)
{
matDesc = default;
return false;
}
if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count)
{
matDesc = default;
return false;
}

var vrmMaterial = vrm.materialProperties[materialIdx];
if (vrmMaterial.shader != ShaderName)
{
// fallback to gltf
matDesc = default;
return false;
}

// use material.name, because material name may renamed in GltfParser.
var name = data.GLTF.materials[materialIdx].name;

//
// import as MToon
//
matDesc = new MaterialDescriptor(name, MToon.Utils.ShaderName);

matDesc.RenderQueue = vrmMaterial.renderQueue;

if (vrmMaterial.textureProperties.ContainsKey(MToon.Utils.PropMainTex))
{
if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, MToon.Utils.PropMainTex, out var texture))
{
matDesc.TextureSlots.Add(MToon.Utils.PropMainTex, texture.Item2);
matDesc.TextureSlots.Add(MToon.Utils.PropShadeTexture, texture.Item2);
}
}

matDesc.Colors[MToon.Utils.PropColor] = Color.white;
matDesc.Colors[MToon.Utils.PropShadeColor] = Color.white;

foreach (var kv in vrmMaterial.keywordMap)
{
if (kv.Value)
{
matDesc.Actions.Add(material => material.EnableKeyword(kv.Key));
}
else
{
matDesc.Actions.Add(material => material.DisableKeyword(kv.Key));
}
}

foreach (var kv in vrmMaterial.tagMap)
{
matDesc.Actions.Add(material => material.SetOverrideTag(kv.Key, kv.Value));
}

if (vrmMaterial.shader == MToon.Utils.ShaderName)
{
// TODO: Material拡張にMToonの項目が追加されたら旧バージョンのshaderPropから変換をかける
// インポート時にUniVRMに含まれるMToonのバージョンに上書きする
matDesc.FloatValues[MToon.Utils.PropVersion] = MToon.Utils.VersionNumber;
}

matDesc.Actions.Add(m =>
{
m.SetFloat(MToon.Utils.PropBlendMode, (float)MToon.RenderMode.TransparentWithZWrite);
MToon.Utils.ValidateProperties(m, true);
});

return true;
}
}
}
11 changes: 11 additions & 0 deletions Assets/VRM/Runtime/IO/VRMZWriteMaterialImporter.cs.meta

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

2 changes: 1 addition & 1 deletion Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MaterialFactory(IReadOnlyDictionary<SubAssetKey, Material> externalMateri
{"VRM/UnlitCutout", "Unlit/Transparent Cutout"},
// 互換性は無いがとりあえず、
{"VRM/UnlitTransparentZWrite", "VRM/MToon"},
{"UniGLTF/StandardVColor", UniGLTF.UniUnlit.Utils.ShaderName},
{"UniGLTF/StandardVColor", UniGLTF.UniUnlit.UniUnlitUtil.ShaderName},
};

public struct MaterialLoadInfo
Expand Down
22 changes: 11 additions & 11 deletions Assets/VRMShaders/GLTF/UniUnlit/Editor/UniUnlitEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class UniUnlitEditor : ShaderGUI

public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
_mainTex = FindProperty(Utils.PropNameMainTex, properties);
_color = FindProperty(Utils.PropNameColor, properties);
_cutoff = FindProperty(Utils.PropNameCutoff, properties);
_blendMode = FindProperty(Utils.PropNameBlendMode, properties);
_cullMode = FindProperty(Utils.PropNameCullMode, properties);
_vColBlendMode = FindProperty(Utils.PropNameVColBlendMode, properties);
_mainTex = FindProperty(UniUnlitUtil.PropNameMainTex, properties);
_color = FindProperty(UniUnlitUtil.PropNameColor, properties);
_cutoff = FindProperty(UniUnlitUtil.PropNameCutoff, properties);
_blendMode = FindProperty(UniUnlitUtil.PropNameBlendMode, properties);
_cullMode = FindProperty(UniUnlitUtil.PropNameCullMode, properties);
_vColBlendMode = FindProperty(UniUnlitUtil.PropNameVColBlendMode, properties);
// _srcBlend = FindProperty(PropNameSrcBlend, properties);
// _dstBlend = FindProperty(PropNameDstBlend, properties);
// _zWrite = FindProperty(PropNameZWrite, properties);
Expand All @@ -46,18 +46,18 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro
public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
var blendMode = UniUnlitRenderMode.Opaque;
if (material.HasProperty(Utils.PropNameStandardShadersRenderMode)) // from Standard shader
if (material.HasProperty(UniUnlitUtil.PropNameStandardShadersRenderMode)) // from Standard shader
{
blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(Utils.PropNameStandardShadersRenderMode));
blendMode = (UniUnlitRenderMode) Math.Min(2f, material.GetFloat(UniUnlitUtil.PropNameStandardShadersRenderMode));
}

// assigns UniUnlit's properties...
base.AssignNewShaderToMaterial(material, oldShader, newShader);

// take over old value
material.SetFloat(Utils.PropNameBlendMode, (float) blendMode);
material.SetFloat(UniUnlitUtil.PropNameBlendMode, (float) blendMode);

Utils.ValidateProperties(material, isRenderModeChangedByUser: true);
UniUnlitUtil.ValidateProperties(material, isRenderModeChangedByUser: true);
}

private void DrawRenderingBox(MaterialEditor materialEditor, Material[] materials)
Expand Down Expand Up @@ -152,7 +152,7 @@ private static void ModeChanged(Material[] materials, bool isRenderModeChangedBy
{
foreach (var material in materials)
{
Utils.ValidateProperties(material, isRenderModeChangedByUser);
UniUnlitUtil.ValidateProperties(material, isRenderModeChangedByUser);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public enum UniUnlitVertexColorBlendOp
Multiply = 1,
}

public static class Utils
public static class UniUnlitUtil
{
public const string ShaderName = "UniGLTF/UniUnlit";
public const string PropNameMainTex = "_MainTex";
Expand Down
11 changes: 11 additions & 0 deletions Assets/VRMShaders/GLTF/UniUnlit/Runtime/UniUnlitUtil.cs.meta

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

3 changes: 0 additions & 3 deletions Assets/VRMShaders/GLTF/UniUnlit/Runtime/Utils.cs.meta

This file was deleted.