diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs index 5d690a094..a77ad917f 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs @@ -204,7 +204,7 @@ namespace VRM // materials foreach (var m in exporter.Materials) { - gltf.extensions.VRM.materialProperties.Add(glTF_VRM_Material.CreateFromMaterial(m, exporter.TextureManager.Textures)); + gltf.extensions.VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, exporter.TextureManager.Textures)); } } } diff --git a/Assets/VRM/UniVRM/Scripts/Format/VRMMaterialExporter.cs b/Assets/VRM/UniVRM/Scripts/Format/VRMMaterialExporter.cs index ce1bbeda8..06817d91d 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/VRMMaterialExporter.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/VRMMaterialExporter.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.Linq; using UniGLTF; +using UniGLTF.ShaderPropExporter; using UnityEngine; @@ -96,5 +99,115 @@ namespace VRM return material; } + + #region CreateFromMaterial + private static readonly string[] VRMExtensionShaders = new string[] + { + "VRM/UnlitTransparentZWrite", + "VRM/MToon" + }; + + static readonly string[] TAGS = new string[]{ + "RenderType", + // "Queue", + }; + + public static glTF_VRM_Material CreateFromMaterial(Material m, List textures) + { + var material = new glTF_VRM_Material + { + name = m.name, + shader = m.shader.name, + renderQueue = m.renderQueue, + }; + + if (!VRMExtensionShaders.Contains(m.shader.name)) + { + material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER; + return material; + } + + var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); + if (prop == null) + { + Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name); + } + else + { + foreach (var keyword in m.shaderKeywords) + { + material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword)); + } + + // get properties + //material.SetProp(prop); + foreach (var kv in prop.Properties) + { + switch (kv.ShaderPropertyType) + { + case ShaderPropertyType.Color: + { + var value = m.GetColor(kv.Key).ToArray(); + material.vectorProperties.Add(kv.Key, value); + } + break; + + case ShaderPropertyType.Range: + case ShaderPropertyType.Float: + { + var value = m.GetFloat(kv.Key); + material.floatProperties.Add(kv.Key, value); + } + break; + + case 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 ShaderPropertyType.Vector: + { + var value = m.GetVector(kv.Key).ToArray(); + material.vectorProperties.Add(kv.Key, value); + } + break; + + default: + throw new NotImplementedException(); + } + } + } + + foreach (var tag in TAGS) + { + var value = m.GetTag(tag, false); + if (!String.IsNullOrEmpty(value)) + { + material.tagMap.Add(tag, value); + } + } + + return material; + } + #endregion } } diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs new file mode 100644 index 000000000..d88e5adc9 --- /dev/null +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs @@ -0,0 +1,64 @@ +using System.Linq; +using System.Collections.Generic; +using UniGLTF; +using UnityEngine; + + +namespace VRM +{ + public static class glTF_VRMExtensions + { + public static glTF_VRM_BlendShapeBind Cerate(Transform root, List meshes, BlendShapeBinding binding) + { + var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath); + var renderer = transform.GetComponent(); + var mesh = renderer.sharedMesh; + var meshIndex = meshes.IndexOf(mesh); + + return new glTF_VRM_BlendShapeBind + { + mesh = meshIndex, + index = binding.Index, + weight = binding.Weight, + }; + } + + public static void Add(this glTF_VRM_BlendShapeMaster master, + BlendShapeClip clip, Transform transform, List meshes) + { + var list = new List(); + if (clip.Values != null) + { + list.AddRange(clip.Values.Select(y => Cerate(transform, meshes.ToList(), y))); + } + + var materialList = new List(); + if (clip.MaterialValues != null) + { + materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind + { + materialName = y.MaterialName, + propertyName = y.ValueName, + targetValue = y.TargetValue.ToArray(), + })); + } + + var group = new glTF_VRM_BlendShapeGroup + { + name = clip.BlendShapeName, + presetName = clip.Preset.ToString().ToLower(), + isBinary = clip.IsBinary, + binds = list, + materialValues = materialList, + }; + master.blendShapeGroups.Add(group); + } + + public static void Apply(this glTF_VRM_DegreeMap map, CurveMapper mapper) + { + map.curve = mapper.Curve.keys.SelectMany(x => new float[] { x.time, x.value, x.inTangent, x.outTangent }).ToArray(); + map.xRange = mapper.CurveXRangeDegree; + map.yRange = mapper.CurveYRangeDegree; + } + } +} diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs.meta b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs.meta new file mode 100644 index 000000000..9291c8bdc --- /dev/null +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRMExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5fbd5248b4a422e47be4510ca85c9ade +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_BlendShape.cs b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_BlendShape.cs index 2e8cbbe57..80e859660 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_BlendShape.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_BlendShape.cs @@ -1,10 +1,9 @@ using System; -using System.Linq; using System.Collections.Generic; using UniGLTF; -using UnityEngine; using UniJSON; + namespace VRM { [Serializable] @@ -42,21 +41,6 @@ namespace VRM f.KeyValue(() => index); f.KeyValue(() => weight); } - - public static glTF_VRM_BlendShapeBind Cerate(Transform root, List meshes, BlendShapeBinding binding) - { - var transform = UniGLTF.UnityExtensions.GetFromPath(root.transform, binding.RelativePath); - var renderer = transform.GetComponent(); - var mesh = renderer.sharedMesh; - var meshIndex = meshes.IndexOf(mesh); - - return new glTF_VRM_BlendShapeBind - { - mesh = meshIndex, - index = binding.Index, - weight = binding.Weight, - }; - } } public enum BlendShapePreset @@ -143,35 +127,6 @@ namespace VRM { public List blendShapeGroups = new List(); - public void Add(BlendShapeClip clip, Transform transform, List meshes) - { - var list = new List(); - if (clip.Values != null) - { - list.AddRange(clip.Values.Select(y => glTF_VRM_BlendShapeBind.Cerate(transform, meshes.ToList(), y))); - } - - var materialList = new List(); - if (clip.MaterialValues != null) - { - materialList.AddRange(clip.MaterialValues.Select(y => new glTF_VRM_MaterialValueBind - { - materialName = y.MaterialName, - propertyName = y.ValueName, - targetValue = y.TargetValue.ToArray(), - })); - } - - var group = new glTF_VRM_BlendShapeGroup - { - name = clip.BlendShapeName, - presetName = clip.Preset.ToString().ToLower(), - isBinary = clip.IsBinary, - binds = list, - materialValues = materialList, - }; - blendShapeGroups.Add(group); - } protected override void SerializeMembers(GLTFJsonFormatter f) { diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_FirstPerson.cs b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_FirstPerson.cs index 2ed9cc4dd..57bcf85a0 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_FirstPerson.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_FirstPerson.cs @@ -1,10 +1,10 @@ using System; -using System.Linq; using System.Collections.Generic; using UniGLTF; using UnityEngine; using UniJSON; + namespace VRM { [Serializable] @@ -29,13 +29,6 @@ namespace VRM f.KeyValue(() => xRange); f.KeyValue(() => yRange); } - - public void Apply(CurveMapper mapper) - { - curve = mapper.Curve.keys.SelectMany(x => new float[] { x.time, x.value, x.inTangent, x.outTangent }).ToArray(); - xRange = mapper.CurveXRangeDegree; - yRange = mapper.CurveYRangeDegree; - } } public enum FirstPersonFlag diff --git a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_Material.cs b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_Material.cs index 35fed6777..726cd58df 100644 --- a/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_Material.cs +++ b/Assets/VRM/UniVRM/Scripts/Format/glTF_VRM_Material.cs @@ -2,9 +2,8 @@ using System.Collections.Generic; using System.Linq; using UniGLTF; -using UnityEngine; using UniJSON; -using UniGLTF.ShaderPropExporter; + namespace VRM { @@ -22,18 +21,7 @@ namespace VRM public Dictionary keywordMap = new Dictionary(); public Dictionary tagMap = new Dictionary(); - static readonly string[] TAGS = new string[]{ - "RenderType", - // "Queue", - }; - - private static readonly string[] VRMExtensionShaders = new string[] - { - "VRM/UnlitTransparentZWrite", - "VRM/MToon" - }; - - private static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER"; + public static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER"; protected override void SerializeMembers(GLTFJsonFormatter f) { @@ -116,102 +104,5 @@ namespace VRM } return materials; } - - public static glTF_VRM_Material CreateFromMaterial(Material m, List textures) - { - var material = new glTF_VRM_Material - { - name = m.name, - shader = m.shader.name, - renderQueue = m.renderQueue, - }; - - if (!VRMExtensionShaders.Contains(m.shader.name)) - { - material.shader = VRM_USE_GLTFSHADER; - return material; - } - - var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name); - if (prop == null) - { - Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name); - } - else - { - foreach (var keyword in m.shaderKeywords) - { - material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword)); - } - - // get properties - //material.SetProp(prop); - foreach (var kv in prop.Properties) - { - switch (kv.ShaderPropertyType) - { - case ShaderPropertyType.Color: - { - var value = m.GetColor(kv.Key).ToArray(); - material.vectorProperties.Add(kv.Key, value); - } - break; - - case ShaderPropertyType.Range: - case ShaderPropertyType.Float: - { - var value = m.GetFloat(kv.Key); - material.floatProperties.Add(kv.Key, value); - } - break; - - case 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 ShaderPropertyType.Vector: - { - var value = m.GetVector(kv.Key).ToArray(); - material.vectorProperties.Add(kv.Key, value); - } - break; - - default: - throw new NotImplementedException(); - } - } - } - - foreach (var tag in TAGS) - { - var value = m.GetTag(tag, false); - if (!String.IsNullOrEmpty(value)) - { - material.tagMap.Add(tag, value); - } - } - - return material; - } } }