Merge pull request #227 from pharan/bonecomponent

Fixed BoneComponent Z-positioning.
This commit is contained in:
Nathan Sweet 2014-05-29 21:14:43 +02:00
commit 147c9bd763
4 changed files with 90 additions and 18 deletions

View File

@ -44,12 +44,29 @@ public class BoneComponent : MonoBehaviour {
public SkeletonRenderer skeletonRenderer; public SkeletonRenderer skeletonRenderer;
public Bone bone; public Bone bone;
public bool followZPosition = true;
public bool followBoneRotation = true;
public SkeletonRenderer SkeletonRenderer {
get { return skeletonRenderer; }
set {
skeletonRenderer = value;
Reset ();
}
}
// TODO: Make the rotation behavior more customizable
// public bool followTransformRotation = false;
// TODO: Make transform follow bone scale? too specific? This is really useful for shared shadow assets.
//public bool followBoneScale = false;
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary> /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
public String boneName; public String boneName;
protected Transform cachedTransform; protected Transform cachedTransform;
protected Transform skeletonTransform; protected Transform skeletonTransform;
public void Reset () { public void Reset () {
bone = null; bone = null;
cachedTransform = transform; cachedTransform = transform;
@ -77,15 +94,28 @@ public class BoneComponent : MonoBehaviour {
} }
} }
Spine.Skeleton skeleton = skeletonRenderer.skeleton;
float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
if (cachedTransform.parent == skeletonTransform) { if (cachedTransform.parent == skeletonTransform) {
cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, cachedTransform.localPosition.z); cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : cachedTransform.localPosition.z);
Vector3 rotation = cachedTransform.localRotation.eulerAngles;
cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); if(followBoneRotation) {
Vector3 rotation = cachedTransform.localRotation.eulerAngles;
cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation * flipRotation);
}
} else { } else {
cachedTransform.position = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, cachedTransform.position.z)); Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f));
Vector3 rotation = skeletonTransform.rotation.eulerAngles; if(!followZPosition) targetWorldPosition.z = cachedTransform.position.z;
cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y,
skeletonTransform.rotation.eulerAngles.z + bone.worldRotation); cachedTransform.position = targetWorldPosition;
if(followBoneRotation) {
Vector3 rotation = skeletonTransform.rotation.eulerAngles;
cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y,
skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation) );
}
} }
} }
} }

View File

@ -34,11 +34,14 @@ using UnityEngine;
[CustomEditor(typeof(BoneComponent))] [CustomEditor(typeof(BoneComponent))]
public class BoneComponentInspector : Editor { public class BoneComponentInspector : Editor {
private SerializedProperty boneName, skeletonRenderer; private SerializedProperty boneName, skeletonRenderer, followZPosition, followBoneRotation;
void OnEnable () { void OnEnable () {
skeletonRenderer = serializedObject.FindProperty("skeletonRenderer"); skeletonRenderer = serializedObject.FindProperty("skeletonRenderer");
boneName = serializedObject.FindProperty("boneName"); boneName = serializedObject.FindProperty("boneName");
followBoneRotation = serializedObject.FindProperty("followBoneRotation");
followZPosition = serializedObject.FindProperty("followZPosition");
} }
override public void OnInspectorGUI () { override public void OnInspectorGUI () {
@ -62,6 +65,9 @@ public class BoneComponentInspector : Editor {
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex]; boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex];
EditorGUILayout.PropertyField(followBoneRotation);
EditorGUILayout.PropertyField(followZPosition);
} }
if (serializedObject.ApplyModifiedProperties() || if (serializedObject.ApplyModifiedProperties() ||

View File

@ -44,12 +44,29 @@ public class BoneComponent : MonoBehaviour {
public SkeletonRenderer skeletonRenderer; public SkeletonRenderer skeletonRenderer;
public Bone bone; public Bone bone;
public bool followZPosition = true;
public bool followBoneRotation = true;
public SkeletonRenderer SkeletonRenderer {
get { return skeletonRenderer; }
set {
skeletonRenderer = value;
Reset ();
}
}
// TODO: Make the rotation behavior more customizable
// public bool followTransformRotation = false;
// TODO: Make transform follow bone scale? too specific? This is really useful for shared shadow assets.
//public bool followBoneScale = false;
/// <summary>If a bone isn't set, boneName is used to find the bone.</summary> /// <summary>If a bone isn't set, boneName is used to find the bone.</summary>
public String boneName; public String boneName;
protected Transform cachedTransform; protected Transform cachedTransform;
protected Transform skeletonTransform; protected Transform skeletonTransform;
public void Reset () { public void Reset () {
bone = null; bone = null;
cachedTransform = transform; cachedTransform = transform;
@ -77,15 +94,28 @@ public class BoneComponent : MonoBehaviour {
} }
} }
Spine.Skeleton skeleton = skeletonRenderer.skeleton;
float flipRotation = (skeleton.flipX ^ skeleton.flipY) ? -1f : 1f;
if (cachedTransform.parent == skeletonTransform) { if (cachedTransform.parent == skeletonTransform) {
cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, cachedTransform.localPosition.z); cachedTransform.localPosition = new Vector3(bone.worldX, bone.worldY, followZPosition ? 0f : cachedTransform.localPosition.z);
Vector3 rotation = cachedTransform.localRotation.eulerAngles;
cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation); if(followBoneRotation) {
Vector3 rotation = cachedTransform.localRotation.eulerAngles;
cachedTransform.localRotation = Quaternion.Euler(rotation.x, rotation.y, bone.worldRotation * flipRotation);
}
} else { } else {
cachedTransform.position = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, cachedTransform.position.z)); Vector3 targetWorldPosition = skeletonTransform.TransformPoint(new Vector3(bone.worldX, bone.worldY, 0f));
Vector3 rotation = skeletonTransform.rotation.eulerAngles; if(!followZPosition) targetWorldPosition.z = cachedTransform.position.z;
cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y,
skeletonTransform.rotation.eulerAngles.z + bone.worldRotation); cachedTransform.position = targetWorldPosition;
if(followBoneRotation) {
Vector3 rotation = skeletonTransform.rotation.eulerAngles;
cachedTransform.rotation = Quaternion.Euler(rotation.x, rotation.y,
skeletonTransform.rotation.eulerAngles.z + (bone.worldRotation * flipRotation) );
}
} }
} }
} }

View File

@ -34,11 +34,14 @@ using UnityEngine;
[CustomEditor(typeof(BoneComponent))] [CustomEditor(typeof(BoneComponent))]
public class BoneComponentInspector : Editor { public class BoneComponentInspector : Editor {
private SerializedProperty boneName, skeletonRenderer; private SerializedProperty boneName, skeletonRenderer, followZPosition, followBoneRotation;
void OnEnable () { void OnEnable () {
skeletonRenderer = serializedObject.FindProperty("skeletonRenderer"); skeletonRenderer = serializedObject.FindProperty("skeletonRenderer");
boneName = serializedObject.FindProperty("boneName"); boneName = serializedObject.FindProperty("boneName");
followBoneRotation = serializedObject.FindProperty("followBoneRotation");
followZPosition = serializedObject.FindProperty("followZPosition");
} }
override public void OnInspectorGUI () { override public void OnInspectorGUI () {
@ -62,6 +65,9 @@ public class BoneComponentInspector : Editor {
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex]; boneName.stringValue = boneIndex == 0 ? null : bones[boneIndex];
EditorGUILayout.PropertyField(followBoneRotation);
EditorGUILayout.PropertyField(followZPosition);
} }
if (serializedObject.ApplyModifiedProperties() || if (serializedObject.ApplyModifiedProperties() ||