///<summary>Returns a new mesh with this mesh set as the <see cref="ParentMesh"/>. public MeshAttachment NewLinkedMesh() { MeshAttachment mesh = new MeshAttachment(Name); mesh.RendererObject = RendererObject; mesh.regionOffsetX = regionOffsetX; mesh.regionOffsetY = regionOffsetY; mesh.regionWidth = regionWidth; mesh.regionHeight = regionHeight; mesh.regionOriginalWidth = regionOriginalWidth; mesh.regionOriginalHeight = regionOriginalHeight; mesh.RegionDegrees = RegionDegrees; mesh.RegionRotate = RegionRotate; mesh.RegionU = RegionU; mesh.RegionV = RegionV; mesh.RegionU2 = RegionU2; mesh.RegionV2 = RegionV2; mesh.Path = Path; mesh.r = r; mesh.g = g; mesh.b = b; mesh.a = a; mesh.deformAttachment = deformAttachment; mesh.ParentMesh = parentMesh != null ? parentMesh : this; mesh.UpdateUVs(); return(mesh); }
/// <summary> /// Creates the mesh attachment by texture. /// </summary> /// <returns>The mesh attachment by texture.</returns> /// <param name="slot">Slot.</param> /// <param name="texture">Texture.</param> private Attachment CreateMeshAttachmentByTexture(Spine.Slot slot, Texture2D texture) { if (slot == null) { return(null); } Spine.MeshAttachment oldAtt = slot.Attachment as Spine.MeshAttachment; if (oldAtt == null || texture == null) { return(null); } Spine.MeshAttachment att = new Spine.MeshAttachment(oldAtt.Name); att.RendererObject = CreateRegion(texture); att.Path = oldAtt.Path; att.Bones = oldAtt.Bones; att.Edges = oldAtt.Edges; att.Triangles = oldAtt.triangles; att.Vertices = oldAtt.Vertices; att.WorldVerticesLength = oldAtt.WorldVerticesLength; att.HullLength = oldAtt.HullLength; att.RegionRotate = false; att.RegionU = 0f; att.RegionV = 1f; att.RegionU2 = 1f; att.RegionV2 = 0f; att.RegionUVs = oldAtt.RegionUVs; att.UpdateUVs(); Material mat = new Material(Shader.Find("Sprites/Default")); mat.mainTexture = texture; (att.RendererObject as Spine.AtlasRegion).page.rendererObject = mat; slot.Attachment = att; return(slot.Attachment); }
private Attachment ReadAttachment(Stream input, Skin skin, String attachmentName, bool nonessential) { float scale = Scale; String name = ReadString(input); if (name == null) { name = attachmentName; } switch ((AttachmentType)input.ReadByte()) { case AttachmentType.region: { String path = ReadString(input); if (path == null) { path = name; } RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = ReadFloat(input) * scale; region.y = ReadFloat(input) * scale; region.scaleX = ReadFloat(input); region.scaleY = ReadFloat(input); region.rotation = ReadFloat(input); region.width = ReadFloat(input) * scale; region.height = ReadFloat(input) * scale; int color = ReadInt(input); region.r = ((color & 0xff000000) >> 24) / 255f; region.g = ((color & 0x00ff0000) >> 16) / 255f; region.b = ((color & 0x0000ff00) >> 8) / 255f; region.a = ((color & 0x000000ff)) / 255f; region.UpdateOffset(); return(region); } case AttachmentType.boundingbox: { BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } box.vertices = ReadFloatArray(input, scale); return(box); } case AttachmentType.mesh: { String path = ReadString(input); if (path == null) { path = name; } MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.regionUVs = ReadFloatArray(input, 1); mesh.triangles = ReadShortArray(input); mesh.vertices = ReadFloatArray(input, scale); mesh.UpdateUVs(); int color = ReadInt(input); mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.HullLength = ReadInt(input, true) * 2; if (nonessential) { mesh.Edges = ReadIntArray(input); mesh.Width = ReadFloat(input) * scale; mesh.Height = ReadFloat(input) * scale; } return(mesh); } case AttachmentType.skinnedmesh: { String path = ReadString(input); if (path == null) { path = name; } SkinnedMeshAttachment mesh = attachmentLoader.NewSkinnedMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; float[] uvs = ReadFloatArray(input, 1); int[] triangles = ReadShortArray(input); int vertexCount = ReadInt(input, true); var weights = new List <float>(uvs.Length * 3 * 3); var bones = new List <int>(uvs.Length * 3); for (int i = 0; i < vertexCount; i++) { int boneCount = (int)ReadFloat(input); bones.Add(boneCount); for (int nn = i + boneCount * 4; i < nn; i += 4) { bones.Add((int)ReadFloat(input)); weights.Add(ReadFloat(input) * scale); weights.Add(ReadFloat(input) * scale); weights.Add(ReadFloat(input)); } } mesh.bones = bones.ToArray(); mesh.weights = weights.ToArray(); mesh.triangles = triangles; mesh.regionUVs = uvs; mesh.UpdateUVs(); int color = ReadInt(input); mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.HullLength = ReadInt(input, true) * 2; if (nonessential) { mesh.Edges = ReadIntArray(input); mesh.Width = ReadFloat(input) * scale; mesh.Height = ReadFloat(input) * scale; } return(mesh); } } return(null); }
private Attachment ReadAttachment(Dictionary <String, Object> map, Skin skin, int slotIndex, String name) { var scale = this.Scale; name = GetString(map, "name", name); var typeName = GetString(map, "type", "region"); if (typeName == "skinnedmesh") { typeName = "weightedmesh"; } if (typeName == "weightedmesh") { typeName = "mesh"; } if (typeName == "weightedlinkedmesh") { typeName = "linkedmesh"; } var type = (AttachmentType)Enum.Parse(typeof(AttachmentType), typeName, true); String path = GetString(map, "path", name); switch (type) { case AttachmentType.Region: RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = GetFloat(map, "x", 0) * scale; region.y = GetFloat(map, "y", 0) * scale; region.scaleX = GetFloat(map, "scaleX", 1); region.scaleY = GetFloat(map, "scaleY", 1); region.rotation = GetFloat(map, "rotation", 0); region.width = GetFloat(map, "width", 32) * scale; region.height = GetFloat(map, "height", 32) * scale; region.UpdateOffset(); if (map.ContainsKey("color")) { var color = (String)map["color"]; region.r = ToColor(color, 0); region.g = ToColor(color, 1); region.b = ToColor(color, 2); region.a = ToColor(color, 3); } region.UpdateOffset(); return(region); case AttachmentType.Boundingbox: BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } ReadVertices(map, box, GetInt(map, "vertexCount", 0) << 1); return(box); case AttachmentType.Mesh: case AttachmentType.Linkedmesh: { MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; if (map.ContainsKey("color")) { var color = (String)map["color"]; mesh.r = ToColor(color, 0); mesh.g = ToColor(color, 1); mesh.b = ToColor(color, 2); mesh.a = ToColor(color, 3); } mesh.Width = GetFloat(map, "width", 0) * scale; mesh.Height = GetFloat(map, "height", 0) * scale; String parent = GetString(map, "parent", null); if (parent != null) { mesh.InheritDeform = GetBoolean(map, "deform", true); linkedMeshes.Add(new LinkedMesh(mesh, GetString(map, "skin", null), slotIndex, parent)); return(mesh); } float[] uvs = GetFloatArray(map, "uvs", 1); ReadVertices(map, mesh, uvs.Length); mesh.triangles = GetIntArray(map, "triangles"); mesh.regionUVs = uvs; mesh.UpdateUVs(); if (map.ContainsKey("hull")) { mesh.HullLength = GetInt(map, "hull", 0) * 2; } if (map.ContainsKey("edges")) { mesh.Edges = GetIntArray(map, "edges"); } return(mesh); } case AttachmentType.Path: { PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, name); if (pathAttachment == null) { return(null); } pathAttachment.closed = GetBoolean(map, "closed", false); pathAttachment.constantSpeed = GetBoolean(map, "constantSpeed", true); int vertexCount = GetInt(map, "vertexCount", 0); ReadVertices(map, pathAttachment, vertexCount << 1); // potential BOZO see Java impl pathAttachment.lengths = GetFloatArray(map, "lengths", scale); return(pathAttachment); } } return(null); }
private Attachment ReadAttachment(Stream input, Skin skin, int slotIndex, String attachmentName, bool nonessential) { float scale = Scale; String name = ReadString(input); if (name == null) { name = attachmentName; } AttachmentType type = (AttachmentType)input.ReadByte(); switch (type) { case AttachmentType.region: { String path = ReadString(input); float rotation = ReadFloat(input); float x = ReadFloat(input); float y = ReadFloat(input); float scaleX = ReadFloat(input); float scaleY = ReadFloat(input); float width = ReadFloat(input); float height = ReadFloat(input); int color = ReadInt(input); if (path == null) { path = name; } RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = x * scale; region.y = y * scale; region.scaleX = scaleX; region.scaleY = scaleY; region.rotation = rotation; region.width = width * scale; region.height = height * scale; region.r = ((color & 0xff000000) >> 24) / 255f; region.g = ((color & 0x00ff0000) >> 16) / 255f; region.b = ((color & 0x0000ff00) >> 8) / 255f; region.a = ((color & 0x000000ff)) / 255f; region.UpdateOffset(); return(region); } case AttachmentType.boundingbox: { float[] vertices = ReadFloatArray(input, ReadVarint(input, true) * 2, scale); BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } box.vertices = vertices; return(box); } case AttachmentType.mesh: { String path = ReadString(input); int color = ReadInt(input); int hullLength = 0; int verticesLength = ReadVarint(input, true) * 2; float[] uvs = ReadFloatArray(input, verticesLength, 1); int[] triangles = ReadShortArray(input); float[] vertices = ReadFloatArray(input, verticesLength, scale); hullLength = ReadVarint(input, true); int[] edges = null; float width = 0, height = 0; if (nonessential) { edges = ReadShortArray(input); width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.vertices = vertices; mesh.triangles = triangles; mesh.regionUVs = uvs; mesh.UpdateUVs(); mesh.HullLength = hullLength; if (nonessential) { mesh.Edges = edges; mesh.Width = width * scale; mesh.Height = height * scale; } return(mesh); } case AttachmentType.linkedmesh: { String path = ReadString(input); int color = ReadInt(input); String skinName = ReadString(input); String parent = ReadString(input); bool inheritFFD = ReadBoolean(input); float width = 0, height = 0; if (nonessential) { width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.inheritFFD = inheritFFD; if (nonessential) { mesh.Width = width * scale; mesh.Height = height * scale; } linkedMeshes.Add(new SkeletonJson.LinkedMesh(mesh, skinName, slotIndex, parent)); return(mesh); } case AttachmentType.weightedmesh: { String path = ReadString(input); int color = ReadInt(input); int vertexCount = ReadVarint(input, true); float[] uvs = ReadFloatArray(input, vertexCount * 2, 1); int[] triangles = ReadShortArray(input); var weights = new List <float>(uvs.Length * 3 * 3); var bones = new List <int>(uvs.Length * 3); for (int i = 0; i < vertexCount; i++) { int boneCount = (int)ReadFloat(input); bones.Add(boneCount); for (int ii = 0; ii < boneCount; ii++) { bones.Add((int)ReadFloat(input)); weights.Add(ReadFloat(input) * scale); weights.Add(ReadFloat(input) * scale); weights.Add(ReadFloat(input)); } } int hullLength = ReadVarint(input, true); int[] edges = null; float width = 0, height = 0; if (nonessential) { edges = ReadShortArray(input); width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } WeightedMeshAttachment mesh = attachmentLoader.NewWeightedMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.bones = bones.ToArray(); mesh.weights = weights.ToArray(); mesh.triangles = triangles; mesh.regionUVs = uvs; mesh.UpdateUVs(); mesh.HullLength = hullLength * 2; if (nonessential) { mesh.Edges = edges; mesh.Width = width * scale; mesh.Height = height * scale; } // return(mesh); } case AttachmentType.weightedlinkedmesh: { String path = ReadString(input); int color = ReadInt(input); String skinName = ReadString(input); String parent = ReadString(input); bool inheritFFD = ReadBoolean(input); float width = 0, height = 0; if (nonessential) { width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } WeightedMeshAttachment mesh = attachmentLoader.NewWeightedMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.inheritFFD = inheritFFD; if (nonessential) { mesh.Width = width * scale; mesh.Height = height * scale; } linkedMeshes.Add(new SkeletonJson.LinkedMesh(mesh, skinName, slotIndex, parent)); return(mesh); } } return(null); }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData boneData = new BoneData(name, parent); boneData.rotation = ReadFloat(input); boneData.x = ReadFloat(input) * scale; boneData.y = ReadFloat(input) * scale; boneData.scaleX = ReadFloat(input); boneData.scaleY = ReadFloat(input); boneData.shearX = ReadFloat(input); boneData.shearY = ReadFloat(input); boneData.length = ReadFloat(input) * scale; boneData.inheritRotation = ReadBoolean(input); boneData.inheritScale = ReadBoolean(input); if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(boneData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData ikConstraintData = new IkConstraintData(ReadString(input)); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { ikConstraintData.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } ikConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; ikConstraintData.mix = ReadFloat(input); ikConstraintData.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(ikConstraintData); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData transformConstraintData = new TransformConstraintData(ReadString(input)); transformConstraintData.bone = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.target = skeletonData.bones.Items[ReadVarint(input, true)]; transformConstraintData.offsetRotation = ReadFloat(input); transformConstraintData.offsetX = ReadFloat(input) * scale; transformConstraintData.offsetY = ReadFloat(input) * scale; transformConstraintData.offsetScaleX = ReadFloat(input); transformConstraintData.offsetScaleY = ReadFloat(input); transformConstraintData.offsetShearY = ReadFloat(input); transformConstraintData.rotateMix = ReadFloat(input); transformConstraintData.translateMix = ReadFloat(input); transformConstraintData.scaleMix = ReadFloat(input); transformConstraintData.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(transformConstraintData); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // Default skin. Skin defaultSkin = ReadSkin(input, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData eventData = new EventData(ReadString(input)); eventData.Int = ReadVarint(input, false); eventData.Float = ReadFloat(input); eventData.String = ReadString(input); skeletonData.events.Add(eventData); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); return(skeletonData); }
private Attachment ReadAttachment(Skin skin, int slotIndex, String name, Dictionary <String, Object> map) { if (map.ContainsKey("name")) { name = (String)map["name"]; } var scale = this.Scale; var type = AttachmentType.region; if (map.ContainsKey("type")) { var typeName = (String)map["type"]; if (typeName == "skinnedmesh") { typeName = "weightedmesh"; } type = (AttachmentType)Enum.Parse(typeof(AttachmentType), typeName, false); } String path = name; if (map.ContainsKey("path")) { path = (String)map["path"]; } switch (type) { case AttachmentType.region: RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = GetFloat(map, "x", 0) * scale; region.y = GetFloat(map, "y", 0) * scale; region.scaleX = GetFloat(map, "scaleX", 1); region.scaleY = GetFloat(map, "scaleY", 1); region.rotation = GetFloat(map, "rotation", 0); region.width = GetFloat(map, "width", 32) * scale; region.height = GetFloat(map, "height", 32) * scale; region.UpdateOffset(); if (map.ContainsKey("color")) { var color = (String)map["color"]; region.r = ToColor(color, 0); region.g = ToColor(color, 1); region.b = ToColor(color, 2); region.a = ToColor(color, 3); } return(region); case AttachmentType.mesh: case AttachmentType.linkedmesh: { MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; if (map.ContainsKey("color")) { var color = (String)map["color"]; mesh.r = ToColor(color, 0); mesh.g = ToColor(color, 1); mesh.b = ToColor(color, 2); mesh.a = ToColor(color, 3); } mesh.Width = GetInt(map, "width", 0) * scale; mesh.Height = GetInt(map, "height", 0) * scale; String parent = GetString(map, "parent", null); if (parent == null) { mesh.vertices = GetFloatArray(map, "vertices", scale); mesh.triangles = GetIntArray(map, "triangles"); mesh.regionUVs = GetFloatArray(map, "uvs", 1); mesh.UpdateUVs(); mesh.HullLength = GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { mesh.Edges = GetIntArray(map, "edges"); } } else { mesh.InheritFFD = GetBoolean(map, "ffd", true); linkedMeshes.Add(new LinkedMesh(mesh, GetString(map, "skin", null), slotIndex, parent)); } return(mesh); } case AttachmentType.weightedmesh: case AttachmentType.weightedlinkedmesh: { WeightedMeshAttachment mesh = attachmentLoader.NewWeightedMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; if (map.ContainsKey("color")) { var color = (String)map["color"]; mesh.r = ToColor(color, 0); mesh.g = ToColor(color, 1); mesh.b = ToColor(color, 2); mesh.a = ToColor(color, 3); } mesh.Width = GetInt(map, "width", 0) * scale; mesh.Height = GetInt(map, "height", 0) * scale; String parent = GetString(map, "parent", null); if (parent == null) { float[] uvs = GetFloatArray(map, "uvs", 1); float[] vertices = GetFloatArray(map, "vertices", 1); var weights = new List <float>(uvs.Length * 3 * 3); var bones = new List <int>(uvs.Length * 3); for (int i = 0, n = vertices.Length; i < n;) { int boneCount = (int)vertices[i++]; bones.Add(boneCount); for (int nn = i + boneCount * 4; i < nn; i += 4) { bones.Add((int)vertices[i]); weights.Add(vertices[i + 1] * scale); weights.Add(vertices[i + 2] * scale); weights.Add(vertices[i + 3]); } } mesh.bones = bones.ToArray(); mesh.weights = weights.ToArray(); mesh.triangles = GetIntArray(map, "triangles"); mesh.regionUVs = uvs; mesh.UpdateUVs(); mesh.HullLength = GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { mesh.Edges = GetIntArray(map, "edges"); } } else { mesh.InheritFFD = GetBoolean(map, "ffd", true); linkedMeshes.Add(new LinkedMesh(mesh, GetString(map, "skin", null), slotIndex, parent)); } return(mesh); } case AttachmentType.boundingbox: BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } box.vertices = GetFloatArray(map, "vertices", scale); return(box); } return(null); }
public SkeletonData ReadSkeletonData(TextReader reader) { if (reader == null) { throw new ArgumentNullException("reader cannot be null."); } var scale = this.Scale; var skeletonData = new SkeletonData(); var root = Json.Deserialize(reader) as Dictionary <String, Object>; if (root == null) { throw new Exception("Invalid JSON."); } // Skeleton. if (root.ContainsKey("skeleton")) { var skeletonMap = (Dictionary <String, Object>)root["skeleton"]; skeletonData.hash = (String)skeletonMap["hash"]; skeletonData.version = (String)skeletonMap["spine"]; skeletonData.width = GetFloat(skeletonMap, "width", 0); skeletonData.height = GetFloat(skeletonMap, "height", 0); } // Bones. foreach (Dictionary <String, Object> boneMap in (List <Object>)root["bones"]) { BoneData parent = null; if (boneMap.ContainsKey("parent")) { parent = skeletonData.FindBone((String)boneMap["parent"]); if (parent == null) { throw new Exception("Parent bone not found: " + boneMap["parent"]); } } var boneData = new BoneData((String)boneMap["name"], parent); boneData.length = GetFloat(boneMap, "length", 0) * scale; boneData.x = GetFloat(boneMap, "x", 0) * scale; boneData.y = GetFloat(boneMap, "y", 0) * scale; boneData.rotation = GetFloat(boneMap, "rotation", 0); boneData.scaleX = GetFloat(boneMap, "scaleX", 1); boneData.scaleY = GetFloat(boneMap, "scaleY", 1); boneData.shearX = GetFloat(boneMap, "shearX", 0); boneData.shearY = GetFloat(boneMap, "shearY", 0); boneData.inheritScale = GetBoolean(boneMap, "inheritScale", true); boneData.inheritRotation = GetBoolean(boneMap, "inheritRotation", true); skeletonData.bones.Add(boneData); } // IK constraints. if (root.ContainsKey("ik")) { foreach (Dictionary <String, Object> ikMap in (List <Object>)root["ik"]) { IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]); foreach (String boneName in (List <Object>)ikMap["bones"]) { BoneData bone = skeletonData.FindBone(boneName); if (bone == null) { throw new Exception("IK bone not found: " + boneName); } ikConstraintData.bones.Add(bone); } String targetName = (String)ikMap["target"]; ikConstraintData.target = skeletonData.FindBone(targetName); if (ikConstraintData.target == null) { throw new Exception("Target bone not found: " + targetName); } ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1; ikConstraintData.mix = GetFloat(ikMap, "mix", 1); skeletonData.ikConstraints.Add(ikConstraintData); } } // Transform constraints. if (root.ContainsKey("transform")) { foreach (Dictionary <String, Object> transformMap in (List <Object>)root["transform"]) { TransformConstraintData transformConstraintData = new TransformConstraintData((String)transformMap["name"]); String boneName = (String)transformMap["bone"]; transformConstraintData.bone = skeletonData.FindBone(boneName); if (transformConstraintData.bone == null) { throw new Exception("Bone not found: " + boneName); } String targetName = (String)transformMap["target"]; transformConstraintData.target = skeletonData.FindBone(targetName); if (transformConstraintData.target == null) { throw new Exception("Target bone not found: " + targetName); } transformConstraintData.offsetRotation = GetFloat(transformMap, "rotation", 0); transformConstraintData.offsetX = GetFloat(transformMap, "x", 0) * scale; transformConstraintData.offsetY = GetFloat(transformMap, "y", 0) * scale; transformConstraintData.offsetScaleX = GetFloat(transformMap, "scaleX", 0); transformConstraintData.offsetScaleY = GetFloat(transformMap, "scaleY", 0); transformConstraintData.offsetShearY = GetFloat(transformMap, "shearY", 0); transformConstraintData.rotateMix = GetFloat(transformMap, "rotateMix", 1); transformConstraintData.translateMix = GetFloat(transformMap, "translateMix", 1); transformConstraintData.scaleMix = GetFloat(transformMap, "scaleMix", 1); transformConstraintData.shearMix = GetFloat(transformMap, "shearMix", 1); skeletonData.transformConstraints.Add(transformConstraintData); } } // Slots. if (root.ContainsKey("slots")) { foreach (Dictionary <String, Object> slotMap in (List <Object>)root["slots"]) { var slotName = (String)slotMap["name"]; var boneName = (String)slotMap["bone"]; BoneData boneData = skeletonData.FindBone(boneName); if (boneData == null) { throw new Exception("Slot bone not found: " + boneName); } var slotData = new SlotData(slotName, boneData); if (slotMap.ContainsKey("color")) { var color = (String)slotMap["color"]; slotData.r = ToColor(color, 0); slotData.g = ToColor(color, 1); slotData.b = ToColor(color, 2); slotData.a = ToColor(color, 3); } if (slotMap.ContainsKey("attachment")) { slotData.attachmentName = (String)slotMap["attachment"]; } if (slotMap.ContainsKey("blend")) { slotData.blendMode = (BlendMode)Enum.Parse(typeof(BlendMode), (String)slotMap["blend"], false); } else { slotData.blendMode = BlendMode.normal; } skeletonData.slots.Add(slotData); } } // Skins. if (root.ContainsKey("skins")) { foreach (KeyValuePair <String, Object> entry in (Dictionary <String, Object>)root["skins"]) { var skin = new Skin(entry.Key); foreach (KeyValuePair <String, Object> slotEntry in (Dictionary <String, Object>)entry.Value) { int slotIndex = skeletonData.FindSlotIndex(slotEntry.Key); foreach (KeyValuePair <String, Object> attachmentEntry in ((Dictionary <String, Object>)slotEntry.Value)) { Attachment attachment = ReadAttachment(skin, slotIndex, attachmentEntry.Key, (Dictionary <String, Object>)attachmentEntry.Value); if (attachment != null) { skin.AddAttachment(slotIndex, attachmentEntry.Key, attachment); } } } skeletonData.skins.Add(skin); if (skin.name == "default") { skeletonData.defaultSkin = skin; } } } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.defaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Slot not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } else { WeightedMeshAttachment mesh = (WeightedMeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (WeightedMeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. 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); }
private Attachment ReadAttachment(NewStream input, Skin skin, int slotIndex, String attachmentName, bool nonessential) { float scale = Scale; String name = ReadString(input); if (name == null) { name = attachmentName; } AttachmentType type = (AttachmentType)input.ReadByte(); switch (type) { case AttachmentType.Region: { String path = ReadString(input); float rotation = ReadFloat(input); float x = ReadFloat(input); float y = ReadFloat(input); float scaleX = ReadFloat(input); float scaleY = ReadFloat(input); float width = ReadFloat(input); float height = ReadFloat(input); int color = ReadInt(input); if (path == null) { path = name; } RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = x * scale; region.y = y * scale; region.scaleX = scaleX; region.scaleY = scaleY; region.rotation = rotation; region.width = width * scale; region.height = height * scale; region.r = ((color & 0xff000000) >> 24) / 255f; region.g = ((color & 0x00ff0000) >> 16) / 255f; region.b = ((color & 0x0000ff00) >> 8) / 255f; region.a = ((color & 0x000000ff)) / 255f; region.UpdateOffset(); return(region); } case AttachmentType.Boundingbox: { int vertexCount = ReadVarint(input, true); Vertices vertices = ReadVertices(input, vertexCount); if (nonessential) { ReadInt(input); //int color = nonessential ? ReadInt(input) : 0; // Avoid unused local warning. } BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } box.worldVerticesLength = vertexCount << 1; box.vertices = vertices.vertices; box.bones = vertices.bones; return(box); } case AttachmentType.Mesh: { String path = ReadString(input); int color = ReadInt(input); int vertexCount = ReadVarint(input, true); float[] uvs = ReadFloatArray(input, vertexCount << 1, 1); int[] triangles = ReadShortArray(input); Vertices vertices = ReadVertices(input, vertexCount); int hullLength = ReadVarint(input, true); int[] edges = null; float width = 0, height = 0; if (nonessential) { edges = ReadShortArray(input); width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.bones = vertices.bones; mesh.vertices = vertices.vertices; mesh.WorldVerticesLength = vertexCount << 1; mesh.triangles = triangles; mesh.regionUVs = uvs; mesh.UpdateUVs(); mesh.HullLength = hullLength << 1; if (nonessential) { mesh.Edges = edges; mesh.Width = width * scale; mesh.Height = height * scale; } return(mesh); } case AttachmentType.Linkedmesh: { String path = ReadString(input); int color = ReadInt(input); String skinName = ReadString(input); String parent = ReadString(input); bool inheritDeform = ReadBoolean(input); float width = 0, height = 0; if (nonessential) { width = ReadFloat(input); height = ReadFloat(input); } if (path == null) { path = name; } MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; mesh.r = ((color & 0xff000000) >> 24) / 255f; mesh.g = ((color & 0x00ff0000) >> 16) / 255f; mesh.b = ((color & 0x0000ff00) >> 8) / 255f; mesh.a = ((color & 0x000000ff)) / 255f; mesh.inheritDeform = inheritDeform; if (nonessential) { mesh.Width = width * scale; mesh.Height = height * scale; } linkedMeshes.Add(new SkeletonJson.LinkedMesh(mesh, skinName, slotIndex, parent)); return(mesh); } case AttachmentType.Path: { bool closed = ReadBoolean(input); bool constantSpeed = ReadBoolean(input); int vertexCount = ReadVarint(input, true); Vertices vertices = ReadVertices(input, vertexCount); float[] lengths = new float[vertexCount / 3]; for (int i = 0, n = lengths.Length; i < n; i++) { lengths[i] = ReadFloat(input) * scale; } if (nonessential) { ReadInt(input); //int color = nonessential ? ReadInt(input) : 0; // Avoid unused local warning. } PathAttachment path = attachmentLoader.NewPathAttachment(skin, name); if (path == null) { return(null); } path.closed = closed; path.constantSpeed = constantSpeed; path.worldVerticesLength = vertexCount << 1; path.vertices = vertices.vertices; path.bones = vertices.bones; path.lengths = lengths; return(path); } } return(null); }
private Attachment ReadAttachment(Stream input, Skin skin, int slotIndex, string attachmentName, bool nonessential) { float scale = this.Scale; string text = this.ReadString(input); if (text == null) { text = attachmentName; } switch (input.ReadByte()) { case 0: { string text2 = this.ReadString(input); float rotation = this.ReadFloat(input); float num = this.ReadFloat(input); float num2 = this.ReadFloat(input); float scaleX = this.ReadFloat(input); float scaleY = this.ReadFloat(input); float num3 = this.ReadFloat(input); float num4 = this.ReadFloat(input); int num5 = SkeletonBinary.ReadInt(input); if (text2 == null) { text2 = text; } RegionAttachment regionAttachment = this.attachmentLoader.NewRegionAttachment(skin, text, text2); if (regionAttachment == null) { return(null); } regionAttachment.Path = text2; regionAttachment.x = num * scale; regionAttachment.y = num2 * scale; regionAttachment.scaleX = scaleX; regionAttachment.scaleY = scaleY; regionAttachment.rotation = rotation; regionAttachment.width = num3 * scale; regionAttachment.height = num4 * scale; regionAttachment.r = (float)((num5 & 4278190080u) >> 24) / 255f; regionAttachment.g = (float)((num5 & 16711680) >> 16) / 255f; regionAttachment.b = (float)((num5 & 65280) >> 8) / 255f; regionAttachment.a = (float)(num5 & 255) / 255f; regionAttachment.UpdateOffset(); return(regionAttachment); } case 1: { int num6 = SkeletonBinary.ReadVarint(input, true); Vertices vertices = this.ReadVertices(input, num6); if (nonessential) { SkeletonBinary.ReadInt(input); } BoundingBoxAttachment boundingBoxAttachment = this.attachmentLoader.NewBoundingBoxAttachment(skin, text); if (boundingBoxAttachment == null) { return(null); } boundingBoxAttachment.worldVerticesLength = num6 << 1; boundingBoxAttachment.vertices = vertices.vertices; boundingBoxAttachment.bones = vertices.bones; return(boundingBoxAttachment); } case 2: { string text3 = this.ReadString(input); int num7 = SkeletonBinary.ReadInt(input); int num8 = SkeletonBinary.ReadVarint(input, true); float[] regionUVs = this.ReadFloatArray(input, num8 << 1, 1f); int[] triangles = this.ReadShortArray(input); Vertices vertices2 = this.ReadVertices(input, num8); int num9 = SkeletonBinary.ReadVarint(input, true); int[] edges = null; float num10 = 0f; float num11 = 0f; if (nonessential) { edges = this.ReadShortArray(input); num10 = this.ReadFloat(input); num11 = this.ReadFloat(input); } if (text3 == null) { text3 = text; } MeshAttachment meshAttachment = this.attachmentLoader.NewMeshAttachment(skin, text, text3); if (meshAttachment == null) { return(null); } meshAttachment.Path = text3; meshAttachment.r = (float)((num7 & 4278190080u) >> 24) / 255f; meshAttachment.g = (float)((num7 & 16711680) >> 16) / 255f; meshAttachment.b = (float)((num7 & 65280) >> 8) / 255f; meshAttachment.a = (float)(num7 & 255) / 255f; meshAttachment.bones = vertices2.bones; meshAttachment.vertices = vertices2.vertices; meshAttachment.WorldVerticesLength = num8 << 1; meshAttachment.triangles = triangles; meshAttachment.regionUVs = regionUVs; meshAttachment.UpdateUVs(); meshAttachment.HullLength = num9 << 1; if (nonessential) { meshAttachment.Edges = edges; meshAttachment.Width = num10 * scale; meshAttachment.Height = num11 * scale; } return(meshAttachment); } case 3: { string text4 = this.ReadString(input); int num12 = SkeletonBinary.ReadInt(input); string skin2 = this.ReadString(input); string parent = this.ReadString(input); bool inheritDeform = SkeletonBinary.ReadBoolean(input); float num13 = 0f; float num14 = 0f; if (nonessential) { num13 = this.ReadFloat(input); num14 = this.ReadFloat(input); } if (text4 == null) { text4 = text; } MeshAttachment meshAttachment2 = this.attachmentLoader.NewMeshAttachment(skin, text, text4); if (meshAttachment2 == null) { return(null); } meshAttachment2.Path = text4; meshAttachment2.r = (float)((num12 & 4278190080u) >> 24) / 255f; meshAttachment2.g = (float)((num12 & 16711680) >> 16) / 255f; meshAttachment2.b = (float)((num12 & 65280) >> 8) / 255f; meshAttachment2.a = (float)(num12 & 255) / 255f; meshAttachment2.inheritDeform = inheritDeform; if (nonessential) { meshAttachment2.Width = num13 * scale; meshAttachment2.Height = num14 * scale; } this.linkedMeshes.Add(new SkeletonJson.LinkedMesh(meshAttachment2, skin2, slotIndex, parent)); return(meshAttachment2); } case 4: { bool closed = SkeletonBinary.ReadBoolean(input); bool constantSpeed = SkeletonBinary.ReadBoolean(input); int num15 = SkeletonBinary.ReadVarint(input, true); Vertices vertices3 = this.ReadVertices(input, num15); float[] array = new float[num15 / 3]; int i = 0; int num16 = array.Length; while (i < num16) { array[i] = this.ReadFloat(input) * scale; i++; } if (nonessential) { SkeletonBinary.ReadInt(input); } PathAttachment pathAttachment = this.attachmentLoader.NewPathAttachment(skin, text); if (pathAttachment == null) { return(null); } pathAttachment.closed = closed; pathAttachment.constantSpeed = constantSpeed; pathAttachment.worldVerticesLength = num15 << 1; pathAttachment.vertices = vertices3.vertices; pathAttachment.bones = vertices3.bones; pathAttachment.lengths = array; return(pathAttachment); } default: return(null); } }
private Attachment ReadAttachment(Skin skin, String name, Dictionary <String, Object> map) { if (map.ContainsKey("name")) { name = (String)map["name"]; } var type = AttachmentType.region; if (map.ContainsKey("type")) { type = (AttachmentType)Enum.Parse(typeof(AttachmentType), (String)map["type"], false); } String path = name; if (map.ContainsKey("path")) { path = (String)map["path"]; } switch (type) { case AttachmentType.region: RegionAttachment region = attachmentLoader.NewRegionAttachment(skin, name, path); if (region == null) { return(null); } region.Path = path; region.x = GetFloat(map, "x", 0) * Scale; region.y = GetFloat(map, "y", 0) * Scale; region.scaleX = GetFloat(map, "scaleX", 1); region.scaleY = GetFloat(map, "scaleY", 1); region.rotation = GetFloat(map, "rotation", 0); region.width = GetFloat(map, "width", 32) * Scale; region.height = GetFloat(map, "height", 32) * Scale; region.UpdateOffset(); if (map.ContainsKey("color")) { var color = (String)map["color"]; region.r = ToColor(color, 0); region.g = ToColor(color, 1); region.b = ToColor(color, 2); region.a = ToColor(color, 3); } return(region); case AttachmentType.mesh: { MeshAttachment mesh = attachmentLoader.NewMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; float[] uvs = GetFloatArray(map, "uvs", 1); int[] triangles = GetIntArray(map, "triangles"); float[] vertices = GetFloatArray(map, "vertices", Scale); mesh.SetMesh(vertices, triangles, uvs); mesh.UpdateUVs(); if (map.ContainsKey("color")) { var color = (String)map["color"]; mesh.r = ToColor(color, 0); mesh.g = ToColor(color, 1); mesh.b = ToColor(color, 2); mesh.a = ToColor(color, 3); } mesh.HullLength = GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { mesh.Edges = GetIntArray(map, "edges"); } mesh.Width = GetInt(map, "width", 0) * Scale; mesh.Height = GetInt(map, "height", 0) * Scale; return(mesh); } case AttachmentType.skinnedmesh: { SkinnedMeshAttachment mesh = attachmentLoader.NewSkinnedMeshAttachment(skin, name, path); if (mesh == null) { return(null); } mesh.Path = path; float[] uvs = GetFloatArray(map, "uvs", 1); int[] triangles = GetIntArray(map, "triangles"); float[] vertices = GetFloatArray(map, "vertices", 1); var weights = new List <float>(uvs.Length * 3 * 3); var bones = new List <int>(uvs.Length * 3); float scale = Scale; for (int i = 0, n = vertices.Length; i < n;) { int boneCount = (int)vertices[i++]; bones.Add(boneCount); for (int nn = i + boneCount * 4; i < nn;) { bones.Add((int)vertices[i]); weights.Add(vertices[i + 1] * scale); weights.Add(vertices[i + 2] * scale); weights.Add(vertices[i + 3]); i += 4; } } mesh.SetMesh(bones.ToArray(), weights.ToArray(), triangles, uvs); mesh.UpdateUVs(); if (map.ContainsKey("color")) { var color = (String)map["color"]; mesh.r = ToColor(color, 0); mesh.g = ToColor(color, 1); mesh.b = ToColor(color, 2); mesh.a = ToColor(color, 3); } mesh.HullLength = GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { mesh.Edges = GetIntArray(map, "edges"); } mesh.Width = GetInt(map, "width", 0) * Scale; mesh.Height = GetInt(map, "height", 0) * Scale; return(mesh); } case AttachmentType.boundingbox: BoundingBoxAttachment box = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (box == null) { return(null); } box.vertices = GetFloatArray(map, "vertices", Scale); return(box); } return(null); }
private Attachment ReadAttachment(Skin skin, string name, Dictionary <string, object> map) { if (map.ContainsKey("name")) { name = (string)map.get_Item("name"); } AttachmentType attachmentType = AttachmentType.region; if (map.ContainsKey("type")) { attachmentType = (AttachmentType)((int)Enum.Parse(typeof(AttachmentType), (string)map.get_Item("type"), false)); } string path = name; if (map.ContainsKey("path")) { path = (string)map.get_Item("path"); } switch (attachmentType) { case AttachmentType.region: { RegionAttachment regionAttachment = this.attachmentLoader.NewRegionAttachment(skin, name, path); if (regionAttachment == null) { return(null); } regionAttachment.Path = path; regionAttachment.x = this.GetFloat(map, "x", 0f) * this.Scale; regionAttachment.y = this.GetFloat(map, "y", 0f) * this.Scale; regionAttachment.scaleX = this.GetFloat(map, "scaleX", 1f); regionAttachment.scaleY = this.GetFloat(map, "scaleY", 1f); regionAttachment.rotation = this.GetFloat(map, "rotation", 0f); regionAttachment.width = this.GetFloat(map, "width", 32f) * this.Scale; regionAttachment.height = this.GetFloat(map, "height", 32f) * this.Scale; regionAttachment.UpdateOffset(); if (map.ContainsKey("color")) { string hexString = (string)map.get_Item("color"); regionAttachment.r = this.ToColor(hexString, 0); regionAttachment.g = this.ToColor(hexString, 1); regionAttachment.b = this.ToColor(hexString, 2); regionAttachment.a = this.ToColor(hexString, 3); } return(regionAttachment); } case AttachmentType.boundingbox: { BoundingBoxAttachment boundingBoxAttachment = this.attachmentLoader.NewBoundingBoxAttachment(skin, name); if (boundingBoxAttachment == null) { return(null); } boundingBoxAttachment.vertices = this.GetFloatArray(map, "vertices", this.Scale); return(boundingBoxAttachment); } case AttachmentType.mesh: { MeshAttachment meshAttachment = this.attachmentLoader.NewMeshAttachment(skin, name, path); if (meshAttachment == null) { return(null); } meshAttachment.Path = path; meshAttachment.vertices = this.GetFloatArray(map, "vertices", this.Scale); meshAttachment.triangles = this.GetIntArray(map, "triangles"); meshAttachment.regionUVs = this.GetFloatArray(map, "uvs", 1f); meshAttachment.UpdateUVs(); if (map.ContainsKey("color")) { string hexString2 = (string)map.get_Item("color"); meshAttachment.r = this.ToColor(hexString2, 0); meshAttachment.g = this.ToColor(hexString2, 1); meshAttachment.b = this.ToColor(hexString2, 2); meshAttachment.a = this.ToColor(hexString2, 3); } meshAttachment.HullLength = this.GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { meshAttachment.Edges = this.GetIntArray(map, "edges"); } meshAttachment.Width = (float)this.GetInt(map, "width", 0) * this.Scale; meshAttachment.Height = (float)this.GetInt(map, "height", 0) * this.Scale; return(meshAttachment); } case AttachmentType.skinnedmesh: { SkinnedMeshAttachment skinnedMeshAttachment = this.attachmentLoader.NewSkinnedMeshAttachment(skin, name, path); if (skinnedMeshAttachment == null) { return(null); } skinnedMeshAttachment.Path = path; float[] floatArray = this.GetFloatArray(map, "uvs", 1f); float[] floatArray2 = this.GetFloatArray(map, "vertices", 1f); List <float> list = new List <float>(floatArray.Length * 3 * 3); List <int> list2 = new List <int>(floatArray.Length * 3); float scale = this.Scale; int i = 0; int num = floatArray2.Length; while (i < num) { int num2 = (int)floatArray2[i++]; list2.Add(num2); int num3 = i + num2 * 4; while (i < num3) { list2.Add((int)floatArray2[i]); list.Add(floatArray2[i + 1] * scale); list.Add(floatArray2[i + 2] * scale); list.Add(floatArray2[i + 3]); i += 4; } } skinnedMeshAttachment.bones = list2.ToArray(); skinnedMeshAttachment.weights = list.ToArray(); skinnedMeshAttachment.triangles = this.GetIntArray(map, "triangles"); skinnedMeshAttachment.regionUVs = floatArray; skinnedMeshAttachment.UpdateUVs(); if (map.ContainsKey("color")) { string hexString3 = (string)map.get_Item("color"); skinnedMeshAttachment.r = this.ToColor(hexString3, 0); skinnedMeshAttachment.g = this.ToColor(hexString3, 1); skinnedMeshAttachment.b = this.ToColor(hexString3, 2); skinnedMeshAttachment.a = this.ToColor(hexString3, 3); } skinnedMeshAttachment.HullLength = this.GetInt(map, "hull", 0) * 2; if (map.ContainsKey("edges")) { skinnedMeshAttachment.Edges = this.GetIntArray(map, "edges"); } skinnedMeshAttachment.Width = (float)this.GetInt(map, "width", 0) * this.Scale; skinnedMeshAttachment.Height = (float)this.GetInt(map, "height", 0) * this.Scale; return(skinnedMeshAttachment); } default: return(null); } }
private Attachment ReadAttachment(Stream input, SkeletonData skeletonData, Skin skin, int slotIndex, string attachmentName, bool nonessential) { float scale = Scale; string text = ReadString(input); if (text == null) { text = attachmentName; } switch (input.ReadByte()) { case 0: { string text2 = ReadString(input); float rotation = ReadFloat(input); float num3 = ReadFloat(input); float num4 = ReadFloat(input); float scaleX = ReadFloat(input); float scaleY = ReadFloat(input); float num5 = ReadFloat(input); float num6 = ReadFloat(input); int num7 = ReadInt(input); if (text2 == null) { text2 = text; } RegionAttachment regionAttachment = attachmentLoader.NewRegionAttachment(skin, text, text2); if (regionAttachment == null) { return(null); } regionAttachment.Path = text2; regionAttachment.x = num3 * scale; regionAttachment.y = num4 * scale; regionAttachment.scaleX = scaleX; regionAttachment.scaleY = scaleY; regionAttachment.rotation = rotation; regionAttachment.width = num5 * scale; regionAttachment.height = num6 * scale; regionAttachment.r = (float)((num7 & 4278190080u) >> 24) / 255f; regionAttachment.g = (float)((num7 & 0xFF0000) >> 16) / 255f; regionAttachment.b = (float)((num7 & 0xFF00) >> 8) / 255f; regionAttachment.a = (float)(num7 & 0xFF) / 255f; regionAttachment.UpdateOffset(); return(regionAttachment); } case 1: { int num20 = ReadVarint(input, optimizePositive: true); Vertices vertices4 = ReadVertices(input, num20); if (nonessential) { ReadInt(input); } BoundingBoxAttachment boundingBoxAttachment = attachmentLoader.NewBoundingBoxAttachment(skin, text); if (boundingBoxAttachment == null) { return(null); } boundingBoxAttachment.worldVerticesLength = num20 << 1; boundingBoxAttachment.vertices = vertices4.vertices; boundingBoxAttachment.bones = vertices4.bones; return(boundingBoxAttachment); } case 2: { string text3 = ReadString(input); int num8 = ReadInt(input); int num9 = ReadVarint(input, optimizePositive: true); float[] regionUVs = ReadFloatArray(input, num9 << 1, 1f); int[] triangles = ReadShortArray(input); Vertices vertices2 = ReadVertices(input, num9); int num10 = ReadVarint(input, optimizePositive: true); int[] edges = null; float num11 = 0f; float num12 = 0f; if (nonessential) { edges = ReadShortArray(input); num11 = ReadFloat(input); num12 = ReadFloat(input); } if (text3 == null) { text3 = text; } MeshAttachment meshAttachment = attachmentLoader.NewMeshAttachment(skin, text, text3); if (meshAttachment == null) { return(null); } meshAttachment.Path = text3; meshAttachment.r = (float)((num8 & 4278190080u) >> 24) / 255f; meshAttachment.g = (float)((num8 & 0xFF0000) >> 16) / 255f; meshAttachment.b = (float)((num8 & 0xFF00) >> 8) / 255f; meshAttachment.a = (float)(num8 & 0xFF) / 255f; meshAttachment.bones = vertices2.bones; meshAttachment.vertices = vertices2.vertices; meshAttachment.WorldVerticesLength = num9 << 1; meshAttachment.triangles = triangles; meshAttachment.regionUVs = regionUVs; meshAttachment.UpdateUVs(); meshAttachment.HullLength = num10 << 1; if (nonessential) { meshAttachment.Edges = edges; meshAttachment.Width = num11 * scale; meshAttachment.Height = num12 * scale; } return(meshAttachment); } case 3: { string text4 = ReadString(input); int num13 = ReadInt(input); string skin2 = ReadString(input); string parent = ReadString(input); bool inheritDeform = ReadBoolean(input); float num14 = 0f; float num15 = 0f; if (nonessential) { num14 = ReadFloat(input); num15 = ReadFloat(input); } if (text4 == null) { text4 = text; } MeshAttachment meshAttachment2 = attachmentLoader.NewMeshAttachment(skin, text, text4); if (meshAttachment2 == null) { return(null); } meshAttachment2.Path = text4; meshAttachment2.r = (float)((num13 & 4278190080u) >> 24) / 255f; meshAttachment2.g = (float)((num13 & 0xFF0000) >> 16) / 255f; meshAttachment2.b = (float)((num13 & 0xFF00) >> 8) / 255f; meshAttachment2.a = (float)(num13 & 0xFF) / 255f; meshAttachment2.inheritDeform = inheritDeform; if (nonessential) { meshAttachment2.Width = num14 * scale; meshAttachment2.Height = num15 * scale; } linkedMeshes.Add(new SkeletonJson.LinkedMesh(meshAttachment2, skin2, slotIndex, parent)); return(meshAttachment2); } case 4: { bool closed = ReadBoolean(input); bool constantSpeed = ReadBoolean(input); int num16 = ReadVarint(input, optimizePositive: true); Vertices vertices3 = ReadVertices(input, num16); float[] array = new float[num16 / 3]; int i = 0; for (int num17 = array.Length; i < num17; i++) { array[i] = ReadFloat(input) * scale; } if (nonessential) { ReadInt(input); } PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, text); if (pathAttachment == null) { return(null); } pathAttachment.closed = closed; pathAttachment.constantSpeed = constantSpeed; pathAttachment.worldVerticesLength = num16 << 1; pathAttachment.vertices = vertices3.vertices; pathAttachment.bones = vertices3.bones; pathAttachment.lengths = array; return(pathAttachment); } case 5: { float rotation2 = ReadFloat(input); float num18 = ReadFloat(input); float num19 = ReadFloat(input); if (nonessential) { ReadInt(input); } PointAttachment pointAttachment = attachmentLoader.NewPointAttachment(skin, text); if (pointAttachment == null) { return(null); } pointAttachment.x = num18 * scale; pointAttachment.y = num19 * scale; pointAttachment.rotation = rotation2; return(pointAttachment); } case 6: { int num = ReadVarint(input, optimizePositive: true); int num2 = ReadVarint(input, optimizePositive: true); Vertices vertices = ReadVertices(input, num2); if (nonessential) { ReadInt(input); } ClippingAttachment clippingAttachment = attachmentLoader.NewClippingAttachment(skin, text); if (clippingAttachment == null) { return(null); } clippingAttachment.EndSlot = skeletonData.slots.Items[num]; clippingAttachment.worldVerticesLength = num2 << 1; clippingAttachment.vertices = vertices.vertices; clippingAttachment.bones = vertices.bones; return(clippingAttachment); } default: return(null); } }
public SkeletonData ReadSkeletonData(Stream input) { if (input == null) { throw new ArgumentNullException("input"); } float scale = Scale; var skeletonData = new SkeletonData(); skeletonData.hash = ReadString(input); if (skeletonData.hash.Length == 0) { skeletonData.hash = null; } skeletonData.version = ReadString(input); if (skeletonData.version.Length == 0) { skeletonData.version = null; } skeletonData.width = ReadFloat(input); skeletonData.height = ReadFloat(input); bool nonessential = ReadBoolean(input); if (nonessential) { skeletonData.fps = ReadFloat(input); skeletonData.imagesPath = ReadString(input); if (skeletonData.imagesPath.Length == 0) { skeletonData.imagesPath = null; } } // Bones. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String name = ReadString(input); BoneData parent = i == 0 ? null : skeletonData.bones.Items[ReadVarint(input, true)]; BoneData data = new BoneData(i, name, parent); data.rotation = ReadFloat(input); data.x = ReadFloat(input) * scale; data.y = ReadFloat(input) * scale; data.scaleX = ReadFloat(input); data.scaleY = ReadFloat(input); data.shearX = ReadFloat(input); data.shearY = ReadFloat(input); data.length = ReadFloat(input) * scale; data.transformMode = TransformModeValues[ReadVarint(input, true)]; if (nonessential) { ReadInt(input); // Skip bone color. } skeletonData.bones.Add(data); } // Slots. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { String slotName = ReadString(input); BoneData boneData = skeletonData.bones.Items[ReadVarint(input, true)]; SlotData slotData = new SlotData(i, slotName, boneData); int color = ReadInt(input); slotData.r = ((color & 0xff000000) >> 24) / 255f; slotData.g = ((color & 0x00ff0000) >> 16) / 255f; slotData.b = ((color & 0x0000ff00) >> 8) / 255f; slotData.a = ((color & 0x000000ff)) / 255f; int darkColor = ReadInt(input); // 0x00rrggbb if (darkColor != -1) { slotData.hasSecondColor = true; slotData.r2 = ((darkColor & 0x00ff0000) >> 16) / 255f; slotData.g2 = ((darkColor & 0x0000ff00) >> 8) / 255f; slotData.b2 = ((darkColor & 0x000000ff)) / 255f; } slotData.attachmentName = ReadString(input); slotData.blendMode = (BlendMode)ReadVarint(input, true); skeletonData.slots.Add(slotData); } // IK constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { IkConstraintData data = new IkConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.mix = ReadFloat(input); data.bendDirection = ReadSByte(input); skeletonData.ikConstraints.Add(data); } // Transform constraints. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { TransformConstraintData data = new TransformConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.bones.Items[ReadVarint(input, true)]; data.local = ReadBoolean(input); data.relative = ReadBoolean(input); data.offsetRotation = ReadFloat(input); data.offsetX = ReadFloat(input) * scale; data.offsetY = ReadFloat(input) * scale; data.offsetScaleX = ReadFloat(input); data.offsetScaleY = ReadFloat(input); data.offsetShearY = ReadFloat(input); data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); data.scaleMix = ReadFloat(input); data.shearMix = ReadFloat(input); skeletonData.transformConstraints.Add(data); } // Path constraints for (int i = 0, n = ReadVarint(input, true); i < n; i++) { PathConstraintData data = new PathConstraintData(ReadString(input)); data.order = ReadVarint(input, true); for (int ii = 0, nn = ReadVarint(input, true); ii < nn; ii++) { data.bones.Add(skeletonData.bones.Items[ReadVarint(input, true)]); } data.target = skeletonData.slots.Items[ReadVarint(input, true)]; data.positionMode = (PositionMode)Enum.GetValues(typeof(PositionMode)).GetValue(ReadVarint(input, true)); data.spacingMode = (SpacingMode)Enum.GetValues(typeof(SpacingMode)).GetValue(ReadVarint(input, true)); data.rotateMode = (RotateMode)Enum.GetValues(typeof(RotateMode)).GetValue(ReadVarint(input, true)); data.offsetRotation = ReadFloat(input); data.position = ReadFloat(input); if (data.positionMode == PositionMode.Fixed) { data.position *= scale; } data.spacing = ReadFloat(input); if (data.spacingMode == SpacingMode.Length || data.spacingMode == SpacingMode.Fixed) { data.spacing *= scale; } data.rotateMix = ReadFloat(input); data.translateMix = ReadFloat(input); skeletonData.pathConstraints.Add(data); } // Default skin. Skin defaultSkin = ReadSkin(input, skeletonData, "default", nonessential); if (defaultSkin != null) { skeletonData.defaultSkin = defaultSkin; skeletonData.skins.Add(defaultSkin); } // Skins. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { skeletonData.skins.Add(ReadSkin(input, skeletonData, ReadString(input), nonessential)); } // Linked meshes. for (int i = 0, n = linkedMeshes.Count; i < n; i++) { SkeletonJson.LinkedMesh linkedMesh = linkedMeshes[i]; Skin skin = linkedMesh.skin == null ? skeletonData.DefaultSkin : skeletonData.FindSkin(linkedMesh.skin); if (skin == null) { throw new Exception("Skin not found: " + linkedMesh.skin); } Attachment parent = skin.GetAttachment(linkedMesh.slotIndex, linkedMesh.parent); if (parent == null) { throw new Exception("Parent mesh not found: " + linkedMesh.parent); } if (linkedMesh.mesh is MeshAttachment) { MeshAttachment mesh = (MeshAttachment)linkedMesh.mesh; mesh.ParentMesh = (MeshAttachment)parent; mesh.UpdateUVs(); } } linkedMeshes.Clear(); // Events. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { EventData data = new EventData(ReadString(input)); data.Int = ReadVarint(input, false); data.Float = ReadFloat(input); data.String = ReadString(input); skeletonData.events.Add(data); } // Animations. for (int i = 0, n = ReadVarint(input, true); i < n; i++) { ReadAnimation(ReadString(input), input, skeletonData); } skeletonData.bones.TrimExcess(); skeletonData.slots.TrimExcess(); skeletonData.skins.TrimExcess(); skeletonData.events.TrimExcess(); skeletonData.animations.TrimExcess(); skeletonData.ikConstraints.TrimExcess(); skeletonData.pathConstraints.TrimExcess(); return(skeletonData); }
private Attachment ReadAttachment(Dictionary <string, object> map, Skin skin, int slotIndex, string name, SkeletonData skeletonData) { float scale = Scale; name = GetString(map, "name", name); string text = GetString(map, "type", "region"); if (text == "skinnedmesh") { text = "weightedmesh"; } if (text == "weightedmesh") { text = "mesh"; } if (text == "weightedlinkedmesh") { text = "linkedmesh"; } AttachmentType attachmentType = (AttachmentType)Enum.Parse(typeof(AttachmentType), text, ignoreCase: true); string @string = GetString(map, "path", name); switch (attachmentType) { case AttachmentType.Region: { RegionAttachment regionAttachment = attachmentLoader.NewRegionAttachment(skin, name, @string); if (regionAttachment == null) { return(null); } regionAttachment.Path = @string; regionAttachment.x = GetFloat(map, "x", 0f) * scale; regionAttachment.y = GetFloat(map, "y", 0f) * scale; regionAttachment.scaleX = GetFloat(map, "scaleX", 1f); regionAttachment.scaleY = GetFloat(map, "scaleY", 1f); regionAttachment.rotation = GetFloat(map, "rotation", 0f); regionAttachment.width = GetFloat(map, "width", 32f) * scale; regionAttachment.height = GetFloat(map, "height", 32f) * scale; regionAttachment.UpdateOffset(); if (map.ContainsKey("color")) { string hexString2 = (string)map["color"]; regionAttachment.r = ToColor(hexString2, 0); regionAttachment.g = ToColor(hexString2, 1); regionAttachment.b = ToColor(hexString2, 2); regionAttachment.a = ToColor(hexString2, 3); } regionAttachment.UpdateOffset(); return(regionAttachment); } case AttachmentType.Boundingbox: { BoundingBoxAttachment boundingBoxAttachment = attachmentLoader.NewBoundingBoxAttachment(skin, name); if (boundingBoxAttachment == null) { return(null); } ReadVertices(map, boundingBoxAttachment, GetInt(map, "vertexCount", 0) << 1); return(boundingBoxAttachment); } case AttachmentType.Mesh: case AttachmentType.Linkedmesh: { MeshAttachment meshAttachment = attachmentLoader.NewMeshAttachment(skin, name, @string); if (meshAttachment == null) { return(null); } meshAttachment.Path = @string; if (map.ContainsKey("color")) { string hexString = (string)map["color"]; meshAttachment.r = ToColor(hexString, 0); meshAttachment.g = ToColor(hexString, 1); meshAttachment.b = ToColor(hexString, 2); meshAttachment.a = ToColor(hexString, 3); } meshAttachment.Width = GetFloat(map, "width", 0f) * scale; meshAttachment.Height = GetFloat(map, "height", 0f) * scale; string string3 = GetString(map, "parent", null); if (string3 != null) { meshAttachment.InheritDeform = GetBoolean(map, "deform", defaultValue: true); linkedMeshes.Add(new LinkedMesh(meshAttachment, GetString(map, "skin", null), slotIndex, string3)); return(meshAttachment); } float[] floatArray = GetFloatArray(map, "uvs", 1f); ReadVertices(map, meshAttachment, floatArray.Length); meshAttachment.triangles = GetIntArray(map, "triangles"); meshAttachment.regionUVs = floatArray; meshAttachment.UpdateUVs(); if (map.ContainsKey("hull")) { meshAttachment.HullLength = GetInt(map, "hull", 0) * 2; } if (map.ContainsKey("edges")) { meshAttachment.Edges = GetIntArray(map, "edges"); } return(meshAttachment); } case AttachmentType.Path: { PathAttachment pathAttachment = attachmentLoader.NewPathAttachment(skin, name); if (pathAttachment == null) { return(null); } pathAttachment.closed = GetBoolean(map, "closed", defaultValue: false); pathAttachment.constantSpeed = GetBoolean(map, "constantSpeed", defaultValue: true); int @int = GetInt(map, "vertexCount", 0); ReadVertices(map, pathAttachment, @int << 1); pathAttachment.lengths = GetFloatArray(map, "lengths", scale); return(pathAttachment); } case AttachmentType.Point: { PointAttachment pointAttachment = attachmentLoader.NewPointAttachment(skin, name); if (pointAttachment == null) { return(null); } pointAttachment.x = GetFloat(map, "x", 0f) * scale; pointAttachment.y = GetFloat(map, "y", 0f) * scale; pointAttachment.rotation = GetFloat(map, "rotation", 0f); return(pointAttachment); } case AttachmentType.Clipping: { ClippingAttachment clippingAttachment = attachmentLoader.NewClippingAttachment(skin, name); if (clippingAttachment == null) { return(null); } string string2 = GetString(map, "end", null); if (string2 != null) { SlotData slotData = skeletonData.FindSlot(string2); if (slotData == null) { throw new Exception("Clipping end slot not found: " + string2); } clippingAttachment.EndSlot = slotData; } ReadVertices(map, clippingAttachment, GetInt(map, "vertexCount", 0) << 1); return(clippingAttachment); } default: return(null); } }