From c90c98e443dff61d8400c39852ef0cdaf509a093 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 25 Aug 2020 18:49:50 +0900 Subject: [PATCH] BindposeGizmo.cs --- Assets/MeshUtility/Runtime.meta | 8 + .../Runtime/BindposeGizmo.cs} | 6 +- .../Runtime/BindposeGizmo.cs.meta} | 0 Assets/MeshUtility/Runtime/MeshUtility.asmdef | 3 + .../Runtime/MeshUtility.asmdef.meta | 7 + Assets/MeshUtility/Runtime/UnityExtensions.cs | 319 ++++++++++++++++++ .../Runtime/UnityExtensions.cs.meta | 11 + .../Extensions/UnityExtensions.cs.meta | 4 +- ProjectSettings/ProjectVersion.txt | 2 +- 9 files changed, 353 insertions(+), 7 deletions(-) create mode 100644 Assets/MeshUtility/Runtime.meta rename Assets/{VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs => MeshUtility/Runtime/BindposeGizmo.cs} (98%) rename Assets/{VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs.meta => MeshUtility/Runtime/BindposeGizmo.cs.meta} (100%) create mode 100644 Assets/MeshUtility/Runtime/MeshUtility.asmdef create mode 100644 Assets/MeshUtility/Runtime/MeshUtility.asmdef.meta create mode 100644 Assets/MeshUtility/Runtime/UnityExtensions.cs create mode 100644 Assets/MeshUtility/Runtime/UnityExtensions.cs.meta diff --git a/Assets/MeshUtility/Runtime.meta b/Assets/MeshUtility/Runtime.meta new file mode 100644 index 000000000..3b08e9593 --- /dev/null +++ b/Assets/MeshUtility/Runtime.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fc6ddf08077eac64fb9e33738e1c314d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs b/Assets/MeshUtility/Runtime/BindposeGizmo.cs similarity index 98% rename from Assets/VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs rename to Assets/MeshUtility/Runtime/BindposeGizmo.cs index e882d73b9..23e233e5e 100644 --- a/Assets/VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs +++ b/Assets/MeshUtility/Runtime/BindposeGizmo.cs @@ -1,16 +1,16 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; -using UniGLTF; +// using UniGLTF; #if UNITY_EDITOR using UnityEditor; #endif -namespace VRM +namespace MeshUtility { [DisallowMultipleComponent] - public class VRMBindposeGizmo : MonoBehaviour + public class BindposeGizmo : MonoBehaviour { [SerializeField] Mesh m_target; diff --git a/Assets/VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs.meta b/Assets/MeshUtility/Runtime/BindposeGizmo.cs.meta similarity index 100% rename from Assets/VRM/UniVRM/Scripts/SkinnedMeshUtility/VRMBindposeGizmo.cs.meta rename to Assets/MeshUtility/Runtime/BindposeGizmo.cs.meta diff --git a/Assets/MeshUtility/Runtime/MeshUtility.asmdef b/Assets/MeshUtility/Runtime/MeshUtility.asmdef new file mode 100644 index 000000000..f161fcafe --- /dev/null +++ b/Assets/MeshUtility/Runtime/MeshUtility.asmdef @@ -0,0 +1,3 @@ +{ + "name": "MeshUtility" +} diff --git a/Assets/MeshUtility/Runtime/MeshUtility.asmdef.meta b/Assets/MeshUtility/Runtime/MeshUtility.asmdef.meta new file mode 100644 index 000000000..4085faa92 --- /dev/null +++ b/Assets/MeshUtility/Runtime/MeshUtility.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 71ab1919192903d44971eedbc26b24d1 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/MeshUtility/Runtime/UnityExtensions.cs b/Assets/MeshUtility/Runtime/UnityExtensions.cs new file mode 100644 index 000000000..a772980d3 --- /dev/null +++ b/Assets/MeshUtility/Runtime/UnityExtensions.cs @@ -0,0 +1,319 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + + +namespace MeshUtility +{ + public struct PosRot + { + public Vector3 Position; + public Quaternion Rotation; + + public static PosRot FromGlobalTransform(Transform t) + { + return new PosRot + { + Position = t.position, + Rotation = t.rotation, + }; + } + } + + public class BlendShape + { + public string Name; + + public BlendShape(string name) + { + Name = name; + } + + public List Positions = new List(); + public List Normals = new List(); + public List Tangents = new List(); + } + + public static class UnityExtensions + { + public static Vector4 ReverseZ(this Vector4 v) + { + return new Vector4(v.x, v.y, -v.z, v.w); + } + + public static Vector3 ReverseZ(this Vector3 v) + { + return new Vector3(v.x, v.y, -v.z); + } + + [Obsolete] + public static Vector2 ReverseY(this Vector2 v) + { + return new Vector2(v.x, -v.y); + } + + public static Vector2 ReverseUV(this Vector2 v) + { + return new Vector2(v.x, 1.0f - v.y); + } + + public static Quaternion ReverseZ(this Quaternion q) + { + float angle; + Vector3 axis; + q.ToAngleAxis(out angle, out axis); + return Quaternion.AngleAxis(-angle, ReverseZ(axis)); + } + + public static Matrix4x4 Matrix4x4FromColumns(Vector4 c0, Vector4 c1, Vector4 c2, Vector4 c3) + { +#if UNITY_2017_1_OR_NEWER + return new Matrix4x4(c0, c1, c2, c3); +#else + var m = default(Matrix4x4); + m.SetColumn(0, c0); + m.SetColumn(1, c1); + m.SetColumn(2, c2); + m.SetColumn(3, c3); + return m; +#endif + } + + public static Matrix4x4 Matrix4x4FromRotation(Quaternion q) + { +#if UNITY_2017_1_OR_NEWER + return Matrix4x4.Rotate(q); +#else + var m = default(Matrix4x4); + m.SetTRS(Vector3.zero, q, Vector3.one); + return m; +#endif + } + + public static Matrix4x4 ReverseZ(this Matrix4x4 m) + { + m.SetTRS(m.ExtractPosition().ReverseZ(), m.ExtractRotation().ReverseZ(), m.ExtractScale()); + return m; + } + + public static Matrix4x4 MatrixFromArray(float[] values) + { + var m = new Matrix4x4(); + m.m00 = values[0]; + m.m10 = values[1]; + m.m20 = values[2]; + m.m30 = values[3]; + m.m01 = values[4]; + m.m11 = values[5]; + m.m21 = values[6]; + m.m31 = values[7]; + m.m02 = values[8]; + m.m12 = values[9]; + m.m22 = values[10]; + m.m32 = values[11]; + m.m03 = values[12]; + m.m13 = values[13]; + m.m23 = values[14]; + m.m33 = values[15]; + return m; + } + + // https://forum.unity.com/threads/how-to-assign-matrix4x4-to-transform.121966/ + public static Quaternion ExtractRotation(this Matrix4x4 matrix) + { + Vector3 forward; + forward.x = matrix.m02; + forward.y = matrix.m12; + forward.z = matrix.m22; + + Vector3 upwards; + upwards.x = matrix.m01; + upwards.y = matrix.m11; + upwards.z = matrix.m21; + + return Quaternion.LookRotation(forward, upwards); + } + + public static Vector3 ExtractPosition(this Matrix4x4 matrix) + { + Vector3 position; + position.x = matrix.m03; + position.y = matrix.m13; + position.z = matrix.m23; + return position; + } + + public static Vector3 ExtractScale(this Matrix4x4 matrix) + { + Vector3 scale; + scale.x = new Vector4(matrix.m00, matrix.m10, matrix.m20, matrix.m30).magnitude; + scale.y = new Vector4(matrix.m01, matrix.m11, matrix.m21, matrix.m31).magnitude; + scale.z = new Vector4(matrix.m02, matrix.m12, matrix.m22, matrix.m32).magnitude; + return scale; + } + + public static string RelativePathFrom(this Transform self, Transform root) + { + var path = new List(); + for (var current = self; current != null; current = current.parent) + { + if (current == root) + { + return String.Join("/", path.ToArray()); + } + + path.Insert(0, current.name); + } + + throw new Exception("no RelativePath"); + } + + public static Transform GetChildByName(this Transform self, string childName) + { + foreach (Transform child in self) + { + if (child.name == childName) + { + return child; + } + } + + throw new KeyNotFoundException(); + } + + public static Transform GetFromPath(this Transform self, string path) + { + var current = self; + + var split = path.Split('/'); + + foreach (var childName in split) + { + current = current.GetChildByName(childName); + } + + return current; + } + + public static IEnumerable GetChildren(this Transform self) + { + foreach (Transform child in self) + { + yield return child; + } + } + + public static IEnumerable Traverse(this Transform t) + { + yield return t; + foreach (Transform x in t) + { + foreach (Transform y in x.Traverse()) + { + yield return y; + } + } + } + + [Obsolete("Use FindDescendant(name)")] + public static Transform FindDescenedant(this Transform t, string name) + { + return FindDescendant(t, name); + } + + public static Transform FindDescendant(this Transform t, string name) + { + return t.Traverse().First(x => x.name == name); + } + + public static IEnumerable Ancestors(this Transform t) + { + yield return t; + if (t.parent != null) + { + foreach (Transform x in t.parent.Ancestors()) + { + yield return x; + } + } + } + + public static float[] ToArray(this Quaternion q) + { + return new float[] { q.x, q.y, q.z, q.w }; + } + + public static float[] ToArray(this Vector3 v) + { + return new float[] { v.x, v.y, v.z }; + } + + public static float[] ToArray(this Vector4 v) + { + return new float[] { v.x, v.y, v.z, v.w }; + } + + public static float[] ToArray(this Color c) + { + return new float[] { c.r, c.g, c.b, c.a }; + } + + public static void ReverseZRecursive(this Transform root) + { + var globalMap = root.Traverse().ToDictionary(x => x, x => PosRot.FromGlobalTransform(x)); + + foreach (var x in root.Traverse()) + { + x.position = globalMap[x].Position.ReverseZ(); + x.rotation = globalMap[x].Rotation.ReverseZ(); + } + } + + public static Mesh GetSharedMesh(this Transform t) + { + var meshFilter = t.GetComponent(); + if (meshFilter != null) + { + return meshFilter.sharedMesh; + } + + var skinnedMeshRenderer = t.GetComponent(); + if (skinnedMeshRenderer != null) + { + return skinnedMeshRenderer.sharedMesh; + } + + return null; + } + + public static Material[] GetSharedMaterials(this Transform t) + { + var renderer = t.GetComponent(); + if (renderer != null) + { + return renderer.sharedMaterials; + } + + return new Material[] { }; + } + + public static bool Has(this Transform transform, T t) where T : Component + { + return transform.GetComponent() == t; + } + + public static T GetOrAddComponent(this GameObject go) where T : Component + { + var c = go.GetComponent(); + if (c != null) + { + return c; + } + return go.AddComponent(); + } + } +} diff --git a/Assets/MeshUtility/Runtime/UnityExtensions.cs.meta b/Assets/MeshUtility/Runtime/UnityExtensions.cs.meta new file mode 100644 index 000000000..07785c58f --- /dev/null +++ b/Assets/MeshUtility/Runtime/UnityExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5294813527b3278458026afc820dd63d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/UniGLTF/Scripts/Extensions/UnityExtensions.cs.meta b/Assets/VRM/UniGLTF/Scripts/Extensions/UnityExtensions.cs.meta index 062aed465..0421f4246 100644 --- a/Assets/VRM/UniGLTF/Scripts/Extensions/UnityExtensions.cs.meta +++ b/Assets/VRM/UniGLTF/Scripts/Extensions/UnityExtensions.cs.meta @@ -1,7 +1,5 @@ fileFormatVersion: 2 -guid: 5294813527b3278458026afc820dd63d -timeCreated: 1515606586 -licenseType: Free +guid: bbcda9130f35803408b216dbc6be05b7 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/ProjectSettings/ProjectVersion.txt b/ProjectSettings/ProjectVersion.txt index 886d7e558..44c6f16be 100644 --- a/ProjectSettings/ProjectVersion.txt +++ b/ProjectSettings/ProjectVersion.txt @@ -1 +1 @@ -m_EditorVersion: 2018.4.23f1 +m_EditorVersion: 2018.4.25f1