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

古いシェーダー名をフォールバック #1011

Merged
merged 2 commits into from
Jun 7, 2021
Merged
Changes from 1 commit
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
42 changes: 35 additions & 7 deletions Assets/VRMShaders/GLTF/IO/Runtime/MaterialFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public MaterialFactory(IReadOnlyDictionary<SubAssetKey, Material> externalMateri
m_externalMap = externalMaterialMap;
}

static Dictionary<string, string> s_fallbackShaders = new Dictionary<string, string>
{
{"VRM/UnlitTexture", "Unlit/Texture"},
{"VRM/UnlitTransparent", "Unlit/Transparent"},
{"VRM/UnlitCutout", "Unlit/Transparent Cutout"},
// 互換性は無いがとりあえず、
{"VRM/UnlitTransparentZWrite", "VRM/Mtoon"},
Copy link
Contributor

Choose a reason for hiding this comment

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

大文字小文字違いますね

{"UniGLTF/StandardVColor", "UniGLTF/UniUnlit"},
};

public struct MaterialLoadInfo
{
public readonly Material Asset;
Expand Down Expand Up @@ -109,29 +119,47 @@ public async Task<Material> LoadAsync(MaterialDescriptor matDesc, GetTextureAsyn
getTexture = (_) => Task.FromResult<Texture>(null);
}

material = new Material(Shader.Find(matDesc.ShaderName));
var shaderName = matDesc.ShaderName;
if (String.IsNullOrEmpty(shaderName))
{
throw new Exception("no shader name");
}
if (s_fallbackShaders.TryGetValue(shaderName, out string fallback))
{
Debug.LogWarning($"fallback: {shaderName} => {fallback}");
shaderName = fallback;
}

var shader = Shader.Find(shaderName);
if (shader == null)
{
throw new Exception($"shader: {shaderName} not found");
}

material = new Material(shader);
material.name = matDesc.SubAssetKey.Name;

foreach(var kv in matDesc.TextureSlots)
foreach (var kv in matDesc.TextureSlots)
{
var texture = await getTexture(kv.Value);
if(texture!=null){
if (texture != null)
{
material.SetTexture(kv.Key, texture);
SetTextureOffsetAndScale(material, kv.Key, kv.Value.Offset, kv.Value.Scale);
}
}

foreach(var kv in matDesc.Colors)
foreach (var kv in matDesc.Colors)
{
material.SetColor(kv.Key, kv.Value);
}

foreach(var kv in matDesc.Vectors)
foreach (var kv in matDesc.Vectors)
{
material.SetVector(kv.Key, kv.Value);
}

foreach(var kv in matDesc.FloatValues)
foreach (var kv in matDesc.FloatValues)
{
material.SetFloat(kv.Key, kv.Value);
}
Expand All @@ -141,7 +169,7 @@ public async Task<Material> LoadAsync(MaterialDescriptor matDesc, GetTextureAsyn
material.renderQueue = matDesc.RenderQueue.Value;
}

foreach(var action in matDesc.Actions)
foreach (var action in matDesc.Actions)
{
action(material);
}
Expand Down