private void ParseBones(Dictionary <String, Object> root) { int index = 0; foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { string boneName = GetString(boneMap, "name", ""); Transform bone = GetBone(_skeleton, boneName); Transform parent = FindChildRecursive(_skeleton.transform, GetString(boneMap, "parent", "")); if (!parent) { parent = _skeleton.transform; } bone.parent = parent; bone.localPosition = new Vector3(GetFloat(boneMap, "x", 0), GetFloat(boneMap, "y", 0), 0); bone.localScale = new Vector3(GetFloat(boneMap, "scaleX", 1), GetFloat(boneMap, "scaleY", 1), 1); bone.localEulerAngles = new Vector3(0, 0, GetFloat(boneMap, "rotation", 0)); SpineBone spineBone = bone.GetComponent <SpineBone>(); if (!spineBone) { spineBone = bone.gameObject.AddComponent <SpineBone>(); } spineBone.Index = index; spineBone.Length = GetFloat(boneMap, "length", 0); index++; } }
private void ParseAttachment(string skinName, string slotName, string attachmentName, Dictionary <String, Object> root) { switch (GetString(root, "type", "region")) { case "region": { SpineAttachment attachment = GetOrCreateAttachment(skinName, slotName, attachmentName, SpineAttachment.AttachmentType.Region); attachment.Sprite = GetSprite(GetString(root, "name", attachmentName)); attachment.PositionOffset = new Vector2(GetFloat(root, "x", 0f), GetFloat(root, "y", 0f)); attachment.ScaleOffset = new Vector2(GetFloat(root, "scaleX", 1f), GetFloat(root, "scaleY", 1f)); attachment.RotationOffset = GetFloat(root, "rotation", 0f); attachment.Refresh(); break; } case "mesh": { Mesh mesh = new Mesh(); mesh.vertices = GetVector3Array(root, "vertices", 2); mesh.uv = GetVector2Array(root, "uvs"); mesh.triangles = GetIntArray(root, "triangles"); SpineMesh spineMesh = new SpineMesh(); spineMesh.Mesh = mesh; spineMesh.Color = ToColor(GetString(root, "color", "ffffffff")); spineMesh.Texture = GetTexture(GetString(root, "name", attachmentName)); SpineAttachment attachment = GetOrCreateAttachment(skinName, slotName, attachmentName, SpineAttachment.AttachmentType.Mesh); attachment.Mesh = spineMesh; break; } case "skinnedmesh": { SpineAttachment attachment = GetOrCreateAttachment(skinName, slotName, attachmentName, SpineAttachment.AttachmentType.SkinnedMesh); Vector2[] uvs = GetVector2Array(root, "uvs"); int[] triangles = GetIntArray(root, "triangles"); List <Vector3> vertices = new List <Vector3>(uvs.Length); float[] meshData = GetFloatArray(root, "vertices", 1); List <BoneWeight> weights = new List <BoneWeight>(); List <Transform> bones = new List <Transform>(); for (int i = 0, n = meshData.Length; i < n;) { int boneCount = (int)meshData[i++]; BoneWeight boneWeight = new BoneWeight(); for (int j = 0; j < boneCount; j++) { int globalBoneIndex = (int)meshData[i]; SpineBone bone = FindBoneByIndex(globalBoneIndex); float x = meshData[i + 1]; float y = meshData[i + 2]; float weight = meshData[i + 3]; int boneIndex; if (bones.Contains(bone.transform)) { boneIndex = bones.IndexOf(bone.transform); } else { boneIndex = bones.Count; bones.Add(bone.transform); } if (j == 0) { Vector3 position = bone.transform.TransformPoint(x, y, attachment.Slot.DrawOrder); vertices.Add(position); } switch (j) { case 0: { boneWeight.boneIndex0 = boneIndex; boneWeight.weight0 = weight; break; } case 1: { boneWeight.boneIndex1 = boneIndex; boneWeight.weight1 = weight; break; } case 2: { boneWeight.boneIndex2 = boneIndex; boneWeight.weight2 = weight; break; } case 3: { boneWeight.boneIndex3 = boneIndex; boneWeight.weight3 = weight; break; } } i += 4; } weights.Add(boneWeight); } Mesh mesh = new Mesh(); mesh.vertices = vertices.ToArray(); mesh.uv = uvs; mesh.triangles = triangles; mesh.boneWeights = weights.ToArray(); Matrix4x4[] bindPoses = new Matrix4x4[bones.Count]; for (int i = 0; i < bones.Count; i++) { bindPoses[i] = bones[i].worldToLocalMatrix; } mesh.bindposes = bindPoses; SpineMesh spineMesh = new SpineMesh(); spineMesh.Mesh = mesh; spineMesh.Color = ToColor(GetString(root, "color", "ffffffff")); spineMesh.Texture = GetTexture(GetString(root, "name", attachmentName)); spineMesh.Bones = bones.ToArray(); attachment.Mesh = spineMesh; break; } } }