Example #1
0
        public static Attachment GetClone(this Attachment o, bool cloneMeshesAsLinked)
        {
            RegionAttachment regionAttachment = o as RegionAttachment;

            if (regionAttachment != null)
            {
                return(regionAttachment.GetClone());
            }
            MeshAttachment meshAttachment = o as MeshAttachment;

            if (meshAttachment != null)
            {
                return((!cloneMeshesAsLinked) ? meshAttachment.GetClone() : meshAttachment.GetLinkedClone());
            }
            BoundingBoxAttachment boundingBoxAttachment = o as BoundingBoxAttachment;

            if (boundingBoxAttachment != null)
            {
                return(boundingBoxAttachment.GetClone());
            }
            PathAttachment pathAttachment = o as PathAttachment;

            if (pathAttachment != null)
            {
                return(pathAttachment.GetClone());
            }
            PointAttachment pointAttachment = o as PointAttachment;

            if (pointAttachment != null)
            {
                return(pointAttachment.GetClone());
            }
            return((o as ClippingAttachment)?.GetClone());
        }
Example #2
0
        public static void DrawPath(Slot s, PathAttachment p, Transform t, bool includeName)
        {
            int worldVerticesLength = p.WorldVerticesLength;

            if (pathVertexBuffer == null || pathVertexBuffer.Length < worldVerticesLength)
            {
                pathVertexBuffer = new float[worldVerticesLength];
            }

            float[] pv = pathVertexBuffer;
            p.ComputeWorldVertices(s, pv);

            var ocolor = Handles.color;

            Handles.color = SpineHandles.PathColor;

            Matrix4x4 m = t.localToWorldMatrix;
            const int step = 6;
            int       n = worldVerticesLength - step;
            Vector3   p0, p1, p2, p3;

            for (int i = 2; i < n; i += step)
            {
                p0 = m.MultiplyPoint(new Vector3(pv[i], pv[i + 1]));
                p1 = m.MultiplyPoint(new Vector3(pv[i + 2], pv[i + 3]));
                p2 = m.MultiplyPoint(new Vector3(pv[i + 4], pv[i + 5]));
                p3 = m.MultiplyPoint(new Vector3(pv[i + 6], pv[i + 7]));
                DrawCubicBezier(p0, p1, p2, p3);
            }

            n += step;
            if (p.Closed)
            {
                p0 = m.MultiplyPoint(new Vector3(pv[n - 4], pv[n - 3]));
                p1 = m.MultiplyPoint(new Vector3(pv[n - 2], pv[n - 1]));
                p2 = m.MultiplyPoint(new Vector3(pv[0], pv[1]));
                p3 = m.MultiplyPoint(new Vector3(pv[2], pv[3]));
                DrawCubicBezier(p0, p1, p2, p3);
            }

            const float endCapSize = 0.05f;
            Vector3     firstPoint = m.MultiplyPoint(new Vector3(pv[2], pv[3]));

            SpineHandles.DrawDot(firstPoint, endCapSize);

            //if (!p.Closed) SpineHandles.DrawDot(m.MultiplyPoint(new Vector3(pv[n - 4], pv[n - 3])), endCapSize);
            if (includeName)
            {
                Handles.Label(firstPoint + new Vector3(0, 0.1f), p.Name, PathNameStyle);
            }

            Handles.color = ocolor;
        }
        public static PathAttachment GetClone(this PathAttachment o)
        {
            PathAttachment dest = new PathAttachment(o.Name)
            {
                lengths       = o.lengths.Clone() as float[],
                closed        = o.closed,
                constantSpeed = o.constantSpeed
            };

            CloneVertexAttachment(o, dest);
            return(dest);
        }
Example #4
0
        public static PathAttachment GetClone(this PathAttachment o)
        {
            PathAttachment pathAttachment = new PathAttachment(o.Name);

            pathAttachment.lengths       = (o.lengths.Clone() as float[]);
            pathAttachment.closed        = o.closed;
            pathAttachment.constantSpeed = o.constantSpeed;
            PathAttachment pathAttachment2 = pathAttachment;

            CloneVertexAttachment(o, pathAttachment2);
            return(pathAttachment2);
        }
        public static Attachment GetClone(this Attachment o, bool cloneMeshesAsLinked)
        {
            RegionAttachment attachment = o as RegionAttachment;

            if (attachment != null)
            {
                return(attachment.GetClone());
            }
            MeshAttachment attachment2 = o as MeshAttachment;

            if (attachment2 != null)
            {
                return(!cloneMeshesAsLinked ? attachment2.GetClone() : attachment2.GetLinkedClone(true));
            }
            BoundingBoxAttachment attachment3 = o as BoundingBoxAttachment;

            if (attachment3 != null)
            {
                return(attachment3.GetClone());
            }
            PathAttachment attachment4 = o as PathAttachment;

            if (attachment4 != null)
            {
                return(attachment4.GetClone());
            }
            PointAttachment attachment5 = o as PointAttachment;

            if (attachment5 != null)
            {
                return(attachment5.GetClone());
            }
            ClippingAttachment attachment6 = o as ClippingAttachment;

            if (attachment6 != null)
            {
                return(attachment6.GetClone());
            }
            return(null);
        }
Example #6
0
        /// <summary>
        /// 主要逻辑copy SkeletonJson.ReadAttachment
        /// </summary>
        /// <param name="sdEX"></param>
        /// <param name="atlasArray"></param>
        /// <returns></returns>
        public Skin GenSkinBySkniDataAsset(SkinDataEx sdEX, Atlas[] atlasArray)
        {
#if SPINE_TK2D
            throw new NotImplementedException("GenSkinBySkniDataAsset Not Implemented");
#endif
            float      scale            = this.skeletonDataAsset.scale;
            var        attachmentLoader = new AtlasAttachmentLoader(atlasArray);
            var        skin             = new Skin(sdEX.SkinName);
            var        linkedMeshes     = new List <SkeletonJson.LinkedMesh>();
            Attachment attachment       = null;
            foreach (var slot in sdEX.AllSlots)
            {
                var slotIndex = skeleton.Data.FindSlotIndex(slot.SlotName);
                foreach (var entry in slot.AllAttachments)
                {
                    switch (entry.type)
                    {
                    case AttachmentType.Region:
                    {
                        RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, entry.AttachmentName, entry.PathName);
                        if (region == null)
                        {
                            Debug.LogError("RegionAttachment New Fail:" + entry.AttachmentName + "," + entry.PathName);
                            continue;
                        }
                        region.Path = entry.PathName;
                        #region Region数据的填充
                        CheckRegionData(entry.FloatValues);
                        Single w = 32, h = 32;
                        foreach (var f in entry.FloatValues)
                        {
                            switch (f.Key)
                            {
                            case "x":
                                region.x = f.FloatValue * scale;
                                break;

                            case "y":
                                region.y = f.FloatValue * scale;
                                break;

                            case "scaleX":
                                region.scaleX = f.FloatValue;
                                break;

                            case "scaleY":
                                region.scaleY = f.FloatValue;
                                break;

                            case "rotation":
                                region.rotation = f.FloatValue;
                                break;

                            case "width":
                                w = f.FloatValue;
                                break;

                            case "height":
                                h = f.FloatValue;
                                break;
                            }
                        }
                        region.width  = w * scale;
                        region.height = h * scale;
                        if (entry.ColorValues != null && entry.ColorValues.Count > 0)
                        {
                            foreach (var c in entry.ColorValues)
                            {
                                switch (c.Key)
                                {
                                case "color":
                                    region.r = c.ColorValue.r;
                                    region.g = c.ColorValue.g;
                                    region.b = c.ColorValue.b;
                                    region.a = c.ColorValue.a;
                                    break;
                                }
                            }
                        }
                        #endregion
                        region.UpdateOffset();
                        attachment = region;
                    }
                    break;

                    case AttachmentType.Boundingbox:
                    {
                        BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, entry.AttachmentName);
                        if (box == null)
                        {
                            Debug.LogError("BoundingBoxAttachment New Fail:" + entry.AttachmentName);
                            continue;
                        }
                        #region Box数据填充
                        Int32 vertexCount = 0;
                        if (entry.IntValues != null && entry.IntValues.Count > 0)
                        {
                            foreach (var i in entry.IntValues)
                            {
                                if (i.Key.Equals("vertexCount"))
                                {
                                    vertexCount = i.IntValue;
                                    break;
                                }
                            }
                        }
                        foreach (var fs in entry.FloatArrayValues)
                        {
                            if (fs.Key.Equals("vertices"))
                            {
                                ReadVertices(fs, box, vertexCount << 1, scale);
                                break;
                            }
                        }
                        #endregion
                        attachment = box;
                    }
                    break;

                    case AttachmentType.Mesh:
                    case AttachmentType.Linkedmesh:
                    {
                        MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, entry.AttachmentName, entry.PathName);
                        if (mesh == null)
                        {
                            Debug.LogError("MeshAttachment New Fail:" + entry.AttachmentName + "," + entry.PathName);
                            continue;
                        }
                        #region Mesh数据填充
                        mesh.Path = entry.PathName;
                        if (entry.ColorValues != null && entry.ColorValues.Count > 0)
                        {
                            foreach (var c in entry.ColorValues)
                            {
                                if (c.Key.Equals("color"))
                                {
                                    mesh.r = c.ColorValue.r;
                                    mesh.g = c.ColorValue.g;
                                    mesh.b = c.ColorValue.b;
                                    mesh.a = c.ColorValue.a;
                                    break;
                                }
                            }
                        }
                        Single w = 0, h = 0;
                        foreach (var f in entry.FloatValues)
                        {
                            switch (f.Key)
                            {
                            case "width":
                                w = f.FloatValue * scale;
                                break;

                            case "height":
                                h = f.FloatValue * scale;
                                break;
                            }
                        }
                        mesh.Width  = w;
                        mesh.Height = h;

                        String  parentStr = null, skinStr = null;
                        Boolean deform = true;
                        if (entry.StringValues != null && entry.StringValues.Count > 0)
                        {
                            foreach (var ss in entry.StringValues)
                            {
                                switch (ss.Key)
                                {
                                case "parent":
                                    parentStr = ss.StringValue;
                                    break;

                                case "skin":
                                    skinStr = ss.StringValue;
                                    break;
                                }
                            }
                        }
                        if (entry.BoolValues != null && entry.BoolValues.Count > 0)
                        {
                            foreach (var b in entry.BoolValues)
                            {
                                if (b.Key.Equals("deform"))
                                {
                                    deform = b.BooleanValue;
                                    break;
                                }
                            }
                        }
                        if (parentStr != null)
                        {
                            mesh.InheritDeform = deform;
                            linkedMeshes.Add(new SkeletonJson.LinkedMesh(mesh, skinStr, slotIndex, parentStr));
                        }
                        KeyFloatArrayPair kfap_Vs = new KeyFloatArrayPair();
                        Single[]          uvs     = null;
                        foreach (var fs in entry.FloatArrayValues)
                        {
                            switch (fs.Key)
                            {
                            case "vertices":
                                kfap_Vs = fs;
                                break;

                            case "uvs":
                                uvs = fs.FloatArrayValue.ToArray();
                                break;
                            }
                        }
                        ReadVertices(kfap_Vs, mesh, uvs.Length, scale);
                        Int32[] triangles = null, edges = null;
                        if (entry.IntArrayValues != null && entry.IntArrayValues.Count > 0)
                        {
                            foreach (var i32s in entry.IntArrayValues)
                            {
                                switch (i32s.Key)
                                {
                                case "triangles":
                                    triangles = i32s.IntArrayValue.ToArray();
                                    break;

                                case "edges":
                                    edges = i32s.IntArrayValue.ToArray();
                                    break;
                                }
                            }
                        }

                        mesh.triangles = triangles;
                        mesh.regionUVs = uvs;
                        mesh.UpdateUVs();

                        Int32 hull = 0;
                        if (entry.IntValues != null && entry.IntValues.Count > 0)
                        {
                            foreach (var i in entry.IntValues)
                            {
                                if (i.Key.Equals("hull"))
                                {
                                    hull = i.IntValue * 2;
                                    break;
                                }
                            }
                        }
                        if (hull != 0)
                        {
                            mesh.HullLength = hull;
                        }
                        if (edges != null)
                        {
                            mesh.Edges = edges;
                        }
                        #endregion
                        attachment = mesh;
                    }
                    break;

                    case AttachmentType.Path:
                    {
                        PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, entry.AttachmentName);
                        if (pathAttachment == null)
                        {
                            Debug.LogError("PathAttachment New Fail:" + entry.AttachmentName);
                            continue;
                        }
                        #region Path填充数据
                        Boolean closed = false, constantSpeed = false;
                        if (entry.BoolValues != null && entry.BoolValues.Count > 0)
                        {
                            foreach (var b in entry.BoolValues)
                            {
                                switch (b.Key)
                                {
                                case "closed":
                                    closed = b.BooleanValue;
                                    break;

                                case "constantSpeed":
                                    constantSpeed = b.BooleanValue;
                                    break;
                                }
                            }
                        }
                        pathAttachment.closed        = closed;
                        pathAttachment.constantSpeed = constantSpeed;

                        Int32 vertexCount = 0;
                        if (entry.IntValues != null && entry.IntValues.Count > 0)
                        {
                            foreach (var i in entry.IntValues)
                            {
                                if (i.Key.Equals("vertexCount"))
                                {
                                    vertexCount = i.IntValue;
                                    break;
                                }
                            }
                        }
                        foreach (var fs in entry.FloatArrayValues)
                        {
                            switch (fs.Key)
                            {
                            case "vertices":
                                ReadVertices(fs, pathAttachment, vertexCount << 1, scale);
                                break;

                            case "lengths":
                                var count = fs.FloatArrayValue.Count;
                                pathAttachment.lengths = new Single[count];
                                for (var idx = 0; idx < count; idx++)
                                {
                                    pathAttachment.lengths[idx] = fs.FloatArrayValue[idx] * scale;
                                }
                                break;
                            }
                        }
                        #endregion
                        attachment = pathAttachment;
                    }
                    break;

                    case AttachmentType.Point:
                    {
                        PointAttachment point = attachmentLoader.NewPointAttachment(skin, entry.AttachmentName);
                        if (point == null)
                        {
                            Debug.LogError("PointAttachment New Fail:" + entry.AttachmentName);
                            continue;
                        }
                        #region Point填充数据
                        Single x = 0, y = 0, r = 0;
                        if (entry.FloatValues != null && entry.FloatValues.Count > 0)
                        {
                            foreach (var f in entry.FloatValues)
                            {
                                switch (f.Key)
                                {
                                case "x":
                                    x = f.FloatValue * scale;
                                    break;

                                case "y":
                                    y = f.FloatValue * scale;
                                    break;

                                case "rotation":
                                    r = f.FloatValue;
                                    break;
                                }
                            }
                        }
                        point.x        = x;
                        point.y        = y;
                        point.rotation = r;
                        #endregion
                        attachment = point;
                    }
                    break;

                    case AttachmentType.Clipping:
                    {
                        ClippingAttachment clip = attachmentLoader.NewClippingAttachment(skin, entry.AttachmentName);
                        if (clip == null)
                        {
                            Debug.LogError("ClippingAttachment New Fail:" + entry.AttachmentName);
                            continue;
                        }
                        #region Clipping填充数据
                        String end = null;
                        if (entry.StringValues != null && entry.StringValues.Count > 0)
                        {
                            foreach (var s in entry.StringValues)
                            {
                                if (s.Key.Equals("end"))
                                {
                                    end = s.StringValue;
                                    break;
                                }
                            }
                        }
                        if (end != null)
                        {
                            SlotData sd = skeleton.Data.FindSlot(end);
                            if (sd == null)
                            {
                                throw new Exception("Clipping end slot not found: " + end);
                            }
                            clip.EndSlot = sd;
                        }

                        Int32 vertexCount = 0;
                        if (entry.IntValues != null && entry.IntValues.Count > 0)
                        {
                            foreach (var i in entry.IntValues)
                            {
                                if (i.Key.Equals("vertexCount"))
                                {
                                    vertexCount = i.IntValue;
                                    break;
                                }
                            }
                        }
                        foreach (var fs in entry.FloatArrayValues)
                        {
                            if (fs.Key.Equals("vertices"))
                            {
                                ReadVertices(fs, clip, vertexCount << 1, scale);
                                break;
                            }
                        }
                        #endregion
                        attachment = clip;
                    }
                    break;

                    default:
                        break;
                    }
                    skin.SetAttachment(slot.SlotName, entry.AttachmentName, attachment, skeleton);
                }
            }
            // Linked meshes.
            for (int i = 0, n = linkedMeshes.Count; i < n; i++)
            {
                var  linkedMesh = linkedMeshes[i];
                Skin sk         = linkedMesh.skin == null ? skeleton.Data.defaultSkin : skeleton.Data.FindSkin(linkedMesh.skin);
                if (sk == null)
                {
                    throw new Exception("Slot not found: " + linkedMesh.skin);
                }
                Attachment parent = sk.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();
            }
            skeleton.Data.Skins.Add(skin);
            return(skin);
        }
		public static PathAttachment GetClone (this PathAttachment o) {
			var newPathAttachment = new PathAttachment(o.Name) {
				lengths = o.lengths.Clone() as float[],
				closed = o.closed,
				constantSpeed = o.constantSpeed
			};
			CloneVertexAttachment(o, newPathAttachment);

			return newPathAttachment;
		}