Exemple #1
0
        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);
        }
Exemple #2
0
        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 Polygon getPolygon(BoundingBoxAttachment attachment)
        {
            int index = BoundingBoxes.IndexOf(attachment);

            return(index == -1 ? null : Polygons[index]);
        }