mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-16 07:34:05 -05:00
Separated VRMMaterialImporter
This commit is contained in:
parent
ef7252fe4d
commit
31dac32e21
|
|
@ -36,7 +36,7 @@ namespace VRM
|
|||
|
||||
public static void LoadFromBytes(VRMImporterContext context)
|
||||
{
|
||||
context.CreateMaterial = VRMImporter.GetMaterialFunc(glTF_VRM_Material.Parse(context.Json));
|
||||
context.MaterialImporter = new VRMMaterialImporter(glTF_VRM_Material.Parse(context.Json));
|
||||
|
||||
gltfImporter.Load(context);
|
||||
|
||||
|
|
@ -45,106 +45,6 @@ namespace VRM
|
|||
context.ShowMeshes();
|
||||
}
|
||||
|
||||
static string[] VRM_SHADER_NAMES =
|
||||
{
|
||||
"Standard",
|
||||
"VRM/UnlitTexture",
|
||||
"VRM/UnlitCutout",
|
||||
"VRM/UnlitTransparent",
|
||||
"VRM/UnlitTransparentZWrite",
|
||||
"VRM/MToon",
|
||||
};
|
||||
|
||||
public static MaterialIO.CreateMaterialFunc GetMaterialFunc(List<glTF_VRM_Material> materials)
|
||||
{
|
||||
var CreateDefault = MaterialIO.CreateMaterialFuncFromShader(new ShaderStore("VRM/UnlitTexture"));
|
||||
var CreateZWrite = MaterialIO.CreateMaterialFuncFromShader(new ShaderStore("VRM/UnlitTransparentZWrite"));
|
||||
MaterialIO.CreateMaterialFunc fallback = (ctx, i) =>
|
||||
{
|
||||
var vrm = ctx.GLTF;
|
||||
if (vrm != null && vrm.materials[i].name.ToLower().Contains("zwrite"))
|
||||
{
|
||||
// 一応。不要かも
|
||||
Debug.Log("fallback to VRM/UnlitTransparentZWrite");
|
||||
return CreateZWrite(ctx, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("fallback to VRM/UnlitTexture");
|
||||
return CreateDefault(ctx, i);
|
||||
}
|
||||
};
|
||||
if (materials == null && materials.Count == 0)
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
|
||||
return (ctx, i) =>
|
||||
{
|
||||
var item = materials[i];
|
||||
var shaderName = item.shader;
|
||||
var shader = Shader.Find(shaderName);
|
||||
if (shader == null)
|
||||
{
|
||||
if (VRM_SHADER_NAMES.Contains(shaderName))
|
||||
{
|
||||
Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName);
|
||||
return fallback(ctx, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarningFormat("unknown shader {0}.", shaderName);
|
||||
return fallback(ctx, i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var material = new Material(shader);
|
||||
material.name = item.name;
|
||||
material.renderQueue = item.renderQueue;
|
||||
|
||||
foreach (var kv in item.floatProperties)
|
||||
{
|
||||
material.SetFloat(kv.Key, kv.Value);
|
||||
}
|
||||
foreach (var kv in item.vectorProperties)
|
||||
{
|
||||
if (item.textureProperties.ContainsKey(kv.Key))
|
||||
{
|
||||
// texture offset & scale
|
||||
material.SetTextureOffset(kv.Key, new Vector2(kv.Value[0], kv.Value[1]));
|
||||
material.SetTextureScale(kv.Key, new Vector2(kv.Value[2], kv.Value[3]));
|
||||
}
|
||||
else
|
||||
{
|
||||
// vector4
|
||||
var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
|
||||
material.SetVector(kv.Key, v);
|
||||
}
|
||||
}
|
||||
foreach (var kv in item.textureProperties)
|
||||
{
|
||||
material.SetTexture(kv.Key, ctx.Textures[kv.Value].Texture);
|
||||
}
|
||||
foreach (var kv in item.keywordMap)
|
||||
{
|
||||
if (kv.Value)
|
||||
{
|
||||
material.EnableKeyword(kv.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
material.DisableKeyword(kv.Key);
|
||||
}
|
||||
}
|
||||
foreach (var kv in item.tagMap)
|
||||
{
|
||||
material.SetOverrideTag(kv.Key, kv.Value);
|
||||
}
|
||||
return material;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#region OnLoad
|
||||
public static Unit OnLoadModel(VRMImporterContext context)
|
||||
|
|
@ -399,13 +299,13 @@ namespace VRM
|
|||
{
|
||||
if (context.GLTF.materials == null || !context.GLTF.materials.Any())
|
||||
{
|
||||
context.Materials.Add(context.CreateMaterial(context, 0));
|
||||
context.Materials.Add(context.MaterialImporter.CreateMaterial(context, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < context.GLTF.materials.Count; ++i)
|
||||
{
|
||||
context.Materials.Add(context.CreateMaterial(context, i));
|
||||
context.Materials.Add(context.MaterialImporter.CreateMaterial(context, i));
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -524,7 +424,7 @@ namespace VRM
|
|||
.ContinueWith(Scheduler.MainThread, x =>
|
||||
{
|
||||
// material function
|
||||
ctx.CreateMaterial = VRMImporter.GetMaterialFunc(x);
|
||||
ctx.MaterialImporter = new VRMMaterialImporter(x);
|
||||
})
|
||||
.OnExecute(Scheduler.ThreadPool, parent =>
|
||||
{
|
||||
|
|
|
|||
121
Scripts/Format/VRMMaterialImporter.cs
Normal file
121
Scripts/Format/VRMMaterialImporter.cs
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
using System.Collections.Generic;
|
||||
using UniGLTF;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace VRM
|
||||
{
|
||||
public class VRMMaterialImporter : MaterialImporter
|
||||
{
|
||||
List<glTF_VRM_Material> m_materials;
|
||||
public VRMMaterialImporter(List<glTF_VRM_Material> materials) : base(new ShaderStore("VRM/UnlitTexture"))
|
||||
{
|
||||
m_materials = materials;
|
||||
/*
|
||||
var CreateDefault = MaterialIO.CreateMaterialFuncFromShader(new ShaderStore("VRM/UnlitTexture"));
|
||||
var CreateZWrite = MaterialIO.CreateMaterialFuncFromShader(new ShaderStore("VRM/UnlitTransparentZWrite"));
|
||||
MaterialIO.CreateMaterialFunc fallback = (ctx, i) =>
|
||||
{
|
||||
var vrm = ctx.GLTF;
|
||||
if (vrm != null && vrm.materials[i].name.ToLower().Contains("zwrite"))
|
||||
{
|
||||
// 一応。不要かも
|
||||
Debug.Log("fallback to VRM/UnlitTransparentZWrite");
|
||||
return CreateZWrite(ctx, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("fallback to VRM/UnlitTexture");
|
||||
return CreateDefault(ctx, i);
|
||||
}
|
||||
};
|
||||
if (materials == null && materials.Count == 0)
|
||||
{
|
||||
return fallback;
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
static string[] VRM_SHADER_NAMES =
|
||||
{
|
||||
"Standard",
|
||||
"VRM/UnlitTexture",
|
||||
"VRM/UnlitCutout",
|
||||
"VRM/UnlitTransparent",
|
||||
"VRM/UnlitTransparentZWrite",
|
||||
"VRM/MToon",
|
||||
};
|
||||
|
||||
public override Material CreateMaterial(ImporterContext ctx, int i)
|
||||
{
|
||||
var item = m_materials[i];
|
||||
var shaderName = item.shader;
|
||||
var shader = Shader.Find(shaderName);
|
||||
if (shader == null)
|
||||
{
|
||||
//
|
||||
// no shader
|
||||
//
|
||||
if (VRM_SHADER_NAMES.Contains(shaderName))
|
||||
{
|
||||
Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName);
|
||||
return base.CreateMaterial(ctx, i);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarningFormat("unknown shader {0}.", shaderName);
|
||||
return base.CreateMaterial(ctx, i);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// restore VRM material
|
||||
//
|
||||
var material = new Material(shader);
|
||||
material.name = item.name;
|
||||
material.renderQueue = item.renderQueue;
|
||||
|
||||
foreach (var kv in item.floatProperties)
|
||||
{
|
||||
material.SetFloat(kv.Key, kv.Value);
|
||||
}
|
||||
foreach (var kv in item.vectorProperties)
|
||||
{
|
||||
if (item.textureProperties.ContainsKey(kv.Key))
|
||||
{
|
||||
// texture offset & scale
|
||||
material.SetTextureOffset(kv.Key, new Vector2(kv.Value[0], kv.Value[1]));
|
||||
material.SetTextureScale(kv.Key, new Vector2(kv.Value[2], kv.Value[3]));
|
||||
}
|
||||
else
|
||||
{
|
||||
// vector4
|
||||
var v = new Vector4(kv.Value[0], kv.Value[1], kv.Value[2], kv.Value[3]);
|
||||
material.SetVector(kv.Key, v);
|
||||
}
|
||||
}
|
||||
foreach (var kv in item.textureProperties)
|
||||
{
|
||||
material.SetTexture(kv.Key, ctx.Textures[kv.Value].Texture);
|
||||
}
|
||||
foreach (var kv in item.keywordMap)
|
||||
{
|
||||
if (kv.Value)
|
||||
{
|
||||
material.EnableKeyword(kv.Key);
|
||||
}
|
||||
else
|
||||
{
|
||||
material.DisableKeyword(kv.Key);
|
||||
}
|
||||
}
|
||||
foreach (var kv in item.tagMap)
|
||||
{
|
||||
material.SetOverrideTag(kv.Key, kv.Value);
|
||||
}
|
||||
|
||||
return material;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Scripts/Format/VRMMaterialImporter.cs.meta
Normal file
12
Scripts/Format/VRMMaterialImporter.cs.meta
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 77db57076c0650c469c3ff0d4853ce21
|
||||
timeCreated: 1533624711
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
2
UniGLTF
2
UniGLTF
|
|
@ -1 +1 @@
|
|||
Subproject commit 826dbf00f6ec734c460d17b297ef5d5cc453fd24
|
||||
Subproject commit 33432a76f9b698e22d44faef508bf28ecbfed804
|
||||
Loading…
Reference in New Issue
Block a user