From f8b965760defd87b4f9b70ecc976be624fefaf3b Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 17 Aug 2021 20:30:01 +0900 Subject: [PATCH] WIP EditorTool --- .../Editor/Components/LookAt/LookAtEditor.cs | 60 ------------ Assets/VRM10/Editor/Components/VRM10Window.cs | 10 -- .../LookAt.meta => EditorTool.meta} | 2 +- .../EditorTool/VRM10ConstraintEditorTool.cs | 47 ++++++++++ .../VRM10ConstraintEditorTool.cs.meta} | 2 +- .../EditorTool/VRM10LookAtEditorTool.cs | 91 +++++++++++++++++++ .../EditorTool/VRM10LookAtEditorTool.cs.meta | 11 +++ .../EditorTool/VRM10SpringBoneEditorTool.cs | 47 ++++++++++ .../VRM10SpringBoneEditorTool.cs.meta | 11 +++ Assets/VRM10/Editor/VRM10.Editor.asmdef | 3 +- 10 files changed, 211 insertions(+), 73 deletions(-) delete mode 100644 Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs rename Assets/VRM10/Editor/{Components/LookAt.meta => EditorTool.meta} (77%) create mode 100644 Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs rename Assets/VRM10/Editor/{Components/LookAt/LookAtEditor.cs.meta => EditorTool/VRM10ConstraintEditorTool.cs.meta} (83%) create mode 100644 Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs create mode 100644 Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs.meta create mode 100644 Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs create mode 100644 Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs.meta diff --git a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs b/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs deleted file mode 100644 index 5a8488792..000000000 --- a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs +++ /dev/null @@ -1,60 +0,0 @@ -using UnityEditor; -using UnityEngine; - -namespace UniVRM10 -{ - public static class LookAtEditor - { - public static void Draw2D(VRM10Controller target) - { - - } - - public static void Draw3D(VRM10Controller target, Transform head) - { - if (target == null) - { - return; - } - if (!target.DrawLookAtGizmo) - { - return; - } - if (head == null) - { - return; - } - - OnSceneGUIOffset(target, head); - if (!Application.isPlaying) - { - // offset - var p = target.Vrm.LookAt.OffsetFromHead; - Handles.Label(head.position, $"fromHead: [{p.x:0.00}, {p.y:0.00}, {p.z:0.00}]"); - } - else - { - target.Vrm.LookAt.OnSceneGUILookAt(head, target.LookAtTargetType, target.Gaze); - } - } - - static void OnSceneGUIOffset(VRM10Controller m_target, Transform head) - { - EditorGUI.BeginChangeCheck(); - - var worldOffset = head.localToWorldMatrix.MultiplyPoint(m_target.Vrm.LookAt.OffsetFromHead); - worldOffset = Handles.PositionHandle(worldOffset, head.rotation); - - Handles.DrawDottedLine(head.position, worldOffset, 5); - Handles.SphereHandleCap(0, head.position, Quaternion.identity, 0.02f, Event.current.type); - Handles.SphereHandleCap(0, worldOffset, Quaternion.identity, 0.02f, Event.current.type); - - if (EditorGUI.EndChangeCheck()) - { - Undo.RecordObject(m_target, "Changed FirstPerson"); - - m_target.Vrm.LookAt.OffsetFromHead = head.worldToLocalMatrix.MultiplyPoint(worldOffset); - } - } - } -} diff --git a/Assets/VRM10/Editor/Components/VRM10Window.cs b/Assets/VRM10/Editor/Components/VRM10Window.cs index 886e0eea7..2b3a3d31c 100644 --- a/Assets/VRM10/Editor/Components/VRM10Window.cs +++ b/Assets/VRM10/Editor/Components/VRM10Window.cs @@ -81,7 +81,6 @@ namespace UniVRM10 enum VRMSceneUI { Constraints, - LookAt, SpringBone, } static VRMSceneUI s_ui = default; @@ -110,11 +109,6 @@ namespace UniVRM10 Tools.hidden = false; break; - case VRMSceneUI.LookAt: - Tools.hidden = true; - LookAtEditor.Draw3D(Root, m_head); - break; - case VRMSceneUI.SpringBone: Tools.hidden = true; SpringBoneEditor.Draw3D(Root, m_so); @@ -169,10 +163,6 @@ namespace UniVRM10 m_scrollView.Draw(this.position.y, DrawConstraints, Repaint); break; - case VRMSceneUI.LookAt: - LookAtEditor.Draw2D(Root); - break; - case VRMSceneUI.SpringBone: SpringBoneEditor.Draw2D(Root, m_so); break; diff --git a/Assets/VRM10/Editor/Components/LookAt.meta b/Assets/VRM10/Editor/EditorTool.meta similarity index 77% rename from Assets/VRM10/Editor/Components/LookAt.meta rename to Assets/VRM10/Editor/EditorTool.meta index a9e2d423a..d950d90b8 100644 --- a/Assets/VRM10/Editor/Components/LookAt.meta +++ b/Assets/VRM10/Editor/EditorTool.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 8823eaceb68051c43be15ea92028d412 +guid: af5da07aebd9a47428d49b56b245b095 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs new file mode 100644 index 000000000..4ead870f0 --- /dev/null +++ b/Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs @@ -0,0 +1,47 @@ +using System; +using UnityEngine; +using UnityEditor; +using UnityEditor.EditorTools; + + +[EditorTool("vrm-1.0/Constraint", typeof(UniVRM10.VRM10Controller))] +class VRM10ConstraintEditorTool : EditorTool +{ + static GUIContent s_cachedIcon; + // NOTE: as were caching this, unity will serialize it between compiles! so if we want to test out new looks, + // just return the new GUIContent and bypass the cache until were happy with the icon... + public override GUIContent toolbarIcon + { + get + { + if (s_cachedIcon == null) + { + s_cachedIcon = EditorGUIUtility.IconContent("RelativeJoint2D Icon", "|vrm-1.0 Constraint"); + } + return s_cachedIcon; + } + } + + // This is called for each window that your tool is active in. Put the functionality of your tool here. + public override void OnToolGUI(EditorWindow window) + { + EditorGUI.BeginChangeCheck(); + + Vector3 position = Tools.handlePosition; + + using (new Handles.DrawingScope(Color.green)) + { + position = Handles.Slider(position, Vector3.right); + } + + if (EditorGUI.EndChangeCheck()) + { + Vector3 delta = position - Tools.handlePosition; + + Undo.RecordObjects(Selection.transforms, "Move Platform"); + + foreach (var transform in Selection.transforms) + transform.position += delta; + } + } +} \ No newline at end of file diff --git a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs.meta b/Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs.meta similarity index 83% rename from Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs.meta rename to Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs.meta index 06c907abd..7a7ffaf74 100644 --- a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs.meta +++ b/Assets/VRM10/Editor/EditorTool/VRM10ConstraintEditorTool.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 0f9ee1d04c43d8048a51e032b19a8ebd +guid: 2ba15b8483408784a85713de9f2a4120 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs new file mode 100644 index 000000000..701b16da0 --- /dev/null +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs @@ -0,0 +1,91 @@ +using System; +using UnityEngine; +using UnityEditor; +using UnityEditor.EditorTools; + + +namespace UniVRM10 +{ + [EditorTool("vrm-1.0/LookAt", typeof(UniVRM10.VRM10Controller))] + class VRM10LookAtEditorTool : EditorTool + { + static GUIContent s_cachedIcon; + public override GUIContent toolbarIcon + { + get + { + if (s_cachedIcon == null) + { + s_cachedIcon = EditorGUIUtility.IconContent("d_BillboardRenderer Icon", "|vrm-1.0 LookAt"); + } + return s_cachedIcon; + } + } + + void OnEnable() + { + EditorTools.activeToolChanged += ActiveToolDidChange; + } + + void OnDisable() + { + EditorTools.activeToolChanged -= ActiveToolDidChange; + } + + void ActiveToolDidChange() + { + if (!EditorTools.IsActiveTool(this)) + { + return; + } + } + + public override void OnToolGUI(EditorWindow window) + { + var root = Selection.activeTransform.GetComponent(); + if (root == null) + { + return; + } + if (!root.DrawLookAtGizmo) + { + return; + } + var humanoid = root.GetComponent(); + var head = humanoid.Head; + if (head == null) + { + return; + } + + { + EditorGUI.BeginChangeCheck(); + + var worldOffset = head.localToWorldMatrix.MultiplyPoint(root.Vrm.LookAt.OffsetFromHead); + worldOffset = Handles.PositionHandle(worldOffset, head.rotation); + + Handles.DrawDottedLine(head.position, worldOffset, 5); + Handles.SphereHandleCap(0, head.position, Quaternion.identity, 0.02f, Event.current.type); + Handles.SphereHandleCap(0, worldOffset, Quaternion.identity, 0.02f, Event.current.type); + + if (EditorGUI.EndChangeCheck()) + { + Undo.RecordObject(root.Vrm, "LookAt.OffsetFromHead"); + + root.Vrm.LookAt.OffsetFromHead = head.worldToLocalMatrix.MultiplyPoint(worldOffset); + } + } + + if (!Application.isPlaying) + { + // offset + var p = root.Vrm.LookAt.OffsetFromHead; + Handles.Label(head.position, $"fromHead: [{p.x:0.00}, {p.y:0.00}, {p.z:0.00}]"); + } + else + { + root.Vrm.LookAt.OnSceneGUILookAt(head, root.LookAtTargetType, root.Gaze); + } + } + } +} diff --git a/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs.meta b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs.meta new file mode 100644 index 000000000..9f4ef9e90 --- /dev/null +++ b/Assets/VRM10/Editor/EditorTool/VRM10LookAtEditorTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a780c0aef1cdb9a4895e352e39ad40b9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs b/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs new file mode 100644 index 000000000..448091e7b --- /dev/null +++ b/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs @@ -0,0 +1,47 @@ +using System; +using UnityEngine; +using UnityEditor; +using UnityEditor.EditorTools; + + +[EditorTool("vrm-1.0/SpringBone", typeof(UniVRM10.VRM10Controller))] +class VRM10SpringBoneEditorTool : EditorTool +{ + static GUIContent s_cachedIcon; + // NOTE: as were caching this, unity will serialize it between compiles! so if we want to test out new looks, + // just return the new GUIContent and bypass the cache until were happy with the icon... + public override GUIContent toolbarIcon + { + get + { + if (s_cachedIcon == null) + { + s_cachedIcon = EditorGUIUtility.IconContent("SpringJoint Icon", "|vrm-1.0 SpringBone"); + } + return s_cachedIcon; + } + } + + // This is called for each window that your tool is active in. Put the functionality of your tool here. + public override void OnToolGUI(EditorWindow window) + { + EditorGUI.BeginChangeCheck(); + + Vector3 position = Tools.handlePosition; + + using (new Handles.DrawingScope(Color.green)) + { + position = Handles.Slider(position, Vector3.right); + } + + if (EditorGUI.EndChangeCheck()) + { + Vector3 delta = position - Tools.handlePosition; + + Undo.RecordObjects(Selection.transforms, "Move Platform"); + + foreach (var transform in Selection.transforms) + transform.position += delta; + } + } +} \ No newline at end of file diff --git a/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs.meta b/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs.meta new file mode 100644 index 000000000..b9441e127 --- /dev/null +++ b/Assets/VRM10/Editor/EditorTool/VRM10SpringBoneEditorTool.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2df947254482d5b4d9b07c268a72db5d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/VRM10.Editor.asmdef b/Assets/VRM10/Editor/VRM10.Editor.asmdef index de8ec9fe7..1e347d844 100644 --- a/Assets/VRM10/Editor/VRM10.Editor.asmdef +++ b/Assets/VRM10/Editor/VRM10.Editor.asmdef @@ -6,7 +6,8 @@ "GUID:5f875fdc81c40184c8333b9d63c6ddd5", "GUID:8d76e605759c3f64a957d63ef96ada7c", "GUID:da3e51d19d51a544fa14d43fee843098", - "GUID:7da8a75dcade2144aab699032d7d7987" + "GUID:7da8a75dcade2144aab699032d7d7987", + "GUID:b7aa47b240b57de44a4b2021c143c9bf" ], "includePlatforms": [ "Editor"