Runtimeエクスポーター向けのシェーダープロパティの事前定義を分離

This commit is contained in:
ousttrue
2019-02-28 21:30:35 +09:00
parent 24332fa3a0
commit 47e2224e95
46 changed files with 281 additions and 186 deletions

View File

@@ -1,9 +1,8 @@
fileFormatVersion: 2
guid: 3fa98374d2ce76443981cc61ab80e6ce
guid: f877649750df3814aa5272250b22678c
folderAsset: yes
timeCreated: 1533035745
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 36a5a07493511bd49b1ad9dbad2683d2
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,99 @@
using System.Reflection;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;
using UniGLTF.ShaderPropExporter;
using System.Collections.Generic;
namespace UniGLTF
{
public static class ShaderPropMenu
{
#if VRM_DEVELOP
[MenuItem("VRM/ShaderProperty/PreExport ShaderProps")]
#endif
public static void PreExport()
{
foreach (var fi in typeof(PreExportShaders).GetFields(
BindingFlags.Static
| BindingFlags.Public
| BindingFlags.NonPublic))
{
var attr = fi.GetCustomAttributes(true).FirstOrDefault(y => y is PreExportShadersAttribute);
if (attr != null)
{
var supportedShaders = fi.GetValue(null) as SupportedShader[];
foreach (var supported in supportedShaders)
{
PreExport(supported);
}
}
}
}
static string EscapeShaderName(string name)
{
return name.Replace("/", "_").Replace(" ", "_");
}
static string ExportDir
{
get
{
return Application.dataPath + "/VRM/ShaderProperty/Runtime";
}
}
static void PreExport(SupportedShader supportedShader)
{
var shader = Shader.Find(supportedShader.ShaderName);
var props = ShaderProps.FromShader(shader);
var path = Path.Combine(ExportDir, supportedShader.TargetFolder);
path = Path.Combine(path, EscapeShaderName(supportedShader.ShaderName) + ".cs").Replace("\\", "/");
Debug.LogFormat("PreExport: {0}", path);
File.WriteAllText(path, ToString(props, shader.name));
}
static string ToString(ShaderProps props, string shaderName)
{
var list = new List<string>();
foreach (var prop in props.Properties)
{
list.Add(string.Format("new ShaderProperty(\"{0}\", ShaderPropertyType.{1})\r\n", prop.Key, prop.ShaderPropertyType));
}
return string.Format(@"using System.Collections.Generic;
namespace UniGLTF.ShaderPropExporter
{{
public static partial class PreShaderPropExporter
{{
[PreExportShader]
static KeyValuePair<string, ShaderProps> {0}
{{
get
{{
return new KeyValuePair<string, ShaderProps>(
""{1}"",
new ShaderProps
{{
Properties = new ShaderProperty[]{{
{2}
}}
}}
);
}}
}}
}}
}}
"
, EscapeShaderName(shaderName)
, shaderName
, string.Join(",", list.ToArray()));
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5eb12c186c6337e4db278b5f01d47cae
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
{
"name": "ShaderProperty.Editor",
"references": [
"ShaderProperty.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: bc66ece0f33b52446a0830c05781d4db
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dd900b6f302c9404b99d77a1ae3d00be
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d9c97ad7f5bbcac489a47a2f34dfff00
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
namespace UniGLTF.ShaderPropExporter
{
public static partial class PreExportShaders
{
const string GLTF_FOLDER = "GLTF";
#pragma warning disable 414
[PreExportShaders]
static SupportedShader[] SupportedShaders = new SupportedShader[]
{
new SupportedShader(GLTF_FOLDER, "Standard"),
new SupportedShader(GLTF_FOLDER, "Unlit/Color"),
new SupportedShader(GLTF_FOLDER, "Unlit/Texture"),
new SupportedShader(GLTF_FOLDER, "Unlit/Transparent"),
new SupportedShader(GLTF_FOLDER, "Unlit/Transparent Cutout"),
new SupportedShader(GLTF_FOLDER, "UniGLTF/UniUnlit"),
};
#pragma warning restore 414
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 90dcef1978c51e74386b76d77689dc82
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF.ShaderPropExporter
{
public class PreExportShadersAttribute : Attribute { }
public class PreExportShaderAttribute : Attribute { }
public struct SupportedShader
{
public string TargetFolder;
public string ShaderName;
public SupportedShader(string targetFolder, string shaderName)
{
TargetFolder = targetFolder;
ShaderName = shaderName;
}
}
public static partial class PreShaderPropExporter
{
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(PreShaderPropExporter).GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
if (prop.GetCustomAttributes(typeof(PreExportShaderAttribute), 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 props;
}
#if UNITY_EDITOR
// fallback
Debug.LogWarningFormat("{0} is not predefined shader. Use ShaderUtil", shaderName);
var shader = Shader.Find(shaderName);
return ShaderProps.FromShader(shader);
#else
return null;
#endif
}
}
}

View File

@@ -0,0 +1,8 @@
{
"name": "ShaderProperty.Runtime",
"references": [],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 301b251fd9834274c9228e0532f444f7
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -62,50 +62,6 @@ namespace UniGLTF.ShaderPropExporter
Properties = properties.ToArray(),
};
}
static string EscapeShaderName(string name)
{
return name.Replace("/", "_").Replace(" ", "_");
}
public string ToString(string shaderName)
{
var list = new List<string>();
foreach (var prop in Properties)
{
list.Add(string.Format("new ShaderProperty(\"{0}\", ShaderPropertyType.{1})\r\n", prop.Key, prop.ShaderPropertyType));
}
return string.Format(@"using System.Collections.Generic;
namespace UniGLTF.ShaderPropExporter
{{
public static partial class PreShaderPropExporter
{{
[PreExportShader]
static KeyValuePair<string, ShaderProps> {0}
{{
get
{{
return new KeyValuePair<string, ShaderProps>(
""{1}"",
new ShaderProps
{{
Properties = new ShaderProperty[]{{
{2}
}}
}}
);
}}
}}
}}
}}
"
, EscapeShaderName(shaderName)
, shaderName
, String.Join(",", list.ToArray()));
}
#endif
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: eb31f565eeca6164694b06ccfe3bc251
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,8 +1,8 @@
namespace UniGLTF.ShaderPropExporter
{
public static partial class PreShaderPropExporter
public static partial class PreExportShaders
{
const string VRM_TARGET_FOLDER = "VRM/Scripts";
const string VRM_TARGET_FOLDER = "VRM";
[PreExportShaders]
public static SupportedShader[] VRMSupportedShaders = new SupportedShader[]
{

View File

@@ -1,7 +1,8 @@
{
"name": "UniGLTF.Editor",
"references": [
"VRM"
"VRM",
"ShaderProperty.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: f877649750df3814aa5272250b22678c
guid: 892bd9a40a0e26a48b55fcb93590bf46
folderAsset: yes
timeCreated: 1533542717
licenseType: Free

View File

@@ -1,133 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF.ShaderPropExporter
{
public class PreExportShadersAttribute : Attribute { }
public class PreExportShaderAttribute : Attribute { }
public struct SupportedShader
{
public string TargetFolder;
public string ShaderName;
public SupportedShader(string targetFolder, string shaderName)
{
TargetFolder = targetFolder;
ShaderName = shaderName;
}
}
public static partial class PreShaderPropExporter
{
const string TARGET_FOLDER = "UniGLTF/Core/Scripts";
#pragma warning disable 414
[PreExportShaders]
static SupportedShader[] SupportedShaders = new SupportedShader[]
{
new SupportedShader(TARGET_FOLDER, "Standard"),
new SupportedShader(TARGET_FOLDER, "Unlit/Color"),
new SupportedShader(TARGET_FOLDER, "Unlit/Texture"),
new SupportedShader(TARGET_FOLDER, "Unlit/Transparent"),
new SupportedShader(TARGET_FOLDER, "Unlit/Transparent Cutout"),
new SupportedShader(TARGET_FOLDER, "UniGLTF/UniUnlit"),
};
#pragma warning restore 414
#if UNITY_EDITOR
[MenuItem(UniGLTFVersion.MENU + "/PreExport ShaderProps")]
public static void PreExport()
{
foreach (var fi in typeof(PreShaderPropExporter).GetFields(
BindingFlags.Static
| BindingFlags.Public
| BindingFlags.NonPublic))
{
var attr = fi.GetCustomAttributes(true).FirstOrDefault(y => y is PreExportShadersAttribute);
if (attr != null)
{
var supportedShaders = fi.GetValue(null) as SupportedShader[];
foreach (var supported in supportedShaders)
{
PreExport(supported);
}
}
}
}
static string EscapeShaderName(string name)
{
return name.Replace("/", "_").Replace(" ", "_");
}
static UnityPath GetExportDir(string target)
{
foreach (var x in UnityPath.FromUnityPath("Assets").TravserseDir())
{
if (x.Value.EndsWith(target))
{
var dir = x.Child("PreExportShaderProps");
dir.EnsureFolder();
return dir;
}
}
throw new Exception(target + " not found");
}
static void PreExport(SupportedShader supportedShader)
{
var path = GetExportDir(supportedShader.TargetFolder).Child(EscapeShaderName(supportedShader.ShaderName) + ".cs");
Debug.LogFormat("PreExport: {0}", path.FullPath);
var shader = Shader.Find(supportedShader.ShaderName);
var props = ShaderProps.FromShader(shader);
File.WriteAllText(path.FullPath, props.ToString(shader.name));
}
#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(PreShaderPropExporter).GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
{
if (prop.GetCustomAttributes(typeof(PreExportShaderAttribute), 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 props;
}
#if UNITY_EDITOR
// fallback
Debug.LogWarningFormat("{0} is not predefined shader. Use ShaderUtil", shaderName);
var shader = Shader.Find(shaderName);
return ShaderProps.FromShader(shader);
#else
return null;
#endif
}
#endregion
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d9c97ad7f5bbcac489a47a2f34dfff00
guid: 98bc58647c431d04f8633bd7341c862b
folderAsset: yes
timeCreated: 1533538930
licenseType: Free

View File

@@ -5,7 +5,8 @@
"DepthFirstScheduler",
"UniUnlit",
"UniHumanoid",
"UniJSON"
"UniJSON",
"ShaderProperty.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [],