This commit is contained in:
Nim XD
2024-08-27 21:01:33 +05:30
parent 99eaf514fd
commit 121a1b7c73
31803 changed files with 623461 additions and 623399 deletions

View File

@@ -1,37 +1,37 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEditor;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonRagdoll2DInspector { }
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEditor;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonRagdoll2DInspector { }
}

View File

@@ -1,46 +1,46 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEditor;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonRagdollInspector : UnityEditor.Editor {
[CustomPropertyDrawer(typeof(SkeletonRagdoll.LayerFieldAttribute))]
public class LayerFieldPropertyDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
property.intValue = EditorGUI.LayerField(position, label, property.intValue);
}
}
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using UnityEditor;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonRagdollInspector : UnityEditor.Editor {
[CustomPropertyDrawer(typeof(SkeletonRagdoll.LayerFieldAttribute))]
public class LayerFieldPropertyDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
property.intValue = EditorGUI.LayerField(position, label, property.intValue);
}
}
}
}

View File

@@ -1,18 +1,18 @@
{
"name": "spine-unity-examples-editor",
"references": [
"spine-unity",
"spine-unity-examples"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
{
"name": "spine-unity-examples-editor",
"references": [
"spine-unity",
"spine-unity-examples"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -1,443 +1,443 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
[RequireComponent(typeof(SkeletonRenderer))]
public class SkeletonRagdoll : MonoBehaviour {
static Transform parentSpaceHelper;
#region Inspector
[Header("Hierarchy")]
[SpineBone]
public string startingBoneName = "";
[SpineBone]
public List<string> stopBoneNames = new List<string>();
[Header("Parameters")]
public bool applyOnStart;
[Tooltip("Warning! You will have to re-enable and tune mix values manually if attempting to remove the ragdoll system.")]
public bool disableIK = true;
public bool disableOtherConstraints = false;
[Space(18)]
[Tooltip("Set RootRigidbody IsKinematic to true when Apply is called.")]
public bool pinStartBone;
[Tooltip("Enable Collision between adjacent ragdoll elements (IE: Neck and Head)")]
public bool enableJointCollision;
public bool useGravity = true;
[Tooltip("If no BoundingBox Attachment is attached to a bone, this becomes the default Width or Radius of a Bone's ragdoll Rigidbody")]
public float thickness = 0.125f;
[Tooltip("Default rotational limit value. Min is negative this value, Max is this value.")]
public float rotationLimit = 20;
public float rootMass = 20;
[Tooltip("If your ragdoll seems unstable or uneffected by limits, try lowering this value.")]
[Range(0.01f, 1f)]
public float massFalloffFactor = 0.4f;
[Tooltip("The layer assigned to all of the rigidbody parts.")]
public int colliderLayer = 0;
[Range(0, 1)]
public float mix = 1;
public bool oldRagdollBehaviour = false;
#endregion
ISkeletonAnimation targetSkeletonComponent;
Skeleton skeleton;
struct BoneFlipEntry {
public BoneFlipEntry (bool flipX, bool flipY) {
this.flipX = flipX;
this.flipY = flipY;
}
public bool flipX;
public bool flipY;
}
Dictionary<Bone, Transform> boneTable = new Dictionary<Bone, Transform>();
Dictionary<Bone, BoneFlipEntry> boneFlipTable = new Dictionary<Bone, BoneFlipEntry>();
Transform ragdollRoot;
public Rigidbody RootRigidbody { get; private set; }
public Bone StartingBone { get; private set; }
Vector3 rootOffset;
public Vector3 RootOffset { get { return this.rootOffset; } }
bool isActive;
public bool IsActive { get { return this.isActive; } }
IEnumerator Start () {
if (parentSpaceHelper == null) {
parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
parentSpaceHelper.hideFlags = HideFlags.HideInHierarchy;
}
targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
skeleton = targetSkeletonComponent.Skeleton;
if (applyOnStart) {
yield return null;
Apply();
}
}
#region API
public Rigidbody[] RigidbodyArray {
get {
if (!isActive)
return new Rigidbody[0];
var rigidBodies = new Rigidbody[boneTable.Count];
int i = 0;
foreach (Transform t in boneTable.Values) {
rigidBodies[i] = t.GetComponent<Rigidbody>();
i++;
}
return rigidBodies;
}
}
public Vector3 EstimatedSkeletonPosition {
get { return RootRigidbody.position - rootOffset; }
}
/// <summary>Instantiates the ragdoll simulation and applies its transforms to the skeleton.</summary>
public void Apply () {
isActive = true;
mix = 1;
StartingBone = skeleton.FindBone(startingBoneName);
RecursivelyCreateBoneProxies(StartingBone);
RootRigidbody = boneTable[StartingBone].GetComponent<Rigidbody>();
RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider>();
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
Transform parentTransform;
boneColliders.Add(t.GetComponent<Collider>());
if (b == StartingBone) {
ragdollRoot = new GameObject("RagdollRoot").transform;
ragdollRoot.SetParent(transform, false);
if (b == skeleton.RootBone) { // RagdollRoot is skeleton root's parent, thus the skeleton's scale and position.
ragdollRoot.localPosition = new Vector3(skeleton.X, skeleton.Y, 0);
ragdollRoot.localRotation = (skeleton.ScaleX < 0) ? Quaternion.Euler(0, 0, 180.0f) : Quaternion.identity;
} else {
ragdollRoot.localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, b.Parent.WorldRotationX - b.Parent.ShearX);
}
parentTransform = ragdollRoot;
rootOffset = t.position - transform.position;
} else {
parentTransform = boneTable[b.Parent];
}
// Add joint and attach to parent.
var rbParent = parentTransform.GetComponent<Rigidbody>();
if (rbParent != null) {
var joint = t.gameObject.AddComponent<HingeJoint>();
joint.connectedBody = rbParent;
Vector3 localPos = parentTransform.InverseTransformPoint(t.position);
localPos.x *= 1;
joint.connectedAnchor = localPos;
joint.axis = Vector3.forward;
joint.GetComponent<Rigidbody>().mass = joint.connectedBody.mass * massFalloffFactor;
joint.limits = new JointLimits {
min = -rotationLimit,
max = rotationLimit,
};
joint.useLimits = true;
joint.enableCollision = enableJointCollision;
}
}
// Ignore collisions among bones.
for (int x = 0; x < boneColliders.Count; x++) {
for (int y = 0; y < boneColliders.Count; y++) {
if (x == y) continue;
Physics.IgnoreCollision(boneColliders[x], boneColliders[y]);
}
}
// Destroy existing override-mode SkeletonUtilityBones.
var utilityBones = GetComponentsInChildren<SkeletonUtilityBone>();
if (utilityBones.Length > 0) {
var destroyedUtilityBoneNames = new List<string>();
foreach (var ub in utilityBones) {
if (ub.mode == SkeletonUtilityBone.Mode.Override) {
destroyedUtilityBoneNames.Add(ub.gameObject.name);
Destroy(ub.gameObject);
}
}
if (destroyedUtilityBoneNames.Count > 0) {
string msg = "Destroyed Utility Bones: ";
for (int i = 0; i < destroyedUtilityBoneNames.Count; i++) {
msg += destroyedUtilityBoneNames[i];
if (i != destroyedUtilityBoneNames.Count - 1) {
msg += ",";
}
}
Debug.LogWarning(msg);
}
}
// Disable skeleton constraints.
if (disableIK) {
var ikConstraints = skeleton.IkConstraints;
for (int i = 0, n = ikConstraints.Count; i < n; i++)
ikConstraints.Items[i].Mix = 0;
}
if (disableOtherConstraints) {
var transformConstraints = skeleton.TransformConstraints;
for (int i = 0, n = transformConstraints.Count; i < n; i++) {
transformConstraints.Items[i].MixRotate = 0;
transformConstraints.Items[i].MixScaleX = 0;
transformConstraints.Items[i].MixScaleY = 0;
transformConstraints.Items[i].MixShearY = 0;
transformConstraints.Items[i].MixX = 0;
transformConstraints.Items[i].MixY = 0;
}
var pathConstraints = skeleton.PathConstraints;
for (int i = 0, n = pathConstraints.Count; i < n; i++) {
pathConstraints.Items[i].MixRotate = 0;
pathConstraints.Items[i].MixX = 0;
pathConstraints.Items[i].MixY = 0;
}
}
targetSkeletonComponent.UpdateWorld += UpdateSpineSkeleton;
}
/// <summary>Transitions the mix value from the current value to a target value.</summary>
public Coroutine SmoothMix (float target, float duration) {
return StartCoroutine(SmoothMixCoroutine(target, duration));
}
IEnumerator SmoothMixCoroutine (float target, float duration) {
float startTime = Time.time;
float startMix = mix;
while (mix > 0) {
skeleton.SetBonesToSetupPose();
mix = Mathf.SmoothStep(startMix, target, (Time.time - startTime) / duration);
yield return null;
}
}
/// <summary>Set the transform world position while preserving the ragdoll parts world position.</summary>
public void SetSkeletonPosition (Vector3 worldPosition) {
if (!isActive) {
Debug.LogWarning("Can't call SetSkeletonPosition while Ragdoll is not active!");
return;
}
Vector3 offset = worldPosition - transform.position;
transform.position = worldPosition;
foreach (Transform t in boneTable.Values)
t.position -= offset;
UpdateSpineSkeleton(null);
skeleton.UpdateWorldTransform();
}
/// <summary>Removes the ragdoll instance and effect from the animated skeleton.</summary>
public void Remove () {
isActive = false;
foreach (var t in boneTable.Values)
Destroy(t.gameObject);
Destroy(ragdollRoot.gameObject);
boneTable.Clear();
targetSkeletonComponent.UpdateWorld -= UpdateSpineSkeleton;
}
public Rigidbody GetRigidbody (string boneName) {
var bone = skeleton.FindBone(boneName);
return (bone != null && boneTable.ContainsKey(bone)) ? boneTable[bone].GetComponent<Rigidbody>() : null;
}
#endregion
void RecursivelyCreateBoneProxies (Bone b) {
string boneName = b.Data.Name;
if (stopBoneNames.Contains(boneName))
return;
var boneGameObject = new GameObject(boneName);
boneGameObject.layer = colliderLayer;
Transform t = boneGameObject.transform;
boneTable.Add(b, t);
t.parent = transform;
t.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
t.localRotation = Quaternion.Euler(0, 0, b.WorldRotationX - b.ShearX);
t.localScale = new Vector3(b.WorldScaleX, b.WorldScaleY, 1);
var colliders = AttachBoundingBoxRagdollColliders(b);
if (colliders.Count == 0) {
float length = b.Data.Length;
if (length == 0) {
var ball = boneGameObject.AddComponent<SphereCollider>();
ball.radius = thickness * 0.5f;
} else {
var box = boneGameObject.AddComponent<BoxCollider>();
box.size = new Vector3(length, thickness, thickness);
box.center = new Vector3(length * 0.5f, 0);
}
}
var rb = boneGameObject.AddComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezePositionZ;
foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child);
}
void UpdateSpineSkeleton (ISkeletonAnimation skeletonRenderer) {
bool parentFlipX;
bool parentFlipY;
GetStartBoneParentFlipState(out parentFlipX, out parentFlipY);
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
bool isStartingBone = b == StartingBone;
var parentBone = b.Parent;
Transform parentTransform = isStartingBone ? ragdollRoot : boneTable[parentBone];
if (!isStartingBone) {
var parentBoneFlip = boneFlipTable[parentBone];
parentFlipX = parentBoneFlip.flipX;
parentFlipY = parentBoneFlip.flipY;
}
bool flipX = parentFlipX ^ (b.ScaleX < 0);
bool flipY = parentFlipY ^ (b.ScaleY < 0);
BoneFlipEntry boneFlip;
boneFlipTable.TryGetValue(b, out boneFlip);
boneFlip.flipX = flipX;
boneFlip.flipY = flipY;
boneFlipTable[b] = boneFlip;
bool flipXOR = flipX ^ flipY;
bool parentFlipXOR = parentFlipX ^ parentFlipY;
if (!oldRagdollBehaviour && isStartingBone) {
if (b != skeleton.RootBone) { // RagdollRoot is not skeleton root.
ragdollRoot.localPosition = new Vector3(parentBone.WorldX, parentBone.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
ragdollRoot.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 parentTransformWorldPosition = parentTransform.position;
Quaternion parentTransformWorldRotation = parentTransform.rotation;
parentSpaceHelper.position = parentTransformWorldPosition;
parentSpaceHelper.rotation = parentTransformWorldRotation;
parentSpaceHelper.localScale = parentTransform.lossyScale;
if (oldRagdollBehaviour) {
if (isStartingBone && b != skeleton.RootBone) {
Vector3 localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
parentSpaceHelper.position = ragdollRoot.TransformPoint(localPosition);
parentSpaceHelper.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
parentSpaceHelper.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 boneWorldPosition = t.position;
Vector3 right = parentSpaceHelper.InverseTransformDirection(t.right);
Vector3 boneLocalPosition = parentSpaceHelper.InverseTransformPoint(boneWorldPosition);
float boneLocalRotation = Mathf.Atan2(right.y, right.x) * Mathf.Rad2Deg;
if (flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR != flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR) boneLocalRotation *= -1f;
if (parentFlipX != flipX) boneLocalRotation += 180;
b.X = Mathf.Lerp(b.X, boneLocalPosition.x, mix);
b.Y = Mathf.Lerp(b.Y, boneLocalPosition.y, mix);
b.Rotation = Mathf.Lerp(b.Rotation, boneLocalRotation, mix);
//b.AppliedRotation = Mathf.Lerp(b.AppliedRotation, boneLocalRotation, mix);
}
}
void GetStartBoneParentFlipState (out bool parentFlipX, out bool parentFlipY) {
parentFlipX = skeleton.ScaleX < 0;
parentFlipY = skeleton.ScaleY < 0;
var parent = this.StartingBone == null ? null : this.StartingBone.Parent;
while (parent != null) {
parentFlipX ^= parent.ScaleX < 0;
parentFlipY ^= parent.ScaleY < 0;
parent = parent.Parent;
}
}
List<Collider> AttachBoundingBoxRagdollColliders (Bone b) {
const string AttachmentNameMarker = "ragdoll";
var colliders = new List<Collider>();
Transform t = boneTable[b];
GameObject go = t.gameObject;
var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
var skinEntries = new List<Skin.SkinEntry>();
foreach (Slot s in skeleton.Slots) {
if (s.Bone == b) {
skin.GetAttachments(skeleton.Slots.IndexOf(s), skinEntries);
foreach (var entry in skinEntries) {
var bbAttachment = entry.Attachment as BoundingBoxAttachment;
if (bbAttachment != null) {
if (!entry.Name.ToLower().Contains(AttachmentNameMarker))
continue;
var bbCollider = go.AddComponent<BoxCollider>();
var bounds = SkeletonUtility.GetBoundingBoxBounds(bbAttachment, thickness);
bbCollider.center = bounds.center;
bbCollider.size = bounds.size;
colliders.Add(bbCollider);
}
}
}
}
return colliders;
}
public class LayerFieldAttribute : PropertyAttribute { }
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
[RequireComponent(typeof(SkeletonRenderer))]
public class SkeletonRagdoll : MonoBehaviour {
static Transform parentSpaceHelper;
#region Inspector
[Header("Hierarchy")]
[SpineBone]
public string startingBoneName = "";
[SpineBone]
public List<string> stopBoneNames = new List<string>();
[Header("Parameters")]
public bool applyOnStart;
[Tooltip("Warning! You will have to re-enable and tune mix values manually if attempting to remove the ragdoll system.")]
public bool disableIK = true;
public bool disableOtherConstraints = false;
[Space(18)]
[Tooltip("Set RootRigidbody IsKinematic to true when Apply is called.")]
public bool pinStartBone;
[Tooltip("Enable Collision between adjacent ragdoll elements (IE: Neck and Head)")]
public bool enableJointCollision;
public bool useGravity = true;
[Tooltip("If no BoundingBox Attachment is attached to a bone, this becomes the default Width or Radius of a Bone's ragdoll Rigidbody")]
public float thickness = 0.125f;
[Tooltip("Default rotational limit value. Min is negative this value, Max is this value.")]
public float rotationLimit = 20;
public float rootMass = 20;
[Tooltip("If your ragdoll seems unstable or uneffected by limits, try lowering this value.")]
[Range(0.01f, 1f)]
public float massFalloffFactor = 0.4f;
[Tooltip("The layer assigned to all of the rigidbody parts.")]
public int colliderLayer = 0;
[Range(0, 1)]
public float mix = 1;
public bool oldRagdollBehaviour = false;
#endregion
ISkeletonAnimation targetSkeletonComponent;
Skeleton skeleton;
struct BoneFlipEntry {
public BoneFlipEntry (bool flipX, bool flipY) {
this.flipX = flipX;
this.flipY = flipY;
}
public bool flipX;
public bool flipY;
}
Dictionary<Bone, Transform> boneTable = new Dictionary<Bone, Transform>();
Dictionary<Bone, BoneFlipEntry> boneFlipTable = new Dictionary<Bone, BoneFlipEntry>();
Transform ragdollRoot;
public Rigidbody RootRigidbody { get; private set; }
public Bone StartingBone { get; private set; }
Vector3 rootOffset;
public Vector3 RootOffset { get { return this.rootOffset; } }
bool isActive;
public bool IsActive { get { return this.isActive; } }
IEnumerator Start () {
if (parentSpaceHelper == null) {
parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
parentSpaceHelper.hideFlags = HideFlags.HideInHierarchy;
}
targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
skeleton = targetSkeletonComponent.Skeleton;
if (applyOnStart) {
yield return null;
Apply();
}
}
#region API
public Rigidbody[] RigidbodyArray {
get {
if (!isActive)
return new Rigidbody[0];
var rigidBodies = new Rigidbody[boneTable.Count];
int i = 0;
foreach (Transform t in boneTable.Values) {
rigidBodies[i] = t.GetComponent<Rigidbody>();
i++;
}
return rigidBodies;
}
}
public Vector3 EstimatedSkeletonPosition {
get { return RootRigidbody.position - rootOffset; }
}
/// <summary>Instantiates the ragdoll simulation and applies its transforms to the skeleton.</summary>
public void Apply () {
isActive = true;
mix = 1;
StartingBone = skeleton.FindBone(startingBoneName);
RecursivelyCreateBoneProxies(StartingBone);
RootRigidbody = boneTable[StartingBone].GetComponent<Rigidbody>();
RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider>();
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
Transform parentTransform;
boneColliders.Add(t.GetComponent<Collider>());
if (b == StartingBone) {
ragdollRoot = new GameObject("RagdollRoot").transform;
ragdollRoot.SetParent(transform, false);
if (b == skeleton.RootBone) { // RagdollRoot is skeleton root's parent, thus the skeleton's scale and position.
ragdollRoot.localPosition = new Vector3(skeleton.X, skeleton.Y, 0);
ragdollRoot.localRotation = (skeleton.ScaleX < 0) ? Quaternion.Euler(0, 0, 180.0f) : Quaternion.identity;
} else {
ragdollRoot.localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, b.Parent.WorldRotationX - b.Parent.ShearX);
}
parentTransform = ragdollRoot;
rootOffset = t.position - transform.position;
} else {
parentTransform = boneTable[b.Parent];
}
// Add joint and attach to parent.
var rbParent = parentTransform.GetComponent<Rigidbody>();
if (rbParent != null) {
var joint = t.gameObject.AddComponent<HingeJoint>();
joint.connectedBody = rbParent;
Vector3 localPos = parentTransform.InverseTransformPoint(t.position);
localPos.x *= 1;
joint.connectedAnchor = localPos;
joint.axis = Vector3.forward;
joint.GetComponent<Rigidbody>().mass = joint.connectedBody.mass * massFalloffFactor;
joint.limits = new JointLimits {
min = -rotationLimit,
max = rotationLimit,
};
joint.useLimits = true;
joint.enableCollision = enableJointCollision;
}
}
// Ignore collisions among bones.
for (int x = 0; x < boneColliders.Count; x++) {
for (int y = 0; y < boneColliders.Count; y++) {
if (x == y) continue;
Physics.IgnoreCollision(boneColliders[x], boneColliders[y]);
}
}
// Destroy existing override-mode SkeletonUtilityBones.
var utilityBones = GetComponentsInChildren<SkeletonUtilityBone>();
if (utilityBones.Length > 0) {
var destroyedUtilityBoneNames = new List<string>();
foreach (var ub in utilityBones) {
if (ub.mode == SkeletonUtilityBone.Mode.Override) {
destroyedUtilityBoneNames.Add(ub.gameObject.name);
Destroy(ub.gameObject);
}
}
if (destroyedUtilityBoneNames.Count > 0) {
string msg = "Destroyed Utility Bones: ";
for (int i = 0; i < destroyedUtilityBoneNames.Count; i++) {
msg += destroyedUtilityBoneNames[i];
if (i != destroyedUtilityBoneNames.Count - 1) {
msg += ",";
}
}
Debug.LogWarning(msg);
}
}
// Disable skeleton constraints.
if (disableIK) {
var ikConstraints = skeleton.IkConstraints;
for (int i = 0, n = ikConstraints.Count; i < n; i++)
ikConstraints.Items[i].Mix = 0;
}
if (disableOtherConstraints) {
var transformConstraints = skeleton.TransformConstraints;
for (int i = 0, n = transformConstraints.Count; i < n; i++) {
transformConstraints.Items[i].MixRotate = 0;
transformConstraints.Items[i].MixScaleX = 0;
transformConstraints.Items[i].MixScaleY = 0;
transformConstraints.Items[i].MixShearY = 0;
transformConstraints.Items[i].MixX = 0;
transformConstraints.Items[i].MixY = 0;
}
var pathConstraints = skeleton.PathConstraints;
for (int i = 0, n = pathConstraints.Count; i < n; i++) {
pathConstraints.Items[i].MixRotate = 0;
pathConstraints.Items[i].MixX = 0;
pathConstraints.Items[i].MixY = 0;
}
}
targetSkeletonComponent.UpdateWorld += UpdateSpineSkeleton;
}
/// <summary>Transitions the mix value from the current value to a target value.</summary>
public Coroutine SmoothMix (float target, float duration) {
return StartCoroutine(SmoothMixCoroutine(target, duration));
}
IEnumerator SmoothMixCoroutine (float target, float duration) {
float startTime = Time.time;
float startMix = mix;
while (mix > 0) {
skeleton.SetBonesToSetupPose();
mix = Mathf.SmoothStep(startMix, target, (Time.time - startTime) / duration);
yield return null;
}
}
/// <summary>Set the transform world position while preserving the ragdoll parts world position.</summary>
public void SetSkeletonPosition (Vector3 worldPosition) {
if (!isActive) {
Debug.LogWarning("Can't call SetSkeletonPosition while Ragdoll is not active!");
return;
}
Vector3 offset = worldPosition - transform.position;
transform.position = worldPosition;
foreach (Transform t in boneTable.Values)
t.position -= offset;
UpdateSpineSkeleton(null);
skeleton.UpdateWorldTransform();
}
/// <summary>Removes the ragdoll instance and effect from the animated skeleton.</summary>
public void Remove () {
isActive = false;
foreach (var t in boneTable.Values)
Destroy(t.gameObject);
Destroy(ragdollRoot.gameObject);
boneTable.Clear();
targetSkeletonComponent.UpdateWorld -= UpdateSpineSkeleton;
}
public Rigidbody GetRigidbody (string boneName) {
var bone = skeleton.FindBone(boneName);
return (bone != null && boneTable.ContainsKey(bone)) ? boneTable[bone].GetComponent<Rigidbody>() : null;
}
#endregion
void RecursivelyCreateBoneProxies (Bone b) {
string boneName = b.Data.Name;
if (stopBoneNames.Contains(boneName))
return;
var boneGameObject = new GameObject(boneName);
boneGameObject.layer = colliderLayer;
Transform t = boneGameObject.transform;
boneTable.Add(b, t);
t.parent = transform;
t.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
t.localRotation = Quaternion.Euler(0, 0, b.WorldRotationX - b.ShearX);
t.localScale = new Vector3(b.WorldScaleX, b.WorldScaleY, 1);
var colliders = AttachBoundingBoxRagdollColliders(b);
if (colliders.Count == 0) {
float length = b.Data.Length;
if (length == 0) {
var ball = boneGameObject.AddComponent<SphereCollider>();
ball.radius = thickness * 0.5f;
} else {
var box = boneGameObject.AddComponent<BoxCollider>();
box.size = new Vector3(length, thickness, thickness);
box.center = new Vector3(length * 0.5f, 0);
}
}
var rb = boneGameObject.AddComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezePositionZ;
foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child);
}
void UpdateSpineSkeleton (ISkeletonAnimation skeletonRenderer) {
bool parentFlipX;
bool parentFlipY;
GetStartBoneParentFlipState(out parentFlipX, out parentFlipY);
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
bool isStartingBone = b == StartingBone;
var parentBone = b.Parent;
Transform parentTransform = isStartingBone ? ragdollRoot : boneTable[parentBone];
if (!isStartingBone) {
var parentBoneFlip = boneFlipTable[parentBone];
parentFlipX = parentBoneFlip.flipX;
parentFlipY = parentBoneFlip.flipY;
}
bool flipX = parentFlipX ^ (b.ScaleX < 0);
bool flipY = parentFlipY ^ (b.ScaleY < 0);
BoneFlipEntry boneFlip;
boneFlipTable.TryGetValue(b, out boneFlip);
boneFlip.flipX = flipX;
boneFlip.flipY = flipY;
boneFlipTable[b] = boneFlip;
bool flipXOR = flipX ^ flipY;
bool parentFlipXOR = parentFlipX ^ parentFlipY;
if (!oldRagdollBehaviour && isStartingBone) {
if (b != skeleton.RootBone) { // RagdollRoot is not skeleton root.
ragdollRoot.localPosition = new Vector3(parentBone.WorldX, parentBone.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
ragdollRoot.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 parentTransformWorldPosition = parentTransform.position;
Quaternion parentTransformWorldRotation = parentTransform.rotation;
parentSpaceHelper.position = parentTransformWorldPosition;
parentSpaceHelper.rotation = parentTransformWorldRotation;
parentSpaceHelper.localScale = parentTransform.lossyScale;
if (oldRagdollBehaviour) {
if (isStartingBone && b != skeleton.RootBone) {
Vector3 localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
parentSpaceHelper.position = ragdollRoot.TransformPoint(localPosition);
parentSpaceHelper.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
parentSpaceHelper.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 boneWorldPosition = t.position;
Vector3 right = parentSpaceHelper.InverseTransformDirection(t.right);
Vector3 boneLocalPosition = parentSpaceHelper.InverseTransformPoint(boneWorldPosition);
float boneLocalRotation = Mathf.Atan2(right.y, right.x) * Mathf.Rad2Deg;
if (flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR != flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR) boneLocalRotation *= -1f;
if (parentFlipX != flipX) boneLocalRotation += 180;
b.X = Mathf.Lerp(b.X, boneLocalPosition.x, mix);
b.Y = Mathf.Lerp(b.Y, boneLocalPosition.y, mix);
b.Rotation = Mathf.Lerp(b.Rotation, boneLocalRotation, mix);
//b.AppliedRotation = Mathf.Lerp(b.AppliedRotation, boneLocalRotation, mix);
}
}
void GetStartBoneParentFlipState (out bool parentFlipX, out bool parentFlipY) {
parentFlipX = skeleton.ScaleX < 0;
parentFlipY = skeleton.ScaleY < 0;
var parent = this.StartingBone == null ? null : this.StartingBone.Parent;
while (parent != null) {
parentFlipX ^= parent.ScaleX < 0;
parentFlipY ^= parent.ScaleY < 0;
parent = parent.Parent;
}
}
List<Collider> AttachBoundingBoxRagdollColliders (Bone b) {
const string AttachmentNameMarker = "ragdoll";
var colliders = new List<Collider>();
Transform t = boneTable[b];
GameObject go = t.gameObject;
var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
var skinEntries = new List<Skin.SkinEntry>();
foreach (Slot s in skeleton.Slots) {
if (s.Bone == b) {
skin.GetAttachments(skeleton.Slots.IndexOf(s), skinEntries);
foreach (var entry in skinEntries) {
var bbAttachment = entry.Attachment as BoundingBoxAttachment;
if (bbAttachment != null) {
if (!entry.Name.ToLower().Contains(AttachmentNameMarker))
continue;
var bbCollider = go.AddComponent<BoxCollider>();
var bounds = SkeletonUtility.GetBoundingBoxBounds(bbAttachment, thickness);
bbCollider.center = bounds.center;
bbCollider.size = bounds.size;
colliders.Add(bbCollider);
}
}
}
}
return colliders;
}
public class LayerFieldAttribute : PropertyAttribute { }
}
}

View File

@@ -1,472 +1,472 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
#if UNITY_2019_2 || UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 // note: 2020.3+ uses old bahavior again
#define HINGE_JOINT_2019_BEHAVIOUR
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
[RequireComponent(typeof(SkeletonRenderer))]
public class SkeletonRagdoll2D : MonoBehaviour {
static Transform parentSpaceHelper;
#region Inspector
[Header("Hierarchy")]
[SpineBone]
public string startingBoneName = "";
[SpineBone]
public List<string> stopBoneNames = new List<string>();
[Header("Parameters")]
public bool applyOnStart;
[Tooltip("Warning! You will have to re-enable and tune mix values manually if attempting to remove the ragdoll system.")]
public bool disableIK = true;
public bool disableOtherConstraints = false;
[Space]
[Tooltip("Set RootRigidbody IsKinematic to true when Apply is called.")]
public bool pinStartBone;
public float gravityScale = 1;
[Tooltip("If no BoundingBox Attachment is attached to a bone, this becomes the default Width or Radius of a Bone's ragdoll Rigidbody")]
public float thickness = 0.125f;
[Tooltip("Default rotational limit value. Min is negative this value, Max is this value.")]
public float rotationLimit = 20;
public float rootMass = 20;
[Tooltip("If your ragdoll seems unstable or uneffected by limits, try lowering this value.")]
[Range(0.01f, 1f)]
public float massFalloffFactor = 0.4f;
[Tooltip("The layer assigned to all of the rigidbody parts.")]
[SkeletonRagdoll.LayerField]
public int colliderLayer = 0;
[Range(0, 1)]
public float mix = 1;
public bool oldRagdollBehaviour = false;
#endregion
ISkeletonAnimation targetSkeletonComponent;
Skeleton skeleton;
struct BoneFlipEntry {
public BoneFlipEntry (bool flipX, bool flipY) {
this.flipX = flipX;
this.flipY = flipY;
}
public bool flipX;
public bool flipY;
}
Dictionary<Bone, Transform> boneTable = new Dictionary<Bone, Transform>();
Dictionary<Bone, BoneFlipEntry> boneFlipTable = new Dictionary<Bone, BoneFlipEntry>();
Transform ragdollRoot;
public Rigidbody2D RootRigidbody { get; private set; }
public Bone StartingBone { get; private set; }
Vector2 rootOffset;
public Vector3 RootOffset { get { return this.rootOffset; } }
bool isActive;
public bool IsActive { get { return this.isActive; } }
IEnumerator Start () {
if (parentSpaceHelper == null) {
parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
}
targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
skeleton = targetSkeletonComponent.Skeleton;
if (applyOnStart) {
yield return null;
Apply();
}
}
#region API
public Rigidbody2D[] RigidbodyArray {
get {
if (!isActive)
return new Rigidbody2D[0];
var rigidBodies = new Rigidbody2D[boneTable.Count];
int i = 0;
foreach (Transform t in boneTable.Values) {
rigidBodies[i] = t.GetComponent<Rigidbody2D>();
i++;
}
return rigidBodies;
}
}
public Vector3 EstimatedSkeletonPosition {
get { return this.RootRigidbody.position - rootOffset; }
}
/// <summary>Instantiates the ragdoll simulation and applies its transforms to the skeleton.</summary>
public void Apply () {
isActive = true;
mix = 1;
Bone startingBone = this.StartingBone = skeleton.FindBone(startingBoneName);
RecursivelyCreateBoneProxies(startingBone);
RootRigidbody = boneTable[startingBone].GetComponent<Rigidbody2D>();
RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider2D>();
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
Transform parentTransform;
boneColliders.Add(t.GetComponent<Collider2D>());
if (b == startingBone) {
ragdollRoot = new GameObject("RagdollRoot").transform;
ragdollRoot.SetParent(transform, false);
if (b == skeleton.RootBone) { // RagdollRoot is skeleton root's parent, thus the skeleton's scale and position.
ragdollRoot.localPosition = new Vector3(skeleton.X, skeleton.Y, 0);
ragdollRoot.localRotation = (skeleton.ScaleX < 0) ? Quaternion.Euler(0, 0, 180.0f) : Quaternion.identity;
} else {
ragdollRoot.localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, b.Parent.WorldRotationX - b.Parent.ShearX);
}
parentTransform = ragdollRoot;
rootOffset = t.position - transform.position;
} else {
parentTransform = boneTable[b.Parent];
}
// Add joint and attach to parent.
var rbParent = parentTransform.GetComponent<Rigidbody2D>();
if (rbParent != null) {
var joint = t.gameObject.AddComponent<HingeJoint2D>();
joint.connectedBody = rbParent;
Vector3 localPos = parentTransform.InverseTransformPoint(t.position);
joint.connectedAnchor = localPos;
joint.GetComponent<Rigidbody2D>().mass = joint.connectedBody.mass * massFalloffFactor;
#if HINGE_JOINT_2019_BEHAVIOUR
float referenceAngle = (rbParent.transform.eulerAngles.z - t.eulerAngles.z + 360f) % 360f;
float minAngle = referenceAngle - rotationLimit;
float maxAngle = referenceAngle + rotationLimit;
if (maxAngle > 180f) {
minAngle -= 360f;
maxAngle -= 360f;
}
#else
float minAngle = -rotationLimit;
float maxAngle = rotationLimit;
#endif
joint.limits = new JointAngleLimits2D {
min = minAngle,
max = maxAngle
};
joint.useLimits = true;
}
}
// Ignore collisions among bones.
for (int x = 0; x < boneColliders.Count; x++) {
for (int y = 0; y < boneColliders.Count; y++) {
if (x == y) continue;
Physics2D.IgnoreCollision(boneColliders[x], boneColliders[y]);
}
}
// Destroy existing override-mode SkeletonUtility bones.
var utilityBones = GetComponentsInChildren<SkeletonUtilityBone>();
if (utilityBones.Length > 0) {
var destroyedUtilityBoneNames = new List<string>();
foreach (var ub in utilityBones) {
if (ub.mode == SkeletonUtilityBone.Mode.Override) {
destroyedUtilityBoneNames.Add(ub.gameObject.name);
Destroy(ub.gameObject);
}
}
if (destroyedUtilityBoneNames.Count > 0) {
string msg = "Destroyed Utility Bones: ";
for (int i = 0; i < destroyedUtilityBoneNames.Count; i++) {
msg += destroyedUtilityBoneNames[i];
if (i != destroyedUtilityBoneNames.Count - 1) {
msg += ",";
}
}
Debug.LogWarning(msg);
}
}
// Disable skeleton constraints.
if (disableIK) {
var ikConstraints = skeleton.IkConstraints;
for (int i = 0, n = ikConstraints.Count; i < n; i++)
ikConstraints.Items[i].Mix = 0;
}
if (disableOtherConstraints) {
var transformConstraints = skeleton.TransformConstraints;
for (int i = 0, n = transformConstraints.Count; i < n; i++) {
transformConstraints.Items[i].MixRotate = 0;
transformConstraints.Items[i].MixScaleX = 0;
transformConstraints.Items[i].MixScaleY = 0;
transformConstraints.Items[i].MixShearY = 0;
transformConstraints.Items[i].MixX = 0;
transformConstraints.Items[i].MixY = 0;
}
var pathConstraints = skeleton.PathConstraints;
for (int i = 0, n = pathConstraints.Count; i < n; i++) {
pathConstraints.Items[i].MixRotate = 0;
pathConstraints.Items[i].MixX = 0;
pathConstraints.Items[i].MixY = 0;
}
}
targetSkeletonComponent.UpdateWorld += UpdateSpineSkeleton;
}
/// <summary>Transitions the mix value from the current value to a target value.</summary>
public Coroutine SmoothMix (float target, float duration) {
return StartCoroutine(SmoothMixCoroutine(target, duration));
}
IEnumerator SmoothMixCoroutine (float target, float duration) {
float startTime = Time.time;
float startMix = mix;
while (mix > 0) {
skeleton.SetBonesToSetupPose();
mix = Mathf.SmoothStep(startMix, target, (Time.time - startTime) / duration);
yield return null;
}
}
/// <summary>Set the transform world position while preserving the ragdoll parts world position.</summary>
public void SetSkeletonPosition (Vector3 worldPosition) {
if (!isActive) {
Debug.LogWarning("Can't call SetSkeletonPosition while Ragdoll is not active!");
return;
}
Vector3 offset = worldPosition - transform.position;
transform.position = worldPosition;
foreach (Transform t in boneTable.Values)
t.position -= offset;
UpdateSpineSkeleton(null);
skeleton.UpdateWorldTransform();
}
/// <summary>Removes the ragdoll instance and effect from the animated skeleton.</summary>
public void Remove () {
isActive = false;
foreach (var t in boneTable.Values)
Destroy(t.gameObject);
Destroy(ragdollRoot.gameObject);
boneTable.Clear();
targetSkeletonComponent.UpdateWorld -= UpdateSpineSkeleton;
}
public Rigidbody2D GetRigidbody (string boneName) {
var bone = skeleton.FindBone(boneName);
return (bone != null && boneTable.ContainsKey(bone)) ? boneTable[bone].GetComponent<Rigidbody2D>() : null;
}
#endregion
/// <summary>Generates the ragdoll simulation's Transform and joint setup.</summary>
void RecursivelyCreateBoneProxies (Bone b) {
string boneName = b.Data.Name;
if (stopBoneNames.Contains(boneName))
return;
var boneGameObject = new GameObject(boneName);
boneGameObject.layer = this.colliderLayer;
Transform t = boneGameObject.transform;
boneTable.Add(b, t);
t.parent = transform;
t.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
t.localRotation = Quaternion.Euler(0, 0, b.WorldRotationX - b.ShearX);
t.localScale = new Vector3(b.WorldScaleX, b.WorldScaleY, 1);
var colliders = AttachBoundingBoxRagdollColliders(b, boneGameObject, skeleton, this.gravityScale);
if (colliders.Count == 0) {
float length = b.Data.Length;
if (length == 0) {
var circle = boneGameObject.AddComponent<CircleCollider2D>();
circle.radius = thickness * 0.5f;
} else {
var box = boneGameObject.AddComponent<BoxCollider2D>();
box.size = new Vector2(length, thickness);
box.offset = new Vector2(length * 0.5f, 0); // box.center in UNITY_4
}
}
var rb = boneGameObject.GetComponent<Rigidbody2D>();
if (rb == null) rb = boneGameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = this.gravityScale;
foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child);
}
/// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary>
void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) {
bool parentFlipX;
bool parentFlipY;
var startingBone = this.StartingBone;
GetStartBoneParentFlipState(out parentFlipX, out parentFlipY);
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
bool isStartingBone = (b == startingBone);
var parentBone = b.Parent;
Transform parentTransform = isStartingBone ? ragdollRoot : boneTable[parentBone];
if (!isStartingBone) {
var parentBoneFlip = boneFlipTable[parentBone];
parentFlipX = parentBoneFlip.flipX;
parentFlipY = parentBoneFlip.flipY;
}
bool flipX = parentFlipX ^ (b.ScaleX < 0);
bool flipY = parentFlipY ^ (b.ScaleY < 0);
BoneFlipEntry boneFlip;
boneFlipTable.TryGetValue(b, out boneFlip);
boneFlip.flipX = flipX;
boneFlip.flipY = flipY;
boneFlipTable[b] = boneFlip;
bool flipXOR = flipX ^ flipY;
bool parentFlipXOR = parentFlipX ^ parentFlipY;
if (!oldRagdollBehaviour && isStartingBone) {
if (b != skeleton.RootBone) { // RagdollRoot is not skeleton root.
ragdollRoot.localPosition = new Vector3(parentBone.WorldX, parentBone.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
ragdollRoot.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 parentTransformWorldPosition = parentTransform.position;
Quaternion parentTransformWorldRotation = parentTransform.rotation;
parentSpaceHelper.position = parentTransformWorldPosition;
parentSpaceHelper.rotation = parentTransformWorldRotation;
parentSpaceHelper.localScale = parentTransform.lossyScale;
if (oldRagdollBehaviour) {
if (isStartingBone && b != skeleton.RootBone) {
Vector3 localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
parentSpaceHelper.position = ragdollRoot.TransformPoint(localPosition);
parentSpaceHelper.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
parentSpaceHelper.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 boneWorldPosition = t.position;
Vector3 right = parentSpaceHelper.InverseTransformDirection(t.right);
Vector3 boneLocalPosition = parentSpaceHelper.InverseTransformPoint(boneWorldPosition);
float boneLocalRotation = Mathf.Atan2(right.y, right.x) * Mathf.Rad2Deg;
if (flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR != flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR) boneLocalRotation *= -1f;
if (parentFlipX != flipX) boneLocalRotation += 180;
b.X = Mathf.Lerp(b.X, boneLocalPosition.x, mix);
b.Y = Mathf.Lerp(b.Y, boneLocalPosition.y, mix);
b.Rotation = Mathf.Lerp(b.Rotation, boneLocalRotation, mix);
//b.AppliedRotation = Mathf.Lerp(b.AppliedRotation, boneLocalRotation, mix);
}
}
void GetStartBoneParentFlipState (out bool parentFlipX, out bool parentFlipY) {
parentFlipX = skeleton.ScaleX < 0;
parentFlipY = skeleton.ScaleY < 0;
var parent = this.StartingBone == null ? null : this.StartingBone.Parent;
while (parent != null) {
parentFlipX ^= parent.ScaleX < 0;
parentFlipY ^= parent.ScaleY < 0;
parent = parent.Parent;
}
}
static List<Collider2D> AttachBoundingBoxRagdollColliders (Bone b, GameObject go, Skeleton skeleton, float gravityScale) {
const string AttachmentNameMarker = "ragdoll";
var colliders = new List<Collider2D>();
var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
var skinEntries = new List<Skin.SkinEntry>();
foreach (Slot slot in skeleton.Slots) {
if (slot.Bone == b) {
skin.GetAttachments(skeleton.Slots.IndexOf(slot), skinEntries);
bool bbAttachmentAdded = false;
foreach (var entry in skinEntries) {
var bbAttachment = entry.Attachment as BoundingBoxAttachment;
if (bbAttachment != null) {
if (!entry.Name.ToLower().Contains(AttachmentNameMarker))
continue;
bbAttachmentAdded = true;
var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(bbAttachment, slot, go, isTrigger: false);
colliders.Add(bbCollider);
}
}
if (bbAttachmentAdded)
SkeletonUtility.AddBoneRigidbody2D(go, isKinematic: false, gravityScale: gravityScale);
}
}
return colliders;
}
static Vector3 FlipScale (bool flipX, bool flipY) {
return new Vector3(flipX ? -1f : 1f, flipY ? -1f : 1f, 1f);
}
#if UNITY_EDITOR
void OnDrawGizmosSelected () {
if (isActive) {
Gizmos.DrawWireSphere(transform.position, thickness * 1.2f);
Vector3 newTransformPos = RootRigidbody.position - rootOffset;
Gizmos.DrawLine(transform.position, newTransformPos);
Gizmos.DrawWireSphere(newTransformPos, thickness * 1.2f);
}
}
#endif
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
// Contributed by: Mitch Thompson
#if UNITY_2019_2 || UNITY_2019_3 || UNITY_2019_4 || UNITY_2020_1 || UNITY_2020_2 // note: 2020.3+ uses old bahavior again
#define HINGE_JOINT_2019_BEHAVIOUR
#endif
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
[RequireComponent(typeof(SkeletonRenderer))]
public class SkeletonRagdoll2D : MonoBehaviour {
static Transform parentSpaceHelper;
#region Inspector
[Header("Hierarchy")]
[SpineBone]
public string startingBoneName = "";
[SpineBone]
public List<string> stopBoneNames = new List<string>();
[Header("Parameters")]
public bool applyOnStart;
[Tooltip("Warning! You will have to re-enable and tune mix values manually if attempting to remove the ragdoll system.")]
public bool disableIK = true;
public bool disableOtherConstraints = false;
[Space]
[Tooltip("Set RootRigidbody IsKinematic to true when Apply is called.")]
public bool pinStartBone;
public float gravityScale = 1;
[Tooltip("If no BoundingBox Attachment is attached to a bone, this becomes the default Width or Radius of a Bone's ragdoll Rigidbody")]
public float thickness = 0.125f;
[Tooltip("Default rotational limit value. Min is negative this value, Max is this value.")]
public float rotationLimit = 20;
public float rootMass = 20;
[Tooltip("If your ragdoll seems unstable or uneffected by limits, try lowering this value.")]
[Range(0.01f, 1f)]
public float massFalloffFactor = 0.4f;
[Tooltip("The layer assigned to all of the rigidbody parts.")]
[SkeletonRagdoll.LayerField]
public int colliderLayer = 0;
[Range(0, 1)]
public float mix = 1;
public bool oldRagdollBehaviour = false;
#endregion
ISkeletonAnimation targetSkeletonComponent;
Skeleton skeleton;
struct BoneFlipEntry {
public BoneFlipEntry (bool flipX, bool flipY) {
this.flipX = flipX;
this.flipY = flipY;
}
public bool flipX;
public bool flipY;
}
Dictionary<Bone, Transform> boneTable = new Dictionary<Bone, Transform>();
Dictionary<Bone, BoneFlipEntry> boneFlipTable = new Dictionary<Bone, BoneFlipEntry>();
Transform ragdollRoot;
public Rigidbody2D RootRigidbody { get; private set; }
public Bone StartingBone { get; private set; }
Vector2 rootOffset;
public Vector3 RootOffset { get { return this.rootOffset; } }
bool isActive;
public bool IsActive { get { return this.isActive; } }
IEnumerator Start () {
if (parentSpaceHelper == null) {
parentSpaceHelper = (new GameObject("Parent Space Helper")).transform;
}
targetSkeletonComponent = GetComponent<SkeletonRenderer>() as ISkeletonAnimation;
if (targetSkeletonComponent == null) Debug.LogError("Attached Spine component does not implement ISkeletonAnimation. This script is not compatible.");
skeleton = targetSkeletonComponent.Skeleton;
if (applyOnStart) {
yield return null;
Apply();
}
}
#region API
public Rigidbody2D[] RigidbodyArray {
get {
if (!isActive)
return new Rigidbody2D[0];
var rigidBodies = new Rigidbody2D[boneTable.Count];
int i = 0;
foreach (Transform t in boneTable.Values) {
rigidBodies[i] = t.GetComponent<Rigidbody2D>();
i++;
}
return rigidBodies;
}
}
public Vector3 EstimatedSkeletonPosition {
get { return this.RootRigidbody.position - rootOffset; }
}
/// <summary>Instantiates the ragdoll simulation and applies its transforms to the skeleton.</summary>
public void Apply () {
isActive = true;
mix = 1;
Bone startingBone = this.StartingBone = skeleton.FindBone(startingBoneName);
RecursivelyCreateBoneProxies(startingBone);
RootRigidbody = boneTable[startingBone].GetComponent<Rigidbody2D>();
RootRigidbody.isKinematic = pinStartBone;
RootRigidbody.mass = rootMass;
var boneColliders = new List<Collider2D>();
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
Transform parentTransform;
boneColliders.Add(t.GetComponent<Collider2D>());
if (b == startingBone) {
ragdollRoot = new GameObject("RagdollRoot").transform;
ragdollRoot.SetParent(transform, false);
if (b == skeleton.RootBone) { // RagdollRoot is skeleton root's parent, thus the skeleton's scale and position.
ragdollRoot.localPosition = new Vector3(skeleton.X, skeleton.Y, 0);
ragdollRoot.localRotation = (skeleton.ScaleX < 0) ? Quaternion.Euler(0, 0, 180.0f) : Quaternion.identity;
} else {
ragdollRoot.localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, b.Parent.WorldRotationX - b.Parent.ShearX);
}
parentTransform = ragdollRoot;
rootOffset = t.position - transform.position;
} else {
parentTransform = boneTable[b.Parent];
}
// Add joint and attach to parent.
var rbParent = parentTransform.GetComponent<Rigidbody2D>();
if (rbParent != null) {
var joint = t.gameObject.AddComponent<HingeJoint2D>();
joint.connectedBody = rbParent;
Vector3 localPos = parentTransform.InverseTransformPoint(t.position);
joint.connectedAnchor = localPos;
joint.GetComponent<Rigidbody2D>().mass = joint.connectedBody.mass * massFalloffFactor;
#if HINGE_JOINT_2019_BEHAVIOUR
float referenceAngle = (rbParent.transform.eulerAngles.z - t.eulerAngles.z + 360f) % 360f;
float minAngle = referenceAngle - rotationLimit;
float maxAngle = referenceAngle + rotationLimit;
if (maxAngle > 180f) {
minAngle -= 360f;
maxAngle -= 360f;
}
#else
float minAngle = -rotationLimit;
float maxAngle = rotationLimit;
#endif
joint.limits = new JointAngleLimits2D {
min = minAngle,
max = maxAngle
};
joint.useLimits = true;
}
}
// Ignore collisions among bones.
for (int x = 0; x < boneColliders.Count; x++) {
for (int y = 0; y < boneColliders.Count; y++) {
if (x == y) continue;
Physics2D.IgnoreCollision(boneColliders[x], boneColliders[y]);
}
}
// Destroy existing override-mode SkeletonUtility bones.
var utilityBones = GetComponentsInChildren<SkeletonUtilityBone>();
if (utilityBones.Length > 0) {
var destroyedUtilityBoneNames = new List<string>();
foreach (var ub in utilityBones) {
if (ub.mode == SkeletonUtilityBone.Mode.Override) {
destroyedUtilityBoneNames.Add(ub.gameObject.name);
Destroy(ub.gameObject);
}
}
if (destroyedUtilityBoneNames.Count > 0) {
string msg = "Destroyed Utility Bones: ";
for (int i = 0; i < destroyedUtilityBoneNames.Count; i++) {
msg += destroyedUtilityBoneNames[i];
if (i != destroyedUtilityBoneNames.Count - 1) {
msg += ",";
}
}
Debug.LogWarning(msg);
}
}
// Disable skeleton constraints.
if (disableIK) {
var ikConstraints = skeleton.IkConstraints;
for (int i = 0, n = ikConstraints.Count; i < n; i++)
ikConstraints.Items[i].Mix = 0;
}
if (disableOtherConstraints) {
var transformConstraints = skeleton.TransformConstraints;
for (int i = 0, n = transformConstraints.Count; i < n; i++) {
transformConstraints.Items[i].MixRotate = 0;
transformConstraints.Items[i].MixScaleX = 0;
transformConstraints.Items[i].MixScaleY = 0;
transformConstraints.Items[i].MixShearY = 0;
transformConstraints.Items[i].MixX = 0;
transformConstraints.Items[i].MixY = 0;
}
var pathConstraints = skeleton.PathConstraints;
for (int i = 0, n = pathConstraints.Count; i < n; i++) {
pathConstraints.Items[i].MixRotate = 0;
pathConstraints.Items[i].MixX = 0;
pathConstraints.Items[i].MixY = 0;
}
}
targetSkeletonComponent.UpdateWorld += UpdateSpineSkeleton;
}
/// <summary>Transitions the mix value from the current value to a target value.</summary>
public Coroutine SmoothMix (float target, float duration) {
return StartCoroutine(SmoothMixCoroutine(target, duration));
}
IEnumerator SmoothMixCoroutine (float target, float duration) {
float startTime = Time.time;
float startMix = mix;
while (mix > 0) {
skeleton.SetBonesToSetupPose();
mix = Mathf.SmoothStep(startMix, target, (Time.time - startTime) / duration);
yield return null;
}
}
/// <summary>Set the transform world position while preserving the ragdoll parts world position.</summary>
public void SetSkeletonPosition (Vector3 worldPosition) {
if (!isActive) {
Debug.LogWarning("Can't call SetSkeletonPosition while Ragdoll is not active!");
return;
}
Vector3 offset = worldPosition - transform.position;
transform.position = worldPosition;
foreach (Transform t in boneTable.Values)
t.position -= offset;
UpdateSpineSkeleton(null);
skeleton.UpdateWorldTransform();
}
/// <summary>Removes the ragdoll instance and effect from the animated skeleton.</summary>
public void Remove () {
isActive = false;
foreach (var t in boneTable.Values)
Destroy(t.gameObject);
Destroy(ragdollRoot.gameObject);
boneTable.Clear();
targetSkeletonComponent.UpdateWorld -= UpdateSpineSkeleton;
}
public Rigidbody2D GetRigidbody (string boneName) {
var bone = skeleton.FindBone(boneName);
return (bone != null && boneTable.ContainsKey(bone)) ? boneTable[bone].GetComponent<Rigidbody2D>() : null;
}
#endregion
/// <summary>Generates the ragdoll simulation's Transform and joint setup.</summary>
void RecursivelyCreateBoneProxies (Bone b) {
string boneName = b.Data.Name;
if (stopBoneNames.Contains(boneName))
return;
var boneGameObject = new GameObject(boneName);
boneGameObject.layer = this.colliderLayer;
Transform t = boneGameObject.transform;
boneTable.Add(b, t);
t.parent = transform;
t.localPosition = new Vector3(b.WorldX, b.WorldY, 0);
t.localRotation = Quaternion.Euler(0, 0, b.WorldRotationX - b.ShearX);
t.localScale = new Vector3(b.WorldScaleX, b.WorldScaleY, 1);
var colliders = AttachBoundingBoxRagdollColliders(b, boneGameObject, skeleton, this.gravityScale);
if (colliders.Count == 0) {
float length = b.Data.Length;
if (length == 0) {
var circle = boneGameObject.AddComponent<CircleCollider2D>();
circle.radius = thickness * 0.5f;
} else {
var box = boneGameObject.AddComponent<BoxCollider2D>();
box.size = new Vector2(length, thickness);
box.offset = new Vector2(length * 0.5f, 0); // box.center in UNITY_4
}
}
var rb = boneGameObject.GetComponent<Rigidbody2D>();
if (rb == null) rb = boneGameObject.AddComponent<Rigidbody2D>();
rb.gravityScale = this.gravityScale;
foreach (Bone child in b.Children)
RecursivelyCreateBoneProxies(child);
}
/// <summary>Performed every skeleton animation update to translate Unity Transforms positions into Spine bone transforms.</summary>
void UpdateSpineSkeleton (ISkeletonAnimation animatedSkeleton) {
bool parentFlipX;
bool parentFlipY;
var startingBone = this.StartingBone;
GetStartBoneParentFlipState(out parentFlipX, out parentFlipY);
foreach (var pair in boneTable) {
var b = pair.Key;
var t = pair.Value;
bool isStartingBone = (b == startingBone);
var parentBone = b.Parent;
Transform parentTransform = isStartingBone ? ragdollRoot : boneTable[parentBone];
if (!isStartingBone) {
var parentBoneFlip = boneFlipTable[parentBone];
parentFlipX = parentBoneFlip.flipX;
parentFlipY = parentBoneFlip.flipY;
}
bool flipX = parentFlipX ^ (b.ScaleX < 0);
bool flipY = parentFlipY ^ (b.ScaleY < 0);
BoneFlipEntry boneFlip;
boneFlipTable.TryGetValue(b, out boneFlip);
boneFlip.flipX = flipX;
boneFlip.flipY = flipY;
boneFlipTable[b] = boneFlip;
bool flipXOR = flipX ^ flipY;
bool parentFlipXOR = parentFlipX ^ parentFlipY;
if (!oldRagdollBehaviour && isStartingBone) {
if (b != skeleton.RootBone) { // RagdollRoot is not skeleton root.
ragdollRoot.localPosition = new Vector3(parentBone.WorldX, parentBone.WorldY, 0);
ragdollRoot.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
ragdollRoot.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 parentTransformWorldPosition = parentTransform.position;
Quaternion parentTransformWorldRotation = parentTransform.rotation;
parentSpaceHelper.position = parentTransformWorldPosition;
parentSpaceHelper.rotation = parentTransformWorldRotation;
parentSpaceHelper.localScale = parentTransform.lossyScale;
if (oldRagdollBehaviour) {
if (isStartingBone && b != skeleton.RootBone) {
Vector3 localPosition = new Vector3(b.Parent.WorldX, b.Parent.WorldY, 0);
parentSpaceHelper.position = ragdollRoot.TransformPoint(localPosition);
parentSpaceHelper.localRotation = Quaternion.Euler(0, 0, parentBone.WorldRotationX - parentBone.ShearX);
parentSpaceHelper.localScale = new Vector3(parentBone.WorldScaleX, parentBone.WorldScaleY, 1);
}
}
Vector3 boneWorldPosition = t.position;
Vector3 right = parentSpaceHelper.InverseTransformDirection(t.right);
Vector3 boneLocalPosition = parentSpaceHelper.InverseTransformPoint(boneWorldPosition);
float boneLocalRotation = Mathf.Atan2(right.y, right.x) * Mathf.Rad2Deg;
if (flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR != flipXOR) boneLocalPosition.y *= -1f;
if (parentFlipXOR) boneLocalRotation *= -1f;
if (parentFlipX != flipX) boneLocalRotation += 180;
b.X = Mathf.Lerp(b.X, boneLocalPosition.x, mix);
b.Y = Mathf.Lerp(b.Y, boneLocalPosition.y, mix);
b.Rotation = Mathf.Lerp(b.Rotation, boneLocalRotation, mix);
//b.AppliedRotation = Mathf.Lerp(b.AppliedRotation, boneLocalRotation, mix);
}
}
void GetStartBoneParentFlipState (out bool parentFlipX, out bool parentFlipY) {
parentFlipX = skeleton.ScaleX < 0;
parentFlipY = skeleton.ScaleY < 0;
var parent = this.StartingBone == null ? null : this.StartingBone.Parent;
while (parent != null) {
parentFlipX ^= parent.ScaleX < 0;
parentFlipY ^= parent.ScaleY < 0;
parent = parent.Parent;
}
}
static List<Collider2D> AttachBoundingBoxRagdollColliders (Bone b, GameObject go, Skeleton skeleton, float gravityScale) {
const string AttachmentNameMarker = "ragdoll";
var colliders = new List<Collider2D>();
var skin = skeleton.Skin ?? skeleton.Data.DefaultSkin;
var skinEntries = new List<Skin.SkinEntry>();
foreach (Slot slot in skeleton.Slots) {
if (slot.Bone == b) {
skin.GetAttachments(skeleton.Slots.IndexOf(slot), skinEntries);
bool bbAttachmentAdded = false;
foreach (var entry in skinEntries) {
var bbAttachment = entry.Attachment as BoundingBoxAttachment;
if (bbAttachment != null) {
if (!entry.Name.ToLower().Contains(AttachmentNameMarker))
continue;
bbAttachmentAdded = true;
var bbCollider = SkeletonUtility.AddBoundingBoxAsComponent(bbAttachment, slot, go, isTrigger: false);
colliders.Add(bbCollider);
}
}
if (bbAttachmentAdded)
SkeletonUtility.AddBoneRigidbody2D(go, isKinematic: false, gravityScale: gravityScale);
}
}
return colliders;
}
static Vector3 FlipScale (bool flipX, bool flipY) {
return new Vector3(flipX ? -1f : 1f, flipY ? -1f : 1f, 1f);
}
#if UNITY_EDITOR
void OnDrawGizmosSelected () {
if (isActive) {
Gizmos.DrawWireSphere(transform.position, thickness * 1.2f);
Vector3 newTransformPos = RootRigidbody.position - rootOffset;
Gizmos.DrawLine(transform.position, newTransformPos);
Gizmos.DrawWireSphere(newTransformPos, thickness * 1.2f);
}
}
#endif
}
}

View File

@@ -1,84 +1,84 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System.Collections;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonUtilityEyeConstraint : SkeletonUtilityConstraint {
public Transform[] eyes;
public float radius = 0.5f;
public Transform target;
public Vector3 targetPosition;
public float speed = 10;
Vector3[] origins;
Vector3 centerPoint;
protected override void OnEnable () {
if (!Application.isPlaying) return;
base.OnEnable();
Bounds centerBounds = new Bounds(eyes[0].localPosition, Vector3.zero);
origins = new Vector3[eyes.Length];
for (int i = 0; i < eyes.Length; i++) {
origins[i] = eyes[i].localPosition;
centerBounds.Encapsulate(origins[i]);
}
centerPoint = centerBounds.center;
}
protected override void OnDisable () {
if (!Application.isPlaying) return;
for (int i = 0; i < eyes.Length; i++) {
eyes[i].localPosition = origins[i];
}
base.OnDisable();
}
public override void DoUpdate () {
if (target != null) targetPosition = target.position;
Vector3 goal = targetPosition;
Vector3 center = transform.TransformPoint(centerPoint);
Vector3 dir = goal - center;
if (dir.magnitude > 1)
dir.Normalize();
for (int i = 0; i < eyes.Length; i++) {
center = transform.TransformPoint(origins[i]);
eyes[i].position = Vector3.MoveTowards(eyes[i].position, center + (dir * radius * hierarchy.PositionScale),
speed * hierarchy.PositionScale * Time.deltaTime);
}
}
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System.Collections;
using UnityEngine;
namespace Spine.Unity.Examples {
public class SkeletonUtilityEyeConstraint : SkeletonUtilityConstraint {
public Transform[] eyes;
public float radius = 0.5f;
public Transform target;
public Vector3 targetPosition;
public float speed = 10;
Vector3[] origins;
Vector3 centerPoint;
protected override void OnEnable () {
if (!Application.isPlaying) return;
base.OnEnable();
Bounds centerBounds = new Bounds(eyes[0].localPosition, Vector3.zero);
origins = new Vector3[eyes.Length];
for (int i = 0; i < eyes.Length; i++) {
origins[i] = eyes[i].localPosition;
centerBounds.Encapsulate(origins[i]);
}
centerPoint = centerBounds.center;
}
protected override void OnDisable () {
if (!Application.isPlaying) return;
for (int i = 0; i < eyes.Length; i++) {
eyes[i].localPosition = origins[i];
}
base.OnDisable();
}
public override void DoUpdate () {
if (target != null) targetPosition = target.position;
Vector3 goal = targetPosition;
Vector3 center = transform.TransformPoint(centerPoint);
Vector3 dir = goal - center;
if (dir.magnitude > 1)
dir.Normalize();
for (int i = 0; i < eyes.Length; i++) {
center = transform.TransformPoint(origins[i]);
eyes[i].position = Vector3.MoveTowards(eyes[i].position, center + (dir * radius * hierarchy.PositionScale),
speed * hierarchy.PositionScale * Time.deltaTime);
}
}
}
}

View File

@@ -1,140 +1,140 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEW_PREFAB_SYSTEM
#endif
using UnityEngine;
namespace Spine.Unity.Examples {
#if NEW_PREFAB_SYSTEM
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
[RequireComponent(typeof(SkeletonUtilityBone))]
public class SkeletonUtilityGroundConstraint : SkeletonUtilityConstraint {
[Tooltip("LayerMask for what objects to raycast against")]
public LayerMask groundMask;
[Tooltip("Use 2D")]
public bool use2D = false;
[Tooltip("Uses SphereCast for 3D mode and CircleCast for 2D mode")]
public bool useRadius = false;
[Tooltip("The Radius")]
public float castRadius = 0.1f;
[Tooltip("How high above the target bone to begin casting from")]
public float castDistance = 5f;
[Tooltip("X-Axis adjustment")]
public float castOffset = 0;
[Tooltip("Y-Axis adjustment")]
public float groundOffset = 0;
[Tooltip("How fast the target IK position adjusts to the ground. Use smaller values to prevent snapping")]
public float adjustSpeed = 5;
Vector3 rayOrigin;
Vector3 rayDir = new Vector3(0, -1, 0);
float hitY;
float lastHitY;
protected override void OnEnable () {
base.OnEnable();
lastHitY = transform.position.y;
}
public override void DoUpdate () {
rayOrigin = transform.position + new Vector3(castOffset, castDistance, 0);
float positionScale = hierarchy.PositionScale;
float adjustDistanceThisFrame = adjustSpeed * positionScale * Time.deltaTime;
hitY = float.MinValue;
if (use2D) {
RaycastHit2D hit;
if (useRadius)
hit = Physics2D.CircleCast(rayOrigin, castRadius, rayDir, castDistance + groundOffset, groundMask);
else
hit = Physics2D.Raycast(rayOrigin, rayDir, castDistance + groundOffset, groundMask);
if (hit.collider != null) {
hitY = hit.point.y + groundOffset;
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
} else {
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
}
} else {
RaycastHit hit;
bool validHit = false;
if (useRadius)
validHit = Physics.SphereCast(rayOrigin, castRadius, rayDir, out hit, castDistance + groundOffset, groundMask);
else
validHit = Physics.Raycast(rayOrigin, rayDir, out hit, castDistance + groundOffset, groundMask);
if (validHit) {
hitY = hit.point.y + groundOffset;
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
} else {
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
}
}
Vector3 v = transform.position;
v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue);
transform.position = v;
bone.bone.X = transform.localPosition.x / hierarchy.PositionScale;
bone.bone.Y = transform.localPosition.y / hierarchy.PositionScale;
lastHitY = hitY;
}
void OnDrawGizmos () {
Vector3 hitEnd = rayOrigin + (rayDir * Mathf.Min(castDistance, rayOrigin.y - hitY));
Vector3 clearEnd = rayOrigin + (rayDir * castDistance);
Gizmos.DrawLine(rayOrigin, hitEnd);
if (useRadius) {
Gizmos.DrawLine(new Vector3(hitEnd.x - castRadius, hitEnd.y - groundOffset, hitEnd.z), new Vector3(hitEnd.x + castRadius, hitEnd.y - groundOffset, hitEnd.z));
Gizmos.DrawLine(new Vector3(clearEnd.x - castRadius, clearEnd.y, clearEnd.z), new Vector3(clearEnd.x + castRadius, clearEnd.y, clearEnd.z));
}
Gizmos.color = Color.red;
Gizmos.DrawLine(hitEnd, clearEnd);
}
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEW_PREFAB_SYSTEM
#endif
using UnityEngine;
namespace Spine.Unity.Examples {
#if NEW_PREFAB_SYSTEM
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
[RequireComponent(typeof(SkeletonUtilityBone))]
public class SkeletonUtilityGroundConstraint : SkeletonUtilityConstraint {
[Tooltip("LayerMask for what objects to raycast against")]
public LayerMask groundMask;
[Tooltip("Use 2D")]
public bool use2D = false;
[Tooltip("Uses SphereCast for 3D mode and CircleCast for 2D mode")]
public bool useRadius = false;
[Tooltip("The Radius")]
public float castRadius = 0.1f;
[Tooltip("How high above the target bone to begin casting from")]
public float castDistance = 5f;
[Tooltip("X-Axis adjustment")]
public float castOffset = 0;
[Tooltip("Y-Axis adjustment")]
public float groundOffset = 0;
[Tooltip("How fast the target IK position adjusts to the ground. Use smaller values to prevent snapping")]
public float adjustSpeed = 5;
Vector3 rayOrigin;
Vector3 rayDir = new Vector3(0, -1, 0);
float hitY;
float lastHitY;
protected override void OnEnable () {
base.OnEnable();
lastHitY = transform.position.y;
}
public override void DoUpdate () {
rayOrigin = transform.position + new Vector3(castOffset, castDistance, 0);
float positionScale = hierarchy.PositionScale;
float adjustDistanceThisFrame = adjustSpeed * positionScale * Time.deltaTime;
hitY = float.MinValue;
if (use2D) {
RaycastHit2D hit;
if (useRadius)
hit = Physics2D.CircleCast(rayOrigin, castRadius, rayDir, castDistance + groundOffset, groundMask);
else
hit = Physics2D.Raycast(rayOrigin, rayDir, castDistance + groundOffset, groundMask);
if (hit.collider != null) {
hitY = hit.point.y + groundOffset;
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
} else {
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
}
} else {
RaycastHit hit;
bool validHit = false;
if (useRadius)
validHit = Physics.SphereCast(rayOrigin, castRadius, rayDir, out hit, castDistance + groundOffset, groundMask);
else
validHit = Physics.Raycast(rayOrigin, rayDir, out hit, castDistance + groundOffset, groundMask);
if (validHit) {
hitY = hit.point.y + groundOffset;
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, hitY, adjustDistanceThisFrame);
} else {
if (Application.isPlaying)
hitY = Mathf.MoveTowards(lastHitY, transform.position.y, adjustDistanceThisFrame);
}
}
Vector3 v = transform.position;
v.y = Mathf.Clamp(v.y, Mathf.Min(lastHitY, hitY), float.MaxValue);
transform.position = v;
bone.bone.X = transform.localPosition.x / hierarchy.PositionScale;
bone.bone.Y = transform.localPosition.y / hierarchy.PositionScale;
lastHitY = hitY;
}
void OnDrawGizmos () {
Vector3 hitEnd = rayOrigin + (rayDir * Mathf.Min(castDistance, rayOrigin.y - hitY));
Vector3 clearEnd = rayOrigin + (rayDir * castDistance);
Gizmos.DrawLine(rayOrigin, hitEnd);
if (useRadius) {
Gizmos.DrawLine(new Vector3(hitEnd.x - castRadius, hitEnd.y - groundOffset, hitEnd.z), new Vector3(hitEnd.x + castRadius, hitEnd.y - groundOffset, hitEnd.z));
Gizmos.DrawLine(new Vector3(clearEnd.x - castRadius, clearEnd.y, clearEnd.z), new Vector3(clearEnd.x + castRadius, clearEnd.y, clearEnd.z));
}
Gizmos.color = Color.red;
Gizmos.DrawLine(hitEnd, clearEnd);
}
}
}

View File

@@ -1,137 +1,137 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
// SkeletonUtilityKinematicShadow allows hinge chains to inherit a velocity interpreted from changes in parent transform position or from unrelated rigidbodies.
// Note: Uncheck "useRootTransformIfNull
public class SkeletonUtilityKinematicShadow : MonoBehaviour {
#region Inspector
[Tooltip("If checked, the hinge chain can inherit your root transform's velocity or position/rotation changes.")]
public bool detachedShadow = false;
public Transform parent;
public bool hideShadow = true;
public PhysicsSystem physicsSystem = PhysicsSystem.Physics3D;
#endregion
GameObject shadowRoot;
readonly List<TransformPair> shadowTable = new List<TransformPair>();
struct TransformPair {
public Transform dest, src;
}
public enum PhysicsSystem {
Physics2D,
Physics3D
};
void Start () {
// Duplicate this gameObject as the "shadow" with a different parent.
shadowRoot = Instantiate<GameObject>(this.gameObject);
Destroy(shadowRoot.GetComponent<SkeletonUtilityKinematicShadow>());
// Prepare shadow gameObject's properties.
var shadowRootTransform = shadowRoot.transform;
shadowRootTransform.position = transform.position;
shadowRootTransform.rotation = transform.rotation;
Vector3 scaleRef = transform.TransformPoint(Vector3.right);
float scale = Vector3.Distance(transform.position, scaleRef);
shadowRootTransform.localScale = Vector3.one;
if (!detachedShadow) {
// Do not change to null coalescing operator (??). Unity overloads null checks for UnityEngine.Objects but not the ?? operator.
if (parent == null)
shadowRootTransform.parent = transform.root;
else
shadowRootTransform.parent = parent;
}
if (hideShadow)
shadowRoot.hideFlags = HideFlags.HideInHierarchy;
var shadowJoints = shadowRoot.GetComponentsInChildren<Joint>();
foreach (Joint j in shadowJoints)
j.connectedAnchor *= scale;
// Build list of bone pairs (matches shadow transforms with bone transforms)
var bones = GetComponentsInChildren<SkeletonUtilityBone>();
var shadowBones = shadowRoot.GetComponentsInChildren<SkeletonUtilityBone>();
foreach (var b in bones) {
if (b.gameObject == this.gameObject)
continue;
System.Type checkType = (physicsSystem == PhysicsSystem.Physics2D) ? typeof(Rigidbody2D) : typeof(Rigidbody);
foreach (var sb in shadowBones) {
if (sb.GetComponent(checkType) != null && sb.boneName == b.boneName) {
shadowTable.Add(new TransformPair {
dest = b.transform,
src = sb.transform
});
break;
}
}
}
// Destroy conflicting and unneeded components
DestroyComponents(shadowBones);
DestroyComponents(GetComponentsInChildren<Joint>());
DestroyComponents(GetComponentsInChildren<Rigidbody>());
DestroyComponents(GetComponentsInChildren<Collider>());
}
static void DestroyComponents (Component[] components) {
for (int i = 0, n = components.Length; i < n; i++)
Destroy(components[i]);
}
void FixedUpdate () {
if (physicsSystem == PhysicsSystem.Physics2D) {
var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody2D>();
shadowRootRigidbody.MovePosition(transform.position);
shadowRootRigidbody.MoveRotation(transform.rotation.eulerAngles.z);
} else {
var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody>();
shadowRootRigidbody.MovePosition(transform.position);
shadowRootRigidbody.MoveRotation(transform.rotation);
}
for (int i = 0, n = shadowTable.Count; i < n; i++) {
var pair = shadowTable[i];
pair.dest.localPosition = pair.src.localPosition;
pair.dest.localRotation = pair.src.localRotation;
}
}
}
}
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
using System.Collections.Generic;
using UnityEngine;
namespace Spine.Unity.Examples {
// SkeletonUtilityKinematicShadow allows hinge chains to inherit a velocity interpreted from changes in parent transform position or from unrelated rigidbodies.
// Note: Uncheck "useRootTransformIfNull
public class SkeletonUtilityKinematicShadow : MonoBehaviour {
#region Inspector
[Tooltip("If checked, the hinge chain can inherit your root transform's velocity or position/rotation changes.")]
public bool detachedShadow = false;
public Transform parent;
public bool hideShadow = true;
public PhysicsSystem physicsSystem = PhysicsSystem.Physics3D;
#endregion
GameObject shadowRoot;
readonly List<TransformPair> shadowTable = new List<TransformPair>();
struct TransformPair {
public Transform dest, src;
}
public enum PhysicsSystem {
Physics2D,
Physics3D
};
void Start () {
// Duplicate this gameObject as the "shadow" with a different parent.
shadowRoot = Instantiate<GameObject>(this.gameObject);
Destroy(shadowRoot.GetComponent<SkeletonUtilityKinematicShadow>());
// Prepare shadow gameObject's properties.
var shadowRootTransform = shadowRoot.transform;
shadowRootTransform.position = transform.position;
shadowRootTransform.rotation = transform.rotation;
Vector3 scaleRef = transform.TransformPoint(Vector3.right);
float scale = Vector3.Distance(transform.position, scaleRef);
shadowRootTransform.localScale = Vector3.one;
if (!detachedShadow) {
// Do not change to null coalescing operator (??). Unity overloads null checks for UnityEngine.Objects but not the ?? operator.
if (parent == null)
shadowRootTransform.parent = transform.root;
else
shadowRootTransform.parent = parent;
}
if (hideShadow)
shadowRoot.hideFlags = HideFlags.HideInHierarchy;
var shadowJoints = shadowRoot.GetComponentsInChildren<Joint>();
foreach (Joint j in shadowJoints)
j.connectedAnchor *= scale;
// Build list of bone pairs (matches shadow transforms with bone transforms)
var bones = GetComponentsInChildren<SkeletonUtilityBone>();
var shadowBones = shadowRoot.GetComponentsInChildren<SkeletonUtilityBone>();
foreach (var b in bones) {
if (b.gameObject == this.gameObject)
continue;
System.Type checkType = (physicsSystem == PhysicsSystem.Physics2D) ? typeof(Rigidbody2D) : typeof(Rigidbody);
foreach (var sb in shadowBones) {
if (sb.GetComponent(checkType) != null && sb.boneName == b.boneName) {
shadowTable.Add(new TransformPair {
dest = b.transform,
src = sb.transform
});
break;
}
}
}
// Destroy conflicting and unneeded components
DestroyComponents(shadowBones);
DestroyComponents(GetComponentsInChildren<Joint>());
DestroyComponents(GetComponentsInChildren<Rigidbody>());
DestroyComponents(GetComponentsInChildren<Collider>());
}
static void DestroyComponents (Component[] components) {
for (int i = 0, n = components.Length; i < n; i++)
Destroy(components[i]);
}
void FixedUpdate () {
if (physicsSystem == PhysicsSystem.Physics2D) {
var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody2D>();
shadowRootRigidbody.MovePosition(transform.position);
shadowRootRigidbody.MoveRotation(transform.rotation.eulerAngles.z);
} else {
var shadowRootRigidbody = shadowRoot.GetComponent<Rigidbody>();
shadowRootRigidbody.MovePosition(transform.position);
shadowRootRigidbody.MoveRotation(transform.rotation);
}
for (int i = 0, n = shadowTable.Count; i < n; i++) {
var pair = shadowTable[i];
pair.dest.localPosition = pair.src.localPosition;
pair.dest.localRotation = pair.src.localRotation;
}
}
}
}