Updated UniGLTF

This commit is contained in:
ousttrue 2018-08-06 17:10:07 +09:00
parent eaa94b802b
commit b709b6ce7b
15 changed files with 87 additions and 239 deletions

View File

@ -0,0 +1,16 @@
namespace UniGLTF.ShaderPropExporter
{
public static partial class PreShaderPropExporter
{
const string VRM_TARGET_FOLDER = "VRM/Scripts";
[PreExportShaders]
public static SupportedShader[] VRMSupportedShaders = new SupportedShader[]
{
new SupportedShader(VRM_TARGET_FOLDER, "VRM/MToon", "_BumpMap"),
new SupportedShader(VRM_TARGET_FOLDER, "VRM/UnlitTexture"),
new SupportedShader(VRM_TARGET_FOLDER, "VRM/UnlitCutout"),
new SupportedShader(VRM_TARGET_FOLDER, "VRM/UnlitTransparent"),
new SupportedShader(VRM_TARGET_FOLDER, "VRM/UnlitTransparentZWrite"),
};
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 50935dd2f9f3fa445a687f30d4dd663b
guid: 625b5ee8b5811dc4a915a2fbb2cb319d
timeCreated: 1533035131
licenseType: Free
MonoImporter:

View File

@ -4,7 +4,7 @@ using System.Linq;
using UniGLTF;
using UnityEngine;
using UniJSON;
using UniGLTF.ShaderPropExporter;
namespace VRM
{
@ -106,13 +106,13 @@ namespace VRM
renderQueue = m.renderQueue,
};
var prop = VRMPreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (prop == null)
{
#if UNITY_EDITOR
// fallback
Debug.LogWarningFormat("Unsupported shader: {0}", m.shader.name);
prop = VRMPreShaderPropExporter.ShaderProps.FromShader(m.shader);
prop = ShaderProps.FromShader(m.shader);
#endif
}
@ -126,24 +126,24 @@ namespace VRM
//material.SetProp(prop);
foreach (var kv in prop.Properties)
{
switch (kv.Value)
switch (kv.ShaderPropertyType)
{
case VRMPreShaderPropExporter.ShaderPropertyType.Color:
case ShaderPropertyType.Color:
{
var value = m.GetColor(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.Range:
case VRMPreShaderPropExporter.ShaderPropertyType.Float:
case ShaderPropertyType.Range:
case ShaderPropertyType.Float:
{
var value = m.GetFloat(kv.Key);
material.floatProperties.Add(kv.Key, value);
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.TexEnv:
case ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(kv.Key);
if (texture != null)
@ -167,7 +167,7 @@ namespace VRM
}
break;
case VRMPreShaderPropExporter.ShaderPropertyType.Vector:
case ShaderPropertyType.Vector:
{
var value = m.GetVector(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);

View File

@ -1,168 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UniGLTF;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
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

@ -1,11 +1,11 @@
using System.Collections.Generic;
namespace VRM
namespace UniGLTF.ShaderPropExporter
{
public static partial class VRMPreShaderPropExporter
public static partial class PreShaderPropExporter
{
[VRMPreExportShader]
[PreExportShader]
static KeyValuePair<string, ShaderProps> VRM_MToon
{
get
@ -14,37 +14,37 @@ namespace VRM
"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)
Properties = new ShaderProperty[]{
new ShaderProperty("_Cutoff", ShaderPropertyType.Range, false)
,new ShaderProperty("_Color", ShaderPropertyType.Color, false)
,new ShaderProperty("_ShadeColor", ShaderPropertyType.Color, false)
,new ShaderProperty("_MainTex", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_ShadeTexture", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_BumpScale", ShaderPropertyType.Float, false)
,new ShaderProperty("_BumpMap", ShaderPropertyType.TexEnv, true)
,new ShaderProperty("_ReceiveShadowRate", ShaderPropertyType.Range, false)
,new ShaderProperty("_ReceiveShadowTexture", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_ShadeShift", ShaderPropertyType.Range, false)
,new ShaderProperty("_ShadeToony", ShaderPropertyType.Range, false)
,new ShaderProperty("_LightColorAttenuation", ShaderPropertyType.Range, false)
,new ShaderProperty("_SphereAdd", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_EmissionColor", ShaderPropertyType.Color, false)
,new ShaderProperty("_EmissionMap", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_OutlineWidthTexture", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_OutlineWidth", ShaderPropertyType.Range, false)
,new ShaderProperty("_OutlineScaledMaxDistance", ShaderPropertyType.Range, false)
,new ShaderProperty("_OutlineColor", ShaderPropertyType.Color, false)
,new ShaderProperty("_OutlineLightingMix", ShaderPropertyType.Range, false)
,new ShaderProperty("_DebugMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_BlendMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_OutlineWidthMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_OutlineColorMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_CullMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_OutlineCullMode", ShaderPropertyType.Float, false)
,new ShaderProperty("_SrcBlend", ShaderPropertyType.Float, false)
,new ShaderProperty("_DstBlend", ShaderPropertyType.Float, false)
,new ShaderProperty("_ZWrite", ShaderPropertyType.Float, false)
,new ShaderProperty("_IsFirstSetup", ShaderPropertyType.Float, false)
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 4629d794c8969c141a4724e182af082e
timeCreated: 1533041756
timeCreated: 1533542890
licenseType: Free
MonoImporter:
serializedVersion: 2

View File

@ -1,11 +1,11 @@
using System.Collections.Generic;
namespace VRM
namespace UniGLTF.ShaderPropExporter
{
public static partial class VRMPreShaderPropExporter
public static partial class PreShaderPropExporter
{
[VRMPreExportShader]
[PreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitCutout
{
get
@ -14,9 +14,9 @@ namespace VRM
"VRM/UnlitCutout",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
,new KeyValuePair<string, ShaderPropertyType>("_Cutoff", ShaderPropertyType.Range)
Properties = new ShaderProperty[]{
new ShaderProperty("_MainTex", ShaderPropertyType.TexEnv, false)
,new ShaderProperty("_Cutoff", ShaderPropertyType.Range, false)
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 611b546ea471ad34cb7d94740c63b558
timeCreated: 1533041756
timeCreated: 1533542890
licenseType: Free
MonoImporter:
serializedVersion: 2

View File

@ -1,11 +1,11 @@
using System.Collections.Generic;
namespace VRM
namespace UniGLTF.ShaderPropExporter
{
public static partial class VRMPreShaderPropExporter
public static partial class PreShaderPropExporter
{
[VRMPreExportShader]
[PreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTexture
{
get
@ -14,8 +14,8 @@ namespace VRM
"VRM/UnlitTexture",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
Properties = new ShaderProperty[]{
new ShaderProperty("_MainTex", ShaderPropertyType.TexEnv, false)
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 22a8083880389b3498f421e6a5c340d5
timeCreated: 1533041756
timeCreated: 1533542890
licenseType: Free
MonoImporter:
serializedVersion: 2

View File

@ -1,11 +1,11 @@
using System.Collections.Generic;
namespace VRM
namespace UniGLTF.ShaderPropExporter
{
public static partial class VRMPreShaderPropExporter
public static partial class PreShaderPropExporter
{
[VRMPreExportShader]
[PreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTransparent
{
get
@ -14,8 +14,8 @@ namespace VRM
"VRM/UnlitTransparent",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
Properties = new ShaderProperty[]{
new ShaderProperty("_MainTex", ShaderPropertyType.TexEnv, false)
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 140d6538826e0eb448929d3e4bb2f1cd
timeCreated: 1533041756
timeCreated: 1533542890
licenseType: Free
MonoImporter:
serializedVersion: 2

View File

@ -1,11 +1,11 @@
using System.Collections.Generic;
namespace VRM
namespace UniGLTF.ShaderPropExporter
{
public static partial class VRMPreShaderPropExporter
public static partial class PreShaderPropExporter
{
[VRMPreExportShader]
[PreExportShader]
static KeyValuePair<string, ShaderProps> VRM_UnlitTransparentZWrite
{
get
@ -14,8 +14,8 @@ namespace VRM
"VRM/UnlitTransparentZWrite",
new ShaderProps
{
Properties = new KeyValuePair<string, ShaderPropertyType>[]{
new KeyValuePair<string, ShaderPropertyType>("_MainTex", ShaderPropertyType.TexEnv)
Properties = new ShaderProperty[]{
new ShaderProperty("_MainTex", ShaderPropertyType.TexEnv, false)
}
}

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 165ec79b7aac1564a850fb3d3d19396e
timeCreated: 1533041756
timeCreated: 1533542890
licenseType: Free
MonoImporter:
serializedVersion: 2

@ -1 +1 @@
Subproject commit dfdfbfe2156bae29bab301b203d94ad087712fc4
Subproject commit c481c92e0bef78ef8920e28343fdbbba5a04fdad