public SkinnedMeshAttachment NewSkinnedMeshAttachment(Skin skin, String name, String path) { AtlasRegion region = atlas.FindRegion(path); if (region == null) { throw new Exception("Region not found in atlas: " + path + " (skinned mesh attachment: " + name + ")"); } SkinnedMeshAttachment attachment = new SkinnedMeshAttachment(name); attachment.Path = path; attachment.RendererObject = region; attachment.RegionU = region.u; attachment.RegionV = region.v; attachment.RegionU2 = region.u2; attachment.RegionV2 = region.v2; attachment.RegionRotate = region.rotate; attachment.regionOffsetX = region.offsetX; attachment.regionOffsetY = region.offsetY; attachment.regionWidth = region.width; attachment.regionHeight = region.height; attachment.regionOriginalWidth = region.originalWidth; attachment.regionOriginalHeight = region.originalHeight; return(attachment); }
public SkinnedMeshAttachment NewSkinnedMeshAttachment (Skin skin, String name, String path) { AtlasRegion region = atlas.FindRegion(path); if (region == null) throw new Exception("Region not found in atlas: " + path + " (skinned mesh attachment: " + name + ")"); SkinnedMeshAttachment attachment = new SkinnedMeshAttachment(name); attachment.RendererObject = region; attachment.RegionU = region.u; attachment.RegionV = region.v; attachment.RegionU2 = region.u2; attachment.RegionV2 = region.v2; attachment.RegionRotate = region.rotate; attachment.regionOffsetX = region.offsetX; attachment.regionOffsetY = region.offsetY; attachment.regionWidth = region.width; attachment.regionHeight = region.height; attachment.regionOriginalWidth = region.originalWidth; attachment.regionOriginalHeight = region.originalHeight; return 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); }
public SkinnedMeshAttachment NewSkinnedMeshAttachment (Skin skin, String name, String path) { ProcessSpriteDefinition(path); SkinnedMeshAttachment mesh = new SkinnedMeshAttachment(name); mesh.Path = path; mesh.RendererObject = material; mesh.RegionU = u; mesh.RegionV = v; mesh.RegionU2 = u2; mesh.RegionV2 = v2; mesh.RegionRotate = regionRotated; mesh.RegionOriginalWidth = regionOriginalWidth; mesh.RegionOriginalHeight = regionOriginalHeight; mesh.RegionWidth = regionWidth; mesh.RegionHeight = regionHeight; mesh.RegionOffsetX = regionOffsetX; mesh.RegionOffsetY = regionOffsetY; return mesh; }
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; mesh.vertices = GetFloatArray(map, "vertices", Scale); mesh.triangles = GetIntArray(map, "triangles"); mesh.regionUVs = GetFloatArray(map, "uvs", 1); 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); 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.bones = bones.ToArray(); mesh.weights = weights.ToArray(); mesh.triangles = GetIntArray(map, "triangles"); mesh.regionUVs = 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); }
static Mesh ExtractSkinnedMeshAttachment (string name, SkinnedMeshAttachment attachment, int slotIndex, SkeletonData skeletonData, List<Transform> boneList, Mesh mesh = null) { Skeleton skeleton = new Skeleton(skeletonData); skeleton.UpdateWorldTransform(); float[] floatVerts = new float[attachment.UVs.Length]; attachment.ComputeWorldVertices(skeleton.Slots[slotIndex], floatVerts); Vector2[] uvs = ExtractUV(attachment.UVs); Vector3[] verts = ExtractVerts(floatVerts); int[] triangles = attachment.Triangles; Color color = new Color(attachment.R, attachment.G, attachment.B, attachment.A); if (mesh == null) mesh = new Mesh(); mesh.triangles = new int[0]; mesh.vertices = verts; mesh.uv = uvs; mesh.triangles = triangles; Color[] colors = new Color[verts.Length]; for (int i = 0; i < verts.Length; i++) colors[i] = color; mesh.colors = colors; mesh.name = name; mesh.RecalculateNormals(); mesh.RecalculateBounds(); //Handle weights and binding Dictionary<int, BoneWeightContainer> weightTable = new Dictionary<int, BoneWeightContainer>(); System.Text.StringBuilder warningBuilder = new System.Text.StringBuilder(); int[] bones = attachment.Bones; float[] weights = attachment.Weights; for (int w = 0, v = 0, b = 0, n = bones.Length; v < n; w += 2) { int nn = bones[v++] + v; for (; v < nn; v++, b += 3) { Transform boneTransform = boneList[bones[v]]; int vIndex = w / 2; float weight = weights[b + 2]; BoneWeightContainer container; if (weightTable.ContainsKey(vIndex)) container = weightTable[vIndex]; else { container = new BoneWeightContainer(); weightTable.Add(vIndex, container); } container.Add(boneTransform, weight); } } BoneWeight[] boneWeights = new BoneWeight[weightTable.Count]; for (int i = 0; i < weightTable.Count; i++) { BoneWeight bw = new BoneWeight(); var container = weightTable[i]; var pairs = container.pairs.OrderByDescending(pair => pair.weight).ToList(); for (int b = 0; b < pairs.Count; b++) { if (b > 3) { if (warningBuilder.Length == 0) warningBuilder.Insert(0, "[SkinnedMeshAttachment " + name + "]\r\nUnity only supports 4 weight influences per vertex! The 4 strongest influences will be used.\r\n"); warningBuilder.AppendFormat("{0} ignored on vertex {1}!\r\n", pairs[b].bone.name, i); continue; } int boneIndex = boneList.IndexOf(pairs[b].bone); float weight = pairs[b].weight; switch (b) { case 0: bw.boneIndex0 = boneIndex; bw.weight0 = weight; break; case 1: bw.boneIndex1 = boneIndex; bw.weight1 = weight; break; case 2: bw.boneIndex2 = boneIndex; bw.weight2 = weight; break; case 3: bw.boneIndex3 = boneIndex; bw.weight3 = weight; break; } } boneWeights[i] = bw; } Matrix4x4[] bindPoses = new Matrix4x4[boneList.Count]; for (int i = 0; i < boneList.Count; i++) { bindPoses[i] = boneList[i].worldToLocalMatrix; } mesh.boneWeights = boneWeights; mesh.bindposes = bindPoses; string warningString = warningBuilder.ToString(); if (warningString.Length > 0) Debug.LogWarning(warningString); return mesh; }
public void Draw(Skeleton skeleton) { float[] vertices = this.vertices; List <Slot> drawOrder = skeleton.DrawOrder; 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(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(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(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]; } } } }
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); } }