Added VRMPreShaderPropExporter

This commit is contained in:
ousttrue
2018-07-31 22:04:01 +09:00
parent a6c7327f41
commit e7ea49f89d
14 changed files with 487 additions and 68 deletions

View File

@@ -4,19 +4,16 @@ using System.Linq;
using UniGLTF;
using UnityEngine;
using UniJSON;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace VRM
{
[Serializable]
public class glTF_VRM_Material : UniGLTF.JsonSerializableBase
public class glTF_VRM_Material : JsonSerializableBase
{
public string name;
public string shader;
public int renderQueue=-1;
public int renderQueue = -1;
public Dictionary<string, float> floatProperties = new Dictionary<string, float>();
public Dictionary<string, float[]> vectorProperties = new Dictionary<string, float[]>();
@@ -60,7 +57,7 @@ namespace VRM
}
{
f.Key("keywordMap"); f.BeginMap();
foreach(var kv in keywordMap)
foreach (var kv in keywordMap)
{
f.Key(kv.Key); f.Value(kv.Value);
}
@@ -78,12 +75,12 @@ namespace VRM
public static List<glTF_VRM_Material> Parse(string src)
{
var json = UniJSON.JsonParser.Parse(src)["extensions"]["VRM"]["materialProperties"];
var json = UniJSON.JsonParser.Parse(src)["extensions"]["VRM"]["materialProperties"];
var materials = json.DeserializeList<glTF_VRM_Material>();
var jsonItems = json.ArrayItems.ToArray();
for(int i=0; i<materials.Count; ++i)
for (int i = 0; i < materials.Count; ++i)
{
materials[i].floatProperties =
materials[i].floatProperties =
jsonItems[i]["floatProperties"].ObjectItems.ToDictionary(x => x.Key, x => x.Value.Value.GetSingle());
materials[i].vectorProperties =
jsonItems[i]["vectorProperties"].ObjectItems.ToDictionary(x => x.Key, x =>
@@ -109,73 +106,87 @@ namespace VRM
renderQueue = m.renderQueue,
};
#if UNITY_EDITOR
for (int i = 0; i < ShaderUtil.GetPropertyCount(m.shader); ++i)
var prop = VRMPreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (prop == null)
{
var name = ShaderUtil.GetPropertyName(m.shader, i);
var propType = ShaderUtil.GetPropertyType(m.shader, i);
switch (propType)
{
case ShaderUtil.ShaderPropertyType.Color:
{
var value = m.GetColor(name).ToArray();
material.vectorProperties.Add(name, value);
}
break;
case ShaderUtil.ShaderPropertyType.Range:
case ShaderUtil.ShaderPropertyType.Float:
{
var value = m.GetFloat(name);
material.floatProperties.Add(name, value);
}
break;
case ShaderUtil.ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(name);
if (texture != null)
{
var value = textures.IndexOf(texture);
if (value == -1)
{
Debug.LogFormat("not found {0}", texture.name);
}
else
{
material.textureProperties.Add(name, value);
}
}
// offset & scaling
var offset = m.GetTextureOffset(name);
var scaling = m.GetTextureScale(name);
material.vectorProperties.Add(name,
new float[] { offset.x, offset.y, scaling.x, scaling.y });
}
break;
case ShaderUtil.ShaderPropertyType.Vector:
{
var value = m.GetVector(name).ToArray();
material.vectorProperties.Add(name, value);
}
break;
default:
throw new NotImplementedException();
}
}
#else
Debug.LogWarning("cannot export material properties on runtime");
#if UNITY_EDITOR
// fallback
Debug.LogWarningFormat("Unsupported shader: {0}", m.shader.name);
prop = VRMPreShaderPropExporter.ShaderProps.FromShader(m.shader);
#endif
}
if (prop == null)
{
Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
}
else
{
// get properties
//material.SetProp(prop);
foreach (var kv in prop.Properties)
{
switch (kv.Value)
{
case VRMPreShaderPropExporter.ShaderPropertyType.Color:
{
var value = m.GetColor(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.Range:
case VRMPreShaderPropExporter.ShaderPropertyType.Float:
{
var value = m.GetFloat(kv.Key);
material.floatProperties.Add(kv.Key, value);
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(kv.Key);
if (texture != null)
{
var value = textures.IndexOf(texture);
if (value == -1)
{
Debug.LogFormat("not found {0}", texture.name);
}
else
{
material.textureProperties.Add(kv.Key, value);
}
}
// offset & scaling
var offset = m.GetTextureOffset(kv.Key);
var scaling = m.GetTextureScale(kv.Key);
material.vectorProperties.Add(kv.Key,
new float[] { offset.x, offset.y, scaling.x, scaling.y });
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.Vector:
{
var value = m.GetVector(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
default:
throw new NotImplementedException();
}
}
}
foreach (var keyword in m.shaderKeywords)
{
material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
}
foreach(var tag in TAGS)
foreach (var tag in TAGS)
{
var value = m.GetTag(tag, false);
if (!String.IsNullOrEmpty(value))

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 3fa98374d2ce76443981cc61ab80e6ce
folderAsset: yes
timeCreated: 1533035745
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,167 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using UniGLTF;
using UnityEditor;
using UnityEngine;
namespace VRM
{
public class VRMPreExportShaderAttribute : Attribute { }
public static partial class VRMPreShaderPropExporter
{
public static string[] SupportedShaders = new string[]
{
"VRM/MToon",
"VRM/UnlitTexture",
"VRM/UnlitCutout",
"VRM/UnlitTransparent",
"VRM/UnlitTransparentZWrite",
};
#if UNITY_EDITOR
[MenuItem(VRMVersion.VRM_VERSION + "/PreExport ShaderProps")]
static void PreExport()
{
foreach (var shaderName in SupportedShaders)
{
var shader = Shader.Find(shaderName);
PreExport(shader);
}
}
static string EscapeShaderName(string name)
{
return name.Replace("/", "_").Replace(" ", "_");
}
const string EXPORT_DIR = "Assets/VRM/Scripts/PreExportShaderProps/";
static void PreExport(Shader shader)
{
var path = UnityPath.FromUnityPath(EXPORT_DIR + EscapeShaderName(shader.name) + ".cs");
Debug.LogFormat("PreExport: {0}", path.FullPath);
var props = ShaderProps.FromShader(shader);
File.WriteAllText(path.FullPath, props.ToString(shader.name));
}
#endif
public enum ShaderPropertyType
{
TexEnv,
Color,
Range,
Float,
Vector,
}
public class ShaderProps
{
public KeyValuePair<string, ShaderPropertyType>[] Properties;
#if UNITY_EDITOR
static ShaderPropertyType ConvType(ShaderUtil.ShaderPropertyType src)
{
switch (src)
{
case ShaderUtil.ShaderPropertyType.TexEnv: return ShaderPropertyType.TexEnv;
case ShaderUtil.ShaderPropertyType.Color: return ShaderPropertyType.Color;
case ShaderUtil.ShaderPropertyType.Float: return ShaderPropertyType.Float;
case ShaderUtil.ShaderPropertyType.Range: return ShaderPropertyType.Range;
case ShaderUtil.ShaderPropertyType.Vector: return ShaderPropertyType.Vector;
default: throw new NotImplementedException();
}
}
public static ShaderProps FromShader(Shader shader)
{
var properties = new List<KeyValuePair<string, ShaderPropertyType>>();
for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i)
{
var name = ShaderUtil.GetPropertyName(shader, i);
var propType = ShaderUtil.GetPropertyType(shader, i);
properties.Add(new KeyValuePair<string, ShaderPropertyType>(name, ConvType(propType)));
}
return new ShaderProps
{
Properties = properties.ToArray(),
};
}
public string ToString(string shaderName)
{
var list = new List<string>();
foreach(var kv in Properties)
{
list.Add(string.Format("new KeyValuePair<string, ShaderPropertyType>(\"{0}\", ShaderPropertyType.{1})\r\n", kv.Key, kv.Value));
}
return string.Format(@"using System.Collections.Generic;
namespace VRM
{{
public static partial class VRMPreShaderPropExporter
{{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> {0}
{{
get
{{
return new KeyValuePair<string, ShaderProps>(
""{1}"",
new ShaderProps
{{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{{
{2}
}}
}}
);
}}
}}
}}
}}
"
, EscapeShaderName(shaderName)
, shaderName
, String.Join(",", list.ToArray()));
}
#endif
}
#region Runtime
static Dictionary<string, ShaderProps> m_shaderPropMap;
public static ShaderProps GetPropsForSupportedShader(string shaderName)
{
if (m_shaderPropMap == null)
{
m_shaderPropMap = new Dictionary<string, ShaderProps>();
foreach (var prop in typeof(VRMPreShaderPropExporter).GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
if (prop.GetCustomAttributes(typeof(VRMPreExportShaderAttribute), true).Any())
{
var kv = (KeyValuePair<string, ShaderProps>)prop.GetValue(null, null);
m_shaderPropMap.Add(kv.Key, kv.Value);
}
}
}
ShaderProps props;
if (!m_shaderPropMap.TryGetValue(shaderName, out props))
{
return null;
}
return props;
}
#endregion
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 50935dd2f9f3fa445a687f30d4dd663b
timeCreated: 1533035131
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,55 @@
using System.Collections.Generic;
namespace VRM
{
public static partial class VRMPreShaderPropExporter
{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> VRM_MToon
{
get
{
return new KeyValuePair<string, ShaderProps>(
"VRM/MToon",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_Cutoff", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_Color", ShaderPropertyType.Color)
,new KeyValuePair<string, ShaderPropertyType>("_ShadeColor", ShaderPropertyType.Color)
,new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_ShadeTexture", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_BumpScale", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_BumpMap", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_ReceiveShadowRate", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_ReceiveShadowTexture", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_ShadeShift", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_ShadeToony", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_LightColorAttenuation", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_SphereAdd", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_EmissionColor", ShaderPropertyType.Color)
,new KeyValuePair<string, ShaderPropertyType>("_EmissionMap", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineWidthTexture", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineWidth", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineScaledMaxDistance", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineColor", ShaderPropertyType.Color)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineLightingMix", ShaderPropertyType.Range)
,new KeyValuePair<string, ShaderPropertyType>("_DebugMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_BlendMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineWidthMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineColorMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_CullMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_OutlineCullMode", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_SrcBlend", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_DstBlend", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_ZWrite", ShaderPropertyType.Float)
,new KeyValuePair<string, ShaderPropertyType>("_IsFirstSetup", ShaderPropertyType.Float)
}
}
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4629d794c8969c141a4724e182af082e
timeCreated: 1533041756
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using System.Collections.Generic;
namespace VRM
{
public static partial class VRMPreShaderPropExporter
{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitCutout
{
get
{
return new KeyValuePair<string, ShaderProps>(
"VRM/UnlitCutout",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_Cutoff", ShaderPropertyType.Range)
}
}
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 611b546ea471ad34cb7d94740c63b558
timeCreated: 1533041756
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace VRM
{
public static partial class VRMPreShaderPropExporter
{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTexture
{
get
{
return new KeyValuePair<string, ShaderProps>(
"VRM/UnlitTexture",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
}
}
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 22a8083880389b3498f421e6a5c340d5
timeCreated: 1533041756
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace VRM
{
public static partial class VRMPreShaderPropExporter
{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTransparent
{
get
{
return new KeyValuePair<string, ShaderProps>(
"VRM/UnlitTransparent",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
}
}
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 140d6538826e0eb448929d3e4bb2f1cd
timeCreated: 1533041756
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace VRM
{
public static partial class VRMPreShaderPropExporter
{
[VRMPreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTransparentZWrite
{
get
{
return new KeyValuePair<string, ShaderProps>(
"VRM/UnlitTransparentZWrite",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
}
}
);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 165ec79b7aac1564a850fb3d3d19396e
timeCreated: 1533041756
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: