Ejemplo n.º 1
0
        public override void Cache(McResourcePack pack)
        {
            if (!Model.Textures.TryGetValue("layer0", out var t))
            {
                t = Model.Textures.FirstOrDefault(x => x.Value != null).Value;
            }

            if (t == null)
            {
                return;
            }

            List <VertexPositionColor> vertices = new List <VertexPositionColor>();

            if (pack.TryGetBitmap(t, out var rawTexture))
            {
                var texture = rawTexture.CloneAs <Rgba32>();

                float toolPosX = 0.0f;
                float toolPosY = 1.0f;
                float toolPosZ = (1f / 16f) * 7.5f;

                for (int y = 0; y < texture.Height; y++)
                {
                    for (int x = 0; x < texture.Width; x++)
                    {
                        var pixel = texture[x, y];
                        if (pixel.A == 0)
                        {
                            continue;
                        }

                        Color color = new Color(pixel.R, pixel.G, pixel.B, pixel.A);

                        ItemModelCube built =
                            new ItemModelCube(new Vector3(1f / texture.Width, 1f / texture.Height, 1f / 16f));
                        built.BuildCube(color);

                        var origin = new Vector3(
                            (toolPosX + (1f / texture.Width) * x),
                            toolPosY - (1f / texture.Height) * y,
                            toolPosZ
                            );

                        vertices.AddRange(Modify(built.Front, origin));
                        vertices.AddRange(Modify(built.Bottom, origin));
                        vertices.AddRange(Modify(built.Back, origin));
                        vertices.AddRange(Modify(built.Top, origin));
                        vertices.AddRange(Modify(built.Left, origin));
                        vertices.AddRange(Modify(built.Right, origin));
                    }
                }
            }

            Vertices = vertices.ToArray();
        }
Ejemplo n.º 2
0
        public override void Cache(McResourcePack pack)
        {
            string t = string.Empty;

            if (!Model.Textures.TryGetValue("layer0", out t))
            {
                t = Model.Textures.FirstOrDefault(x => x.Value != null).Value;
            }

            if (t == default)
            {
                return;
            }

            List <VertexPositionColor> vertices = new List <VertexPositionColor>();
            List <short> indexes = new List <short>();

            if (pack.TryGetBitmap(t, out var rawTexture))
            {
                var texture = rawTexture.CloneAs <Rgba32>();

                int   i               = 0;
                float toolPosX        = 0.0f;
                float toolPosY        = 1.0f;
                float toolPosZ        = (1f / 16f) * 7.5f;
                int   verticesPerTool = texture.Width * texture.Height * 36;


                for (int y = 0; y < texture.Height; y++)
                {
                    for (int x = 0; x < texture.Width; x++)
                    {
                        var pixel = texture[x, y];
                        if (pixel.A == 0)
                        {
                            continue;
                        }

                        Color color = new Color(pixel.R, pixel.G, pixel.B, pixel.A);

                        ItemModelCube built =
                            new ItemModelCube(new Vector3(1f / texture.Width, 1f / texture.Height, 1f / 16f));
                        built.BuildCube(color);

                        var origin = new Vector3(
                            (toolPosX + (1f / texture.Width) * x),
                            toolPosY - (1f / texture.Height) * y,
                            toolPosZ
                            );

                        vertices = ModifyCubeIndexes(vertices, ref built.Front, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Back, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Top, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Bottom, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Left, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Right, origin);

                        var indices = built.Front.indexes
                                      .Concat(built.Back.indexes)
                                      .Concat(built.Top.indexes)
                                      .Concat(built.Bottom.indexes)
                                      .Concat(built.Left.indexes)
                                      .Concat(built.Right.indexes)
                                      .ToArray();

                        indexes.AddRange(indices);
                    }
                }
            }

            Vertices = vertices.ToArray();

            for (var index = 0; index < Vertices.Length; index++)
            {
                var vertice = Vertices[index];
                Vertices[index] = vertice;
            }

            Indexes = indexes.ToArray();
        }
Ejemplo n.º 3
0
        public override bool Cache(ResourceManager pack)
        {
            if (_cached)
            {
                return(true);
            }

            _cached = true;

            if (!Model.Textures.TryGetValue("layer0", out var texture))
            {
                texture = Model.Textures.FirstOrDefault(x => x.Value != null).Value;
            }

            if (texture == null)
            {
                return(false);
            }

            List <VertexPositionColor> vertices = new List <VertexPositionColor>();

            if (pack.TryGetBitmap(texture, out var bitmap))
            {
                if (Model.ParentName.Contains("handheld", StringComparison.InvariantCultureIgnoreCase))
                {
                    bitmap.Mutate(
                        x =>
                    {
                        x.Flip(FlipMode.Horizontal);
                        x.Rotate(RotateMode.Rotate90);
                    });
                }

                try
                {
                    if (bitmap.TryGetSinglePixelSpan(out var pixels))
                    {
                        var pixelSize = new Vector3(16f / bitmap.Width, 16f / bitmap.Height, 1f);

                        for (int y = 0; y < bitmap.Height; y++)
                        {
                            for (int x = 0; x < bitmap.Width; x++)
                            {
                                var pixel = pixels[(bitmap.Width * (bitmap.Height - 1 - y)) + (x)];

                                if (pixel.A == 0)
                                {
                                    continue;
                                }

                                Color color  = new Color(pixel.R, pixel.G, pixel.B, pixel.A);
                                var   origin = new Vector3((x * pixelSize.X), y * pixelSize.Y, 0f);

                                ItemModelCube built = new ItemModelCube(pixelSize, color);

                                vertices.AddRange(
                                    Modify(
                                        built.Front.Concat(built.Bottom).Concat(built.Back).Concat(built.Top)
                                        .Concat(built.Left).Concat(built.Right), origin));
                            }
                        }

                        this.Size = new Vector3(16f, 16f, 1f);
                    }
                }
                finally
                {
                    bitmap.Dispose();
                }
            }

            Vertices = vertices.ToArray();

            return(true);
        }
Ejemplo n.º 4
0
        private void Cache(McResourcePack pack)
        {
            var t = Model.Textures.FirstOrDefault(x => x.Value != null);

            if (t.Value == default)
            {
                return;
            }

            List <VertexPositionColor> vertices = new List <VertexPositionColor>();
            List <short> indexes = new List <short>();

            if (pack.TryGetBitmap(t.Value, out Bitmap texture))
            {
                int   i               = 0;
                float toolPosX        = 0.0f;
                float toolPosY        = 0.0f;
                float toolPosZ        = 0.0f;
                int   verticesPerTool = texture.Width * texture.Height * 36;


                for (int y = 0; y < texture.Height; y++)
                {
                    for (int x = 0; x < texture.Width; x++)
                    {
                        var pixel = texture.GetPixel(x, y);
                        if (pixel.A == 0)
                        {
                            continue;
                        }

                        Color color = new Color(pixel.R, pixel.G, pixel.B, pixel.A);

                        ItemModelCube built = new ItemModelCube(new Vector3(1f / texture.Width));
                        built.BuildCube(color);

                        var origin = new Vector3(toolPosX + (1f / texture.Width) * x, toolPosY - (1f / texture.Height) * y, toolPosZ);

                        vertices = ModifyCubeIndexes(vertices, ref built.Front, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Back, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Top, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Bottom, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Left, origin);
                        vertices = ModifyCubeIndexes(vertices, ref built.Right, origin);

                        var indices = built.Front.indexes
                                      .Concat(built.Back.indexes)
                                      .Concat(built.Top.indexes)
                                      .Concat(built.Bottom.indexes)
                                      .Concat(built.Left.indexes)
                                      .Concat(built.Right.indexes)
                                      .ToArray();

                        indexes.AddRange(indices);
                    }
                }
            }

            Vertices = vertices.ToArray();

            for (var index = 0; index < Vertices.Length; index++)
            {
                var vertice = Vertices[index];
                Vertices[index] = vertice;
            }

            Indexes = indexes.ToArray();
        }