Эх сурвалжийг харах

[csharp] Port of commit 1dbbfda: Added skeleton reference scale. Fixed leftover gravity and wind timeline scale.

Harald Csaszar 1 жил өмнө
parent
commit
8e56a6c920

+ 20 - 20
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;

+ 1 - 3
spine-csharp/src/Skeleton.cs

@@ -42,9 +42,7 @@ namespace Spine {
 		internal ExposedList<IUpdatable> updateCache = new ExposedList<IUpdatable>();
 		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;
 
 		/// <summary>The skeleton's setup pose data.</summary>
 		public SkeletonData Data { get { return data; } }

+ 5 - 4
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);

+ 6 - 1
spine-csharp/src/SkeletonData.cs

@@ -44,7 +44,7 @@ namespace Spine {
 		internal ExposedList<TransformConstraintData> transformConstraints = new ExposedList<TransformConstraintData>();
 		internal ExposedList<PathConstraintData> pathConstraints = new ExposedList<PathConstraintData>();
 		internal ExposedList<PhysicsConstraintData> physicsConstraints = new ExposedList<PhysicsConstraintData>();
-		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; } }
+
+		/// <summary> Baseline scale factor for applying distance-dependent effects on non-scalable properties, such as angle or scale. Default
+		/// is 100.</summary>
+		public float ReferenceScale { get { return referenceScale; } set { referenceScale = value; } }
+
 		/// <summary>The Spine version used to export this data, or null.</summary>
 		public string Version { get { return version; } set { version = value; } }
 

+ 4 - 6
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));
 					}
 				}
 			}

+ 1 - 1
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",