예제 #1
0
    static int GetAttachment(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 3 && TypeChecker.CheckTypes <int, string>(L, 2))
            {
                Spine.Skeleton   obj  = (Spine.Skeleton)ToLua.CheckObject <Spine.Skeleton>(L, 1);
                int              arg0 = (int)LuaDLL.lua_tonumber(L, 2);
                string           arg1 = ToLua.ToString(L, 3);
                Spine.Attachment o    = obj.GetAttachment(arg0, arg1);
                ToLua.PushObject(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <string, string>(L, 2))
            {
                Spine.Skeleton   obj  = (Spine.Skeleton)ToLua.CheckObject <Spine.Skeleton>(L, 1);
                string           arg0 = ToLua.ToString(L, 2);
                string           arg1 = ToLua.ToString(L, 3);
                Spine.Attachment o    = obj.GetAttachment(arg0, arg1);
                ToLua.PushObject(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: Spine.Skeleton.GetAttachment"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
예제 #2
0
 public void StartReplace()
 {
     Spine.Slot       baseWQ             = skeleton.FindSlot(slot);
     Spine.Attachment originalAttachment = baseWQ.Attachment;
     Spine.Attachment newAttachment      = originalAttachment.GetRemappedClone
                                               (Img, originalAttachment.GetMaterial());
     baseWQ.Bone.ScaleY   = -thisSlotBaseData.ScaleY;
     baseWQ.Bone.Rotation = thisSlotBaseData.Rotation - 45;
     baseWQ.Bone.SetLocalPosition(new Vector2(0, -thisSlotBaseData.Y * 2));
     baseWQ.Attachment = newAttachment;
 }
예제 #3
0
		public void SetToSetupPose () {
			r = data.r;
			g = data.g;
			b = data.b;
			a = data.a;
			if (data.attachmentName == null)
				Attachment = null;
			else {
				attachment = null;
				Attachment = bone.skeleton.GetAttachment(data.index, data.attachmentName);
			}
		}
예제 #4
0
    private void ShowCorrectImage()
    {
        string bodyName   = GetBodyName(_bodyStatus.displayLevel);
        string cannonName = GetBarralName(_cannonStatus.displayLevel);

        foreach (Spine.Slot slot in _skeletonAnim.skeleton.slots)
        {
            bool show = false;

            if (slot.Data.Name == cannonName)
            {
                show = true;
//				slot.Bone.FlipX = _cannonStatus.displayFlipX;
            }

            if (slot.Data.Name == bodyName)
            {
                show            = true;
                slot.Bone.FlipX = _bodyStatus.displayFlipX ^ _cannonStatus.displayFlipX;
            }

            if (show)
            {
                if (_attachmentMap.ContainsKey(slot.Data.Name))
                {
                    Spine.Attachment attachment = _attachmentMap[slot.Data.Name];
                    slot.Data.AttachmentName = attachment.Name;
                    slot.Attachment          = attachment;
                }
            }
            else
            {
                if (slot.Attachment != null)
                {
                    _attachmentMap[slot.Data.Name] = slot.Attachment;
                    slot.Data.AttachmentName       = null;
                    slot.Attachment = null;
                }
            }
        }

        if (_cannonStatus.displayFlipX)
        {
            _spine.transform.localScale = new Vector3(-_spineScale, _spineScale, _spineScale);
        }
        else
        {
            _spine.transform.localScale = new Vector3(_spineScale, _spineScale, _spineScale);
        }
    }
예제 #5
0
 public void StartReplace2()
 {
     Spine.Slot       baseWQ             = skeleton.FindSlot(slot);
     Spine.Attachment originalAttachment = baseWQ.Attachment;
     Spine.Attachment newAttachment      = originalAttachment.GetRemappedClone
                                               (Img, originalAttachment.GetMaterial());
     baseWQ.Attachment = newAttachment;
     //
     Spine.Slot       otherWQ             = skeleton.FindSlot("R_" + slot);
     Spine.Attachment originalAttachment2 = otherWQ.Attachment;
     Spine.Attachment newAttachment2      = originalAttachment2.GetRemappedClone
                                                (Img, originalAttachment2.GetMaterial());
     otherWQ.Attachment = newAttachment2;
 }
예제 #6
0
 static int AddAttachment(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 4);
         Spine.Skin       obj  = (Spine.Skin)ToLua.CheckObject <Spine.Skin>(L, 1);
         int              arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
         string           arg1 = ToLua.CheckString(L, 3);
         Spine.Attachment arg2 = (Spine.Attachment)ToLua.CheckObject <Spine.Attachment>(L, 4);
         obj.AddAttachment(arg0, arg1, arg2);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #7
0
 static int GetAttachment(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 3);
         Spine.Skin       obj  = (Spine.Skin)ToLua.CheckObject <Spine.Skin>(L, 1);
         int              arg0 = (int)LuaDLL.luaL_checknumber(L, 2);
         string           arg1 = ToLua.CheckString(L, 3);
         Spine.Attachment o    = obj.GetAttachment(arg0, arg1);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
예제 #8
0
        public void Draw(Skeleton skeleton)
        {
            var   drawOrder = skeleton.DrawOrder;
            var   drawOrderItems = skeleton.DrawOrder.Items;
            float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;
            Color color = new Color();

            if (VertexEffect != null)
            {
                VertexEffect.Begin(skeleton);
            }

            for (int i = 0, n = drawOrder.Count; i < n; i++)
            {
                Slot       slot              = drawOrderItems[i];
                Attachment attachment        = slot.Attachment;
                float      attachmentZOffset = z + zSpacing * i;

                float   attachmentColorR, attachmentColorG, attachmentColorB, attachmentColorA;
                object  textureObject = null;
                int     verticesCount = 0;
                float[] vertices      = this.vertices;
                int     indicesCount  = 0;
                int[]   indices       = null;
                float[] uvs           = null;

                if (attachment is RegionAttachment)
                {
                    RegionAttachment regionAttachment = (RegionAttachment)attachment;
                    attachmentColorR = regionAttachment.R; attachmentColorG = regionAttachment.G; attachmentColorB = regionAttachment.B; attachmentColorA = regionAttachment.A;
                    AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
                    textureObject = region.page.rendererObject;
                    verticesCount = 4;
                    regionAttachment.ComputeWorldVertices(slot.Bone, vertices, 0, 2);
                    indicesCount = 6;
                    indices      = quadTriangles;
                    uvs          = regionAttachment.UVs;
                }
                else if (attachment is MeshAttachment)
                {
                    MeshAttachment mesh = (MeshAttachment)attachment;
                    attachmentColorR = mesh.R; attachmentColorG = mesh.G; attachmentColorB = mesh.B; attachmentColorA = mesh.A;
                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    textureObject = region.page.rendererObject;
                    int vertexCount = mesh.WorldVerticesLength;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    verticesCount = vertexCount >> 1;
                    mesh.ComputeWorldVertices(slot, vertices);
                    indicesCount = mesh.Triangles.Length;
                    indices      = mesh.Triangles;
                    uvs          = mesh.UVs;
                }
                else if (attachment is ClippingAttachment)
                {
                    ClippingAttachment clip = (ClippingAttachment)attachment;
                    clipper.ClipStart(slot, clip);
                    continue;
                }
                else
                {
                    continue;
                }

                // set blend state
                BlendState blend = slot.Data.BlendMode == BlendMode.Additive ? BlendState.Additive : defaultBlendState;
                if (device.BlendState != blend)
                {
                    //End();
                    //device.BlendState = blend;
                }

                // calculate color
                float a = skeletonA * slot.A * attachmentColorA;
                if (premultipliedAlpha)
                {
                    color = new Color(
                        skeletonR * slot.R * attachmentColorR * a,
                        skeletonG * slot.G * attachmentColorG * a,
                        skeletonB * slot.B * attachmentColorB * a, a);
                }
                else
                {
                    color = new Color(
                        skeletonR * slot.R * attachmentColorR,
                        skeletonG * slot.G * attachmentColorG,
                        skeletonB * slot.B * attachmentColorB, a);
                }

                Color darkColor = new Color();
                if (slot.HasSecondColor)
                {
                    if (premultipliedAlpha)
                    {
                        darkColor = new Color(slot.R2 * a, slot.G2 * a, slot.B2 * a);
                    }
                    else
                    {
                        darkColor = new Color(slot.R2 * a, slot.G2 * a, slot.B2 * a);
                    }
                }
                darkColor.A = premultipliedAlpha ? (byte)255 : (byte)0;

                // clip
                if (clipper.IsClipping)
                {
                    clipper.ClipTriangles(vertices, verticesCount << 1, indices, indicesCount, uvs);
                    vertices      = clipper.ClippedVertices.Items;
                    verticesCount = clipper.ClippedVertices.Count >> 1;
                    indices       = clipper.ClippedTriangles.Items;
                    indicesCount  = clipper.ClippedTriangles.Count;
                    uvs           = clipper.ClippedUVs.Items;
                }

                if (verticesCount == 0 || indicesCount == 0)
                {
                    continue;
                }

                // submit to batch
                MeshItem item = batcher.NextItem(verticesCount, indicesCount);
                if (textureObject is Texture2D)
                {
                    item.texture = (Texture2D)textureObject;
                }
                else
                {
                    item.textureLayers = (Texture2D[])textureObject;
                    item.texture       = item.textureLayers[0];
                }
                for (int ii = 0, nn = indicesCount; ii < nn; ii++)
                {
                    item.triangles[ii] = indices[ii];
                }
                VertexPositionColorTextureColor[] itemVertices = item.vertices;
                for (int ii = 0, v = 0, nn = verticesCount << 1; v < nn; ii++, v += 2)
                {
                    itemVertices[ii].Color               = color;
                    itemVertices[ii].Color2              = darkColor;
                    itemVertices[ii].Position.X          = vertices[v];
                    itemVertices[ii].Position.Y          = vertices[v + 1];
                    itemVertices[ii].Position.Z          = attachmentZOffset;
                    itemVertices[ii].TextureCoordinate.X = uvs[v];
                    itemVertices[ii].TextureCoordinate.Y = uvs[v + 1];
                    if (VertexEffect != null)
                    {
                        VertexEffect.Transform(ref itemVertices[ii]);
                    }
                }

                clipper.ClipEnd(slot);
            }
            clipper.ClipEnd();
            if (VertexEffect != null)
            {
                VertexEffect.End();
            }
        }
예제 #9
0
        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);
        }
예제 #10
0
        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"))
            {
                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"))
            {
                var 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"))
            {
                var 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 bool ApplyFFD (Attachment sourceAttachment) {
			return this == sourceAttachment || (inheritFFD && parentMesh == sourceAttachment);
		}
예제 #12
0
		public void AddAttachment (int slotIndex, String name, Attachment attachment) {
			if (attachment == null) throw new ArgumentNullException("attachment", "attachment cannot be null.");
			attachments[new AttachmentKeyTuple(slotIndex, name)] = attachment;
		}
예제 #13
0
 public void AddAttachment(int slotIndex, String name, Attachment attachment)
 {
     if (attachment == null) throw new ArgumentNullException("attachment cannot be null.");
     attachments.Add(new KeyValuePair<int, String>(slotIndex, name), attachment);
 }
        public void Draw(Skeleton skeleton)
        {
            float[]     vertices = this.vertices;
            List <Slot> drawOrder = skeleton.DrawOrder;
            float       x = skeleton.X, y = skeleton.Y;
            float       skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;

            for (int i = 0, n = drawOrder.Count; i < n; i++)
            {
                Slot       slot       = drawOrder[i];
                Attachment attachment = slot.Attachment;
                if (attachment is RegionAttachment)
                {
                    RegionAttachment regionAttachment = (RegionAttachment)attachment;
                    BlendState       blend            = slot.Data.AdditiveBlending ? BlendState.Additive : defaultBlendState;
                    if (device.BlendState != blend)
                    {
                        End();
                        device.BlendState = blend;
                    }

                    MeshItem item = batcher.NextItem(4, 6);
                    item.triangles = quadTriangles;
                    VertexPositionColorTexture[] itemVertices = item.vertices;

                    AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
                    item.texture = (Texture2D)region.page.rendererObject;

                    Color color;
                    float a = skeletonA * slot.A * regionAttachment.A;
                    if (premultipliedAlpha)
                    {
                        color = new Color(
                            skeletonR * slot.R * regionAttachment.R * a,
                            skeletonG * slot.G * regionAttachment.G * a,
                            skeletonB * slot.B * regionAttachment.B * a, a);
                    }
                    else
                    {
                        color = new Color(
                            skeletonR * slot.R * regionAttachment.R,
                            skeletonG * slot.G * regionAttachment.G,
                            skeletonB * slot.B * regionAttachment.B, a);
                    }
                    itemVertices[TL].Color = color;
                    itemVertices[BL].Color = color;
                    itemVertices[BR].Color = color;
                    itemVertices[TR].Color = color;

                    regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices);
                    itemVertices[TL].Position.X = vertices[RegionAttachment.X1];
                    itemVertices[TL].Position.Y = vertices[RegionAttachment.Y1];
                    itemVertices[TL].Position.Z = 0;
                    itemVertices[BL].Position.X = vertices[RegionAttachment.X2];
                    itemVertices[BL].Position.Y = vertices[RegionAttachment.Y2];
                    itemVertices[BL].Position.Z = 0;
                    itemVertices[BR].Position.X = vertices[RegionAttachment.X3];
                    itemVertices[BR].Position.Y = vertices[RegionAttachment.Y3];
                    itemVertices[BR].Position.Z = 0;
                    itemVertices[TR].Position.X = vertices[RegionAttachment.X4];
                    itemVertices[TR].Position.Y = vertices[RegionAttachment.Y4];
                    itemVertices[TR].Position.Z = 0;

                    float[] uvs = regionAttachment.UVs;
                    itemVertices[TL].TextureCoordinate.X = uvs[RegionAttachment.X1];
                    itemVertices[TL].TextureCoordinate.Y = uvs[RegionAttachment.Y1];
                    itemVertices[BL].TextureCoordinate.X = uvs[RegionAttachment.X2];
                    itemVertices[BL].TextureCoordinate.Y = uvs[RegionAttachment.Y2];
                    itemVertices[BR].TextureCoordinate.X = uvs[RegionAttachment.X3];
                    itemVertices[BR].TextureCoordinate.Y = uvs[RegionAttachment.Y3];
                    itemVertices[TR].TextureCoordinate.X = uvs[RegionAttachment.X4];
                    itemVertices[TR].TextureCoordinate.Y = uvs[RegionAttachment.Y4];
                }
                else if (attachment is MeshAttachment)
                {
                    MeshAttachment mesh        = (MeshAttachment)attachment;
                    int            vertexCount = mesh.Vertices.Length;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    mesh.ComputeWorldVertices(x, y, slot, vertices);

                    int[]    triangles = mesh.Triangles;
                    MeshItem item      = batcher.NextItem(vertexCount, triangles.Length);
                    item.triangles = triangles;

                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    item.texture = (Texture2D)region.page.rendererObject;

                    Color color;
                    float a = skeletonA * slot.A * mesh.A;
                    if (premultipliedAlpha)
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R * a,
                            skeletonG * slot.G * mesh.G * a,
                            skeletonB * slot.B * mesh.B * a, a);
                    }
                    else
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R,
                            skeletonG * slot.G * mesh.G,
                            skeletonB * slot.B * mesh.B, a);
                    }

                    float[] uvs = mesh.UVs;
                    VertexPositionColorTexture[] itemVertices = item.vertices;
                    for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2)
                    {
                        itemVertices[ii].Color               = color;
                        itemVertices[ii].Position.X          = vertices[v];
                        itemVertices[ii].Position.Y          = vertices[v + 1];
                        itemVertices[ii].Position.Z          = 0;
                        itemVertices[ii].TextureCoordinate.X = uvs[v];
                        itemVertices[ii].TextureCoordinate.Y = uvs[v + 1];
                    }
                }
                else if (attachment is SkinnedMeshAttachment)
                {
                    SkinnedMeshAttachment mesh = (SkinnedMeshAttachment)attachment;
                    int vertexCount            = mesh.UVs.Length;
                    if (vertices.Length < vertexCount)
                    {
                        vertices = new float[vertexCount];
                    }
                    mesh.ComputeWorldVertices(x, y, slot, vertices);

                    int[]    triangles = mesh.Triangles;
                    MeshItem item      = batcher.NextItem(vertexCount, triangles.Length);
                    item.triangles = triangles;

                    AtlasRegion region = (AtlasRegion)mesh.RendererObject;
                    item.texture = (Texture2D)region.page.rendererObject;

                    Color color;
                    float a = skeletonA * slot.A * mesh.A;
                    if (premultipliedAlpha)
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R * a,
                            skeletonG * slot.G * mesh.G * a,
                            skeletonB * slot.B * mesh.B * a, a);
                    }
                    else
                    {
                        color = new Color(
                            skeletonR * slot.R * mesh.R,
                            skeletonG * slot.G * mesh.G,
                            skeletonB * slot.B * mesh.B, a);
                    }

                    float[] uvs = mesh.UVs;
                    VertexPositionColorTexture[] itemVertices = item.vertices;
                    for (int ii = 0, v = 0; v < vertexCount; ii++, v += 2)
                    {
                        itemVertices[ii].Color               = color;
                        itemVertices[ii].Position.X          = vertices[v];
                        itemVertices[ii].Position.Y          = vertices[v + 1];
                        itemVertices[ii].Position.Z          = 0;
                        itemVertices[ii].TextureCoordinate.X = uvs[v];
                        itemVertices[ii].TextureCoordinate.Y = uvs[v + 1];
                    }
                }
            }
        }
예제 #15
0
 public static bool IsRenderable(this Attachment a)
 {
     return(a is RegionAttachment || a is MeshAttachment);
 }
			public static Texture2D GetAttachmentIcon (Attachment attachment) {
				if (attachment is RegionAttachment)
					return Icons.image;
				// Analysis disable once CanBeReplacedWithTryCastAndCheckForNull
				else if (attachment is MeshAttachment)
					return ((MeshAttachment)attachment).IsWeighted() ? Icons.weights : Icons.mesh;
				else if (attachment is BoundingBoxAttachment)
					return Icons.boundingBox;
				else if (attachment is PathAttachment)
					return Icons.path;
				else
					return Icons.warning;
			}
예제 #17
0
		private void SortPathConstraintAttachment (Attachment attachment, Bone slotBone) {
			var pathAttachment = attachment as PathAttachment;
			if (pathAttachment == null) return;
			int[] pathBones = pathAttachment.bones;
			if (pathBones == null)
				SortBone(slotBone);
			else {
				var bones = this.bones;
				for (int i = 0, n = pathBones.Length; i < n; i++)
					SortBone(bones.Items[pathBones[i]]);
			}
		}
예제 #18
0
		static Material ExtractMaterial (Attachment attachment) {
			if (attachment == null || attachment is BoundingBoxAttachment)
				return null;

			if (attachment is RegionAttachment) {
				var att = (RegionAttachment)attachment;
				return (Material)((AtlasRegion)att.RendererObject).page.rendererObject;
			} else if (attachment is MeshAttachment) {
				var att = (MeshAttachment)attachment;
				return (Material)((AtlasRegion)att.RendererObject).page.rendererObject;
			} else {
				return null;
			}
		}
예제 #19
0
 public bool ApplyFFD(Attachment sourceAttachment)
 {
     return(this == sourceAttachment || (inheritFFD && parentMesh == sourceAttachment));
 }
예제 #20
0
			public LinkedMesh (Attachment mesh, String skin, int slotIndex, String parent) {
				this.mesh = mesh;
				this.skin = skin;
				this.slotIndex = slotIndex;
				this.parent = parent;
			}
예제 #21
0
        private void ReadAnimation(String name, Dictionary <String, Object> map, SkeletonData skeletonData)
        {
            var   timelines = new List <Timeline>();
            float duration  = 0;
            float scale     = Scale;

            if (map.ContainsKey("slots"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["slots"])
                {
                    String slotName    = entry.Key;
                    int    slotIndex   = skeletonData.FindSlotIndex(slotName);
                    var    timelineMap = (Dictionary <String, Object>)entry.Value;

                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "color")
                        {
                            var timeline = new ColorTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float  time = (float)valueMap["time"];
                                String c    = (String)valueMap["color"];
                                timeline.SetFrame(frameIndex, time, ToColor(c, 0), ToColor(c, 1), ToColor(c, 2), ToColor(c, 3));
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 5 - 5]);
                        }
                        else if (timelineName == "attachment")
                        {
                            var timeline = new AttachmentTimeline(values.Count);
                            timeline.slotIndex = slotIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex++, time, (String)valueMap["name"]);
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a slot: " + timelineName + " (" + slotName + ")");
                        }
                    }
                }
            }

            if (map.ContainsKey("bones"))
            {
                foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)map["bones"])
                {
                    String boneName  = entry.Key;
                    int    boneIndex = skeletonData.FindBoneIndex(boneName);
                    if (boneIndex == -1)
                    {
                        throw new Exception("Bone not found: " + boneName);
                    }

                    var timelineMap = (Dictionary <String, Object>)entry.Value;
                    foreach (KeyValuePair <String, Object> timelineEntry in timelineMap)
                    {
                        var values       = (List <Object>)timelineEntry.Value;
                        var timelineName = (String)timelineEntry.Key;
                        if (timelineName == "rotate")
                        {
                            var timeline = new RotateTimeline(values.Count);
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex, time, (float)valueMap["angle"]);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 2 - 2]);
                        }
                        else if (timelineName == "translate" || timelineName == "scale")
                        {
                            TranslateTimeline timeline;
                            float             timelineScale = 1;
                            if (timelineName == "scale")
                            {
                                timeline = new ScaleTimeline(values.Count);
                            }
                            else
                            {
                                timeline      = new TranslateTimeline(values.Count);
                                timelineScale = scale;
                            }
                            timeline.boneIndex = boneIndex;

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                float x    = valueMap.ContainsKey("x") ? (float)valueMap["x"] : 0;
                                float y    = valueMap.ContainsKey("y") ? (float)valueMap["y"] : 0;
                                timeline.SetFrame(frameIndex, time, (float)x * timelineScale, (float)y * timelineScale);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 3 - 3]);
                        }
                        else if (timelineName == "flipX" || timelineName == "flipY")
                        {
                            bool x        = timelineName == "flipX";
                            var  timeline = x ? new FlipXTimeline(values.Count) : new FlipYTimeline(values.Count);
                            timeline.boneIndex = boneIndex;

                            String field      = x ? "x" : "y";
                            int    frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float time = (float)valueMap["time"];
                                timeline.SetFrame(frameIndex, time, valueMap.ContainsKey(field) ? (bool)valueMap[field] : false);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 2 - 2]);
                        }
                        else
                        {
                            throw new Exception("Invalid timeline type for a bone: " + timelineName + " (" + boneName + ")");
                        }
                    }
                }
            }

            if (map.ContainsKey("ik"))
            {
                foreach (KeyValuePair <String, Object> ikMap in (Dictionary <String, Object>)map["ik"])
                {
                    IkConstraintData ikConstraint = skeletonData.FindIkConstraint(ikMap.Key);
                    var values   = (List <Object>)ikMap.Value;
                    var timeline = new IkConstraintTimeline(values.Count);
                    timeline.ikConstraintIndex = skeletonData.ikConstraints.IndexOf(ikConstraint);
                    int frameIndex = 0;
                    foreach (Dictionary <String, Object> valueMap in values)
                    {
                        float time         = (float)valueMap["time"];
                        float mix          = valueMap.ContainsKey("mix") ? (float)valueMap["mix"] : 1;
                        bool  bendPositive = valueMap.ContainsKey("bendPositive") ? (bool)valueMap["bendPositive"] : true;
                        timeline.SetFrame(frameIndex, time, mix, bendPositive ? 1 : -1);
                        ReadCurve(timeline, frameIndex, valueMap);
                        frameIndex++;
                    }
                    timelines.Add(timeline);
                    duration = Math.Max(duration, timeline.frames[timeline.FrameCount * 3 - 3]);
                }
            }

            if (map.ContainsKey("ffd"))
            {
                foreach (KeyValuePair <String, Object> ffdMap in (Dictionary <String, Object>)map["ffd"])
                {
                    Skin skin = skeletonData.FindSkin(ffdMap.Key);
                    foreach (KeyValuePair <String, Object> slotMap in (Dictionary <String, Object>)ffdMap.Value)
                    {
                        int slotIndex = skeletonData.FindSlotIndex(slotMap.Key);
                        foreach (KeyValuePair <String, Object> meshMap in (Dictionary <String, Object>)slotMap.Value)
                        {
                            var        values     = (List <Object>)meshMap.Value;
                            var        timeline   = new FFDTimeline(values.Count);
                            Attachment attachment = skin.GetAttachment(slotIndex, meshMap.Key);
                            if (attachment == null)
                            {
                                throw new Exception("FFD attachment not found: " + meshMap.Key);
                            }
                            timeline.slotIndex  = slotIndex;
                            timeline.attachment = attachment;

                            int vertexCount;
                            if (attachment is MeshAttachment)
                            {
                                vertexCount = ((MeshAttachment)attachment).vertices.Length;
                            }
                            else
                            {
                                vertexCount = ((SkinnedMeshAttachment)attachment).Weights.Length / 3 * 2;
                            }

                            int frameIndex = 0;
                            foreach (Dictionary <String, Object> valueMap in values)
                            {
                                float[] vertices;
                                if (!valueMap.ContainsKey("vertices"))
                                {
                                    if (attachment is MeshAttachment)
                                    {
                                        vertices = ((MeshAttachment)attachment).vertices;
                                    }
                                    else
                                    {
                                        vertices = new float[vertexCount];
                                    }
                                }
                                else
                                {
                                    var verticesValue = (List <Object>)valueMap["vertices"];
                                    vertices = new float[vertexCount];
                                    int start = GetInt(valueMap, "offset", 0);
                                    if (scale == 1)
                                    {
                                        for (int i = 0, n = verticesValue.Count; i < n; i++)
                                        {
                                            vertices[i + start] = (float)verticesValue[i];
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0, n = verticesValue.Count; i < n; i++)
                                        {
                                            vertices[i + start] = (float)verticesValue[i] * scale;
                                        }
                                    }
                                    if (attachment is MeshAttachment)
                                    {
                                        float[] meshVertices = ((MeshAttachment)attachment).vertices;
                                        for (int i = 0; i < vertexCount; i++)
                                        {
                                            vertices[i] += meshVertices[i];
                                        }
                                    }
                                }

                                timeline.SetFrame(frameIndex, (float)valueMap["time"], vertices);
                                ReadCurve(timeline, frameIndex, valueMap);
                                frameIndex++;
                            }
                            timelines.Add(timeline);
                            duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
                        }
                    }
                }
            }

            if (map.ContainsKey("drawOrder") || map.ContainsKey("draworder"))
            {
                var values     = (List <Object>)map[map.ContainsKey("drawOrder") ? "drawOrder" : "draworder"];
                var timeline   = new DrawOrderTimeline(values.Count);
                int slotCount  = skeletonData.slots.Count;
                int frameIndex = 0;
                foreach (Dictionary <String, Object> drawOrderMap in values)
                {
                    int[] drawOrder = null;
                    if (drawOrderMap.ContainsKey("offsets"))
                    {
                        drawOrder = new int[slotCount];
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            drawOrder[i] = -1;
                        }
                        var   offsets = (List <Object>)drawOrderMap["offsets"];
                        int[] unchanged = new int[slotCount - offsets.Count];
                        int   originalIndex = 0, unchangedIndex = 0;
                        foreach (Dictionary <String, Object> offsetMap in offsets)
                        {
                            int slotIndex = skeletonData.FindSlotIndex((String)offsetMap["slot"]);
                            if (slotIndex == -1)
                            {
                                throw new Exception("Slot not found: " + offsetMap["slot"]);
                            }
                            // Collect unchanged items.
                            while (originalIndex != slotIndex)
                            {
                                unchanged[unchangedIndex++] = originalIndex++;
                            }
                            // Set changed items.
                            int index = originalIndex + (int)(float)offsetMap["offset"];
                            drawOrder[index] = originalIndex++;
                        }
                        // Collect remaining unchanged items.
                        while (originalIndex < slotCount)
                        {
                            unchanged[unchangedIndex++] = originalIndex++;
                        }
                        // Fill in unchanged items.
                        for (int i = slotCount - 1; i >= 0; i--)
                        {
                            if (drawOrder[i] == -1)
                            {
                                drawOrder[i] = unchanged[--unchangedIndex];
                            }
                        }
                    }
                    timeline.SetFrame(frameIndex++, (float)drawOrderMap["time"], drawOrder);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            if (map.ContainsKey("events"))
            {
                var eventsMap  = (List <Object>)map["events"];
                var timeline   = new EventTimeline(eventsMap.Count);
                int frameIndex = 0;
                foreach (Dictionary <String, Object> eventMap in eventsMap)
                {
                    EventData eventData = skeletonData.FindEvent((String)eventMap["name"]);
                    if (eventData == null)
                    {
                        throw new Exception("Event not found: " + eventMap["name"]);
                    }
                    var e = new Event(eventData);
                    e.Int    = GetInt(eventMap, "int", eventData.Int);
                    e.Float  = GetFloat(eventMap, "float", eventData.Float);
                    e.String = GetString(eventMap, "string", eventData.String);
                    timeline.SetFrame(frameIndex++, (float)eventMap["time"], e);
                }
                timelines.Add(timeline);
                duration = Math.Max(duration, timeline.frames[timeline.FrameCount - 1]);
            }

            timelines.TrimExcess();
            skeletonData.animations.Add(new Animation(name, timelines, duration));
        }
예제 #22
0
		internal void SetToSetupPose (int slotIndex) {
			r = data.r;
			g = data.g;
			b = data.b;
			a = data.a;
			if (data.attachmentName == null)
				Attachment = null;
			else {
				attachment = null;
				Attachment = bone.skeleton.GetAttachment(slotIndex, data.attachmentName);
			}
		}
예제 #23
0
        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.events.TrimExcess();
            skeletonData.animations.TrimExcess();
            skeletonData.ikConstraints.TrimExcess();
            return(skeletonData);
        }
예제 #24
0
        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);
        }
예제 #25
0
 public static bool IsRenderable(this Attachment a)
 {
     return(a is IHasRendererObject);
 }
예제 #26
0
		private void SortPathConstraintAttachment (Attachment attachment, Bone slotBone) {
			if (!(attachment is PathAttachment)) return;
			int[] pathBones = ((PathAttachment)attachment).bones;
			if (pathBones == null)
				SortBone(slotBone);
			else {
				var bones = this.bones;
				for (int i = 0, n = pathBones.Length; i < n;) {
					int nn = pathBones[i++];
					nn += i;
					while (i < nn)
						SortBone(bones.Items[pathBones[i++]]);
				}
			}
		}
예제 #27
0
    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;
        }
    }