add Physics support
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using RootMotion;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
[CustomEditor(typeof(BipedRagdollCreator))]
|
||||
public class BipedRagdollCreatorInspector : Editor {
|
||||
|
||||
public BipedRagdollCreator script { get { return target as BipedRagdollCreator; }}
|
||||
|
||||
private GUIStyle miniLabelStyle = new GUIStyle();
|
||||
|
||||
void OnEnable() {
|
||||
if (script == null) return;
|
||||
//if (Application.isPlaying) return;
|
||||
|
||||
// Autodetection
|
||||
if (script.references.IsEmpty(false)) {
|
||||
Animator animator = script.gameObject.GetComponent<Animator>();
|
||||
|
||||
if (animator == null && script.references.root != null) {
|
||||
animator = script.references.root.GetComponentInChildren<Animator>();
|
||||
if (animator == null) animator = GetAnimatorInParents(script.references.root);
|
||||
}
|
||||
|
||||
if (animator != null) {
|
||||
script.references = BipedRagdollReferences.FromAvatar(animator);
|
||||
|
||||
} else {
|
||||
|
||||
BipedReferences r = new BipedReferences();
|
||||
BipedReferences.AutoDetectReferences(ref r, script.transform, BipedReferences.AutoDetectParams.Default);
|
||||
if (r.isFilled) script.references = BipedRagdollReferences.FromBipedReferences(r);
|
||||
}
|
||||
|
||||
if (!OnRoot()) {
|
||||
Debug.LogWarning("BipedRagdollCreator must be added to the root of the character. Destroying the component.");
|
||||
DestroyImmediate(script);
|
||||
return;
|
||||
}
|
||||
|
||||
string msg = string.Empty;
|
||||
if (script.references.IsValid(ref msg)) {
|
||||
script.options = BipedRagdollCreator.AutodetectOptions(script.references);
|
||||
//BipedRagdollCreator.Create(script.references, script.options);
|
||||
|
||||
//if (animator != null) DestroyImmediate(animator);
|
||||
//if (script.GetComponent<Animation>() != null) DestroyImmediate(script.GetComponent<Animation>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool OnRoot() {
|
||||
if (script.references.Contains(script.transform, true)) return false;
|
||||
if (script.references.root != null) {
|
||||
var bipedRagdollCreatorOnChild = script.references.root.GetComponentInChildren<BipedRagdollCreator>();
|
||||
if (bipedRagdollCreatorOnChild != null && bipedRagdollCreatorOnChild.transform != script.references.root) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
miniLabelStyle.wordWrap = true;
|
||||
miniLabelStyle.fontSize = 9;
|
||||
miniLabelStyle.normal.textColor = EditorStyles.miniLabel.normal.textColor;
|
||||
|
||||
serializedObject.Update();
|
||||
|
||||
//if (Application.isPlaying) {
|
||||
// GUILayout.BeginVertical("Box");
|
||||
// GUILayout.Label("Can not edit ragdolls in play mode.");
|
||||
// GUILayout.EndVertical();
|
||||
// return;
|
||||
//}
|
||||
|
||||
GUI.changed = false;
|
||||
GUILayout.Space(10);
|
||||
EditorGUILayout.BeginVertical("Box");
|
||||
int indent = EditorGUI.indentLevel;
|
||||
EditorGUI.indentLevel = 1;
|
||||
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("references"), new GUIContent("References", "Definition of the biped ragdoll."), true);
|
||||
|
||||
if (GUI.changed) {
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(script);
|
||||
}
|
||||
|
||||
string msg = string.Empty;
|
||||
bool referencesValid = script.references.IsValid(ref msg);
|
||||
if (serializedObject.FindProperty("references").isExpanded) GUILayout.Space(5);
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
if (referencesValid) {
|
||||
|
||||
if (!script.canBuild) {
|
||||
UnityEditor.EditorApplication.isPaused = true;
|
||||
GUILayout.Space(5);
|
||||
|
||||
if (GUILayout.Button("Create a Ragdoll")) {
|
||||
|
||||
script.canBuild = true;
|
||||
}
|
||||
GUILayout.Label("Clears all existing physics components, creates a new ragdoll and starts live-updating. NB! THIS CAN NOT BE UNDONE!", miniLabelStyle);
|
||||
GUILayout.Space(5);
|
||||
}
|
||||
|
||||
if (script.canBuild) {
|
||||
GUI.changed = false;
|
||||
//GUILayout.Space(5);
|
||||
EditorGUILayout.BeginVertical("Box");
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty("options"), new GUIContent("Options", "Options for automatic ragdoll generation."), true);
|
||||
|
||||
if (GUI.changed) {
|
||||
serializedObject.ApplyModifiedProperties();
|
||||
EditorUtility.SetDirty(script);
|
||||
}
|
||||
if (serializedObject.FindProperty("options").isExpanded) GUILayout.Space(5);
|
||||
EditorGUILayout.EndVertical();
|
||||
|
||||
BipedRagdollCreator.Create(script.references, script.options);
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
if (GUILayout.Button("Done")) {
|
||||
UnityEditor.EditorApplication.isPaused = false;
|
||||
EditorGUI.indentLevel = indent;
|
||||
DestroyImmediate(script);
|
||||
return;
|
||||
}
|
||||
|
||||
GUILayout.Label("Removes this component.", miniLabelStyle);
|
||||
|
||||
if (GUILayout.Button("Start Editing Manually")) {
|
||||
script.gameObject.AddComponent<RagdollEditor>();
|
||||
EditorGUI.indentLevel = indent;
|
||||
DestroyImmediate(script);
|
||||
return;
|
||||
}
|
||||
|
||||
GUILayout.Label("Replaces this component with the RagdollEditor.", miniLabelStyle);
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
}
|
||||
} else {
|
||||
GUILayout.Space(10);
|
||||
GUILayout.BeginVertical("Box");
|
||||
GUILayout.Label(msg);
|
||||
GUILayout.EndVertical();
|
||||
GUILayout.Space(10);
|
||||
|
||||
if (script.canBuild) RagdollCreator.ClearAll(script.transform);
|
||||
}
|
||||
|
||||
EditorGUI.indentLevel = indent;
|
||||
}
|
||||
|
||||
private static Animator GetAnimatorInParents(Transform transform) {
|
||||
if (transform.GetComponent<Animator>() != null) return transform.GetComponent<Animator>();
|
||||
if (transform.parent == null) return null;
|
||||
return GetAnimatorInParents(transform.parent);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3b4d10c84c3764847812025f2e5986e6
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
[CustomEditor(typeof(CharacterJoint))]
|
||||
public class CharacterJointInspector : JointInspector {
|
||||
|
||||
private static Color
|
||||
highTwistColor = new Color(1, 0.75f, 0, 0.3f),
|
||||
lowTwistColor = new Color(0.8f, 0.65f, 0, 0.3f),
|
||||
swing1Color = new Color(0, 1, 0, 0.3f),
|
||||
swing2Color = new Color(0, 0, 1, 0.3f);
|
||||
|
||||
public static void DrawJoint(CharacterJoint joint, bool modifiable = true, float alphaMlp = 1f) {
|
||||
if (joint == null) return;
|
||||
|
||||
Vector3 axis = Vector3.zero;
|
||||
if (joint.axis != Vector3.zero) axis = joint.axis.normalized;
|
||||
Vector3 swingAxis = Vector3.zero;
|
||||
if (joint.swingAxis != Vector3.zero) swingAxis = joint.swingAxis.normalized;
|
||||
Vector3 crossAxis = swingAxis;
|
||||
if (swingAxis != axis) crossAxis = Vector3.Cross(swingAxis, axis);
|
||||
|
||||
float newLowTwistLimit = DrawJointLimit(joint as Joint, "Low Twist Limit", axis, joint.lowTwistLimit.limit, MlpAlpha(lowTwistColor, alphaMlp), -25, modifiable);
|
||||
joint.lowTwistLimit = NewJointLimit(newLowTwistLimit, joint.lowTwistLimit, -180, 180);
|
||||
|
||||
float newHighTwistLimit = joint.highTwistLimit.limit;
|
||||
if (newLowTwistLimit != 0 || (newLowTwistLimit == 0 && newHighTwistLimit != 0)) newHighTwistLimit = DrawJointLimit(joint as Joint, "High Twist Limit", axis, joint.highTwistLimit.limit, MlpAlpha(highTwistColor, alphaMlp), 25, modifiable);
|
||||
joint.highTwistLimit = NewJointLimit(newHighTwistLimit, joint.highTwistLimit, -180, 180);
|
||||
|
||||
float newSwing1Limit = DrawJointLimit(joint as Joint, "Swing1 Limit", swingAxis, -joint.swing1Limit.limit, MlpAlpha(swing1Color, alphaMlp), 25, false);
|
||||
newSwing1Limit = DrawJointLimit(joint as Joint, "Swing1 Limit", swingAxis, -newSwing1Limit, MlpAlpha(swing1Color, alphaMlp), 25, modifiable);
|
||||
joint.swing1Limit = NewJointLimit(newSwing1Limit, joint.swing1Limit, 0, 180);
|
||||
|
||||
float newSwing2Limit = DrawJointLimit(joint as Joint, "Swing2 Limit", crossAxis, -joint.swing2Limit.limit, MlpAlpha(swing2Color, alphaMlp), 25, false);
|
||||
newSwing2Limit = DrawJointLimit(joint as Joint, "Swing2 Limit", crossAxis, -newSwing2Limit, MlpAlpha(swing2Color, alphaMlp), 25, modifiable);
|
||||
joint.swing2Limit = NewJointLimit(newSwing2Limit, joint.swing2Limit, 0, 180);
|
||||
}
|
||||
|
||||
public void OnSceneGUI() {
|
||||
DrawJoint(target as CharacterJoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df75c9b4717ec4cf996ae3055c26373e
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,250 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEditor;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
// Tools for editing Colliders.
|
||||
public class ColliderTools: Editor {
|
||||
|
||||
public static void ConvertToSphereCollider(CapsuleCollider capsule) {
|
||||
SphereCollider sphere = Undo.AddComponent(capsule.gameObject, typeof(SphereCollider)) as SphereCollider;
|
||||
|
||||
sphere.center = capsule.center;
|
||||
sphere.radius = capsule.radius;
|
||||
|
||||
Undo.DestroyObjectImmediate(capsule);
|
||||
}
|
||||
|
||||
public static void ConvertToSphereCollider(BoxCollider box) {
|
||||
SphereCollider sphere = Undo.AddComponent(box.gameObject, typeof(SphereCollider)) as SphereCollider;
|
||||
|
||||
sphere.center = box.center;
|
||||
sphere.radius = Mathf.Max(new float[3] { box.size.x, box.size.y, box.size.z }) * 0.5f;
|
||||
|
||||
Undo.DestroyObjectImmediate(box);
|
||||
}
|
||||
|
||||
public static void ConvertToBoxCollider(SphereCollider sphere) {
|
||||
BoxCollider box = Undo.AddComponent(sphere.gameObject, typeof(BoxCollider)) as BoxCollider;
|
||||
|
||||
box.center = sphere.center;
|
||||
box.size = Vector3.one * sphere.radius * 2f;
|
||||
|
||||
Undo.DestroyObjectImmediate(sphere);
|
||||
}
|
||||
|
||||
public static void ConvertToCapsuleCollider(SphereCollider sphere) {
|
||||
CapsuleCollider capsule = Undo.AddComponent(sphere.gameObject, typeof(CapsuleCollider)) as CapsuleCollider;
|
||||
|
||||
capsule.center = sphere.center;
|
||||
capsule.radius = sphere.radius;
|
||||
capsule.height = sphere.radius;
|
||||
|
||||
Undo.DestroyObjectImmediate(sphere);
|
||||
}
|
||||
|
||||
public static void ConvertToBoxCollider(CapsuleCollider capsule) {
|
||||
BoxCollider box = Undo.AddComponent(capsule.gameObject, typeof(BoxCollider)) as BoxCollider;
|
||||
|
||||
box.center = capsule.center;
|
||||
|
||||
float diameter = capsule.radius * 2f;
|
||||
float height = Mathf.Max(diameter, capsule.height);
|
||||
|
||||
if (capsule.direction == 0) box.size = new Vector3(height, diameter, diameter);
|
||||
else if (capsule.direction == 1) box.size = new Vector3(diameter, height, diameter);
|
||||
else if (capsule.direction == 2) box.size = new Vector3(diameter, diameter, height);
|
||||
|
||||
Undo.DestroyObjectImmediate(capsule);
|
||||
}
|
||||
|
||||
public static void ConvertToCapsuleCollider(BoxCollider box) {
|
||||
float absX = Mathf.Abs(box.size.x);
|
||||
float absY = Mathf.Abs(box.size.y);
|
||||
float absZ = Mathf.Abs(box.size.z);
|
||||
|
||||
CapsuleCollider capsule = Undo.AddComponent(box.gameObject, typeof(CapsuleCollider)) as CapsuleCollider;
|
||||
|
||||
capsule.center = box.center;
|
||||
|
||||
capsule.direction = 0;
|
||||
if (absY > absX && absY > absZ) capsule.direction = 1;
|
||||
if (absZ > absX && absZ > absY) capsule.direction = 2;
|
||||
|
||||
if (capsule.direction == 0) {
|
||||
capsule.height = absX;
|
||||
capsule.radius = Mathf.Max(absY, absZ) * 0.5f;
|
||||
} else if (capsule.direction == 1) {
|
||||
capsule.height = absY;
|
||||
capsule.radius = Mathf.Max(absX, absZ) * 0.5f;
|
||||
} else if (capsule.direction == 2) {
|
||||
capsule.height = absZ;
|
||||
capsule.radius = Mathf.Max(absX, absY) * 0.5f;
|
||||
}
|
||||
|
||||
Undo.DestroyObjectImmediate(box);
|
||||
}
|
||||
|
||||
public static void SetColliderCenterWorld(Collider r, Collider s, Vector3 value, Transform root) {
|
||||
Vector3 newCenter = r.transform.InverseTransformPoint(value);
|
||||
Vector3 centerWorld = Vector3.zero;
|
||||
Vector3 deltaWorld = Vector3.zero;
|
||||
|
||||
if (r is BoxCollider) {
|
||||
var box = r as BoxCollider;
|
||||
centerWorld = r.transform.TransformPoint(box.center);
|
||||
deltaWorld = value - centerWorld;
|
||||
box.center = newCenter;
|
||||
|
||||
if (s != null && s is BoxCollider) s.transform.GetComponent<BoxCollider>().center += s.transform.InverseTransformVector(SymmetryTools.Mirror(deltaWorld, root));
|
||||
return;
|
||||
}
|
||||
|
||||
if (r is CapsuleCollider) {
|
||||
var capsule = r as CapsuleCollider;
|
||||
centerWorld = r.transform.TransformPoint(capsule.center);
|
||||
deltaWorld = value - centerWorld;
|
||||
capsule.center = newCenter;
|
||||
|
||||
if (s != null && s is CapsuleCollider) s.transform.GetComponent<CapsuleCollider>().center += s.transform.InverseTransformVector(SymmetryTools.Mirror(deltaWorld, root));
|
||||
return;
|
||||
}
|
||||
|
||||
if (r is SphereCollider) {
|
||||
var sphere = r as SphereCollider;
|
||||
centerWorld = r.transform.TransformPoint(sphere.center);
|
||||
deltaWorld = value - centerWorld;
|
||||
sphere.center = newCenter;
|
||||
|
||||
if (s != null && s is SphereCollider) s.transform.GetComponent<SphereCollider>().center += s.transform.InverseTransformVector(SymmetryTools.Mirror(deltaWorld, root));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 GetColliderCenterWorld(Collider r) {
|
||||
if (r == null) return Vector3.zero;
|
||||
|
||||
if (r is BoxCollider) {
|
||||
return r.transform.TransformPoint((r as BoxCollider).center);
|
||||
}
|
||||
|
||||
if (r is CapsuleCollider) {
|
||||
return r.transform.TransformPoint((r as CapsuleCollider).center);
|
||||
}
|
||||
|
||||
if (r is SphereCollider) {
|
||||
return r.transform.TransformPoint((r as SphereCollider).center);
|
||||
}
|
||||
|
||||
return r.transform.position;
|
||||
}
|
||||
|
||||
public static void SetColliderSize(Collider r, Collider s, Vector3 value, Vector3 lastValue, Vector3 lastValueS, Transform root) {
|
||||
Vector3 delta = Vector3.zero;
|
||||
Vector3 deltaM = Vector3.zero;
|
||||
|
||||
if (r is BoxCollider) {
|
||||
var box = r as BoxCollider;
|
||||
delta = value - box.size;
|
||||
box.size = value;
|
||||
|
||||
if (s != null && s is BoxCollider) {
|
||||
var boxS = s as BoxCollider;
|
||||
|
||||
deltaM = SymmetryTools.MirrorDelta(r.transform, s.transform, root, delta);
|
||||
boxS.size += deltaM;
|
||||
}
|
||||
}
|
||||
|
||||
if (r is CapsuleCollider) {
|
||||
var capsule = r as CapsuleCollider;
|
||||
|
||||
delta = value - GetCapsuleSize(capsule);
|
||||
SetCapsuleSize(capsule, value, lastValue);
|
||||
|
||||
if (s != null && s is CapsuleCollider) {
|
||||
var capsuleS = s as CapsuleCollider;
|
||||
|
||||
if (capsuleS != null) {
|
||||
deltaM = SymmetryTools.MirrorDelta(r.transform, s.transform, root, delta);
|
||||
Vector3 capsuleSSize = GetCapsuleSize(capsuleS);
|
||||
capsuleSSize += deltaM;
|
||||
|
||||
SetCapsuleSize(capsuleS, capsuleSSize, lastValueS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (r is SphereCollider) {
|
||||
var sphere = r as SphereCollider;
|
||||
|
||||
delta = value - GetColliderSize(r);
|
||||
SetSphereSize(sphere, value, lastValue);
|
||||
|
||||
if (s != null && s is SphereCollider) {
|
||||
var sphereS = s as SphereCollider;
|
||||
Vector3 sphereSSize = GetColliderSize(s);
|
||||
sphereSSize += delta;
|
||||
|
||||
if (sphereS != null) {
|
||||
SetSphereSize(sphereS, sphereSSize, lastValueS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 GetColliderSize(Collider r) {
|
||||
if (r == null) return Vector3.zero;
|
||||
|
||||
if (r is BoxCollider) return (r as BoxCollider).size;
|
||||
if (r is CapsuleCollider) return GetCapsuleSize(r as CapsuleCollider);
|
||||
if (r is SphereCollider) return Vector3.one * (r as SphereCollider).radius;
|
||||
|
||||
return r.transform.localScale;
|
||||
}
|
||||
|
||||
public static void SetCapsuleSize(CapsuleCollider capsule, Vector3 size, Vector3 lastSize) {
|
||||
if (capsule.direction == 0) {
|
||||
capsule.height = size.x;
|
||||
|
||||
float absY = Mathf.Abs(size.y - lastSize.y);
|
||||
float absZ = Mathf.Abs(size.z - lastSize.z);
|
||||
capsule.radius = absY > absZ? size.y: size.z;
|
||||
}
|
||||
|
||||
if (capsule.direction == 1) {
|
||||
capsule.height = size.y;
|
||||
|
||||
float absX = Mathf.Abs(size.x - lastSize.x);
|
||||
float absZ = Mathf.Abs(size.z - lastSize.z);
|
||||
capsule.radius = absX > absZ? size.x: size.z;
|
||||
}
|
||||
|
||||
if (capsule.direction == 2) {
|
||||
capsule.height = size.z;
|
||||
|
||||
float absX = Mathf.Abs(size.x - lastSize.x);
|
||||
float absY = Mathf.Abs(size.y - lastSize.y);
|
||||
capsule.radius = absX > absY? size.x: size.y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void SetSphereSize(SphereCollider sphere, Vector3 size, Vector3 lastSize) {
|
||||
if (size.x != lastSize.z) {
|
||||
sphere.radius += size.x - lastSize.x;
|
||||
} else if (size.y != lastSize.y) {
|
||||
sphere.radius += size.y - lastSize.y;
|
||||
} else if (size.z != lastSize.z) {
|
||||
sphere.radius += size.z - lastSize.z;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 GetCapsuleSize(CapsuleCollider capsule) {
|
||||
if (capsule.direction == 0) return new Vector3(capsule.height, capsule.radius, capsule.radius);
|
||||
if (capsule.direction == 1) return new Vector3(capsule.radius, capsule.height, capsule.radius);
|
||||
return new Vector3(capsule.radius, capsule.radius, capsule.height);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9ed7bfe787e81435c8b76e75353a45fb
|
||||
timeCreated: 1444653054
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,56 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
[CustomEditor(typeof(ConfigurableJoint))]
|
||||
public class ConfigurableJointInspector : JointInspector {
|
||||
|
||||
private static Color
|
||||
highAngularXColor = new Color(1, 0.75f, 0, 0.3f),
|
||||
lowAngularXColor = new Color(0.8f, 0.65f, 0, 0.3f),
|
||||
angularYColor = new Color(0, 1, 0, 0.3f),
|
||||
angularZColor = new Color(0, 0, 1, 0.3f);
|
||||
|
||||
private const string lowX = "Low X", highX = "High X", y = "Y", z = "Z";
|
||||
|
||||
public static void DrawJoint(ConfigurableJoint joint, bool modifiable = true, float alphaMlp = 1f) {
|
||||
if (joint == null) return;
|
||||
|
||||
Vector3 axis = Vector3.zero;
|
||||
if (joint.axis != Vector3.zero) axis = joint.axis.normalized;
|
||||
Vector3 secondaryAxis = Vector3.zero;
|
||||
if (joint.secondaryAxis != Vector3.zero) secondaryAxis = joint.secondaryAxis.normalized;
|
||||
Vector3 crossAxis = secondaryAxis;
|
||||
if (secondaryAxis != axis) crossAxis = Vector3.Cross(secondaryAxis, axis);
|
||||
|
||||
if (joint.angularXMotion == ConfigurableJointMotion.Limited) {
|
||||
float newLowAngularXLimit = DrawJointLimit(joint as Joint, lowX, axis, joint.lowAngularXLimit.limit, MlpAlpha(lowAngularXColor, alphaMlp), -25, modifiable);
|
||||
joint.lowAngularXLimit = NewJointLimit(newLowAngularXLimit, joint.lowAngularXLimit, -180, 180);
|
||||
|
||||
float newHighAngularXLimit = joint.highAngularXLimit.limit;
|
||||
if (newLowAngularXLimit != 0 || (newLowAngularXLimit == 0 && newHighAngularXLimit != 0)) newHighAngularXLimit = DrawJointLimit(joint as Joint, highX, axis, joint.highAngularXLimit.limit, MlpAlpha(highAngularXColor, alphaMlp), 25, modifiable);
|
||||
joint.highAngularXLimit = NewJointLimit(newHighAngularXLimit, joint.highAngularXLimit, -180, 180);
|
||||
}
|
||||
|
||||
if (joint.angularYMotion == ConfigurableJointMotion.Limited) {
|
||||
float newAngularYLimit = DrawJointLimit(joint as Joint, y, secondaryAxis, -joint.angularYLimit.limit, MlpAlpha(angularYColor, alphaMlp), 25, false);
|
||||
newAngularYLimit = DrawJointLimit(joint as Joint, y, secondaryAxis, -newAngularYLimit, MlpAlpha(angularYColor, alphaMlp), 25, modifiable);
|
||||
joint.angularYLimit = NewJointLimit(newAngularYLimit, joint.angularYLimit, 0, 180);
|
||||
}
|
||||
|
||||
if (joint.angularZMotion == ConfigurableJointMotion.Limited) {
|
||||
float newAngularZLimit = DrawJointLimit(joint as Joint, z, crossAxis, -joint.angularZLimit.limit, MlpAlpha(angularZColor, alphaMlp), 25, false);
|
||||
newAngularZLimit = DrawJointLimit(joint as Joint, z, crossAxis, -newAngularZLimit, MlpAlpha(angularZColor, alphaMlp), 25, modifiable);
|
||||
joint.angularZLimit = NewJointLimit(newAngularZLimit, joint.angularZLimit, 0, 180);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSceneGUI() {
|
||||
DrawJoint(target as ConfigurableJoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 79e48320bea5b4a49a128eabdb1b39b2
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,27 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
/// <summary>
|
||||
/// Converting 3D joints to ConfigurableJoints
|
||||
/// </summary>
|
||||
public class ConvertToConfigurableJoints: Editor {
|
||||
|
||||
/// <summary>
|
||||
/// Converts any 3D joints on the selected Transform and it's children to ConfigurableJoints
|
||||
/// </summary>
|
||||
//[MenuItem ("Component/Physics/Convert to ConfigurableJoints")]
|
||||
[MenuItem ("GameObject/Convert to ConfigurableJoints")]
|
||||
public static void ConvertSelected() {
|
||||
if (Selection.activeGameObject == null) {
|
||||
Debug.Log("Please select the root of the ragdoll that you wish to convert to use ConfigurableJoints.");
|
||||
return;
|
||||
}
|
||||
|
||||
// @todo undo
|
||||
|
||||
JointConverter.ToConfigurable(Selection.activeGameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c49a237763c444bda35d3387fed6c35
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RootMotion.Dynamics
|
||||
{
|
||||
public class DragRig : MonoBehaviour
|
||||
{
|
||||
|
||||
private Rigidbody rig;
|
||||
private void Start()
|
||||
{
|
||||
rig = transform.GetComponent<Rigidbody>();
|
||||
}
|
||||
private void OnMouseDrag()
|
||||
{
|
||||
Vector3 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
rig.AddForce(0.01f * Time.deltaTime * (cursorPos-transform.position).normalized,ForceMode.Force);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b32271905ed4afe44b53328a916f382a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,139 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
public class JointInspector : Editor {
|
||||
|
||||
#region Public methods
|
||||
|
||||
private static bool isDragging;
|
||||
|
||||
public static float DrawJointLimit(Joint joint, string label, Vector3 axis, float limit, Color color, float openValue, bool drawHandles) {
|
||||
if (Event.current.type == EventType.MouseDown) isDragging = false;
|
||||
if (Event.current.type == EventType.MouseUp) isDragging = false;
|
||||
|
||||
float radius = HandleUtility.GetHandleSize(joint.transform.position);
|
||||
Vector3 center = joint.transform.TransformPoint(joint.anchor);
|
||||
|
||||
Quaternion axisOffset = Quaternion.AngleAxis(GetViewOffset(joint, axis), axis);
|
||||
Vector3 limitAxis = joint.transform.TransformDirection(axisOffset * axis).normalized;
|
||||
Vector3 limitAxisOrtho = joint.transform.TransformDirection(axisOffset * new Vector3(axis.z, axis.x, axis.y)).normalized;
|
||||
Vector3 limitAxisCross = Vector3.Cross(limitAxis, limitAxisOrtho);
|
||||
|
||||
Handles.color = color;
|
||||
Handles.DrawSolidArc(center, limitAxis, limitAxisCross, -limit, radius);
|
||||
|
||||
Handles.color = new Color(color.r, color.g, color.b, 1f);
|
||||
GUI.color = new Color(color.r, color.g, color.b, 1f);
|
||||
|
||||
//if (limitAxis != Vector3.zero) Handles.CircleCap(0, center, Quaternion.LookRotation(limitAxis), radius);
|
||||
|
||||
Quaternion angleAxis = Quaternion.AngleAxis(-limit, limitAxis);
|
||||
Vector3 handleVector = angleAxis * limitAxisCross;
|
||||
|
||||
Quaternion handleRotation = Quaternion.AngleAxis(-limit, limitAxis);
|
||||
if (limitAxisCross != Vector3.zero && limitAxisOrtho != Vector3.zero) {
|
||||
handleRotation = Quaternion.AngleAxis(-limit, limitAxis) * Quaternion.LookRotation(limitAxisCross) * Quaternion.LookRotation(limitAxisOrtho);
|
||||
}
|
||||
|
||||
if (!drawHandles) return limit;
|
||||
|
||||
float newLimit = Inspector.ScaleValueHandleSphere(limit, center + handleVector * radius, Quaternion.identity, radius, 1);
|
||||
//float newLimit = Handles.han(handleRotation, center + handleVector * radius, limit);
|
||||
|
||||
string labelInfo = label;
|
||||
|
||||
if (newLimit == 0) {
|
||||
labelInfo = "Open " + label;
|
||||
if (Inspector.SphereButton(center + handleVector * radius, handleRotation, radius * 0.2f, radius * 0.07f)) {
|
||||
newLimit = openValue;
|
||||
}
|
||||
}
|
||||
|
||||
Handles.Label(center + handleVector * radius * 1.2f, labelInfo);
|
||||
|
||||
if (newLimit != limit) {
|
||||
if (!isDragging) {
|
||||
Undo.RecordObject(joint, "Change Joint Limits");
|
||||
isDragging = true;
|
||||
}
|
||||
}
|
||||
|
||||
GUI.color = Color.white;
|
||||
return newLimit;
|
||||
}
|
||||
|
||||
public static SoftJointLimit NewJointLimit(float limit, SoftJointLimit referenceJointLimit, float min, float max) {
|
||||
SoftJointLimit newJointLimit = new SoftJointLimit();
|
||||
newJointLimit.limit = Mathf.Clamp(limit, min, max);
|
||||
newJointLimit.bounciness = referenceJointLimit.bounciness;
|
||||
return newJointLimit;
|
||||
}
|
||||
|
||||
public static Color MlpAlpha(Color color, float alphaMlp) {
|
||||
return new Color(color.r, color.g, color.b, color.a * alphaMlp);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static float GetViewOffset(Joint joint, Vector3 axis) {
|
||||
if (joint.connectedBody == null) return 0;
|
||||
|
||||
Vector3 directionAxis = joint.transform.right;
|
||||
Axis viewAxis = GetViewAxis(joint);
|
||||
|
||||
switch(viewAxis) {
|
||||
case Axis.Y: directionAxis = joint.transform.up; break;
|
||||
case Axis.Z: directionAxis = joint.transform.forward; break;
|
||||
}
|
||||
|
||||
if (FlipAxis(joint, viewAxis)) directionAxis = -directionAxis;
|
||||
|
||||
Vector3 axisWorld = joint.transform.TransformDirection(axis).normalized;
|
||||
Vector3 axisWorldOrtho = joint.transform.TransformDirection(new Vector3(axis.z, axis.x, axis.y)).normalized;
|
||||
|
||||
float dot = Vector3.Dot(axisWorldOrtho, directionAxis);
|
||||
float dot2 = Vector3.Dot(axisWorldOrtho, Vector3.Cross(directionAxis, axisWorld));
|
||||
|
||||
if (dot >= 0.5f) return -90;
|
||||
if (dot2 >= 0.5f) return 0;
|
||||
if (dot < -0.5f) return 90;
|
||||
if (dot2 < -0.5f) return 180;
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static Axis GetViewAxis(Joint joint) {
|
||||
if (joint.connectedBody == null) return Axis.Z;
|
||||
|
||||
CapsuleCollider capsule = joint.GetComponent<CapsuleCollider>();
|
||||
if (capsule != null && capsule.center != Vector3.zero) return AxisTools.ToAxis(capsule.center);
|
||||
else {
|
||||
BoxCollider box = joint.GetComponent<BoxCollider>();
|
||||
if (box != null && box.center != Vector3.zero) return AxisTools.ToAxis(box.center);
|
||||
}
|
||||
|
||||
return AxisTools.GetAxisToPoint(joint.transform, joint.connectedBody.worldCenterOfMass);
|
||||
}
|
||||
|
||||
private static bool FlipAxis(Joint joint, Axis axis) {
|
||||
Vector3 direction = Vector3.Normalize(joint.transform.position - joint.connectedBody.worldCenterOfMass);
|
||||
|
||||
CapsuleCollider capsuleCollider = joint.GetComponent<CapsuleCollider>();
|
||||
if (capsuleCollider != null) {
|
||||
direction = Vector3.Normalize(joint.transform.position - (joint.transform.position - (joint.transform.rotation * capsuleCollider.center)));
|
||||
}
|
||||
|
||||
switch(axis) {
|
||||
case Axis.X:
|
||||
return Vector3.Dot(joint.transform.right, direction) < 0;
|
||||
case Axis.Y:
|
||||
return Vector3.Dot(joint.transform.up, direction) < 0;
|
||||
default: return Vector3.Dot(joint.transform.forward, direction) < 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c166e20a769d4ec3a0f504c4fa3ffe7
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,276 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEditor;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
public enum JointAxis {
|
||||
Primary,
|
||||
Secondary,
|
||||
Tertiary
|
||||
}
|
||||
|
||||
// Tools for editing Joints.
|
||||
public class JointTools: Editor {
|
||||
|
||||
public static Quaternion LocalToJointSpace(ConfigurableJoint joint) {
|
||||
Vector3 forward = Vector3.Cross (joint.axis, joint.secondaryAxis).normalized;
|
||||
Vector3 up = Vector3.Cross(forward, joint.axis).normalized;
|
||||
|
||||
if (forward == up) {
|
||||
Debug.LogWarning("Joint " + joint.name + " secondaryAxis is in the exact same direction as it's axis. Please make sure they are not aligned.");
|
||||
}
|
||||
|
||||
return Quaternion.LookRotation(forward, up);
|
||||
}
|
||||
|
||||
public static Quaternion LocalToJointSpace(CharacterJoint joint) {
|
||||
Vector3 forward = Vector3.Cross (joint.axis, joint.swingAxis).normalized;
|
||||
Vector3 up = Vector3.Cross(forward, joint.axis).normalized;
|
||||
|
||||
if (forward == up) {
|
||||
Debug.LogWarning("Joint " + joint.name + " swingAxis is in the exact same direction as it's axis. Please make sure they are not aligned.");
|
||||
}
|
||||
|
||||
return Quaternion.LookRotation(forward, up);
|
||||
}
|
||||
|
||||
public static Quaternion JointToLocalSpace(ConfigurableJoint joint) {
|
||||
return Quaternion.Inverse(LocalToJointSpace(joint));
|
||||
}
|
||||
|
||||
public static Quaternion JointToLocalSpace(CharacterJoint joint) {
|
||||
return Quaternion.Inverse(LocalToJointSpace(joint));
|
||||
}
|
||||
|
||||
public static JointAxis GetJointAxis(ConfigurableJoint joint, Vector3 vLocal) {
|
||||
return GetJointAxis(joint.axis, joint.secondaryAxis, vLocal);
|
||||
}
|
||||
|
||||
public static JointAxis GetJointAxis(CharacterJoint joint, Vector3 vLocal) {
|
||||
return GetJointAxis(joint.axis, joint.swingAxis, vLocal);
|
||||
}
|
||||
|
||||
public static JointAxis GetJointAxis(Vector3 axis, Vector3 secondaryAxis, Vector3 vLocal) {
|
||||
vLocal = vLocal.normalized;
|
||||
|
||||
float axisDot = Mathf.Abs(Vector3.Dot(axis, vLocal));
|
||||
float secondaryAxisDot = Mathf.Abs(Vector3.Dot(secondaryAxis, vLocal));
|
||||
float tertiaryAxisDot = Mathf.Abs(Vector3.Dot(Vector3.Cross(axis, secondaryAxis), vLocal));
|
||||
|
||||
if (axisDot > secondaryAxisDot && axisDot > tertiaryAxisDot) return JointAxis.Primary;
|
||||
if (secondaryAxisDot > axisDot && secondaryAxisDot > tertiaryAxisDot) return JointAxis.Secondary;
|
||||
return JointAxis.Tertiary;
|
||||
}
|
||||
|
||||
public static JointAxis GetSymmetricJointAxis(ConfigurableJoint joint, JointAxis jointAxis, ConfigurableJoint symmetricJoint, Transform root) {
|
||||
return GetSymmetricJointAxis(joint.transform, joint.axis, joint.secondaryAxis, jointAxis, symmetricJoint.transform, symmetricJoint.axis, symmetricJoint.secondaryAxis, root);
|
||||
}
|
||||
|
||||
public static JointAxis GetSymmetricJointAxis(CharacterJoint joint, JointAxis jointAxis, CharacterJoint symmetricJoint, Transform root) {
|
||||
return GetSymmetricJointAxis(joint.transform, joint.axis, joint.swingAxis, jointAxis, symmetricJoint.transform, symmetricJoint.axis, symmetricJoint.swingAxis, root);
|
||||
}
|
||||
|
||||
public static JointAxis GetSymmetricJointAxis(Transform jointTransform, Vector3 axis, Vector3 secondaryAxis, JointAxis jointAxis, Transform symmetricTransform, Vector3 symmetricAxis, Vector3 symmetricSecondaryAxis, Transform root) {
|
||||
Vector3 a = JointAxisToVector3(axis, secondaryAxis, jointAxis);
|
||||
Vector3 aWorld = jointTransform.rotation * a;
|
||||
|
||||
Vector3 aWorldMirror = SymmetryTools.Mirror(aWorld, root);
|
||||
Vector3 aLocalMirror = Quaternion.Inverse(symmetricTransform.rotation) * aWorldMirror;
|
||||
|
||||
Vector3 sAVector = AxisTools.GetAxisVectorToDirection(symmetricTransform, aWorldMirror);
|
||||
Vector3 sA = Vector3.Project(aLocalMirror, sAVector);
|
||||
|
||||
return GetJointAxis(symmetricAxis, symmetricSecondaryAxis, sA);
|
||||
}
|
||||
|
||||
public static Vector3 JointAxisToVector3(ConfigurableJoint joint, JointAxis jointAxis) {
|
||||
return JointAxisToVector3(joint.axis, joint.secondaryAxis, jointAxis);
|
||||
}
|
||||
|
||||
public static Vector3 JointAxisToVector3(CharacterJoint joint, JointAxis jointAxis) {
|
||||
return JointAxisToVector3(joint.axis, joint.swingAxis, jointAxis);
|
||||
}
|
||||
|
||||
public static Vector3 JointAxisToVector3(Vector3 axis, Vector3 secondaryAxis, JointAxis jointAxis) {
|
||||
switch(jointAxis) {
|
||||
case JointAxis.Primary: return axis;
|
||||
case JointAxis.Secondary: return secondaryAxis;
|
||||
default: return Vector3.Cross(axis, secondaryAxis);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyXDeltaToJointLimit(ref ConfigurableJoint joint, float delta, JointAxis jointAxis, bool low) {
|
||||
switch(jointAxis) {
|
||||
case JointAxis.Primary:
|
||||
if (low) {
|
||||
SoftJointLimit lowX = joint.lowAngularXLimit;
|
||||
lowX.limit += delta;
|
||||
joint.lowAngularXLimit = lowX;
|
||||
} else {
|
||||
SoftJointLimit highX = joint.highAngularXLimit;
|
||||
highX.limit -= delta;
|
||||
joint.highAngularXLimit = highX;
|
||||
}
|
||||
break;
|
||||
case JointAxis.Secondary:
|
||||
SoftJointLimit y = joint.angularYLimit;
|
||||
y.limit += delta * 0.5f;
|
||||
joint.angularYLimit = y;
|
||||
break;
|
||||
case JointAxis.Tertiary:
|
||||
SoftJointLimit z = joint.angularZLimit;
|
||||
z.limit += delta * 0.5f;
|
||||
joint.angularZLimit = z;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyXDeltaToJointLimit(ref CharacterJoint joint, float delta, JointAxis jointAxis, bool low) {
|
||||
switch(jointAxis) {
|
||||
case JointAxis.Primary:
|
||||
if (low) {
|
||||
SoftJointLimit lowX = joint.lowTwistLimit;
|
||||
lowX.limit += delta;
|
||||
joint.lowTwistLimit = lowX;
|
||||
} else {
|
||||
SoftJointLimit highX = joint.highTwistLimit;
|
||||
highX.limit -= delta;
|
||||
joint.highTwistLimit = highX;
|
||||
}
|
||||
break;
|
||||
case JointAxis.Secondary:
|
||||
SoftJointLimit y = joint.swing1Limit;
|
||||
y.limit += delta * 0.5f;
|
||||
joint.swing1Limit = y;
|
||||
break;
|
||||
case JointAxis.Tertiary:
|
||||
SoftJointLimit z = joint.swing2Limit;
|
||||
z.limit += delta * 0.5f;
|
||||
joint.swing2Limit = z;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyDeltaToJointLimit(ref ConfigurableJoint joint, float delta, JointAxis jointAxis) {
|
||||
switch(jointAxis) {
|
||||
case JointAxis.Primary:
|
||||
SoftJointLimit lowX = joint.lowAngularXLimit;
|
||||
lowX.limit += delta;
|
||||
joint.lowAngularXLimit = lowX;
|
||||
|
||||
SoftJointLimit highX = joint.highAngularXLimit;
|
||||
highX.limit += delta;
|
||||
joint.highAngularXLimit = highX;
|
||||
break;
|
||||
case JointAxis.Secondary:
|
||||
SoftJointLimit y = joint.angularYLimit;
|
||||
y.limit += delta;
|
||||
joint.angularYLimit = y;
|
||||
break;
|
||||
case JointAxis.Tertiary:
|
||||
SoftJointLimit z = joint.angularZLimit;
|
||||
z.limit += delta;
|
||||
joint.angularZLimit = z;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ApplyDeltaToJointLimit(ref CharacterJoint joint, float delta, JointAxis jointAxis) {
|
||||
switch(jointAxis) {
|
||||
case JointAxis.Primary:
|
||||
SoftJointLimit lowX = joint.lowTwistLimit;
|
||||
lowX.limit += delta;
|
||||
joint.lowTwistLimit = lowX;
|
||||
|
||||
SoftJointLimit highX = joint.highTwistLimit;
|
||||
highX.limit += delta;
|
||||
joint.highTwistLimit = highX;
|
||||
break;
|
||||
case JointAxis.Secondary:
|
||||
SoftJointLimit y = joint.swing1Limit;
|
||||
y.limit += delta;
|
||||
joint.swing1Limit = y;
|
||||
break;
|
||||
case JointAxis.Tertiary:
|
||||
SoftJointLimit z = joint.swing2Limit;
|
||||
z.limit += delta;
|
||||
joint.swing2Limit = z;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static Vector3 GetLowXAxisWorld(Joint joint) {
|
||||
return joint.transform.rotation * joint.axis;
|
||||
}
|
||||
|
||||
public static Vector3 GetHighXAxisWorld(Joint joint) {
|
||||
return joint.transform.rotation * -joint.axis;
|
||||
}
|
||||
|
||||
public static void SwitchXY(ref ConfigurableJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Yellow/Green Joint Axis");
|
||||
|
||||
Vector3 axis = joint.axis;
|
||||
joint.axis = joint.secondaryAxis;
|
||||
joint.secondaryAxis = axis;
|
||||
}
|
||||
|
||||
public static void SwitchXZ(ref ConfigurableJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Yellow/Blue Joint Axis");
|
||||
|
||||
Vector3 cross = Vector3.Cross(joint.axis, joint.secondaryAxis).normalized;
|
||||
joint.axis = cross;
|
||||
}
|
||||
|
||||
public static void SwitchYZ(ref ConfigurableJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Green/Blue Joint Axis");
|
||||
|
||||
Vector3 cross = Vector3.Cross(joint.axis, joint.secondaryAxis).normalized;
|
||||
joint.secondaryAxis = cross;
|
||||
}
|
||||
|
||||
public static void SwitchXY(ref CharacterJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Yellow/Green Joint Axis");
|
||||
|
||||
Vector3 axis = joint.axis;
|
||||
joint.axis = joint.swingAxis;
|
||||
joint.swingAxis = axis;
|
||||
}
|
||||
|
||||
public static void SwitchXZ(ref CharacterJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Yellow/Blue Joint Axis");
|
||||
|
||||
Vector3 cross = Vector3.Cross(joint.axis, joint.swingAxis).normalized;
|
||||
joint.axis = cross;
|
||||
}
|
||||
|
||||
public static void SwitchYZ(ref CharacterJoint joint) {
|
||||
Undo.RecordObject(joint, "Switch Green/Blue Joint Axis");
|
||||
|
||||
Vector3 cross = Vector3.Cross(joint.axis, joint.swingAxis).normalized;
|
||||
joint.swingAxis = cross;
|
||||
}
|
||||
|
||||
public static void InvertAxis(ref Joint joint) {
|
||||
Undo.RecordObject(joint, "Invert Axis");
|
||||
|
||||
joint.axis = -joint.axis;
|
||||
}
|
||||
|
||||
public static void InvertSecondaryAxis(ref Joint joint) {
|
||||
if (joint is ConfigurableJoint) {
|
||||
var j = joint as ConfigurableJoint;
|
||||
Undo.RecordObject(j, "Invert Secondary Axis");
|
||||
j.secondaryAxis = -j.secondaryAxis;
|
||||
}
|
||||
|
||||
if (joint is CharacterJoint) {
|
||||
var j = joint as CharacterJoint;
|
||||
Undo.RecordObject(j, "Invert Swing Axis");
|
||||
j.swingAxis = -j.swingAxis;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d88fb763cb86436197038e855690500
|
||||
timeCreated: 1444653109
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,566 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections;
|
||||
using RootMotion;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
[CustomEditor(typeof(RagdollEditor))]
|
||||
public class RagdollEditorInspector : Editor {
|
||||
|
||||
public RagdollEditor script { get { return target as RagdollEditor; }}
|
||||
|
||||
private float massMlp = 1f;
|
||||
private Transform t;
|
||||
private bool isDragging;
|
||||
|
||||
public override void OnInspectorGUI() {
|
||||
DrawDefaultInspector();
|
||||
GUILayout.Space(10);
|
||||
|
||||
EditorGUILayout.LabelField("Rigidbodies", EditorStyles.boldLabel);
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
massMlp = EditorGUILayout.FloatField("Mass Multiplier", massMlp);
|
||||
if (massMlp <= 0f) massMlp = 0.00001f;
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Multiply", "Multiplies the mass of all child Rigidbodies."))) {
|
||||
Rigidbody[] rigidbodies = GetRigidbodies();
|
||||
Undo.RecordObjects(rigidbodies, "Multiply Mass");
|
||||
|
||||
float totalMass = 0f;
|
||||
foreach (Rigidbody r in rigidbodies) {
|
||||
r.mass *= massMlp;
|
||||
totalMass += r.mass;
|
||||
}
|
||||
Debug.Log("Character mass: " + totalMass);
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
if (GUILayout.Button(new GUIContent("Set All Kinematic", "Enables 'Is Kinematic' on all child Rigidbodies."))) {
|
||||
Rigidbody[] rigidbodies = GetRigidbodies();
|
||||
Undo.RecordObjects(rigidbodies, "Set All Kinematic");
|
||||
|
||||
foreach (Rigidbody r in rigidbodies) {
|
||||
r.isKinematic = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Set All Non-Kinematic", "Disables 'Is Kinematic' on all child Rigidbodies."))) {
|
||||
Rigidbody[] rigidbodies = GetRigidbodies();
|
||||
Undo.RecordObjects(rigidbodies, "Set All Non-Kinematic");
|
||||
|
||||
foreach (Rigidbody r in rigidbodies) {
|
||||
r.isKinematic = false;
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
EditorGUILayout.LabelField("Joints", EditorStyles.boldLabel);
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
if (GUILayout.Button(new GUIContent("Disable Preprocessing", "Unchecks 'Enable Preprocessing' on all joints. This can make ragdolls more stable."))) {
|
||||
Joint[] joints = GetJoints();
|
||||
Undo.RecordObjects(joints, "Disable Preprocessing");
|
||||
|
||||
foreach (Joint j in joints) {
|
||||
j.enablePreprocessing = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (GUILayout.Button(new GUIContent("Enable Preprocessing", "Checks 'Enable Preprocessing' on all joints."))) {
|
||||
Joint[] joints = GetJoints();
|
||||
Undo.RecordObjects(joints, "Enable Preprocessing");
|
||||
|
||||
foreach (Joint j in joints) {
|
||||
j.enablePreprocessing = true;
|
||||
}
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
|
||||
GUILayout.Label("Use the Scene View gizmos to edit Colliders and Joint limits", EditorStyles.miniLabel);
|
||||
}
|
||||
|
||||
private Rigidbody[] GetRigidbodies() {
|
||||
return (Rigidbody[])script.GetComponentsInChildren<Rigidbody>();
|
||||
}
|
||||
|
||||
private Joint[] GetJoints() {
|
||||
return (Joint[])script.GetComponentsInChildren<Joint>();
|
||||
}
|
||||
|
||||
void OnEnable() {
|
||||
t = script.transform;
|
||||
}
|
||||
|
||||
void OnDisable() {
|
||||
if (script == null) return;
|
||||
|
||||
Collider[] colliders = script.transform.GetComponentsInChildren<Collider>();
|
||||
foreach (Collider collider in colliders) collider.enabled = true;
|
||||
}
|
||||
|
||||
void OnDestroy() {
|
||||
if (t == null) return;
|
||||
|
||||
Collider[] colliders = t.GetComponentsInChildren<Collider>();
|
||||
foreach (Collider collider in colliders) collider.enabled = true;
|
||||
}
|
||||
|
||||
void OnSceneGUI() {
|
||||
Rigidbody[] rigidbodies = script.transform.GetComponentsInChildren<Rigidbody>();
|
||||
if (rigidbodies.Length == 0) return;
|
||||
|
||||
Collider[] colliders = script.transform.GetComponentsInChildren<Collider>();
|
||||
|
||||
if (script.selectedRigidbody == null) script.selectedRigidbody = rigidbodies[0];
|
||||
if (script.selectedCollider == null && colliders.Length > 0) script.selectedCollider = colliders[0];
|
||||
|
||||
Rigidbody symmetricRigidbody = script.symmetry && script.selectedRigidbody != null? SymmetryTools.GetSymmetric(script.selectedRigidbody, rigidbodies, script.transform): null;
|
||||
Collider symmetricCollider = script.symmetry && script.selectedCollider != null? SymmetryTools.GetSymmetric(script.selectedCollider, colliders, script.transform): null;
|
||||
|
||||
Joint joint = script.selectedRigidbody != null? script.selectedRigidbody.GetComponent<Joint>(): null;
|
||||
Joint symmetricJoint = joint != null && symmetricRigidbody != null? symmetricRigidbody.GetComponent<Joint>(): null;
|
||||
|
||||
// Selected
|
||||
Transform selected = script.mode == RootMotion.Dynamics.RagdollEditor.Mode.Colliders? (script.selectedCollider != null? script.selectedCollider.transform: null): (script.selectedRigidbody != null? script.selectedRigidbody.transform: null);
|
||||
|
||||
if (selected != null) {
|
||||
Handles.BeginGUI();
|
||||
GUILayout.BeginVertical(new GUIContent("Ragdoll Editor", string.Empty), "Window", GUILayout.Width(200), GUILayout.Height(20));
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Label(selected.name);
|
||||
if (GUILayout.Button("Select GameObject", EditorStyles.miniButton)) {
|
||||
Selection.activeGameObject = selected.gameObject;
|
||||
}
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
//GUILayout.BeginVertical("Box");
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Label("Edit Mode", GUILayout.Width(86));
|
||||
script.mode = (RagdollEditor.Mode)EditorGUILayout.EnumPopup(string.Empty, script.mode, GUILayout.Width(100));
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Label("Symmetry", GUILayout.Width(86));
|
||||
script.symmetry = GUILayout.Toggle(script.symmetry, string.Empty);
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Space(10);
|
||||
|
||||
// COLLIDERS
|
||||
if (script.mode == RagdollEditor.Mode.Colliders && selected != null) {
|
||||
|
||||
if (script.selectedCollider is CapsuleCollider) {
|
||||
var capsule = script.selectedCollider as CapsuleCollider;
|
||||
var capsuleS = symmetricCollider != null? symmetricCollider.transform.GetComponent<CapsuleCollider>(): null;
|
||||
|
||||
if (GUILayout.Button("Convert To Box Collider")) {
|
||||
ColliderTools.ConvertToBoxCollider(capsule);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (capsuleS != null) ColliderTools.ConvertToBoxCollider(capsuleS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Convert To Sphere Collider")) {
|
||||
ColliderTools.ConvertToSphereCollider(capsule);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (capsuleS != null) ColliderTools.ConvertToSphereCollider(capsuleS);
|
||||
return;
|
||||
}
|
||||
|
||||
string capsuleDir = capsule.direction == 0? "X": (capsule.direction == 1? "Y": "Z");
|
||||
|
||||
if (GUILayout.Button("Direction: " + capsuleDir)) {
|
||||
if (capsuleS == null) {
|
||||
Undo.RecordObject(capsule, "Change Capsule Direction");
|
||||
} else {
|
||||
Undo.RecordObjects(new Object[2] { capsule, capsuleS }, "Change Capsule Direction");
|
||||
}
|
||||
|
||||
capsule.direction ++;
|
||||
if (capsule.direction > 2) capsule.direction = 0;
|
||||
if (capsuleS != null) capsuleS.direction = capsule.direction;
|
||||
}
|
||||
} else if (script.selectedCollider is BoxCollider) {
|
||||
var box = script.selectedCollider as BoxCollider;
|
||||
var boxS = symmetricCollider != null? symmetricCollider.transform.GetComponent<BoxCollider>(): null;
|
||||
|
||||
if (GUILayout.Button("Convert To Capsule Collider")) {
|
||||
ColliderTools.ConvertToCapsuleCollider(box);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (boxS != null) ColliderTools.ConvertToCapsuleCollider(boxS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Convert To Sphere Collider")) {
|
||||
ColliderTools.ConvertToSphereCollider(box);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (boxS != null) ColliderTools.ConvertToSphereCollider(boxS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Rotate Collider")) {
|
||||
if (boxS == null) {
|
||||
Undo.RecordObject(box, "Rotate Collider");
|
||||
} else {
|
||||
Undo.RecordObjects(new Object[2] { box, boxS }, "Rotate Collider");
|
||||
}
|
||||
|
||||
box.size = new Vector3(box.size.y, box.size.z, box.size.x);
|
||||
if (boxS != null) boxS.size = box.size;
|
||||
}
|
||||
} else if (script.selectedCollider is SphereCollider) {
|
||||
var sphere = script.selectedCollider as SphereCollider;
|
||||
var sphereS = symmetricCollider != null? symmetricCollider.transform.GetComponent<SphereCollider>(): null;
|
||||
|
||||
if (GUILayout.Button("Convert To Capsule Collider")) {
|
||||
ColliderTools.ConvertToCapsuleCollider(sphere);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (sphereS != null) ColliderTools.ConvertToCapsuleCollider(sphereS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Convert To Box Collider")) {
|
||||
ColliderTools.ConvertToBoxCollider(sphere);
|
||||
script.selectedCollider = selected.GetComponent<Collider>();
|
||||
if (sphereS != null) ColliderTools.ConvertToBoxCollider(sphereS);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// JOINTS
|
||||
if (script.mode == RagdollEditor.Mode.Joints) {
|
||||
if (joint != null && (joint is CharacterJoint || joint is ConfigurableJoint)) {
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
GUILayout.Label("Connected Body");
|
||||
|
||||
var lastConnectedBody = joint.connectedBody;
|
||||
var newConnectedBody = (Rigidbody)EditorGUILayout.ObjectField(joint.connectedBody, typeof(Rigidbody), true);
|
||||
if (newConnectedBody != lastConnectedBody) {
|
||||
Undo.RecordObject(joint, "Changing Joint ConnectedBody");
|
||||
joint.connectedBody = newConnectedBody;
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
|
||||
if (joint is CharacterJoint) {
|
||||
var j = joint as CharacterJoint;
|
||||
var sJ = symmetricJoint != null && symmetricJoint is CharacterJoint? symmetricJoint as CharacterJoint: null;
|
||||
|
||||
if (GUILayout.Button("Convert to Configurable")) {
|
||||
JointConverter.CharacterToConfigurable(j);
|
||||
|
||||
if (sJ != null) {
|
||||
JointConverter.CharacterToConfigurable(sJ);
|
||||
}
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Switch Yellow/Green")) {
|
||||
JointTools.SwitchXY(ref j);
|
||||
if (sJ != null) JointTools.SwitchXY(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Switch Yellow/Blue")) {
|
||||
JointTools.SwitchXZ(ref j);
|
||||
if (sJ != null) JointTools.SwitchXZ(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Switch Green/Blue")) {
|
||||
JointTools.SwitchYZ(ref j);
|
||||
if (sJ != null) JointTools.SwitchYZ(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Invert Yellow")) {
|
||||
JointTools.InvertAxis(ref joint);
|
||||
if (sJ != null) JointTools.InvertAxis(ref symmetricJoint);
|
||||
}
|
||||
}
|
||||
|
||||
if (joint is ConfigurableJoint) {
|
||||
var j = joint as ConfigurableJoint;
|
||||
var sJ = symmetricJoint != null && symmetricJoint is ConfigurableJoint? symmetricJoint as ConfigurableJoint: null;
|
||||
|
||||
if (GUILayout.Button("Switch Yellow/Green")) {
|
||||
JointTools.SwitchXY(ref j);
|
||||
if (sJ != null) JointTools.SwitchXY(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Switch Yellow/Blue")) {
|
||||
JointTools.SwitchXZ(ref j);
|
||||
if (sJ != null) JointTools.SwitchXZ(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Switch Green/Blue")) {
|
||||
JointTools.SwitchYZ(ref j);
|
||||
if (sJ != null) JointTools.SwitchYZ(ref sJ);
|
||||
}
|
||||
|
||||
if (GUILayout.Button("Invert Yellow")) {
|
||||
JointTools.InvertAxis(ref joint);
|
||||
if (sJ != null) JointTools.InvertAxis(ref symmetricJoint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
//GUILayout.EndArea();
|
||||
Handles.EndGUI();
|
||||
|
||||
if (script.mode == RagdollEditor.Mode.Joints && joint != null) {
|
||||
if (joint is CharacterJoint) {
|
||||
var j = joint as CharacterJoint;
|
||||
|
||||
SoftJointLimit lowTwistLimit = j.lowTwistLimit;
|
||||
SoftJointLimit highTwistLimit = j.highTwistLimit;
|
||||
SoftJointLimit swing1Limit = j.swing1Limit;
|
||||
SoftJointLimit swing2Limit = j.swing2Limit;
|
||||
|
||||
CharacterJointInspector.DrawJoint(j);
|
||||
|
||||
if (symmetricJoint != null && symmetricJoint is CharacterJoint) {
|
||||
var sJ = symmetricJoint as CharacterJoint;
|
||||
|
||||
CharacterJointInspector.DrawJoint(sJ, false, 0.5f);
|
||||
|
||||
// Low Twist
|
||||
if (lowTwistLimit.limit != j.lowTwistLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
|
||||
Vector3 lowXAxisWorld = JointTools.GetLowXAxisWorld(j);
|
||||
Vector3 lowXAxisWorldS = JointTools.GetLowXAxisWorld(sJ);
|
||||
Vector3 lowXAxisWorldMirror = SymmetryTools.Mirror(lowXAxisWorld, script.transform);
|
||||
bool low = Vector3.Dot(lowXAxisWorldMirror, lowXAxisWorldS) < 0f;
|
||||
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Primary, sJ, script.transform);
|
||||
float delta = j.lowTwistLimit.limit - lowTwistLimit.limit;
|
||||
|
||||
JointTools.ApplyXDeltaToJointLimit(ref sJ, delta, sJointAxis, low);
|
||||
}
|
||||
|
||||
// High Twist
|
||||
if (highTwistLimit.limit != j.highTwistLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
|
||||
Vector3 highXAxisWorld = JointTools.GetHighXAxisWorld(j);
|
||||
Vector3 highXAxisWorldS = JointTools.GetHighXAxisWorld(sJ);
|
||||
Vector3 highXAxisWorldMirror = SymmetryTools.Mirror(highXAxisWorld, script.transform);
|
||||
bool low = Vector3.Dot(highXAxisWorldMirror, highXAxisWorldS) > 0f;
|
||||
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Primary, sJ, script.transform);
|
||||
float delta = j.highTwistLimit.limit - highTwistLimit.limit;
|
||||
|
||||
JointTools.ApplyXDeltaToJointLimit(ref sJ, -delta, sJointAxis, low);
|
||||
}
|
||||
|
||||
// Swing 1
|
||||
if (swing1Limit.limit != j.swing1Limit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Secondary, sJ, script.transform);
|
||||
float delta = j.swing1Limit.limit - swing1Limit.limit;
|
||||
|
||||
JointTools.ApplyDeltaToJointLimit(ref sJ, delta, sJointAxis);
|
||||
}
|
||||
|
||||
// Swing 2
|
||||
if (swing2Limit.limit != j.swing2Limit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Tertiary, sJ, script.transform);
|
||||
float delta = j.swing2Limit.limit - swing2Limit.limit;
|
||||
|
||||
JointTools.ApplyDeltaToJointLimit(ref sJ, delta, sJointAxis);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (joint is ConfigurableJoint) {
|
||||
|
||||
var j = joint as ConfigurableJoint;
|
||||
|
||||
SoftJointLimit lowAngularXLimit = j.lowAngularXLimit;
|
||||
SoftJointLimit highAngularXLimit = j.highAngularXLimit;
|
||||
SoftJointLimit angularYLimit = j.angularYLimit;
|
||||
SoftJointLimit angularZLimit = j.angularZLimit;
|
||||
|
||||
ConfigurableJointInspector.DrawJoint(j);
|
||||
|
||||
if (symmetricJoint != null && symmetricJoint is ConfigurableJoint) {
|
||||
var sJ = symmetricJoint as ConfigurableJoint;
|
||||
|
||||
ConfigurableJointInspector.DrawJoint(sJ, false, 0.5f);
|
||||
|
||||
// Low X
|
||||
if (lowAngularXLimit.limit != j.lowAngularXLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
|
||||
Vector3 lowXAxisWorld = JointTools.GetLowXAxisWorld(j);
|
||||
Vector3 lowXAxisWorldS = JointTools.GetLowXAxisWorld(sJ);
|
||||
Vector3 lowXAxisWorldMirror = SymmetryTools.Mirror(lowXAxisWorld, script.transform);
|
||||
bool low = Vector3.Dot(lowXAxisWorldMirror, lowXAxisWorldS) < 0f;
|
||||
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Primary, sJ, script.transform);
|
||||
float delta = j.lowAngularXLimit.limit - lowAngularXLimit.limit;
|
||||
|
||||
JointTools.ApplyXDeltaToJointLimit(ref sJ, delta, sJointAxis, low);
|
||||
}
|
||||
|
||||
// High X
|
||||
if (highAngularXLimit.limit != j.highAngularXLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
|
||||
Vector3 highXAxisWorld = JointTools.GetHighXAxisWorld(j);
|
||||
Vector3 highXAxisWorldS = JointTools.GetHighXAxisWorld(sJ);
|
||||
Vector3 highXAxisWorldMirror = SymmetryTools.Mirror(highXAxisWorld, script.transform);
|
||||
bool low = Vector3.Dot(highXAxisWorldMirror, highXAxisWorldS) > 0f;
|
||||
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Primary, sJ, script.transform);
|
||||
float delta = j.highAngularXLimit.limit - highAngularXLimit.limit;
|
||||
|
||||
JointTools.ApplyXDeltaToJointLimit(ref sJ, -delta, sJointAxis, low);
|
||||
}
|
||||
|
||||
// Y
|
||||
if (angularYLimit.limit != j.angularYLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Secondary, sJ, script.transform);
|
||||
float delta = j.angularYLimit.limit - angularYLimit.limit;
|
||||
|
||||
JointTools.ApplyDeltaToJointLimit(ref sJ, delta, sJointAxis);
|
||||
}
|
||||
|
||||
// Z
|
||||
if (angularZLimit.limit != j.angularZLimit.limit) {
|
||||
Undo.RecordObject(sJ, "Change Joint Limits");
|
||||
JointAxis sJointAxis = JointTools.GetSymmetricJointAxis(j, JointAxis.Tertiary, sJ, script.transform);
|
||||
float delta = j.angularZLimit.limit - angularZLimit.limit;
|
||||
|
||||
JointTools.ApplyDeltaToJointLimit(ref sJ, delta, sJointAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!Application.isPlaying) {
|
||||
foreach (Collider c in colliders) {
|
||||
c.enabled = script.mode == RagdollEditor.Mode.Colliders;
|
||||
}
|
||||
}
|
||||
|
||||
// HANDLES
|
||||
Color color = new Color(0.2f, 0.9f, 0.5f);
|
||||
Handles.color = color;
|
||||
|
||||
if (Event.current.type == EventType.MouseUp) isDragging = false;
|
||||
if (Event.current.type == EventType.MouseDown) isDragging = false;
|
||||
|
||||
if (script.mode == RagdollEditor.Mode.Colliders) {
|
||||
|
||||
// Select/move scale colliders
|
||||
for (int i = 0; i < colliders.Length; i++) {
|
||||
|
||||
if (colliders[i] == script.selectedCollider) {
|
||||
|
||||
// Moving and scaling selected colliders
|
||||
switch(Tools.current) {
|
||||
case Tool.Move:
|
||||
Vector3 oldPosition = ColliderTools.GetColliderCenterWorld(colliders[i]);
|
||||
Vector3 newPosition = Handles.PositionHandle(oldPosition, colliders[i].transform.rotation);
|
||||
if (newPosition != oldPosition) {
|
||||
if (!isDragging) {
|
||||
Undo.RecordObjects(SymmetryTools.GetColliderPair(colliders[i], symmetricCollider), "Move Colliders");
|
||||
|
||||
isDragging = true;
|
||||
}
|
||||
|
||||
ColliderTools.SetColliderCenterWorld(colliders[i], symmetricCollider, newPosition, script.transform);
|
||||
}
|
||||
break;
|
||||
case Tool.Scale:
|
||||
Vector3 position = ColliderTools.GetColliderCenterWorld(colliders[i]);
|
||||
Vector3 oldSize = ColliderTools.GetColliderSize(colliders[i]);
|
||||
Vector3 oldSizeS = ColliderTools.GetColliderSize(symmetricCollider);
|
||||
Vector3 newSize = Handles.ScaleHandle(oldSize, position, colliders[i].transform.rotation, HandleUtility.GetHandleSize(position));
|
||||
if (newSize != oldSize) {
|
||||
if (!isDragging) {
|
||||
Undo.RecordObjects(SymmetryTools.GetColliderPair(colliders[i], symmetricCollider), "Scale Colliders");
|
||||
|
||||
isDragging = true;
|
||||
}
|
||||
|
||||
ColliderTools.SetColliderSize(colliders[i], symmetricCollider, newSize, oldSize, oldSizeS, script.transform);
|
||||
}
|
||||
|
||||
Handles.color = color;
|
||||
break;
|
||||
}
|
||||
|
||||
// Dot on selected collider
|
||||
if (Tools.current != Tool.Scale) {
|
||||
Handles.color = new Color(0.8f, 0.8f, 0.8f);
|
||||
Vector3 center = ColliderTools.GetColliderCenterWorld(colliders[i]);
|
||||
float size = GetHandleSize(center);
|
||||
Inspector.CubeCap(0, center, colliders[i].transform.rotation, size);
|
||||
Handles.color = color;
|
||||
}
|
||||
|
||||
} else {
|
||||
// Selecting colliders
|
||||
Vector3 center = ColliderTools.GetColliderCenterWorld(colliders[i]);
|
||||
float size = GetHandleSize(center);
|
||||
|
||||
if (Inspector.DotButton(center, Quaternion.identity, size * 0.5f, size * 0.5f)) {
|
||||
Undo.RecordObject(script, "Change RagdollEditor Selection");
|
||||
|
||||
script.selectedCollider = colliders[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
} else {
|
||||
// Select joints
|
||||
for (int i = 0; i < rigidbodies.Length; i++) {
|
||||
|
||||
if (rigidbodies[i] == script.selectedRigidbody) {
|
||||
// Dots on selected joints
|
||||
Handles.color = new Color(0.8f, 0.8f, 0.8f);
|
||||
Vector3 center = rigidbodies[i].transform.position;
|
||||
float size = GetHandleSize(center);
|
||||
Inspector.CubeCap(0, center, rigidbodies[i].transform.rotation, size);
|
||||
Handles.color = color;
|
||||
} else {
|
||||
// Selecting joints
|
||||
Vector3 center = rigidbodies[i].transform.position;
|
||||
float size = GetHandleSize(center);
|
||||
|
||||
if (Inspector.DotButton(center, Quaternion.identity, size * 0.5f, size * 0.5f)) {
|
||||
Undo.RecordObject(script, "Change RagdollEditor Selection");
|
||||
|
||||
script.selectedRigidbody = rigidbodies[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Handles.color = Color.white;
|
||||
}
|
||||
|
||||
private static float GetHandleSize(Vector3 position) {
|
||||
float s = HandleUtility.GetHandleSize(position) * 0.1f;
|
||||
return Mathf.Lerp(s, 0.025f, 0.2f);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1f61264d173e24310b255e76adcfff81
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEditor;
|
||||
|
||||
namespace RootMotion.Dynamics {
|
||||
|
||||
// Tools for symmetric operations
|
||||
public class SymmetryTools: Editor {
|
||||
|
||||
public static Rigidbody GetSymmetric(Rigidbody r, Rigidbody[] R, Transform root) {
|
||||
Vector3 localPos = root.InverseTransformPoint(r.position);
|
||||
|
||||
Rigidbody closest = null;
|
||||
float closestDistance = 0.01f;
|
||||
|
||||
foreach (Rigidbody r2 in R) {
|
||||
if (r != r2) {
|
||||
Vector3 localPos2 = root.InverseTransformPoint(r2.position);
|
||||
localPos2.x = -localPos2.x;
|
||||
|
||||
float dist = Vector3.Distance(localPos, localPos2);
|
||||
if (dist < closestDistance) {
|
||||
closest = r2;
|
||||
closestDistance = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
public static Collider GetSymmetric(Collider c, Collider[] C, Transform root) {
|
||||
Vector3 localPos = root.InverseTransformPoint(c.transform.position);
|
||||
|
||||
Collider closest = null;
|
||||
float closestDistance = 0.01f;
|
||||
|
||||
foreach (Collider c2 in C) {
|
||||
if (c != c2) {
|
||||
Vector3 localPos2 = root.InverseTransformPoint(c2.transform.position);
|
||||
localPos2.x = -localPos2.x;
|
||||
|
||||
float dist = Vector3.Distance(localPos, localPos2);
|
||||
if (dist < closestDistance) {
|
||||
closest = c2;
|
||||
closestDistance = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return closest;
|
||||
}
|
||||
|
||||
public static Vector3 MirrorDelta(Transform r, Transform s, Transform root, Vector3 delta) {
|
||||
Vector3 dXW = r.right * delta.x;
|
||||
Vector3 dYW = r.up * delta.y;
|
||||
Vector3 dZW = r.forward * delta.z;
|
||||
|
||||
Vector3 dXWM = Mirror(dXW, root);
|
||||
Vector3 dYWM = Mirror(dYW, root);
|
||||
Vector3 dZWM = Mirror(dZW, root);
|
||||
|
||||
Vector3 deltaS = Vector3.zero;
|
||||
deltaS = DeltaSize(deltaS, s, dXWM, delta.x);
|
||||
deltaS = DeltaSize(deltaS, s, dYWM, delta.y);
|
||||
deltaS = DeltaSize(deltaS, s, dZWM, delta.z);
|
||||
|
||||
return deltaS;
|
||||
}
|
||||
|
||||
public static Vector3 Mirror(Vector3 v, Transform root) {
|
||||
Vector3 local = root.InverseTransformVector(v);
|
||||
return root.TransformVector(-local.x, local.y, local.z);
|
||||
}
|
||||
|
||||
public static Vector3 DeltaSize(Vector3 size, Transform t, Vector3 direction, float delta) {
|
||||
Axis axis = AxisTools.GetAxisToDirection(t, direction);
|
||||
|
||||
switch(axis) {
|
||||
case Axis.X: size.x += delta; break;
|
||||
case Axis.Y: size.y += delta; break;
|
||||
case Axis.Z: size.z += delta; break;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
public static Collider[] GetColliderPair(Collider r, Collider s) {
|
||||
if (s == null) return new Collider[1] {r};
|
||||
return new Collider[2] {r, s};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd9c3cde48ab14fb2991ac46cf73af84
|
||||
timeCreated: 1444653494
|
||||
licenseType: Store
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user