VRM/Runtime/Format から UnityEngine 依存を除去

This commit is contained in:
ousttrue
2020-12-01 20:26:34 +09:00
parent 4ffd97c2e9
commit 90e9766190
20 changed files with 191 additions and 162 deletions

View File

@@ -1,19 +0,0 @@
using System.Linq;
using UnityEngine;
using System.Collections.Generic;
namespace UniJSON
{
public static class JsonParserExtensions
{
public static List<T> DeserializeList<T>(this ListTreeNode<JsonValue> jsonList)
{
return jsonList.ArrayItems().Select(x => {
return JsonUtility.FromJson<T>(new Utf8String(x.Value.Bytes).ToString());
}).ToList();
}
}
}

View File

@@ -0,0 +1,52 @@
/// <summary>
/// Unity互換のためのダミー
/// </summary>
#if UNITY_5_6_OR_NEWER
#else
using System;
namespace UnityEngine
{
public struct Vector2
{
public float x;
public float y;
}
public struct Vector3 : IEquatable<Vector3>
{
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

View File

@@ -1,7 +1,5 @@
fileFormatVersion: 2
guid: d0a724a200cc85b4887efa87292c6626
timeCreated: 1515608626
licenseType: Free
guid: 967f9ca5fbe35cc42b1b5a294958ab41
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -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<Transform> 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<Transform> nodes)
{
var description = ScriptableObject.CreateInstance<UniHumanoid.AvatarDescription>();
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;
}
}
}

View File

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

View File

@@ -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<Transform> 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<Transform> nodes)
{
var description = ScriptableObject.CreateInstance<UniHumanoid.AvatarDescription>();
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;
}
}
}
}

View File

@@ -23,39 +23,10 @@ namespace VRM
public static readonly string VRM_USE_GLTFSHADER = "VRM_USE_GLTFSHADER";
public static List<glTF_VRM_Material> 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<glTF_VRM_Material> Parse(ListTreeNode<JsonValue> json)
{
var materials = json.DeserializeList<glTF_VRM_Material>();
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;
}
}
}

View File

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

View File

@@ -50,7 +50,7 @@ namespace VRM
}
}
SetMaterialImporter(new VRMMaterialImporter(this, glTF_VRM_Material.Parse(Json)));
SetMaterialImporter(new VRMMaterialImporter(this, VRM.materialProperties));
}
#region OnLoad