Example #1
0
        public static MiNET.Utils.Skins.Skin UpdateTexture(this MiNET.Utils.Skins.Skin skin, PooledTexture2D texture)
        {
            Image <Rgba32> skinTexture;

            using (MemoryStream ms = new MemoryStream())
            {
                texture.SaveAsPng(ms, texture.Width, texture.Height);
                ms.Position = 0;

                skinTexture = Image.Load(ms, new PngDecoder()).CloneAs <Rgba32>();
            }

            byte[] skinData;
            using (MemoryStream ms = new MemoryStream())
            {
                if (skinTexture.TryGetSinglePixelSpan(out var span))
                {
                    foreach (var value in span)
                    {
                        ms.WriteByte(value.R);
                        ms.WriteByte(value.G);
                        ms.WriteByte(value.B);
                        ms.WriteByte(value.A);
                    }
                }

                skinData = ms.ToArray();
            }

            skin.Width  = skinTexture.Width;
            skin.Height = skinTexture.Height;
            skin.Data   = skinData;

            return(skin);
        }
Example #2
0
        public static MiNET.Utils.Skins.Skin ToSkin(this EntityModel model)
        {
            var settings = new JsonSerializerSettings();

            settings.NullValueHandling     = NullValueHandling.Ignore;
            settings.DefaultValueHandling  = DefaultValueHandling.IgnoreAndPopulate;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            //settings.Formatting = Formatting.Indented;
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            settings.Converters.Add(new StringEnumConverter {
                NamingStrategy = new CamelCaseNamingStrategy()
            });

            MiNET.Utils.Skins.Skin skin = null;
            Geometry geometry           = new Geometry();

            geometry.Bones = new List <Bone>();

            foreach (var bone in model.Bones)
            {
                var a = new Bone();

                if (Enum.TryParse <BoneName>(bone.Name, true, out var boneName))
                {
                    a.Name = boneName;
                }
                else
                {
                    a.Name = BoneName.Unknown;
                }

                if (!string.IsNullOrWhiteSpace(bone.Parent))
                {
                    if (Enum.TryParse <BoneName>(bone.Parent, true, out var parentName))
                    {
                        if (parentName != a.Name)
                        {
                            a.Parent = parentName;
                        }
                    }
                }

                if (bone.NeverRender)
                {
                    a.NeverRender = true;
                }

                if (bone.Pivot.HasValue)
                {
                    a.Pivot = new float[] { bone.Pivot.Value.X, bone.Pivot.Value.Y, bone.Pivot.Value.Z };
                }

                if (bone.Rotation.HasValue)
                {
                    a.Rotation = new float[] { bone.Rotation.Value.X, bone.Rotation.Value.Y, bone.Rotation.Value.Z };
                }

                if (bone.BindPoseRotation.HasValue)
                {
                    if (a.Rotation == null || a.Rotation.Length == 0)
                    {
                        a.Rotation = new float[3];
                    }

                    a.Rotation[0] += bone.BindPoseRotation.Value.X;
                    a.Rotation[1] += bone.BindPoseRotation.Value.Y;
                    a.Rotation[2] += bone.BindPoseRotation.Value.Z;
                }

                if (bone.Cubes != null)
                {
                    a.Cubes = new List <Cube>();

                    foreach (var c in bone.Cubes)
                    {
                        var newCube = new ExtendedCube()
                        {
                            Inflate = (float)(c.Inflate.HasValue ? c.Inflate.Value : 0f),
                            Origin  = new float[] { c.Origin.X, c.Origin.Y, c.Origin.Z },
                            Size    = new float[] { c.Size.X, c.Size.Y, c.Size.Z },
                            Uv      = new float[] { c.Uv.South.Origin.X, c.Uv.South.Origin.Y }
                        };

                        if (c.Mirror.HasValue)
                        {
                            newCube.Mirror = c.Mirror.Value;
                        }

                        if (c.Rotation.HasValue)
                        {
                            newCube.Rotation = new float[] { c.Rotation.Value.X, c.Rotation.Value.Y, c.Rotation.Value.Z }
                        }
                        ;

                        if (c.Pivot.HasValue)
                        {
                            newCube.Pivot = new float[] { c.Pivot.Value.X, c.Pivot.Value.Y, c.Pivot.Value.Z }
                        }
                        ;

                        a.Cubes.Add(newCube);
                    }
                }

                geometry.Bones.Add(a);
            }

            // geometry.Bones = model.Bones.Select(x => ).ToList();

            //   geometry.Name = model.Description.Identifier;
            //geometry.TextureHeight = (int) model.Description.TextureHeight;
            // geometry.TextureWidth = (int) model.Description.TextureWidth;

            geometry.Description = new Description()
            {
                Identifier          = $"geometry.humanoid.customSlim",
                TextureHeight       = (int)model.Description.TextureHeight,
                TextureWidth        = (int)model.Description.TextureWidth,
                VisibleBoundsHeight = (int)model.Description.VisibleBoundsHeight,
                VisibleBoundsWidth  = (int)model.Description.VisibleBoundsWidth,
                VisibleBoundsOffset = new int[]
                {
                    (int)model.Description.VisibleBoundsOffset.X,
                    (int)model.Description.VisibleBoundsOffset.Y, (int)model.Description.VisibleBoundsOffset.Z
                }
            };

            //geometry.Subdivide(true, true, true, false);

            skin = new MiNET.Utils.Skins.Skin()
            {
                SkinId            = $"{Guid.NewGuid().ToString()}.steve",
                SkinResourcePatch =
                    new MiNET.Utils.Skins.SkinResourcePatch()
                {
                    Geometry = new MiNET.Utils.Skins.GeometryIdentifier()
                    {
                        Default = geometry.Description.Identifier
                    }
                },
                GeometryData = JsonConvert.SerializeObject(
                    new Dictionary <string, object>()
                {
                    { "format_version", "1.12.0" }, { "minecraft:geometry", new[] { geometry } },
                }, Formatting.None, settings),
                SkinColor = "#0",
                ArmSize   = "slim",
                Data      = null
            };

            return(skin);
        }