/// <param name="parent">May be null.</param> public BoneData (int index, String name, BoneData parent) { if (index < 0) throw new ArgumentException("index must be >= 0", "index"); if (name == null) throw new ArgumentNullException("name", "name cannot be null."); this.index = index; this.name = name; this.parent = parent; }
public SlotData(String name, BoneData boneData) { if (name == null) throw new ArgumentNullException("name cannot be null."); if (boneData == null) throw new ArgumentNullException("boneData cannot be null."); this.name = name; this.boneData = boneData; }
/// <param name="parent">May be null.</param> public Bone(BoneData data, Bone parent) { if (data == null) throw new ArgumentNullException("data cannot be null."); this.data = data; this.parent = parent; SetToSetupPose(); }
/** @param parent May be null. */ public Bone(BoneData data, Bone parent) { if (data == null) throw new ArgumentNullException("data cannot be null."); Data = data; Parent = parent; SetToBindPose(); }
public SlotData (int index, String name, BoneData boneData) { if (index < 0) throw new ArgumentException ("index must be >= 0.", "index"); if (name == null) throw new ArgumentNullException("name", "name cannot be null."); if (boneData == null) throw new ArgumentNullException("boneData", "boneData cannot be null."); this.index = index; this.name = name; this.boneData = boneData; }
//[SpineAttachment(dataField: "DataAsset")] //public string attachment; private void Start() { skeletonAnimation = GetComponent <SkeletonAnimation>(); spineAnimationState = skeletonAnimation.AnimationState; skeleton = skeletonAnimation.Skeleton; // thisSlotBaseData = skeleton.FindSlot(slot).Bone.Data; }
/** @param parent May be null. */ public BoneData(String name, BoneData parent) { if (name == null) throw new ArgumentNullException("name cannot be null."); Name = name; Parent = parent; ScaleX = 1; ScaleY = 1; }
/// <param name="parent">May be null.</param> public Bone (BoneData data, Skeleton skeleton, Bone parent) { if (data == null) throw new ArgumentNullException("data", "data cannot be null."); if (skeleton == null) throw new ArgumentNullException("skeleton", "skeleton cannot be null."); this.data = data; this.skeleton = skeleton; this.parent = parent; SetToSetupPose(); }
public SlotData(String name, BoneData boneData) { if (name == null) throw new ArgumentNullException("name cannot be null."); if (boneData == null) throw new ArgumentNullException("boneData cannot be null."); Name = name; BoneData = boneData; R = 1; G = 1; B = 1; A = 1; }
static int FindBone(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); Spine.SkeletonData obj = (Spine.SkeletonData)ToLua.CheckObject <Spine.SkeletonData>(L, 1); string arg0 = ToLua.CheckString(L, 2); Spine.BoneData o = obj.FindBone(arg0); ToLua.PushObject(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private static BoneMatrix GetInheritedInternal(BoneData boneData, BoneMatrix parentMatrix) { float num12; if (boneData.parent == null) { return(new BoneMatrix(boneData)); } float a = parentMatrix.a; float b = parentMatrix.b; float c = parentMatrix.c; float d = parentMatrix.d; BoneMatrix matrix = new BoneMatrix { x = ((a * boneData.x) + (b * boneData.y)) + parentMatrix.x, y = ((c * boneData.x) + (d * boneData.y)) + parentMatrix.y }; switch (boneData.transformMode) { case TransformMode.Normal: { float num5 = (boneData.rotation + 90f) + boneData.shearY; float num6 = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; float num7 = MathUtils.CosDeg(num5) * boneData.scaleY; float num8 = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; float num9 = MathUtils.SinDeg(num5) * boneData.scaleY; matrix.a = (a * num6) + (b * num8); matrix.b = (a * num7) + (b * num9); matrix.c = (c * num6) + (d * num8); matrix.d = (c * num7) + (d * num9); return(matrix); } case TransformMode.NoRotationOrReflection: { float num11 = (a * a) + (c * c); if (num11 <= 0.0001f) { a = 0f; c = 0f; num12 = 90f - (MathUtils.Atan2(d, b) * 57.29578f); break; } num11 = Math.Abs((float)((a * d) - (b * c))) / num11; b = c * num11; d = a * num11; num12 = MathUtils.Atan2(c, a) * 57.29578f; break; } case TransformMode.NoScale: case TransformMode.NoScaleOrReflection: { float num19 = MathUtils.CosDeg(boneData.rotation); float num20 = MathUtils.SinDeg(boneData.rotation); float x = (a * num19) + (b * num20); float y = (c * num19) + (d * num20); float num23 = (float)Math.Sqrt((double)((x * x) + (y * y))); if (num23 > 1E-05f) { num23 = 1f / num23; } x *= num23; y *= num23; num23 = (float)Math.Sqrt((double)((x * x) + (y * y))); float radians = 1.570796f + MathUtils.Atan2(y, x); float num25 = MathUtils.Cos(radians) * num23; float num26 = MathUtils.Sin(radians) * num23; float num27 = MathUtils.CosDeg(boneData.shearX) * boneData.scaleX; float num28 = MathUtils.CosDeg(90f + boneData.shearY) * boneData.scaleY; float num29 = MathUtils.SinDeg(boneData.shearX) * boneData.scaleX; float num30 = MathUtils.SinDeg(90f + boneData.shearY) * boneData.scaleY; if ((boneData.transformMode != TransformMode.NoScaleOrReflection) && (((a * d) - (b * c)) < 0f)) { num25 = -num25; num26 = -num26; } matrix.a = (x * num27) + (num25 * num29); matrix.b = (x * num28) + (num25 * num30); matrix.c = (y * num27) + (num26 * num29); matrix.d = (y * num28) + (num26 * num30); return(matrix); } case (TransformMode.NoScale | TransformMode.NoRotationOrReflection): case 4: case 5: return(matrix); case TransformMode.OnlyTranslation: { float num10 = (boneData.rotation + 90f) + boneData.shearY; matrix.a = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; matrix.b = MathUtils.CosDeg(num10) * boneData.scaleY; matrix.c = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; matrix.d = MathUtils.SinDeg(num10) * boneData.scaleY; return(matrix); } default: return(matrix); } float degrees = (boneData.rotation + boneData.shearX) - num12; float num14 = ((boneData.rotation + boneData.shearY) - num12) + 90f; float num15 = MathUtils.CosDeg(degrees) * boneData.scaleX; float num16 = MathUtils.CosDeg(num14) * boneData.scaleY; float num17 = MathUtils.SinDeg(degrees) * boneData.scaleX; float num18 = MathUtils.SinDeg(num14) * boneData.scaleY; matrix.a = (a * num15) - (b * num17); matrix.b = (a * num16) - (b * num18); matrix.c = (c * num15) + (d * num17); matrix.d = (c * num16) + (d * num18); return(matrix); }
static Bone GetExtractionBone () { if (extractionBone != null) return extractionBone; SkeletonData skelData = new SkeletonData(); BoneData data = new BoneData(0, "temp", null); data.ScaleX = 1; data.ScaleY = 1; data.Length = 100; skelData.Bones.Add(data); Skeleton skeleton = new Skeleton(skelData); Bone bone = new Bone(data, skeleton, null); bone.UpdateWorldTransform(); extractionBone = bone; return extractionBone; }
static string GetPathRecurse (BoneData b) { if (b == null) { return ""; } return GetPathRecurse(b.Parent) + "/" + b.Name; }
public SkeletonData ReadSkeletonData(Stream input, String skelPath, String animPath, int limitCount = 0, bool backUp = true) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) { data.position *= scale; } data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { data.spacing *= scale; } data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } long _pos = input.Position; // Animations. int _animationCount = ReadVarint(input, true); // 传入的animPath是个bundle path,首先我们要获得真的地址 var resolver = ""; //new Ngame.ABSystem.AssetBundlePathResolver(); update wumiao var realAnimPath = ""; //resolver.ABResPlayGroundAssetPath + "/" + animPath; update wumiao //如果存在先删除,清空数据 if (Directory.Exists(realAnimPath)) { Directory.Delete(realAnimPath, true); } if (!Directory.Exists(realAnimPath)) { Directory.CreateDirectory(realAnimPath); } int _length = skelPath.LastIndexOf("."); string _backupPath = skelPath.Insert(_length, "_backup"); //超过限制数量就拆动画, 否则重定向输入源到backup // HACK 怨念临时修改,只要动画数量不为0就拆,这里传入的limitcount一定是0 if (_animationCount > limitCount) { //备份 if (backUp) { if (File.Exists(_backupPath)) { // redo UnityEditor.AssetDatabase.DeleteAsset(_backupPath); } UnityEditor.AssetDatabase.CopyAsset(skelPath, _backupPath); } } else { if (backUp && File.Exists(_backupPath)) { //如果存在备份文件,则把input源重定向到备份文件 input = new MemoryStream(UnityEditor.AssetDatabase.LoadAssetAtPath <TextAsset>(_backupPath).bytes); } else { Debug.LogErrorFormat("{0}动画已拆分并且不存在备份"); goto EndOfSplit; } } string fullPath = Path.GetFullPath(skelPath); using (FileStream fs = new FileStream(fullPath, FileMode.Truncate)) { byte[] bytes = new byte[_pos]; input.Seek(0, SeekOrigin.Begin); input.Read(bytes, 0, (int)_pos); fs.Write(bytes, 0, bytes.Length); _animationCount = ReadVarint(input, true); //将动画长度写为0 fs.WriteByte(Byte.MinValue); //约定动画的长度不超过255 fs.WriteByte((byte)_animationCount); for (int i = 0, n = _animationCount; i < n; i++) { long _prePos = input.Position; string _aniName = ReadString(input); //强制规范命名。播放动画时同样需要 string _aniFileName = _aniName; foreach (char invalidChar in Path.GetInvalidFileNameChars()) { _aniFileName = _aniFileName.Replace(invalidChar.ToString(), "_"); } long _afterPos = input.Position; long _count = _afterPos - _prePos; bytes = new byte[_count]; input.Seek(_prePos, SeekOrigin.Begin); input.Read(bytes, 0, (int)_count); fs.Write(bytes, 0, bytes.Length); string _name = Path.GetFileName(fullPath); string _aniPath = realAnimPath + "/" + _aniFileName + ".bytes"; using (FileStream anifs = new FileStream(_aniPath, FileMode.Create)) { _prePos = input.Position; ReadAnimation(_aniName, input, skeletonData); _afterPos = input.Position; _count = _afterPos - _prePos; bytes = new byte[_count]; input.Seek(_prePos, SeekOrigin.Begin); input.Read(bytes, 0, (int)_count); anifs.Write(bytes, 0, bytes.Length); anifs.Close(); } } //Assets/NScene/Resources/ //int _index = animPath.IndexOf("Resources/"); //animPath = animPath.Remove(0, _index + 10); // 怨念修改 // 现在传入的已经是bundle path了,直接写入文件即可 WriteString(fs, animPath); fs.Close(); } EndOfSplit: skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.Length = getFloat(boneMap, "length", 0) * Scale; boneData.X = getFloat(boneMap, "x", 0) * Scale; boneData.Y = getFloat(boneMap, "y", 0) * Scale; boneData.Rotation = getFloat(boneMap, "rotation", 0); boneData.ScaleX = getFloat(boneMap, "scaleX", 1); boneData.ScaleY = getFloat(boneMap, "scaleY", 1); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { var slots = (List <Object>)root["slots"]; foreach (Dictionary <String, Object> slotMap in (List <Object>)slots) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.R = toColor(color, 0); slotData.G = toColor(color, 1); slotData.B = toColor(color, 2); slotData.A = toColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.AttachmentName = (String)slotMap["attachment"]; } skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { Dictionary <String, Object> skinMap = (Dictionary <String, Object>)root["skins"]; foreach (KeyValuePair <String, Object> entry in skinMap) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value)) { Attachment attachment = readAttachment(skin, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value); skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.Name == "default") { skeletonData.DefaultSkin = skin; } } } // Animations. if (root.ContainsKey("animations")) { Dictionary <String, Object> animationMap = (Dictionary <String, Object>)root["animations"]; foreach (KeyValuePair <String, Object> entry in animationMap) { readAnimation(entry.Key, (Dictionary <String, Object>)entry.Value, skeletonData); } } skeletonData.Bones.TrimExcess(); skeletonData.Slots.TrimExcess(); skeletonData.Skins.TrimExcess(); skeletonData.Animations.TrimExcess(); return(skeletonData); }
/// <param name="parent">May be null.</param> public BoneData (String name, BoneData parent) { if (name == null) throw new ArgumentNullException("name cannot be null."); this.name = name; this.parent = parent; }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input cannot be null."); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = null; int parentIndex = ReadInt(input, true) - 1; if (parentIndex != -1) parent = skeletonData.bones.Items[parentIndex]; BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.flipX = ReadBoolean(input); boneData.flipY = ReadBoolean(input); boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadInt(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadInt(input, true); ii < nn; ii++) ikConstraintData.bones.Add(skeletonData.bones.Items[ReadInt(input, true)]); ikConstraintData.target = skeletonData.bones.Items[ReadInt(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Slots. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadInt(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadInt(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadInt(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Events. for (int i = 0, n = ReadInt(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadInt(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadInt(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader", "reader cannot be null."); } float scale = Scale; SkeletonData skeletonData = new SkeletonData(); Dictionary <string, object> dictionary = Json.Deserialize(reader) as Dictionary <string, object>; if (dictionary == null) { throw new Exception("Invalid JSON."); } if (dictionary.ContainsKey("skeleton")) { Dictionary <string, object> dictionary2 = (Dictionary <string, object>)dictionary["skeleton"]; skeletonData.hash = (string)dictionary2["hash"]; skeletonData.version = (string)dictionary2["spine"]; skeletonData.width = GetFloat(dictionary2, "width", 0f); skeletonData.height = GetFloat(dictionary2, "height", 0f); skeletonData.fps = GetFloat(dictionary2, "fps", 0f); skeletonData.imagesPath = GetString(dictionary2, "images", null); } foreach (Dictionary <string, object> item in (List <object>)dictionary["bones"]) { BoneData boneData = null; if (item.ContainsKey("parent")) { boneData = skeletonData.FindBone((string)item["parent"]); if (boneData == null) { throw new Exception("Parent bone not found: " + item["parent"]); } } BoneData boneData2 = new BoneData(skeletonData.Bones.Count, (string)item["name"], boneData); boneData2.length = GetFloat(item, "length", 0f) * scale; boneData2.x = GetFloat(item, "x", 0f) * scale; boneData2.y = GetFloat(item, "y", 0f) * scale; boneData2.rotation = GetFloat(item, "rotation", 0f); boneData2.scaleX = GetFloat(item, "scaleX", 1f); boneData2.scaleY = GetFloat(item, "scaleY", 1f); boneData2.shearX = GetFloat(item, "shearX", 0f); boneData2.shearY = GetFloat(item, "shearY", 0f); string @string = GetString(item, "transform", TransformMode.Normal.ToString()); boneData2.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), @string, ignoreCase: true); skeletonData.bones.Add(boneData2); } if (dictionary.ContainsKey("slots")) { foreach (Dictionary <string, object> item2 in (List <object>)dictionary["slots"]) { string name = (string)item2["name"]; string text = (string)item2["bone"]; BoneData boneData3 = skeletonData.FindBone(text); if (boneData3 == null) { throw new Exception("Slot bone not found: " + text); } SlotData slotData = new SlotData(skeletonData.Slots.Count, name, boneData3); if (item2.ContainsKey("color")) { string hexString = (string)item2["color"]; slotData.r = ToColor(hexString, 0); slotData.g = ToColor(hexString, 1); slotData.b = ToColor(hexString, 2); slotData.a = ToColor(hexString, 3); } if (item2.ContainsKey("dark")) { string hexString2 = (string)item2["dark"]; slotData.r2 = ToColor(hexString2, 0, 6); slotData.g2 = ToColor(hexString2, 1, 6); slotData.b2 = ToColor(hexString2, 2, 6); slotData.hasSecondColor = true; } slotData.attachmentName = GetString(item2, "attachment", null); if (item2.ContainsKey("blend")) { slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (string)item2["blend"], ignoreCase: true); } else { slotData.blendMode = BlendMode.Normal; } skeletonData.slots.Add(slotData); } } if (dictionary.ContainsKey("ik")) { foreach (Dictionary <string, object> item3 in (List <object>)dictionary["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((string)item3["name"]); ikConstraintData.order = GetInt(item3, "order", 0); foreach (string item4 in (List <object>)item3["bones"]) { BoneData boneData4 = skeletonData.FindBone(item4); if (boneData4 == null) { throw new Exception("IK constraint bone not found: " + item4); } ikConstraintData.bones.Add(boneData4); } string text3 = (string)item3["target"]; ikConstraintData.target = skeletonData.FindBone(text3); if (ikConstraintData.target == null) { throw new Exception("Target bone not found: " + text3); } ikConstraintData.bendDirection = (GetBoolean(item3, "bendPositive", defaultValue: true) ? 1 : (-1)); ikConstraintData.mix = GetFloat(item3, "mix", 1f); skeletonData.ikConstraints.Add(ikConstraintData); } } if (dictionary.ContainsKey("transform")) { foreach (Dictionary <string, object> item5 in (List <object>)dictionary["transform"]) { TransformConstraintData transformConstraintData = new TransformConstraintData((string)item5["name"]); transformConstraintData.order = GetInt(item5, "order", 0); foreach (string item6 in (List <object>)item5["bones"]) { BoneData boneData5 = skeletonData.FindBone(item6); if (boneData5 == null) { throw new Exception("Transform constraint bone not found: " + item6); } transformConstraintData.bones.Add(boneData5); } string text5 = (string)item5["target"]; transformConstraintData.target = skeletonData.FindBone(text5); if (transformConstraintData.target == null) { throw new Exception("Target bone not found: " + text5); } transformConstraintData.local = GetBoolean(item5, "local", defaultValue: false); transformConstraintData.relative = GetBoolean(item5, "relative", defaultValue: false); transformConstraintData.offsetRotation = GetFloat(item5, "rotation", 0f); transformConstraintData.offsetX = GetFloat(item5, "x", 0f) * scale; transformConstraintData.offsetY = GetFloat(item5, "y", 0f) * scale; transformConstraintData.offsetScaleX = GetFloat(item5, "scaleX", 0f); transformConstraintData.offsetScaleY = GetFloat(item5, "scaleY", 0f); transformConstraintData.offsetShearY = GetFloat(item5, "shearY", 0f); transformConstraintData.rotateMix = GetFloat(item5, "rotateMix", 1f); transformConstraintData.translateMix = GetFloat(item5, "translateMix", 1f); transformConstraintData.scaleMix = GetFloat(item5, "scaleMix", 1f); transformConstraintData.shearMix = GetFloat(item5, "shearMix", 1f); skeletonData.transformConstraints.Add(transformConstraintData); } } if (dictionary.ContainsKey("path")) { foreach (Dictionary <string, object> item7 in (List <object>)dictionary["path"]) { PathConstraintData pathConstraintData = new PathConstraintData((string)item7["name"]); pathConstraintData.order = GetInt(item7, "order", 0); foreach (string item8 in (List <object>)item7["bones"]) { BoneData boneData6 = skeletonData.FindBone(item8); if (boneData6 == null) { throw new Exception("Path bone not found: " + item8); } pathConstraintData.bones.Add(boneData6); } string text7 = (string)item7["target"]; pathConstraintData.target = skeletonData.FindSlot(text7); if (pathConstraintData.target == null) { throw new Exception("Target slot not found: " + text7); } pathConstraintData.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(item7, "positionMode", "percent"), ignoreCase: true); pathConstraintData.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(item7, "spacingMode", "length"), ignoreCase: true); pathConstraintData.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(item7, "rotateMode", "tangent"), ignoreCase: true); pathConstraintData.offsetRotation = GetFloat(item7, "rotation", 0f); pathConstraintData.position = GetFloat(item7, "position", 0f); if (pathConstraintData.positionMode == PositionMode.Fixed) { pathConstraintData.position *= scale; } pathConstraintData.spacing = GetFloat(item7, "spacing", 0f); if (pathConstraintData.spacingMode == SpacingMode.Length || pathConstraintData.spacingMode == SpacingMode.Fixed) { pathConstraintData.spacing *= scale; } pathConstraintData.rotateMix = GetFloat(item7, "rotateMix", 1f); pathConstraintData.translateMix = GetFloat(item7, "translateMix", 1f); skeletonData.pathConstraints.Add(pathConstraintData); } } if (dictionary.ContainsKey("skins")) { foreach (KeyValuePair <string, object> item9 in (Dictionary <string, object>)dictionary["skins"]) { Skin skin = new Skin(item9.Key); foreach (KeyValuePair <string, object> item10 in (Dictionary <string, object>)item9.Value) { int slotIndex = skeletonData.FindSlotIndex(item10.Key); foreach (KeyValuePair <string, object> item11 in (Dictionary <string, object>)item10.Value) { try { Attachment attachment = ReadAttachment((Dictionary <string, object>)item11.Value, skin, slotIndex, item11.Key, skeletonData); if (attachment != null) { skin.AddAttachment(slotIndex, item11.Key, attachment); } } catch (Exception innerException) { throw new Exception("Error reading attachment: " + item11.Key + ", skin: " + skin, innerException); } } } skeletonData.skins.Add(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } int i = 0; for (int count = linkedMeshes.Count; i < count; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin2 = (linkedMesh.skin != null) ? skeletonData.FindSkin(linkedMesh.skin) : skeletonData.defaultSkin; if (skin2 == null) { throw new Exception("Slot not found: " + linkedMesh.skin); } Attachment attachment2 = skin2.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (attachment2 == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } linkedMesh.mesh.ParentMesh = (MeshAttachment)attachment2; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); if (dictionary.ContainsKey("events")) { foreach (KeyValuePair <string, object> item12 in (Dictionary <string, object>)dictionary["events"]) { Dictionary <string, object> map = (Dictionary <string, object>)item12.Value; EventData eventData = new EventData(item12.Key); eventData.Int = GetInt(map, "int", 0); eventData.Float = GetFloat(map, "float", 0f); eventData.String = GetString(map, "string", string.Empty); skeletonData.events.Add(eventData); } } if (dictionary.ContainsKey("animations")) { foreach (KeyValuePair <string, object> item13 in (Dictionary <string, object>)dictionary["animations"]) { try { ReadAnimation((Dictionary <string, object>)item13.Value, item13.Key, skeletonData); } catch (Exception innerException2) { throw new Exception("Error reading animation: " + item13.Key, innerException2); } } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary<String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); } // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } var boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.bones.Add(boneData); } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary<String, Object> ikMap in (List<Object>)root["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]); foreach (String boneName in (List<Object>)ikMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("IK bone not found: " + boneName); ikConstraintData.bones.Add(bone); } String targetName = (String)ikMap["target"]; ikConstraintData.target = skeletonData.FindBone(targetName); if (ikConstraintData.target == null) throw new Exception("Target bone not found: " + targetName); ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1; ikConstraintData.mix = GetFloat(ikMap, "mix", 1); skeletonData.ikConstraints.Add(ikConstraintData); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary<String, Object> transformMap in (List<Object>)root["ik"]) { TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]); String boneName = (String)transformMap["bone"]; transformConstraintData.target = skeletonData.FindBone(boneName); if (transformConstraintData.target == null) throw new Exception("Bone not found: " + boneName); String targetName = (String)transformMap["target"]; transformConstraintData.target = skeletonData.FindBone(targetName); if (transformConstraintData.target == null) throw new Exception("Target bone not found: " + targetName); transformConstraintData.translateMix = GetFloat(transformMap, "mix", 1); transformConstraintData.x = GetFloat(transformMap, "x", 0); transformConstraintData.y = GetFloat(transformMap, "y", 0); skeletonData.transformConstraints.Add(transformConstraintData); } } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); var slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.attachmentName = (String)slotMap["attachment"]; if (slotMap.ContainsKey("blend")) slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); else slotData.blendMode = BlendMode.normal; skeletonData.slots.Add(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) { var skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.skins.Add(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; var eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.events.Add(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; int darkColor = ReadInt(input); // 0x00rrggbb if (darkColor != -1) { slotData.hasSecondColor = true; slotData.r2 = ((darkColor & 0x00ff0000) >> 16) / 255f; slotData.g2 = ((darkColor & 0x0000ff00) >> 8) / 255f; slotData.b2 = ((darkColor & 0x000000ff)) / 255f; } slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.local = ReadBoolean(input); data.relative = ReadBoolean(input); data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) { data.position *= scale; } data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { data.spacing *= scale; } data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, skeletonData, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, skeletonData, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary <String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } var boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.flipX = GetBoolean(boneMap, "flipX", false); boneData.flipY = GetBoolean(boneMap, "flipY", false); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.bones.Add(boneData); } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary <String, Object> ikMap in (List <Object>)root["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]); foreach (String boneName in (List <Object>)ikMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("IK bone not found: " + boneName); } ikConstraintData.bones.Add(bone); } String targetName = (String)ikMap["target"]; ikConstraintData.target = skeletonData.FindBone(targetName); if (ikConstraintData.target == null) { throw new Exception("Target bone not found: " + targetName); } ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1; ikConstraintData.mix = GetFloat(ikMap, "mix", 1); skeletonData.ikConstraints.Add(ikConstraintData); } } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } var slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.attachmentName = (String)slotMap["attachment"]; } if (slotMap.ContainsKey("additive")) { slotData.additiveBlending = (bool)slotMap["additive"]; } skeletonData.slots.Add(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["skins"]) { var skin = new Skin(entry.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value); if (attachment != null) { skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } } skeletonData.skins.Add(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"]) { var entryMap = (Dictionary <String, Object>)entry.Value; var eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.events.Add(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"]) { ReadAnimation(entry.Key, (Dictionary <String, Object>)entry.Value, skeletonData); } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.animations.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } var scale = this.Scale; var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary <String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } var boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * scale; boneData.x = GetFloat(boneMap, "x", 0) * scale; boneData.y = GetFloat(boneMap, "y", 0) * scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.bones.Add(boneData); } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary <String, Object> ikMap in (List <Object>)root["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]); foreach (String boneName in (List <Object>)ikMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("IK bone not found: " + boneName); } ikConstraintData.bones.Add(bone); } String targetName = (String)ikMap["target"]; ikConstraintData.target = skeletonData.FindBone(targetName); if (ikConstraintData.target == null) { throw new Exception("Target bone not found: " + targetName); } ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1; ikConstraintData.mix = GetFloat(ikMap, "mix", 1); skeletonData.ikConstraints.Add(ikConstraintData); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary <String, Object> transformMap in (List <Object>)root["transform"]) { TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]); String boneName = (String)transformMap["bone"]; transformConstraintData.bone = skeletonData.FindBone(boneName); if (transformConstraintData.bone == null) { throw new Exception("Bone not found: " + boneName); } String targetName = (String)transformMap["target"]; transformConstraintData.target = skeletonData.FindBone(targetName); if (transformConstraintData.target == null) { throw new Exception("Target bone not found: " + targetName); } transformConstraintData.translateMix = GetFloat(transformMap, "translateMix", 1); transformConstraintData.x = GetFloat(transformMap, "x", 0) * scale; transformConstraintData.y = GetFloat(transformMap, "y", 0) * scale; skeletonData.transformConstraints.Add(transformConstraintData); } } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } var slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.attachmentName = (String)slotMap["attachment"]; } if (slotMap.ContainsKey("blend")) { slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); } else { slotData.blendMode = BlendMode.normal; } skeletonData.slots.Add(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["skins"]) { var skin = new Skin(entry.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, slotIndex, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value); if (attachment != null) { skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } } skeletonData.skins.Add(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Slot not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { UnityEngine.Debug.Log(linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"]) { var entryMap = (Dictionary <String, Object>)entry.Value; var eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.events.Add(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"]) { ReadAnimation(entry.Key, (Dictionary <String, Object>)entry.Value, skeletonData); } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
static BoneMatrix GetInheritedInternal(BoneData boneData, BoneMatrix parentMatrix) { var parent = boneData.parent; if (parent == null) { return(new BoneMatrix(boneData)); // isRootBone } float pa = parentMatrix.a, pb = parentMatrix.b, pc = parentMatrix.c, pd = parentMatrix.d; BoneMatrix result = default(BoneMatrix); result.x = pa * boneData.x + pb * boneData.y + parentMatrix.x; result.y = pc * boneData.x + pd * boneData.y + parentMatrix.y; switch (boneData.transformMode) { case TransformMode.Normal: { float rotationY = boneData.rotation + 90 + boneData.shearY; float la = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; float lb = MathUtils.CosDeg(rotationY) * boneData.scaleY; float lc = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; float ld = MathUtils.SinDeg(rotationY) * boneData.scaleY; result.a = pa * la + pb * lc; result.b = pa * lb + pb * ld; result.c = pc * la + pd * lc; result.d = pc * lb + pd * ld; break; } case TransformMode.OnlyTranslation: { float rotationY = boneData.rotation + 90 + boneData.shearY; result.a = MathUtils.CosDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; result.b = MathUtils.CosDeg(rotationY) * boneData.scaleY; result.c = MathUtils.SinDeg(boneData.rotation + boneData.shearX) * boneData.scaleX; result.d = MathUtils.SinDeg(rotationY) * boneData.scaleY; break; } case TransformMode.NoRotationOrReflection: { float s = pa * pa + pc * pc, prx; if (s > 0.0001f) { s = Math.Abs(pa * pd - pb * pc) / s; pb = pc * s; pd = pa * s; prx = MathUtils.Atan2(pc, pa) * MathUtils.RadDeg; } else { pa = 0; pc = 0; prx = 90 - MathUtils.Atan2(pd, pb) * MathUtils.RadDeg; } float rx = boneData.rotation + boneData.shearX - prx; float ry = boneData.rotation + boneData.shearY - prx + 90; float la = MathUtils.CosDeg(rx) * boneData.scaleX; float lb = MathUtils.CosDeg(ry) * boneData.scaleY; float lc = MathUtils.SinDeg(rx) * boneData.scaleX; float ld = MathUtils.SinDeg(ry) * boneData.scaleY; result.a = pa * la - pb * lc; result.b = pa * lb - pb * ld; result.c = pc * la + pd * lc; result.d = pc * lb + pd * ld; break; } case TransformMode.NoScale: case TransformMode.NoScaleOrReflection: { float cos = MathUtils.CosDeg(boneData.rotation), sin = MathUtils.SinDeg(boneData.rotation); float za = pa * cos + pb * sin; float zc = pc * cos + pd * sin; float s = (float)Math.Sqrt(za * za + zc * zc); if (s > 0.00001f) { s = 1 / s; } za *= s; zc *= s; s = (float)Math.Sqrt(za * za + zc * zc); float r = MathUtils.PI / 2 + MathUtils.Atan2(zc, za); float zb = MathUtils.Cos(r) * s; float zd = MathUtils.Sin(r) * s; float la = MathUtils.CosDeg(boneData.shearX) * boneData.scaleX; float lb = MathUtils.CosDeg(90 + boneData.shearY) * boneData.scaleY; float lc = MathUtils.SinDeg(boneData.shearX) * boneData.scaleX; float ld = MathUtils.SinDeg(90 + boneData.shearY) * boneData.scaleY; if (boneData.transformMode != TransformMode.NoScaleOrReflection ? pa * pd - pb * pc < 0 : false) { zb = -zb; zd = -zd; } result.a = za * la + zb * lc; result.b = za * lb + zb * ld; result.c = zc * la + zd * lc; result.d = zc * lb + zd * ld; break; } } return(result); }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input"); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); ikConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input)); transformConstraintData.bone = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.translateMix = ReadFloat(input); transformConstraintData.x = ReadFloat(input) * scale; transformConstraintData.y = ReadFloat(input) * scale; skeletonData.transformConstraints.Add(transformConstraintData); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadVarint(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"]) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.attachmentName = (String)slotMap["attachment"]; } if (slotMap.ContainsKey("additive")) { slotData.additiveBlending = (bool)slotMap["additive"]; } skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["skins"]) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value); if (attachment != null) { skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } } skeletonData.AddSkin(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"]) { var entryMap = (Dictionary <String, Object>)entry.Value; EventData eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.AddEvent(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"]) { ReadAnimation(entry.Key, (Dictionary <String, Object>)entry.Value, skeletonData); } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.animations.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader", "reader cannot be null."); } var scale = this.Scale; var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary <String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); skeletonData.fps = GetFloat(skeletonMap, "fps", 0); skeletonData.imagesPath = GetString(skeletonMap, "images", null); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent); data.length = GetFloat(boneMap, "length", 0) * scale; data.x = GetFloat(boneMap, "x", 0) * scale; data.y = GetFloat(boneMap, "y", 0) * scale; data.rotation = GetFloat(boneMap, "rotation", 0); data.scaleX = GetFloat(boneMap, "scaleX", 1); data.scaleY = GetFloat(boneMap, "scaleY", 1); data.shearX = GetFloat(boneMap, "shearX", 0); data.shearY = GetFloat(boneMap, "shearY", 0); string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString()); data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true); skeletonData.bones.Add(data); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } var data = new SlotData(skeletonData.Slots.Count, slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; data.r = ToColor(color, 0); data.g = ToColor(color, 1); data.b = ToColor(color, 2); data.a = ToColor(color, 3); } data.attachmentName = GetString(slotMap, "attachment", null); if (slotMap.ContainsKey("blend")) { data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); } else { data.blendMode = BlendMode.normal; } skeletonData.slots.Add(data); } } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["ik"]) { IkConstraintData data = new IkConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List <Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("IK constraint bone not found: " + boneName); } data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) { throw new Exception("Target bone not found: " + targetName); } data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1; data.mix = GetFloat(constraintMap, "mix", 1); skeletonData.ikConstraints.Add(data); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["transform"]) { TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List <Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("Transform constraint bone not found: " + boneName); } data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) { throw new Exception("Target bone not found: " + targetName); } data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.offsetX = GetFloat(constraintMap, "x", 0) * scale; data.offsetY = GetFloat(constraintMap, "y", 0) * scale; data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0); data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0); data.offsetShearY = GetFloat(constraintMap, "shearY", 0); data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); data.scaleMix = GetFloat(constraintMap, "scaleMix", 1); data.shearMix = GetFloat(constraintMap, "shearMix", 1); skeletonData.transformConstraints.Add(data); } } // Path constraints. if (root.ContainsKey("path")) { foreach (Dictionary <String, Object> constraintMap in (List <Object>)root["path"]) { PathConstraintData data = new PathConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List <Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("Path bone not found: " + boneName); } data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindSlot(targetName); if (data.target == null) { throw new Exception("Target slot not found: " + targetName); } data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true); data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true); data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true); data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.position = GetFloat(constraintMap, "position", 0); if (data.positionMode == PositionMode.Fixed) { data.position *= scale; } data.spacing = GetFloat(constraintMap, "spacing", 0); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { data.spacing *= scale; } data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); skeletonData.pathConstraints.Add(data); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair <String, Object> skinMap in (Dictionary <String, Object>)root["skins"]) { var skin = new Skin(skinMap.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)skinMap.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> entry in ((Dictionary <String, Object>)slotEntry.Value)) { try { Attachment attachment = ReadAttachment((Dictionary <String, Object>)entry.Value, skin, slotIndex, entry.Key); if (attachment != null) { skin.AddAttachment(slotIndex, entry.Key, attachment); } } catch (Exception e) { throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e); } } } skeletonData.skins.Add(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Slot not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["events"]) { var entryMap = (Dictionary <String, Object>)entry.Value; var data = new EventData(entry.Key); data.Int = GetInt(entryMap, "int", 0); data.Float = GetFloat(entryMap, "float", 0); data.String = GetString(entryMap, "string", string.Empty); skeletonData.events.Add(data); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["animations"]) { try { ReadAnimation((Dictionary <String, Object>)entry.Value, entry.Key, skeletonData); } catch (Exception e) { throw new Exception("Error reading animation: " + entry.Key, e); } } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData (Stream input) { if (input == null) throw new ArgumentNullException("input"); float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) skeletonData.hash = null; skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) skeletonData.version = null; skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) skeletonData.imagesPath = null; } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) ReadInt(input); // Skip bone color. skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Skin not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) ReadAnimation(ReadString(input), input, skeletonData); skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * Scale; boneData.x = GetFloat(boneMap, "x", 0) * Scale; boneData.y = GetFloat(boneMap, "y", 0) * Scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.attachmentName = (String)slotMap["attachment"]; if (slotMap.ContainsKey("additive")) slotData.additiveBlending = (bool)slotMap["additive"]; skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["skins"]) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); if (attachment != null) skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; EventData eventData = new EventData(entry.Key); eventData.Int = GetInt(entryMap, "int", 0); eventData.Float = GetFloat(entryMap, "float", 0); eventData.String = GetString(entryMap, "string", null); skeletonData.AddEvent(eventData); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) ReadAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.animations.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input cannot be null."); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = null; int parentIndex = ReadInt(input, true) - 1; if (parentIndex != -1) { parent = skeletonData.bones[parentIndex]; } BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.flipX = ReadBoolean(input); boneData.flipY = ReadBoolean(input); boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadInt(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadInt(input, true); ii < nn; ii++) { ikConstraintData.bones.Add(skeletonData.bones[ReadInt(input, true)]); } ikConstraintData.target = skeletonData.bones[ReadInt(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Slots. for (int i = 0, n = ReadInt(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones[ReadInt(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadInt(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadInt(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); } // Events. for (int i = 0, n = ReadInt(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadInt(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadInt(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
public SkeletonData ReadSkeletonData (TextReader reader) { if (reader == null) throw new ArgumentNullException("reader", "reader cannot be null."); var scale = this.Scale; var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary<String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); skeletonData.fps = GetFloat(skeletonMap, "fps", 0); skeletonData.imagesPath = GetString(skeletonMap, "images", null); } // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } var data = new BoneData(skeletonData.Bones.Count, (String)boneMap["name"], parent); data.length = GetFloat(boneMap, "length", 0) * scale; data.x = GetFloat(boneMap, "x", 0) * scale; data.y = GetFloat(boneMap, "y", 0) * scale; data.rotation = GetFloat(boneMap, "rotation", 0); data.scaleX = GetFloat(boneMap, "scaleX", 1); data.scaleY = GetFloat(boneMap, "scaleY", 1); data.shearX = GetFloat(boneMap, "shearX", 0); data.shearY = GetFloat(boneMap, "shearY", 0); string tm = GetString(boneMap, "transform", TransformMode.Normal.ToString()); data.transformMode = (TransformMode)Enum.Parse(typeof(TransformMode), tm, true); skeletonData.bones.Add(data); } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary<String, Object> slotMap in (List<Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); var data = new SlotData(skeletonData.Slots.Count, slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; data.r = ToColor(color, 0); data.g = ToColor(color, 1); data.b = ToColor(color, 2); data.a = ToColor(color, 3); } data.attachmentName = GetString(slotMap, "attachment", null); if (slotMap.ContainsKey("blend")) data.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); else data.blendMode = BlendMode.normal; skeletonData.slots.Add(data); } } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["ik"]) { IkConstraintData data = new IkConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("IK constraint bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) throw new Exception("Target bone not found: " + targetName); data.bendDirection = GetBoolean(constraintMap, "bendPositive", true) ? 1 : -1; data.mix = GetFloat(constraintMap, "mix", 1); skeletonData.ikConstraints.Add(data); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["transform"]) { TransformConstraintData data = new TransformConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("Transform constraint bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindBone(targetName); if (data.target == null) throw new Exception("Target bone not found: " + targetName); data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.offsetX = GetFloat(constraintMap, "x", 0) * scale; data.offsetY = GetFloat(constraintMap, "y", 0) * scale; data.offsetScaleX = GetFloat(constraintMap, "scaleX", 0); data.offsetScaleY = GetFloat(constraintMap, "scaleY", 0); data.offsetShearY = GetFloat(constraintMap, "shearY", 0); data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); data.scaleMix = GetFloat(constraintMap, "scaleMix", 1); data.shearMix = GetFloat(constraintMap, "shearMix", 1); skeletonData.transformConstraints.Add(data); } } // Path constraints. if(root.ContainsKey("path")) { foreach (Dictionary<String, Object> constraintMap in (List<Object>)root["path"]) { PathConstraintData data = new PathConstraintData((String)constraintMap["name"]); data.order = GetInt(constraintMap, "order", 0); foreach (String boneName in (List<Object>)constraintMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) throw new Exception("Path bone not found: " + boneName); data.bones.Add(bone); } String targetName = (String)constraintMap["target"]; data.target = skeletonData.FindSlot(targetName); if (data.target == null) throw new Exception("Target slot not found: " + targetName); data.positionMode = (PositionMode)Enum.Parse(typeof(PositionMode), GetString(constraintMap, "positionMode", "percent"), true); data.spacingMode = (SpacingMode)Enum.Parse(typeof(SpacingMode), GetString(constraintMap, "spacingMode", "length"), true); data.rotateMode = (RotateMode)Enum.Parse(typeof(RotateMode), GetString(constraintMap, "rotateMode", "tangent"), true); data.offsetRotation = GetFloat(constraintMap, "rotation", 0); data.position = GetFloat(constraintMap, "position", 0); if (data.positionMode == PositionMode.Fixed) data.position *= scale; data.spacing = GetFloat(constraintMap, "spacing", 0); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) data.spacing *= scale; data.rotateMix = GetFloat(constraintMap, "rotateMix", 1); data.translateMix = GetFloat(constraintMap, "translateMix", 1); skeletonData.pathConstraints.Add(data); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair<String, Object> skinMap in (Dictionary<String, Object>)root["skins"]) { var skin = new Skin(skinMap.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)skinMap.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> entry in ((Dictionary<String, Object>)slotEntry.Value)) { try { Attachment attachment = ReadAttachment((Dictionary<String, Object>)entry.Value, skin, slotIndex, entry.Key); if (attachment != null) skin.AddAttachment(slotIndex, entry.Key, attachment); } catch (Exception e) { throw new Exception("Error reading attachment: " + entry.Key + ", skin: " + skin, e); } } } skeletonData.skins.Add(skin); if (skin.name == "default") skeletonData.defaultSkin = skin; } } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) throw new Exception("Slot not found: " + linkedMesh.skin); Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) throw new Exception("Parent mesh not found: " + linkedMesh.parent); linkedMesh.mesh.ParentMesh = (MeshAttachment)parent; linkedMesh.mesh.UpdateUVs(); } linkedMeshes.Clear(); // Events. if (root.ContainsKey("events")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["events"]) { var entryMap = (Dictionary<String, Object>)entry.Value; var data = new EventData(entry.Key); data.Int = GetInt(entryMap, "int", 0); data.Float = GetFloat(entryMap, "float", 0); data.String = GetString(entryMap, "string", string.Empty); skeletonData.events.Add(data); } } // Animations. if (root.ContainsKey("animations")) { foreach (KeyValuePair<String, Object> entry in (Dictionary<String, Object>)root["animations"]) { try { ReadAnimation((Dictionary<String, Object>)entry.Value, entry.Key, skeletonData); } catch (Exception e) { throw new Exception("Error reading animation: " + entry.Key, e); } } } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return skeletonData; }
static string GetPath (BoneData b) { return GetPathRecurse(b).Substring(1); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) throw new ArgumentNullException("reader cannot be null."); SkeletonData skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary<String, Object>; if (root == null) throw new Exception("Invalid JSON."); // Bones. foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) throw new Exception("Parent bone not found: " + boneMap["parent"]); } BoneData boneData = new BoneData((String)boneMap["name"], parent); boneData.Length = getFloat(boneMap, "length", 0) * Scale; boneData.X = getFloat(boneMap, "x", 0) * Scale; boneData.Y = getFloat(boneMap, "y", 0) * Scale; boneData.Rotation = getFloat(boneMap, "rotation", 0); boneData.ScaleX = getFloat(boneMap, "scaleX", 1); boneData.ScaleY = getFloat(boneMap, "scaleY", 1); skeletonData.AddBone(boneData); } // Slots. if (root.ContainsKey("slots")) { var slots = (List<Object>)root["slots"]; foreach (Dictionary<String, Object> slotMap in (List<Object>)slots) { String slotName = (String)slotMap["name"]; String boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) throw new Exception("Slot bone not found: " + boneName); SlotData slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { String color = (String)slotMap["color"]; slotData.R = toColor(color, 0); slotData.G = toColor(color, 1); slotData.B = toColor(color, 2); slotData.A = toColor(color, 3); } if (slotMap.ContainsKey("attachment")) slotData.AttachmentName = (String)slotMap["attachment"]; skeletonData.AddSlot(slotData); } } // Skins. if (root.ContainsKey("skins")) { Dictionary<String, Object> skinMap = (Dictionary<String, Object>)root["skins"]; foreach (KeyValuePair<String, Object> entry in skinMap) { Skin skin = new Skin(entry.Key); foreach (KeyValuePair<String, Object> slotEntry in (Dictionary<String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair<String, Object> attachmentEntry in ((Dictionary<String, Object>)slotEntry.Value)) { Attachment attachment = readAttachment(skin, attachmentEntry.Key, (Dictionary<String, Object>)attachmentEntry.Value); skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } skeletonData.AddSkin(skin); if (skin.Name == "default") skeletonData.DefaultSkin = skin; } } // Animations. if (root.ContainsKey("animations")) { Dictionary<String, Object> animationMap = (Dictionary<String, Object>)root["animations"]; foreach (KeyValuePair<String, Object> entry in animationMap) readAnimation(entry.Key, (Dictionary<String, Object>)entry.Value, skeletonData); } skeletonData.Bones.TrimExcess(); skeletonData.Slots.TrimExcess(); skeletonData.Skins.TrimExcess(); skeletonData.Animations.TrimExcess(); return skeletonData; }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData boneData = new BoneData(name, parent); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.rotation = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.inheritScale = ReadBoolean(input); boneData.inheritRotation = ReadBoolean(input); if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } ikConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input)); transformConstraintData.bone = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.translateMix = ReadFloat(input); transformConstraintData.x = ReadFloat(input) * scale; transformConstraintData.y = ReadFloat(input) * scale; skeletonData.transformConstraints.Add(transformConstraintData); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadVarint(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
// --- Bones. public void AddBone(BoneData bone) { if (bone == null) throw new ArgumentNullException("bone cannot be null."); bones.Add(bone); }
void HeroCharacterSet() { Debug.Log("Hero_Character_Set"); string w0 = "wuqi"; string w1 = "R_wuqi"; weaponBaseData = skeleton.FindSlot(w0).Bone.Data; weaponBaseData2 = skeleton.FindSlot(w1).Bone.Data; // Material M = GetComponent <MeshRenderer>().material; if (skeleton.FindSlot(w0).Attachment != null) { M.shader = skeleton.FindSlot(w0).Attachment.GetMaterial().shader; } if (CMC.notShowWeaponSlot) { skeleton.FindSlot(w0).A = 0; skeleton.FindSlot(w1).A = 0; } else { skeleton.FindSlot(w0).A = 1; skeleton.FindSlot(w1).A = 1; // EquipItem weapon = SDDataManager.Instance.GetEquipItemById(CMC.weaponId); if (weapon == null) { return; } List <Sprite> allSprites = Resources.LoadAll <Sprite>("Spine/WeaponVision/").ToList(); weaponSprite = allSprites.Find(x => x.name == weapon.ID); if (weaponSprite == null) { return; } Spine.Slot baseWQ = skeleton.FindSlot(w0); string an = baseWQ.Data.AttachmentName; Spine.Attachment originalAttachment = baseWQ.Attachment; Spine.Attachment newAttachment = originalAttachment.GetRemappedClone(weaponSprite, M); if (weapon.WeaponRace.WeaponClass == SDConstants.WeaponClass.Sharp) { Spine.Slot otherWQ = skeleton.FindSlot(w1); string newWSName = weaponSprite.name + "_2"; weaponSprite2 = allSprites.Find(x => x.name == newWSName); if (weaponSprite2 != null) { Spine.Attachment oa1 = otherWQ.Attachment; Spine.Attachment newoa1 = oa1.GetRemappedClone(weaponSprite2, M); otherWQ.Attachment = newoa1; } } else if (weapon.WeaponRace.WeaponClass == SDConstants.WeaponClass.Claymore) { baseWQ.Bone.ScaleY = -weaponBaseData.ScaleY; baseWQ.Bone.Rotation = weaponBaseData.Rotation - 45; baseWQ.Bone.SetLocalPosition(new Vector2(0, -weaponBaseData.Y * 2)); } baseWQ.Attachment = newAttachment; } }