From 90e9766190c904aaff585968a3dd07264aef29fe Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 1 Dec 2020 20:26:34 +0900 Subject: [PATCH] =?UTF-8?q?VRM/Runtime/Format=20=E3=81=8B=E3=82=89=20Unity?= =?UTF-8?q?Engine=20=E4=BE=9D=E5=AD=98=E3=82=92=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/JsonParserExtensions.cs | 19 --- .../Format/UnityEngineCompatibility.cs | 52 ++++++++ .../Format/UnityEngineCompatibility.cs.meta} | 4 +- .../Extensions/VrmHumanoidExtensions.cs | 117 ++++++++++++++++++ .../Extensions/VrmHumanoidExtensions.cs.meta | 11 ++ .../glTF_VRMExtensions.cs | 0 .../glTF_VRMExtensions.cs.meta | 0 .../VRM/Runtime/Format/glTF_VRM_Humanoid.cs | 111 +---------------- .../VRM/Runtime/Format/glTF_VRM_Material.cs | 29 ----- Assets/VRM/Runtime/IO.meta | 8 ++ .../VRM/Runtime/{Format => IO}/VRMExporter.cs | 0 .../{Format => IO}/VRMExporter.cs.meta | 0 .../VRM/Runtime/{Format => IO}/VRMImporter.cs | 0 .../{Format => IO}/VRMImporter.cs.meta | 0 .../{Format => IO}/VRMImporterContext.cs | 2 +- .../{Format => IO}/VRMImporterContext.cs.meta | 0 .../{Format => IO}/VRMMaterialExporter.cs | 0 .../VRMMaterialExporter.cs.meta | 0 .../{Format => IO}/VRMMaterialImporter.cs | 0 .../VRMMaterialImporter.cs.meta | 0 20 files changed, 191 insertions(+), 162 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs rename Assets/UniGLTF/Runtime/{Extensions/JsonParserExtensions.cs.meta => UniGLTF/Format/UnityEngineCompatibility.cs.meta} (71%) create mode 100644 Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs create mode 100644 Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs.meta rename Assets/VRM/Runtime/{Format => Extensions}/glTF_VRMExtensions.cs (100%) rename Assets/VRM/Runtime/{Format => Extensions}/glTF_VRMExtensions.cs.meta (100%) create mode 100644 Assets/VRM/Runtime/IO.meta rename Assets/VRM/Runtime/{Format => IO}/VRMExporter.cs (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMExporter.cs.meta (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMImporter.cs (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMImporter.cs.meta (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMImporterContext.cs (99%) rename Assets/VRM/Runtime/{Format => IO}/VRMImporterContext.cs.meta (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMMaterialExporter.cs (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMMaterialExporter.cs.meta (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMMaterialImporter.cs (100%) rename Assets/VRM/Runtime/{Format => IO}/VRMMaterialImporter.cs.meta (100%) diff --git a/Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs deleted file mode 100644 index 2163a4204..000000000 --- a/Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Linq; -using UnityEngine; -using System.Collections.Generic; - - -namespace UniJSON -{ - public static class JsonParserExtensions - { - public static List DeserializeList(this ListTreeNode jsonList) - { - return jsonList.ArrayItems().Select(x => { - - return JsonUtility.FromJson(new Utf8String(x.Value.Bytes).ToString()); - - }).ToList(); - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs new file mode 100644 index 000000000..3606ed0e3 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs @@ -0,0 +1,52 @@ +/// +/// Unity互換のためのダミー +/// + +#if UNITY_5_6_OR_NEWER +#else +using System; + +namespace UnityEngine +{ + public struct Vector2 + { + public float x; + public float y; + } + + public struct Vector3 : IEquatable + { + public float x; + public float y; + public float z; + + public static Vector3 zero => new Vector3(); + + public bool Equals(Vector3 other) + { + if (x != other.x) return false; + if (y != other.y) return false; + if (z != other.z) return false; + return true; + } + + public static bool operator ==(Vector3 lhs, Vector3 rhs) + { + return lhs.Equals(rhs); + } + + public static bool operator !=(Vector3 lhs, Vector3 rhs) + { + return !(lhs == rhs); + } + } + + public struct Vector4 + { + public float x; + public float y; + public float z; + public float w; + } +} +#endif diff --git a/Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs.meta similarity index 71% rename from Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs.meta index 6aa0b19a6..520967fe6 100644 --- a/Assets/UniGLTF/Runtime/Extensions/JsonParserExtensions.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/UnityEngineCompatibility.cs.meta @@ -1,7 +1,5 @@ fileFormatVersion: 2 -guid: d0a724a200cc85b4887efa87292c6626 -timeCreated: 1515608626 -licenseType: Free +guid: 967f9ca5fbe35cc42b1b5a294958ab41 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs b/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs new file mode 100644 index 000000000..7c8c948e9 --- /dev/null +++ b/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace VRM +{ + public static class VRMBoneExtensions + { + public static VRMBone FromHumanBodyBone(this HumanBodyBones human) + { + return human.ToVrmBone(); + } + + public static HumanBodyBones ToHumanBodyBone(this VRMBone bone) + { +#if UNITY_5_6_OR_NEWER +#else + if (bone == VRMBone.upperChest) + { + return HumanBodyBones.LastBone; + } +#endif + return bone.ToUnityBone(); + + } + } + + public static class VRMHumanoidExtensions + { + public static void SetNodeIndex(this glTF_VRM_Humanoid self, HumanBodyBones _key, int node) + { + var key = _key.FromHumanBodyBone(); + var index = self.humanBones.FindIndex(x => x.vrmBone == key); + if (index == -1 || self.humanBones[index] == null) + { + // not found + self.humanBones.Add(new glTF_VRM_HumanoidBone + { + vrmBone = key, + node = node + }); + } + else + { + self.humanBones[index].node = node; + } + } + + public static void Apply(this glTF_VRM_Humanoid self, UniHumanoid.AvatarDescription desc, List nodes) + { + self.armStretch = desc.armStretch; + self.legStretch = desc.legStretch; + self.upperArmTwist = desc.upperArmTwist; + self.lowerArmTwist = desc.lowerArmTwist; + self.upperLegTwist = desc.upperLegTwist; + self.lowerLegTwist = desc.lowerArmTwist; + self.feetSpacing = desc.feetSpacing; + self.hasTranslationDoF = desc.hasTranslationDoF; + + foreach (var x in desc.human) + { + var key = x.humanBone.FromHumanBodyBone(); + var found = self.humanBones.FirstOrDefault(y => y.vrmBone == key); + if (found == null) + { + found = new glTF_VRM_HumanoidBone + { + vrmBone = key + }; + self.humanBones.Add(found); + } + + found.node = nodes.FindIndex(y => y.name == x.boneName); + + found.useDefaultValues = x.useDefaultValues; + found.axisLength = x.axisLength; + found.center = x.center; + found.max = x.max; + found.min = x.min; + } + } + + public static UniHumanoid.AvatarDescription ToDescription(this glTF_VRM_Humanoid self, List nodes) + { + var description = ScriptableObject.CreateInstance(); + description.upperLegTwist = self.upperLegTwist; + description.lowerLegTwist = self.lowerLegTwist; + description.upperArmTwist = self.upperArmTwist; + description.lowerArmTwist = self.lowerArmTwist; + description.armStretch = self.armStretch; + description.legStretch = self.legStretch; + description.hasTranslationDoF = self.hasTranslationDoF; + + var boneLimits = new UniHumanoid.BoneLimit[self.humanBones.Count]; + int index = 0; + foreach (var x in self.humanBones) + { + if (x.node < 0 || x.node >= nodes.Count) continue; + boneLimits[index] = new UniHumanoid.BoneLimit + { + boneName = nodes[x.node].name, + useDefaultValues = x.useDefaultValues, + axisLength = x.axisLength, + center = x.center, + min = x.min, + max = x.max, + humanBone = x.vrmBone.ToHumanBodyBone(), + }; + index++; + } + + description.human = boneLimits; + + return description; + } + } +} diff --git a/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs.meta b/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs.meta new file mode 100644 index 000000000..bf5b5b8c5 --- /dev/null +++ b/Assets/VRM/Runtime/Extensions/VrmHumanoidExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 158e5a48e96dfce4a92244763bc1d69d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/Runtime/Format/glTF_VRMExtensions.cs b/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs similarity index 100% rename from Assets/VRM/Runtime/Format/glTF_VRMExtensions.cs rename to Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs diff --git a/Assets/VRM/Runtime/Format/glTF_VRMExtensions.cs.meta b/Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/glTF_VRMExtensions.cs.meta rename to Assets/VRM/Runtime/Extensions/glTF_VRMExtensions.cs.meta diff --git a/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs b/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs index a0ee09857..27e1051f7 100644 --- a/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs +++ b/Assets/VRM/Runtime/Format/glTF_VRM_Humanoid.cs @@ -2,10 +2,8 @@ using System.Collections.Generic; using System.Linq; using UniGLTF; -using UniJSON; using UnityEngine; - namespace VRM { public enum VRMBone @@ -69,26 +67,6 @@ namespace VRM unknown, } - public static class VRMBoneExtensions - { - public static VRMBone FromHumanBodyBone(this HumanBodyBones human) - { - return human.ToVrmBone(); - } - - public static HumanBodyBones ToHumanBodyBone(this VRMBone bone) - { -#if UNITY_5_6_OR_NEWER -#else - if (bone == VRMBone.upperChest) - { - return HumanBodyBones.LastBone; - } -#endif - return bone.ToUnityBone(); - } - } - [Serializable] [JsonSchema(Title = "vrm.humanoid.bone")] public class glTF_VRM_HumanoidBone @@ -214,92 +192,5 @@ namespace VRM [JsonSchema(Description = "Unity's HumanDescription.hasTranslationDoF")] public bool hasTranslationDoF = false; - - public void SetNodeIndex(HumanBodyBones _key, int node) - { - var key = _key.FromHumanBodyBone(); - var index = humanBones.FindIndex(x => x.vrmBone == key); - if (index == -1 || humanBones[index] == null) - { - // not found - humanBones.Add(new glTF_VRM_HumanoidBone - { - vrmBone = key, - node = node - }); - } - else - { - humanBones[index].node = node; - } - } - - public void Apply(UniHumanoid.AvatarDescription desc, List nodes) - { - armStretch = desc.armStretch; - legStretch = desc.legStretch; - upperArmTwist = desc.upperArmTwist; - lowerArmTwist = desc.lowerArmTwist; - upperLegTwist = desc.upperLegTwist; - lowerLegTwist = desc.lowerArmTwist; - feetSpacing = desc.feetSpacing; - hasTranslationDoF = desc.hasTranslationDoF; - - foreach (var x in desc.human) - { - var key = x.humanBone.FromHumanBodyBone(); - var found = humanBones.FirstOrDefault(y => y.vrmBone == key); - if (found == null) - { - found = new glTF_VRM_HumanoidBone - { - vrmBone = key - }; - humanBones.Add(found); - } - - found.node = nodes.FindIndex(y => y.name == x.boneName); - - found.useDefaultValues = x.useDefaultValues; - found.axisLength = x.axisLength; - found.center = x.center; - found.max = x.max; - found.min = x.min; - } - } - - public UniHumanoid.AvatarDescription ToDescription(List nodes) - { - var description = ScriptableObject.CreateInstance(); - description.upperLegTwist = upperLegTwist; - description.lowerLegTwist = lowerLegTwist; - description.upperArmTwist = upperArmTwist; - description.lowerArmTwist = lowerArmTwist; - description.armStretch = armStretch; - description.legStretch = legStretch; - description.hasTranslationDoF = hasTranslationDoF; - - var boneLimits = new UniHumanoid.BoneLimit[humanBones.Count]; - int index = 0; - foreach (var x in humanBones) - { - if (x.node < 0 || x.node >= nodes.Count) continue; - boneLimits[index] = new UniHumanoid.BoneLimit - { - boneName = nodes[x.node].name, - useDefaultValues = x.useDefaultValues, - axisLength = x.axisLength, - center = x.center, - min = x.min, - max = x.max, - humanBone = x.vrmBone.ToHumanBodyBone(), - }; - index++; - } - - description.human = boneLimits; - - return description; - } } -} \ No newline at end of file +} diff --git a/Assets/VRM/Runtime/Format/glTF_VRM_Material.cs b/Assets/VRM/Runtime/Format/glTF_VRM_Material.cs index 47288a410..846ab0b82 100644 --- a/Assets/VRM/Runtime/Format/glTF_VRM_Material.cs +++ b/Assets/VRM/Runtime/Format/glTF_VRM_Material.cs @@ -23,39 +23,10 @@ namespace VRM public static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER"; - public static List Parse(string src) - { - var json = JsonParser.Parse(src)["extensions"]["VRM"]["materialProperties"]; - return Parse(json); - } - static Utf8String s_floatProperties = Utf8String.From("floatProperties"); static Utf8String s_vectorProperties = Utf8String.From("vectorProperties"); static Utf8String s_keywordMap = Utf8String.From("keywordMap"); static Utf8String s_tagMap = Utf8String.From("tagMap"); static Utf8String s_textureProperties = Utf8String.From("textureProperties"); - - public static List Parse(ListTreeNode json) - { - var materials = json.DeserializeList(); - var jsonItems = json.ArrayItems().ToArray(); - for (int i = 0; i < materials.Count; ++i) - { - materials[i].floatProperties = - jsonItems[i][s_floatProperties].ObjectItems().ToDictionary(x => x.Key.GetString(), x => x.Value.GetSingle()); - materials[i].vectorProperties = - jsonItems[i][s_vectorProperties].ObjectItems().ToDictionary(x => x.Key.GetString(), x => - { - return x.Value.ArrayItems().Select(y => y.GetSingle()).ToArray(); - }); - materials[i].keywordMap = - jsonItems[i][s_keywordMap].ObjectItems().ToDictionary(x => x.Key.GetString(), x => x.Value.GetBoolean()); - materials[i].tagMap = - jsonItems[i][s_tagMap].ObjectItems().ToDictionary(x => x.Key.GetString(), x => x.Value.GetString()); - materials[i].textureProperties = - jsonItems[i][s_textureProperties].ObjectItems().ToDictionary(x => x.Key.GetString(), x => x.Value.GetInt32()); - } - return materials; - } } } diff --git a/Assets/VRM/Runtime/IO.meta b/Assets/VRM/Runtime/IO.meta new file mode 100644 index 000000000..0c716eb7d --- /dev/null +++ b/Assets/VRM/Runtime/IO.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0a722962c8944b04db7bf2bd5d39bd5c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/Runtime/Format/VRMExporter.cs b/Assets/VRM/Runtime/IO/VRMExporter.cs similarity index 100% rename from Assets/VRM/Runtime/Format/VRMExporter.cs rename to Assets/VRM/Runtime/IO/VRMExporter.cs diff --git a/Assets/VRM/Runtime/Format/VRMExporter.cs.meta b/Assets/VRM/Runtime/IO/VRMExporter.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/VRMExporter.cs.meta rename to Assets/VRM/Runtime/IO/VRMExporter.cs.meta diff --git a/Assets/VRM/Runtime/Format/VRMImporter.cs b/Assets/VRM/Runtime/IO/VRMImporter.cs similarity index 100% rename from Assets/VRM/Runtime/Format/VRMImporter.cs rename to Assets/VRM/Runtime/IO/VRMImporter.cs diff --git a/Assets/VRM/Runtime/Format/VRMImporter.cs.meta b/Assets/VRM/Runtime/IO/VRMImporter.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/VRMImporter.cs.meta rename to Assets/VRM/Runtime/IO/VRMImporter.cs.meta diff --git a/Assets/VRM/Runtime/Format/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs similarity index 99% rename from Assets/VRM/Runtime/Format/VRMImporterContext.cs rename to Assets/VRM/Runtime/IO/VRMImporterContext.cs index 8cc975736..96a8ee827 100644 --- a/Assets/VRM/Runtime/Format/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -50,7 +50,7 @@ namespace VRM } } - SetMaterialImporter(new VRMMaterialImporter(this, glTF_VRM_Material.Parse(Json))); + SetMaterialImporter(new VRMMaterialImporter(this, VRM.materialProperties)); } #region OnLoad diff --git a/Assets/VRM/Runtime/Format/VRMImporterContext.cs.meta b/Assets/VRM/Runtime/IO/VRMImporterContext.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/VRMImporterContext.cs.meta rename to Assets/VRM/Runtime/IO/VRMImporterContext.cs.meta diff --git a/Assets/VRM/Runtime/Format/VRMMaterialExporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs similarity index 100% rename from Assets/VRM/Runtime/Format/VRMMaterialExporter.cs rename to Assets/VRM/Runtime/IO/VRMMaterialExporter.cs diff --git a/Assets/VRM/Runtime/Format/VRMMaterialExporter.cs.meta b/Assets/VRM/Runtime/IO/VRMMaterialExporter.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/VRMMaterialExporter.cs.meta rename to Assets/VRM/Runtime/IO/VRMMaterialExporter.cs.meta diff --git a/Assets/VRM/Runtime/Format/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs similarity index 100% rename from Assets/VRM/Runtime/Format/VRMMaterialImporter.cs rename to Assets/VRM/Runtime/IO/VRMMaterialImporter.cs diff --git a/Assets/VRM/Runtime/Format/VRMMaterialImporter.cs.meta b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs.meta similarity index 100% rename from Assets/VRM/Runtime/Format/VRMMaterialImporter.cs.meta rename to Assets/VRM/Runtime/IO/VRMMaterialImporter.cs.meta