Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.0] constraitn の実装 #952

Merged
merged 37 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
20431da
RotationOffset
ousttrue May 7, 2021
d883671
VRM10RotationConstraintEditor
ousttrue Apr 27, 2021
60737fb
Clamp180
ousttrue Apr 27, 2021
9dd7071
DrawDstLocal
ousttrue Apr 27, 2021
bb9d913
Coords
ousttrue Apr 28, 2021
e7fe9af
SrcDrawCurrent, DstDrawCurrent
ousttrue Apr 28, 2021
864415f
impl DstDrawModelCoords
ousttrue Apr 28, 2021
8dd9ef6
rename Write to Draw
ousttrue May 10, 2021
43fc06c
TR
ousttrue May 10, 2021
22b3613
refactoring
ousttrue May 10, 2021
c806dc6
GetSourceCurrent
ousttrue May 10, 2021
46add88
fix GetSourceCurrent local
ousttrue May 10, 2021
b3dd5dc
fix operator*
ousttrue May 10, 2021
2469d65
IVRM10SourceDestination
ousttrue May 13, 2021
6192092
VRM10PositionConstraintEditor
ousttrue May 13, 2021
720bb3e
WIP aim
ousttrue May 13, 2021
673aac4
DrawAimUp
ousttrue May 13, 2021
ce8e0bf
CalcYawPitch
ousttrue May 14, 2021
12e0cc0
WIP aim
ousttrue May 14, 2021
f95123e
euler
ousttrue May 14, 2021
a1af1a9
CalcYawPitch
ousttrue May 14, 2021
79465de
impl rotationConstraint SourceOffset
ousttrue May 17, 2021
1e80c61
impl rotationConstraint DestinationOffset
ousttrue May 17, 2021
2538466
position/rotation constraint の共通部分を VRM10PostionRotationConstraintEdi…
ousttrue May 17, 2021
cc3568d
Inverse
ousttrue May 17, 2021
da4ae3f
UpdateDelta
ousttrue May 17, 2021
c2641b2
Delta
ousttrue May 17, 2021
cf1b684
fix
ousttrue May 17, 2021
962ee32
ApplyDelta
ousttrue May 17, 2021
51d5144
remove IVRM10ConstraintSourceDestinationExtensions
ousttrue May 18, 2021
b99b66f
GetSourceCoords
ousttrue May 18, 2021
a6f4e1c
GetDstCoords
ousttrue May 18, 2021
607d0e0
fix postition and rotation
ousttrue May 18, 2021
2bb6d10
rename
ousttrue May 18, 2021
9a2fb09
fix aim logic
ousttrue May 18, 2021
f1e0a2f
fix
ousttrue May 18, 2021
6d82fc1
桁数
ousttrue May 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/VRM10/Editor/Components/Constraint.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Assets/VRM10/Editor/Components/Constraint/TRExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
public static class TRExtensions
{
public static void Draw(this TR tr, float size)
{
Handles.matrix = tr.TRS(size);

// Handles.CubeHandleCap(0, Vector3.zero, Quaternion.identity, size, EventType.Repaint);
Handles.color = Color.red;
Handles.DrawLine(Vector3.zero, Vector3.right);
Handles.color = Color.green;
Handles.DrawLine(Vector3.zero, Vector3.up);
Handles.color = Color.blue;
Handles.DrawLine(Vector3.zero, Vector3.forward);

Handles.color = Color.white;
// xy
Handles.DrawLine(Vector3.right + Vector3.up, Vector3.right);
Handles.DrawLine(Vector3.right + Vector3.up, Vector3.up);
// yz
Handles.DrawLine(Vector3.up + Vector3.forward, Vector3.forward);
Handles.DrawLine(Vector3.up + Vector3.forward, Vector3.up);
// zx
Handles.DrawLine(Vector3.forward + Vector3.right, Vector3.forward);
Handles.DrawLine(Vector3.forward + Vector3.right, Vector3.right);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
[CustomEditor(typeof(VRM10AimConstraint))]
public class VRM10AimConstraintEditor : Editor
{
VRM10AimConstraint m_target;

void OnEnable()
{
m_target = (VRM10AimConstraint)target;
}

static GUIStyle s_style;
static GUIStyle Style
{
get
{
if (s_style == null)
{
s_style = new GUIStyle("box");
}
return s_style;
}
}

static void DrawAimUp(Quaternion rot, Vector3 pos, Color c)
{
Handles.matrix = Matrix4x4.identity;
Handles.color = c;
// aim
var aim = pos + rot * Vector3.forward * 0.3f;
Handles.DrawLine(pos, aim);
Handles.Label(aim, "aim");
// up
var up = pos + rot * Vector3.up * 0.3f;
Handles.DrawLine(pos, up);
Handles.Label(up, "up");
}

public void OnSceneGUI()
{
if (m_target.Source == null)
{
return;
}

// this to target line
Handles.color = Color.yellow;
Handles.DrawLine(m_target.Source.position, m_target.transform.position);

var pos = m_target.transform.position;

var pr = m_target.ParentRotation;

if (m_target.Logic == null)
{
EditorGUI.BeginChangeCheck();
TR.FromWorld(m_target.transform).Draw(0.2f);

Handles.matrix = Matrix4x4.identity;
var rot = Handles.RotationHandle(pr * m_target.DestinationOffset, pos);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(m_target, "Rotated RotateAt Point");
m_target.DestinationOffset = Quaternion.Inverse(pr) * rot;
}

DrawAimUp(rot, pos, Color.yellow);
}
else
{
var init = m_target.ParentRotation * m_target.Logic.InitialLocalRotation;
DrawAimUp(init * m_target.DestinationOffset, m_target.transform.position, Color.yellow);
new TR(init, m_target.transform.position).Draw(0.2f);
DrawAimUp(init * m_target.DestinationOffset * m_target.Delta, m_target.transform.position, Color.magenta);

var sb = new StringBuilder();
sb.AppendLine($"yaw: {m_target.Yaw:0.}");
sb.AppendLine($"pitch: {m_target.Pitch:0.}");
Handles.Label(m_target.transform.position, sb.ToString(), Style);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
[CustomEditor(typeof(VRM10PositionConstraint))]
public class VRM10PositionConstraintEditor : VRM10PositionRotationConstraintEditorBase
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
public abstract class VRM10PositionRotationConstraintEditorBase : Editor
{
VRM10RotationPositionConstraintBase m_target;

void OnEnable()
{
m_target = (VRM10RotationPositionConstraintBase)target;
}

static GUIStyle s_style;
static GUIStyle Style
{
get
{
if (s_style == null)
{
s_style = new GUIStyle("box");
}
return s_style;
}
}

/// <summary>
/// Euler各を +- 180 にクランプする
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
static Vector3 Clamp180(Vector3 v)
{
var x = v.x;
while (x < -180) x += 360;
while (x > 180) x -= 360;
var y = v.y;
while (y < -180) y += 360;
while (y > 180) y -= 360;
var z = v.z;
while (z < -180) z += 360;
while (z > 180) z -= 360;
return new Vector3(x, y, z);
}

public void OnSceneGUI()
{
if (m_target.GetSource() == null)
{
return;
}

// source offset
if (!Application.isPlaying)
{
EditorGUI.BeginChangeCheck();
Quaternion offset = Handles.RotationHandle(m_target.SourceOffset, m_target.GetSource().position);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(m_target.GetComponent(), "source offset");
m_target.SourceOffset = offset;
}
}

// dest offset
if (!Application.isPlaying)
{
EditorGUI.BeginChangeCheck();
Quaternion offset = Handles.RotationHandle(m_target.DestinationOffset, m_target.GetComponent().transform.position);
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(m_target.GetComponent(), "dest offset");
m_target.DestinationOffset = offset;
}
}

// this to target line
Handles.color = Color.yellow;
Handles.DrawLine(m_target.GetSource().position, m_target.GetComponent().transform.position);

var delta = Clamp180(m_target.Delta);

// show source
{
var sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine();
sb.AppendLine($"source: {m_target.SourceCoordinate}");
sb.AppendLine($"{delta.x:0.00}");
sb.AppendLine($"{delta.y:0.00}");
sb.Append($"{delta.z:0.00}");
Handles.Label(m_target.GetSource().position, sb.ToString(), Style);
}

// show dst
{
var sb = new StringBuilder();
sb.AppendLine($"constraint: {m_target.DestinationCoordinate}");
sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.X) ? $"freeze" : $"{delta.x:0.00}");
sb.AppendLine(m_target.FreezeAxes.HasFlag(AxisMask.Y) ? $"freeze" : $"{delta.y:0.00}");
sb.Append(m_target.FreezeAxes.HasFlag(AxisMask.Z) ? $"freeze" : $"{delta.z:0.00}");
Handles.Label(m_target.GetComponent().transform.position, sb.ToString(), Style);
}

m_target.GetSourceCoords().Draw(0.2f);
if (Application.isPlaying)
{
Handles.matrix = m_target.GetSourceCurrent().TRS(0.05f);
Handles.color = Color.yellow;
Handles.DrawWireCube(Vector3.zero, Vector3.one);
}

m_target.GetDstCoords().Draw(0.2f);
if (Application.isPlaying)
{
Handles.matrix = m_target.GetDstCurrent().TRS(0.05f);
Handles.color = Color.yellow;
Handles.DrawWireCube(Vector3.zero, Vector3.one);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text;
using UnityEditor;
using UnityEngine;

namespace UniVRM10
{
[CustomEditor(typeof(VRM10RotationConstraint))]
public class VRM10RotationConstraintEditor : VRM10PositionRotationConstraintEditorBase
{
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions Assets/VRM10/Editor/EnumFlagAttributeDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using UnityEngine;
using UnityEditor;

namespace UniVRM10
{
/// <summary>
/// Flag設定したEnumのインスペクター表示を変えるクラス
/// </summary>
[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public sealed class EnumFlagsAttributeDrawer : PropertyDrawer
{
public override void OnGUI(
Rect position,
SerializedProperty prop,
GUIContent label
)
{
var buttonsIntValue = 0;
var enumLength = prop.enumNames.Length;
var labelWidth = EditorGUIUtility.labelWidth;
var buttonPressed = new bool[enumLength];
var buttonWidth = (position.width - labelWidth) / enumLength;

var labelPos = new Rect(
position.x,
position.y,
labelWidth,
position.height
);
EditorGUI.LabelField(labelPos, label);
EditorGUI.BeginChangeCheck();

for (int i = 0; i < enumLength; i++)
{
buttonPressed[i] = (prop.intValue & (1 << i)) == 1 << i;

var buttonPos = new Rect(
position.x + labelWidth + buttonWidth * i,
position.y,
buttonWidth,
position.height
);

buttonPressed[i] = GUI.Toggle(
buttonPos,
buttonPressed[i],
prop.enumNames[i],
"Button"
);

if (buttonPressed[i])
{
buttonsIntValue += 1 << i;
}
}

if (EditorGUI.EndChangeCheck())
{
prop.intValue = buttonsIntValue;
}
}
}
}
Loading