WIP EditorTool

This commit is contained in:
ousttrue
2021-08-17 20:30:01 +09:00
parent 2e50a09cc8
commit f8b965760d
10 changed files with 211 additions and 73 deletions

View File

@@ -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);
}
}
}
}

View File

@@ -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;

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 8823eaceb68051c43be15ea92028d412
guid: af5da07aebd9a47428d49b56b245b095
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -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;
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 0f9ee1d04c43d8048a51e032b19a8ebd
guid: 2ba15b8483408784a85713de9f2a4120
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -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<VRM10Controller>();
if (root == null)
{
return;
}
if (!root.DrawLookAtGizmo)
{
return;
}
var humanoid = root.GetComponent<UniHumanoid.Humanoid>();
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);
}
}
}
}

View File

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

View File

@@ -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;
}
}
}

View File

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

View File

@@ -6,7 +6,8 @@
"GUID:5f875fdc81c40184c8333b9d63c6ddd5",
"GUID:8d76e605759c3f64a957d63ef96ada7c",
"GUID:da3e51d19d51a544fa14d43fee843098",
"GUID:7da8a75dcade2144aab699032d7d7987"
"GUID:7da8a75dcade2144aab699032d7d7987",
"GUID:b7aa47b240b57de44a4b2021c143c9bf"
],
"includePlatforms": [
"Editor"