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);
        }
Example #2
0
        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);
        }