Merge branch '3.8' into 4.0-beta

This commit is contained in:
Harald Csaszar 2021-04-21 20:33:49 +02:00
commit 204f8672ed
6 changed files with 178 additions and 40 deletions

View File

@ -399,6 +399,8 @@
* `BoneFollower` and `BoneFollowerGraphic` components now provide better support for following bones when the skeleton's Transform is not the parent of the follower's Transform. Previously e.g. rotating a common parent Transform did not lead to the desired result, as well as negatively scaling a skeleton's Transform when it is not a parent of the follower's Transform. * `BoneFollower` and `BoneFollowerGraphic` components now provide better support for following bones when the skeleton's Transform is not the parent of the follower's Transform. Previously e.g. rotating a common parent Transform did not lead to the desired result, as well as negatively scaling a skeleton's Transform when it is not a parent of the follower's Transform.
* URP and LWRP `Sprite` and `SkeletonLit` shaders no longer require `Advanced - Add Normals` enabled to properly cast and receive shadows. It is recommended to disable `Add Normals` if normals are otherwise not needed. * URP and LWRP `Sprite` and `SkeletonLit` shaders no longer require `Advanced - Add Normals` enabled to properly cast and receive shadows. It is recommended to disable `Add Normals` if normals are otherwise not needed.
* Added an example component `RootMotionDeltaCompensation` located in `Spine Examples/Scripts/Sample Components` which can be used for applying simple delta compensation. You can enable and disable the component to toggle delta compensation of the currently playing animation on and off. * Added an example component `RootMotionDeltaCompensation` located in `Spine Examples/Scripts/Sample Components` which can be used for applying simple delta compensation. You can enable and disable the component to toggle delta compensation of the currently playing animation on and off.
* Root motion delta compensation now allows to only adjust X or Y components instead of both. Adds two parameters to `SkeletonRootMotionBase.AdjustRootMotionToDistance()` which default to adjusting both X and Y as before. The `RootMotionDeltaCompensation` example component exposes these parameters as public attributes.
* Root motion delta compensation now allows to also add translation root motion to e.g. adjust a horizontal jump upwards or downwards over time. This is necessary because a Y root motion of zero cannot be scaled to become non-zero.
* **Changes of default values** * **Changes of default values**
* `SkeletonMecanim`'s `Layer Mix Mode` now defaults to `MixMode.MixNext` instead of `MixMode.MixAlways`. * `SkeletonMecanim`'s `Layer Mix Mode` now defaults to `MixMode.MixNext` instead of `MixMode.MixAlways`.

View File

@ -5,12 +5,22 @@ namespace Spine.Unity.Examples {
public class RootMotionDeltaCompensation : MonoBehaviour { public class RootMotionDeltaCompensation : MonoBehaviour {
protected SkeletonRootMotionBase rootMotion; [SerializeField] protected SkeletonRootMotionBase rootMotion;
public Transform targetPosition; public Transform targetPosition;
public int trackIndex = 0; public int trackIndex = 0;
public bool adjustX = true;
public bool adjustY = true;
public float minScaleX = -999;
public float minScaleY = -999;
public float maxScaleX = 999;
public float maxScaleY = 999;
public bool allowXTranslation = false;
public bool allowYTranslation = true;
void Start () { void Start () {
rootMotion = this.GetComponent<SkeletonRootMotionBase>(); if (rootMotion == null)
rootMotion = this.GetComponent<SkeletonRootMotionBase>();
} }
void Update () { void Update () {
@ -18,12 +28,21 @@ namespace Spine.Unity.Examples {
} }
void OnDisable () { void OnDisable () {
rootMotion.rootMotionScaleX = rootMotion.rootMotionScaleY = 1; if (adjustX)
rootMotion.rootMotionScaleX = 1;
if (adjustY)
rootMotion.rootMotionScaleY = 1;
if (allowXTranslation)
rootMotion.rootMotionTranslateXPerY = 0;
if (allowYTranslation)
rootMotion.rootMotionTranslateYPerX = 0;
} }
void AdjustDelta() { void AdjustDelta() {
Vector3 toTarget = targetPosition.position - this.transform.position; Vector3 toTarget = targetPosition.position - this.transform.position;
rootMotion.AdjustRootMotionToDistance(toTarget, trackIndex); rootMotion.AdjustRootMotionToDistance(toTarget, trackIndex, adjustX, adjustY,
minScaleX, maxScaleX, minScaleY, maxScaleY,
allowXTranslation, allowYTranslation);
} }
} }
} }

View File

@ -39,6 +39,8 @@ namespace Spine.Unity.Editor {
protected SerializedProperty transformPositionY; protected SerializedProperty transformPositionY;
protected SerializedProperty rootMotionScaleX; protected SerializedProperty rootMotionScaleX;
protected SerializedProperty rootMotionScaleY; protected SerializedProperty rootMotionScaleY;
protected SerializedProperty rootMotionTranslateXPerY;
protected SerializedProperty rootMotionTranslateYPerX;
protected SerializedProperty rigidBody2D; protected SerializedProperty rigidBody2D;
protected SerializedProperty rigidBody; protected SerializedProperty rigidBody;
@ -47,6 +49,8 @@ namespace Spine.Unity.Editor {
protected GUIContent transformPositionYLabel; protected GUIContent transformPositionYLabel;
protected GUIContent rootMotionScaleXLabel; protected GUIContent rootMotionScaleXLabel;
protected GUIContent rootMotionScaleYLabel; protected GUIContent rootMotionScaleYLabel;
protected GUIContent rootMotionTranslateXPerYLabel;
protected GUIContent rootMotionTranslateYPerXLabel;
protected GUIContent rigidBody2DLabel; protected GUIContent rigidBody2DLabel;
protected GUIContent rigidBodyLabel; protected GUIContent rigidBodyLabel;
@ -57,6 +61,8 @@ namespace Spine.Unity.Editor {
transformPositionY = serializedObject.FindProperty("transformPositionY"); transformPositionY = serializedObject.FindProperty("transformPositionY");
rootMotionScaleX = serializedObject.FindProperty("rootMotionScaleX"); rootMotionScaleX = serializedObject.FindProperty("rootMotionScaleX");
rootMotionScaleY = serializedObject.FindProperty("rootMotionScaleY"); rootMotionScaleY = serializedObject.FindProperty("rootMotionScaleY");
rootMotionTranslateXPerY = serializedObject.FindProperty("rootMotionTranslateXPerY");
rootMotionTranslateYPerX = serializedObject.FindProperty("rootMotionTranslateYPerX");
rigidBody2D = serializedObject.FindProperty("rigidBody2D"); rigidBody2D = serializedObject.FindProperty("rigidBody2D");
rigidBody = serializedObject.FindProperty("rigidBody"); rigidBody = serializedObject.FindProperty("rigidBody");
@ -65,6 +71,8 @@ namespace Spine.Unity.Editor {
transformPositionYLabel = new UnityEngine.GUIContent("Y", "Use the Y-movement of the bone."); transformPositionYLabel = new UnityEngine.GUIContent("Y", "Use the Y-movement of the bone.");
rootMotionScaleXLabel = new UnityEngine.GUIContent("Root Motion Scale (X)", "Scale applied to the horizontal root motion delta. Can be used for delta compensation to e.g. stretch a jump to the desired distance."); rootMotionScaleXLabel = new UnityEngine.GUIContent("Root Motion Scale (X)", "Scale applied to the horizontal root motion delta. Can be used for delta compensation to e.g. stretch a jump to the desired distance.");
rootMotionScaleYLabel = new UnityEngine.GUIContent("Root Motion Scale (Y)", "Scale applied to the vertical root motion delta. Can be used for delta compensation to e.g. stretch a jump to the desired distance."); rootMotionScaleYLabel = new UnityEngine.GUIContent("Root Motion Scale (Y)", "Scale applied to the vertical root motion delta. Can be used for delta compensation to e.g. stretch a jump to the desired distance.");
rootMotionTranslateXPerYLabel = new UnityEngine.GUIContent("Root Motion Translate (X)", "Added X translation per root motion Y delta. Can be used for delta compensation when scaling is not enough, to e.g. offset a horizontal jump to a vertically different goal.");
rootMotionTranslateYPerXLabel = new UnityEngine.GUIContent("Root Motion Translate (Y)", "Added Y translation per root motion X delta. Can be used for delta compensation when scaling is not enough, to e.g. offset a horizontal jump to a vertically different goal.");
rigidBody2DLabel = new UnityEngine.GUIContent("Rigidbody2D", rigidBody2DLabel = new UnityEngine.GUIContent("Rigidbody2D",
"Optional Rigidbody2D: Assign a Rigidbody2D here if you want " + "Optional Rigidbody2D: Assign a Rigidbody2D here if you want " +
" to apply the root motion to the rigidbody instead of the Transform." + " to apply the root motion to the rigidbody instead of the Transform." +
@ -92,10 +100,12 @@ namespace Spine.Unity.Editor {
EditorGUILayout.PropertyField(rootMotionScaleX, rootMotionScaleXLabel); EditorGUILayout.PropertyField(rootMotionScaleX, rootMotionScaleXLabel);
EditorGUILayout.PropertyField(rootMotionScaleY, rootMotionScaleYLabel); EditorGUILayout.PropertyField(rootMotionScaleY, rootMotionScaleYLabel);
EditorGUILayout.PropertyField(rootMotionTranslateXPerY, rootMotionTranslateXPerYLabel);
EditorGUILayout.PropertyField(rootMotionTranslateYPerX, rootMotionTranslateYPerXLabel);
} }
protected virtual void OptionalPropertyFields () { protected virtual void OptionalPropertyFields () {
//EditorGUILayout.LabelField("Optional", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(rigidBody2D, rigidBody2DLabel); EditorGUILayout.PropertyField(rigidBody2D, rigidBody2DLabel);
EditorGUILayout.PropertyField(rigidBody, rigidBodyLabel); EditorGUILayout.PropertyField(rigidBody, rigidBodyLabel);
} }

View File

@ -73,6 +73,15 @@ namespace Spine.Unity {
return GetAnimationRootMotion(start, end, animation); return GetAnimationRootMotion(start, end, animation);
} }
public override RootMotionInfo GetRootMotionInfo (int layerIndex) {
var pair = skeletonMecanim.Translator.GetActiveAnimationAndTime(layerIndex);
var animation = pair.Key;
var time = pair.Value;
if (animation == null)
return new RootMotionInfo();
return GetAnimationRootMotionInfo(animation, time);
}
protected override void Reset () { protected override void Reset () {
base.Reset(); base.Reset();
mecanimLayerFlags = DefaultMecanimLayerFlags; mecanimLayerFlags = DefaultMecanimLayerFlags;

View File

@ -67,6 +67,16 @@ namespace Spine.Unity {
return GetAnimationRootMotion(start, end, animation); return GetAnimationRootMotion(start, end, animation);
} }
public override RootMotionInfo GetRootMotionInfo (int trackIndex) {
TrackEntry track = animationState.GetCurrent(trackIndex);
if (track == null)
return new RootMotionInfo();
var animation = track.Animation;
float time = track.AnimationTime;
return GetAnimationRootMotionInfo(track.Animation, time);
}
protected override float AdditionalScale { protected override float AdditionalScale {
get { get {
return canvas ? canvas.referencePixelsPerUnit: 1.0f; return canvas ? canvas.referencePixelsPerUnit: 1.0f;

View File

@ -30,6 +30,7 @@
using UnityEngine; using UnityEngine;
using System.Collections.Generic; using System.Collections.Generic;
using Spine.Unity.AnimationTools; using Spine.Unity.AnimationTools;
using System;
namespace Spine.Unity { namespace Spine.Unity {
@ -47,6 +48,10 @@ namespace Spine.Unity {
public float rootMotionScaleX = 1; public float rootMotionScaleX = 1;
public float rootMotionScaleY = 1; public float rootMotionScaleY = 1;
/// <summary>Skeleton space X translation per skeleton space Y translation root motion.</summary>
public float rootMotionTranslateXPerY = 0;
/// <summary>Skeleton space Y translation per skeleton space X translation root motion.</summary>
public float rootMotionTranslateYPerX = 0;
[Header("Optional")] [Header("Optional")]
public Rigidbody2D rigidBody2D; public Rigidbody2D rigidBody2D;
@ -62,6 +67,7 @@ namespace Spine.Unity {
protected int rootMotionBoneIndex; protected int rootMotionBoneIndex;
protected List<Bone> topLevelBones = new List<Bone>(); protected List<Bone> topLevelBones = new List<Bone>();
protected Vector2 initialOffset = Vector2.zero; protected Vector2 initialOffset = Vector2.zero;
protected Vector2 tempSkeletonDisplacement;
protected Vector2 rigidbodyDisplacement; protected Vector2 rigidbodyDisplacement;
protected virtual void Reset () { protected virtual void Reset () {
@ -94,11 +100,16 @@ namespace Spine.Unity {
rigidBody.MovePosition(transform.position rigidBody.MovePosition(transform.position
+ new Vector3(rigidbodyDisplacement.x, rigidbodyDisplacement.y, 0)); + new Vector3(rigidbodyDisplacement.x, rigidbodyDisplacement.y, 0));
} }
Vector2 parentBoneScale;
GetScaleAffectingRootMotion(out parentBoneScale);
ClearEffectiveBoneOffsets(parentBoneScale);
rigidbodyDisplacement = Vector2.zero; rigidbodyDisplacement = Vector2.zero;
tempSkeletonDisplacement = Vector2.zero;
} }
protected virtual void OnDisable () { protected virtual void OnDisable () {
rigidbodyDisplacement = Vector2.zero; rigidbodyDisplacement = Vector2.zero;
tempSkeletonDisplacement = Vector2.zero;
} }
protected void FindRigidbodyComponent () { protected void FindRigidbodyComponent () {
@ -117,6 +128,15 @@ namespace Spine.Unity {
abstract protected Vector2 CalculateAnimationsMovementDelta (); abstract protected Vector2 CalculateAnimationsMovementDelta ();
abstract public Vector2 GetRemainingRootMotion (int trackIndex = 0); abstract public Vector2 GetRemainingRootMotion (int trackIndex = 0);
public struct RootMotionInfo {
public Vector2 start;
public Vector2 current;
public Vector2 mid;
public Vector2 end;
public bool timeIsPastMid;
};
abstract public RootMotionInfo GetRootMotionInfo (int trackIndex = 0);
public void SetRootMotionBone (string name) { public void SetRootMotionBone (string name) {
var skeleton = skeletonComponent.Skeleton; var skeleton = skeletonComponent.Skeleton;
int index = skeleton.FindBoneIndex(name); int index = skeleton.FindBoneIndex(name);
@ -131,14 +151,31 @@ namespace Spine.Unity {
} }
} }
public void AdjustRootMotionToDistance (Vector2 distanceToTarget, int trackIndex = 0) { public void AdjustRootMotionToDistance (Vector2 distanceToTarget, int trackIndex = 0, bool adjustX = true, bool adjustY = true,
Vector2 remainingRootMotion = GetRemainingRootMotion(trackIndex); float minX = 0, float maxX = float.MaxValue, float minY = 0, float maxY = float.MaxValue,
if (remainingRootMotion.x == 0) bool allowXTranslation = false, bool allowYTranslation = false) {
remainingRootMotion.x = 0.0001f;
if (remainingRootMotion.y == 0) Vector2 distanceToTargetSkeletonSpace = (Vector2)transform.InverseTransformVector(distanceToTarget);
remainingRootMotion.y = 0.0001f; Vector2 scaleAffectingRootMotion = GetScaleAffectingRootMotion();
rootMotionScaleX = distanceToTarget.x / remainingRootMotion.x; if (UsesRigidbody)
rootMotionScaleY = distanceToTarget.y / remainingRootMotion.y; distanceToTargetSkeletonSpace -= tempSkeletonDisplacement;
Vector2 remainingRootMotionSkeletonSpace = GetRemainingRootMotion(trackIndex);
remainingRootMotionSkeletonSpace.Scale(scaleAffectingRootMotion);
if (remainingRootMotionSkeletonSpace.x == 0)
remainingRootMotionSkeletonSpace.x = 0.0001f;
if (remainingRootMotionSkeletonSpace.y == 0)
remainingRootMotionSkeletonSpace.y = 0.0001f;
if (adjustX)
rootMotionScaleX = Math.Min(maxX, Math.Max(minX, distanceToTargetSkeletonSpace.x / remainingRootMotionSkeletonSpace.x));
if (adjustY)
rootMotionScaleY = Math.Min(maxY, Math.Max(minY, distanceToTargetSkeletonSpace.y / remainingRootMotionSkeletonSpace.y));
if (allowXTranslation)
rootMotionTranslateXPerY = (distanceToTargetSkeletonSpace.x - remainingRootMotionSkeletonSpace.x * rootMotionScaleX) / remainingRootMotionSkeletonSpace.y;
if (allowYTranslation)
rootMotionTranslateYPerX = (distanceToTargetSkeletonSpace.y - remainingRootMotionSkeletonSpace.y * rootMotionScaleY) / remainingRootMotionSkeletonSpace.x;
} }
public Vector2 GetAnimationRootMotion (Animation animation) { public Vector2 GetAnimationRootMotion (Animation animation) {
@ -155,6 +192,21 @@ namespace Spine.Unity {
return Vector2.zero; return Vector2.zero;
} }
public RootMotionInfo GetAnimationRootMotionInfo (Animation animation, float currentTime) {
RootMotionInfo rootMotion = new RootMotionInfo();
var timeline = animation.FindTranslateTimelineForBone(rootMotionBoneIndex);
if (timeline != null) {
float duration = animation.duration;
float mid = duration * 0.5f;
rootMotion.start = timeline.Evaluate(0);
rootMotion.current = timeline.Evaluate(currentTime);
rootMotion.mid = timeline.Evaluate(mid);
rootMotion.end = timeline.Evaluate(duration);
rootMotion.timeIsPastMid = currentTime > mid;
}
return rootMotion;
}
Vector2 GetTimelineMovementDelta (float startTime, float endTime, Vector2 GetTimelineMovementDelta (float startTime, float endTime,
TranslateTimeline timeline, Animation animation) { TranslateTimeline timeline, Animation animation) {
@ -182,15 +234,39 @@ namespace Spine.Unity {
if (!this.isActiveAndEnabled) if (!this.isActiveAndEnabled)
return; // Root motion is only applied when component is enabled. return; // Root motion is only applied when component is enabled.
var movementDelta = CalculateAnimationsMovementDelta(); var boneLocalDelta = CalculateAnimationsMovementDelta();
Vector2 parentBoneScale; Vector2 parentBoneScale;
AdjustMovementDeltaToConfiguration(ref movementDelta, out parentBoneScale, animatedSkeletonComponent.Skeleton); Vector2 skeletonDelta = GetSkeletonSpaceMovementDelta(boneLocalDelta, out parentBoneScale);
ApplyRootMotion(movementDelta, parentBoneScale); ApplyRootMotion(skeletonDelta, parentBoneScale);
} }
void AdjustMovementDeltaToConfiguration (ref Vector2 localDelta, out Vector2 parentBoneScale, Skeleton skeleton) { void ApplyRootMotion (Vector2 skeletonDelta, Vector2 parentBoneScale) {
localDelta.x *= skeleton.ScaleX; // Apply root motion to Transform or RigidBody;
localDelta.y *= skeleton.ScaleY; if (UsesRigidbody) {
rigidbodyDisplacement += (Vector2)transform.TransformVector(skeletonDelta);
// Accumulated displacement is applied on the next Physics update in FixedUpdate.
// Until the next Physics update, tempBoneDisplacement is offsetting bone locations
// to prevent stutter which would otherwise occur if we don't move every Update.
tempSkeletonDisplacement += skeletonDelta;
SetEffectiveBoneOffsetsTo(tempSkeletonDisplacement, parentBoneScale);
}
else {
transform.position += transform.TransformVector(skeletonDelta);
ClearEffectiveBoneOffsets(parentBoneScale);
}
}
Vector2 GetScaleAffectingRootMotion () {
Vector2 parentBoneScale;
return GetScaleAffectingRootMotion(out parentBoneScale);
}
Vector2 GetScaleAffectingRootMotion (out Vector2 parentBoneScale) {
var skeleton = skeletonComponent.Skeleton;
Vector2 totalScale = Vector2.one;
totalScale.x *= skeleton.ScaleX;
totalScale.y *= skeleton.ScaleY;
parentBoneScale = Vector2.one; parentBoneScale = Vector2.one;
Bone scaleBone = rootMotionBone; Bone scaleBone = rootMotionBone;
@ -198,37 +274,49 @@ namespace Spine.Unity {
parentBoneScale.x *= scaleBone.ScaleX; parentBoneScale.x *= scaleBone.ScaleX;
parentBoneScale.y *= scaleBone.ScaleY; parentBoneScale.y *= scaleBone.ScaleY;
} }
localDelta = Vector2.Scale(localDelta, parentBoneScale); totalScale = Vector2.Scale(totalScale, parentBoneScale);
totalScale *= AdditionalScale;
localDelta *= AdditionalScale; return totalScale;
localDelta.x *= rootMotionScaleX;
localDelta.y *= rootMotionScaleY;
if (!transformPositionX) localDelta.x = 0f;
if (!transformPositionY) localDelta.y = 0f;
} }
void ApplyRootMotion (Vector2 localDelta, Vector2 parentBoneScale) { Vector2 GetSkeletonSpaceMovementDelta (Vector2 boneLocalDelta, out Vector2 parentBoneScale) {
// Apply root motion to Transform or RigidBody; Vector2 skeletonDelta = boneLocalDelta;
if (UsesRigidbody) { Vector2 totalScale = GetScaleAffectingRootMotion(out parentBoneScale);
rigidbodyDisplacement += (Vector2)transform.TransformVector(localDelta); skeletonDelta.Scale(totalScale);
// Accumulated displacement is applied on the next Physics update (FixedUpdate)
}
else {
transform.position += transform.TransformVector(localDelta);
}
Vector2 rootMotionTranslation = new Vector2(
rootMotionTranslateXPerY * skeletonDelta.y,
rootMotionTranslateYPerX * skeletonDelta.x);
skeletonDelta.x *= rootMotionScaleX;
skeletonDelta.y *= rootMotionScaleY;
skeletonDelta.x += rootMotionTranslation.x;
skeletonDelta.y += rootMotionTranslation.y;
if (!transformPositionX) skeletonDelta.x = 0f;
if (!transformPositionY) skeletonDelta.y = 0f;
return skeletonDelta;
}
void SetEffectiveBoneOffsetsTo (Vector2 displacementSkeletonSpace, Vector2 parentBoneScale) {
// Move top level bones in opposite direction of the root motion bone // Move top level bones in opposite direction of the root motion bone
var skeleton = skeletonComponent.Skeleton;
foreach (var topLevelBone in topLevelBones) { foreach (var topLevelBone in topLevelBones) {
if (topLevelBone == rootMotionBone) { if (topLevelBone == rootMotionBone) {
if (transformPositionX) topLevelBone.x = 0; if (transformPositionX) topLevelBone.x = displacementSkeletonSpace.x / skeleton.ScaleX;
if (transformPositionY) topLevelBone.y = 0; if (transformPositionY) topLevelBone.y = displacementSkeletonSpace.y / skeleton.ScaleY;
} }
else { else {
if (transformPositionX) topLevelBone.x = -(rootMotionBone.x - initialOffset.x) * parentBoneScale.x; float offsetX = (initialOffset.x - rootMotionBone.x) * parentBoneScale.x;
if (transformPositionY) topLevelBone.y = -(rootMotionBone.y - initialOffset.y) * parentBoneScale.y; float offsetY = (initialOffset.y - rootMotionBone.y) * parentBoneScale.y;
if (transformPositionX) topLevelBone.x = (displacementSkeletonSpace.x / skeleton.ScaleX) + offsetX;
if (transformPositionY) topLevelBone.y = (displacementSkeletonSpace.y / skeleton.ScaleY) + offsetY;
} }
} }
} }
void ClearEffectiveBoneOffsets (Vector2 parentBoneScale) {
SetEffectiveBoneOffsetsTo(Vector2.zero, parentBoneScale);
}
} }
} }