From 8e56a6c9206546f59de9236e30569f611a6fe249 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 4 Mar 2024 16:24:45 +0100 Subject: [PATCH] [csharp] Port of commit 1dbbfda: Added skeleton reference scale. Fixed leftover gravity and wind timeline scale. --- spine-csharp/src/PhysicsConstraint.cs | 40 +++++++++++++-------------- spine-csharp/src/Skeleton.cs | 4 +-- spine-csharp/src/SkeletonBinary.cs | 9 +++--- spine-csharp/src/SkeletonData.cs | 7 ++++- spine-csharp/src/SkeletonJson.cs | 10 +++---- spine-csharp/src/package.json | 2 +- 6 files changed, 37 insertions(+), 35 deletions(-) diff --git a/spine-csharp/src/PhysicsConstraint.cs b/spine-csharp/src/PhysicsConstraint.cs index 646c79c87..bcea4092e 100644 --- a/spine-csharp/src/PhysicsConstraint.cs +++ b/spine-csharp/src/PhysicsConstraint.cs @@ -156,7 +156,7 @@ namespace Spine { ux = bx; uy = by; } else { - float remaining = this.remaining, i = inertia, step = data.step; + float a = this.remaining, i = inertia, t = data.step, f = skeleton.data.referenceScale; if (x || y) { if (x) { xOffset += (ux - bx) * i; @@ -166,22 +166,22 @@ namespace Spine { yOffset += (uy - by) * i; uy = by; } - if (remaining >= step) { - float m = massInverse * step, e = strength, w = wind * 100, g = gravity * -100; - float d = (float)Math.Pow(damping, 60 * step); + if (a >= t) { + float m = massInverse * t, e = strength, w = wind * f, g = gravity * f; + float d = (float)Math.Pow(damping, 60 * t); do { if (x) { xVelocity += (w - xOffset * e) * m; - xOffset += xVelocity * step; + xOffset += xVelocity * t; xVelocity *= d; } if (y) { - yVelocity += (g - yOffset * e) * m; - yOffset += yVelocity * step; + yVelocity -= (g + yOffset * e) * m; + yOffset += yVelocity * t; yVelocity *= d; } - remaining -= step; - } while (remaining >= step); + a -= t; + } while (a >= t); } if (x) bone.worldX += xOffset * mix * data.x; if (y) bone.worldY += yOffset * mix * data.y; @@ -205,31 +205,31 @@ namespace Spine { float r = l * bone.WorldScaleX; if (r > 0) scaleOffset += ((cx - bone.worldX) * c + (cy - bone.worldY) * s) * i / r; } - remaining = this.remaining; - if (remaining >= step) { - float m = massInverse * step, e = strength, w = wind, g = gravity; - float d = (float)Math.Pow(damping, 60 * step); + a = this.remaining; + if (a >= t) { + float m = massInverse * t, e = strength, w = wind, g = gravity; + float d = (float)Math.Pow(damping, 60 * t), h = l / f; while (true) { - remaining -= step; + a -= t; if (scaleX) { scaleVelocity += (w * c - g * s - scaleOffset * e) * m; - scaleOffset += scaleVelocity * step; + scaleOffset += scaleVelocity * t; scaleVelocity *= d; } if (rotateOrShearX) { - rotateVelocity += (-0.01f * l * (w * s + g * c) - rotateOffset * e) * m; - rotateOffset += rotateVelocity * step; + rotateVelocity -= ((w * s + g * c) * h + rotateOffset * e) * m; + rotateOffset += rotateVelocity * t; rotateVelocity *= d; - if (remaining < step) break; + if (a < t) break; float r = rotateOffset * mr + ca; c = (float)Math.Cos(r); s = (float)Math.Sin(r); - } else if (remaining < step) // + } else if (a < t) // break; } } } - this.remaining = remaining; + this.remaining = a; } cx = bone.worldX; cy = bone.worldY; diff --git a/spine-csharp/src/Skeleton.cs b/spine-csharp/src/Skeleton.cs index 508e18e17..70f5f8e6d 100644 --- a/spine-csharp/src/Skeleton.cs +++ b/spine-csharp/src/Skeleton.cs @@ -42,9 +42,7 @@ namespace Spine { internal ExposedList updateCache = new ExposedList(); internal Skin skin; internal float r = 1, g = 1, b = 1, a = 1; - internal float x, y; - internal float scaleX = 1, scaleY = 1; - internal float time; + internal float x, y, scaleX = 1, scaleY = 1, time; /// The skeleton's setup pose data. public SkeletonData Data { get { return data; } } diff --git a/spine-csharp/src/SkeletonBinary.cs b/spine-csharp/src/SkeletonBinary.cs index d48b185e7..569ff53e0 100644 --- a/spine-csharp/src/SkeletonBinary.cs +++ b/spine-csharp/src/SkeletonBinary.cs @@ -151,6 +151,7 @@ namespace Spine { skeletonData.y = input.ReadFloat(); skeletonData.width = input.ReadFloat(); skeletonData.height = input.ReadFloat(); + skeletonData.referenceScale = input.ReadFloat() * scale; bool nonessential = input.ReadBoolean(); @@ -322,8 +323,8 @@ namespace Spine { data.strength = input.ReadFloat(); data.damping = input.ReadFloat(); data.massInverse = input.ReadFloat(); - data.wind = input.ReadFloat() * scale; - data.gravity = input.ReadFloat() * scale; + data.wind = input.ReadFloat(); + data.gravity = input.ReadFloat(); data.mix = input.ReadFloat(); flags = input.Read(); if ((flags & 1) != 0) data.inertiaGlobal = true; @@ -1007,10 +1008,10 @@ namespace Spine { ReadTimeline(input, timelines, new PhysicsConstraintMassTimeline(frameCount, bezierCount, index), 1); break; case PHYSICS_WIND: - ReadTimeline(input, timelines, new PhysicsConstraintWindTimeline(frameCount, bezierCount, index), scale); + ReadTimeline(input, timelines, new PhysicsConstraintWindTimeline(frameCount, bezierCount, index), 1); break; case PHYSICS_GRAVITY: - ReadTimeline(input, timelines, new PhysicsConstraintGravityTimeline(frameCount, bezierCount, index), scale); + ReadTimeline(input, timelines, new PhysicsConstraintGravityTimeline(frameCount, bezierCount, index), 1); break; case PHYSICS_MIX: ReadTimeline(input, timelines, new PhysicsConstraintMixTimeline(frameCount, bezierCount, index), 1); diff --git a/spine-csharp/src/SkeletonData.cs b/spine-csharp/src/SkeletonData.cs index 528005bd6..94e193eae 100644 --- a/spine-csharp/src/SkeletonData.cs +++ b/spine-csharp/src/SkeletonData.cs @@ -44,7 +44,7 @@ namespace Spine { internal ExposedList transformConstraints = new ExposedList(); internal ExposedList pathConstraints = new ExposedList(); internal ExposedList physicsConstraints = new ExposedList(); - internal float x, y, width, height; + internal float x, y, width, height, referenceScale = 100; internal string version, hash; // Nonessential. @@ -88,6 +88,11 @@ namespace Spine { public float Y { get { return y; } set { y = value; } } public float Width { get { return width; } set { width = value; } } public float Height { get { return height; } set { height = value; } } + + /// Baseline scale factor for applying distance-dependent effects on non-scalable properties, such as angle or scale. Default + /// is 100. + public float ReferenceScale { get { return referenceScale; } set { referenceScale = value; } } + /// The Spine version used to export this data, or null. public string Version { get { return version; } set { version = value; } } diff --git a/spine-csharp/src/SkeletonJson.cs b/spine-csharp/src/SkeletonJson.cs index 30578cd81..a8df5d504 100644 --- a/spine-csharp/src/SkeletonJson.cs +++ b/spine-csharp/src/SkeletonJson.cs @@ -108,6 +108,7 @@ namespace Spine { skeletonData.y = GetFloat(skeletonMap, "y", 0); skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); + skeletonData.referenceScale = GetFloat(skeletonMap, "referenceScale", 100) * scale; skeletonData.fps = GetFloat(skeletonMap, "fps", 30); skeletonData.imagesPath = GetString(skeletonMap, "images", null); skeletonData.audioPath = GetString(skeletonMap, "audio", null); @@ -307,8 +308,8 @@ namespace Spine { data.strength = GetFloat(constraintMap, "strength", 100); data.damping = GetFloat(constraintMap, "damping", 1); data.massInverse = 1f / GetFloat(constraintMap, "mass", 1); - data.wind = GetFloat(constraintMap, "wind", 0) * scale; - data.gravity = GetFloat(constraintMap, "gravity", 0) * scale; + data.wind = GetFloat(constraintMap, "wind", 0); + data.gravity = GetFloat(constraintMap, "gravity", 0); data.mix = GetFloat(constraintMap, "mix", 1); data.inertiaGlobal = GetBoolean(constraintMap, "inertiaGlobal", false); data.strengthGlobal = GetBoolean(constraintMap, "strengthGlobal", false); @@ -1044,7 +1045,6 @@ namespace Spine { } CurveTimeline1 timeline; - float timelineScale = 1.0f; if (timelineName == "inertia") timeline = new PhysicsConstraintInertiaTimeline(frames, frames, index); else if (timelineName == "strength") @@ -1055,15 +1055,13 @@ namespace Spine { timeline = new PhysicsConstraintMassTimeline(frames, frames, index); else if (timelineName == "wind") { timeline = new PhysicsConstraintWindTimeline(frames, frames, index); - timelineScale = scale; } else if (timelineName == "gravity") { timeline = new PhysicsConstraintGravityTimeline(frames, frames, index); - timelineScale = scale; } else if (timelineName == "mix") // timeline = new PhysicsConstraintMixTimeline(frames, frames, index); else continue; - timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, 0, timelineScale)); + timelines.Add(ReadTimeline(ref keyMapEnumerator, timeline, 0, 1)); } } } diff --git a/spine-csharp/src/package.json b/spine-csharp/src/package.json index d4cd47460..f521abc9a 100644 --- a/spine-csharp/src/package.json +++ b/spine-csharp/src/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.spine-csharp", "displayName": "spine-csharp Runtime", "description": "This plugin provides the spine-csharp core runtime.", - "version": "4.2.12", + "version": "4.2.13", "unity": "2018.3", "author": { "name": "Esoteric Software",