纹理

纹理

Minecraft 中的所有纹理都是位于命名空间 textures 文件夹内的 PNG 文件,不支持 JPG、GIF 等其他格式。引用纹理的标识符路径通常相对于 textures 文件夹。例如,examplemod:block/example_block 指向 assets/examplemod/textures/block/example_block.png。

纹理尺寸通常应为 2 的幂,例如 16×16 或 32×32。现代 Minecraft 原生支持大于 16×16 的方块和物品纹理。对于 GUI 背景等由自己渲染、尺寸不是 2 的幂的纹理,可以创建下一个足够大的 2 次幂尺寸空图(通常为 256×256),将纹理放在左上角,其余部分留空,再在使用该纹理的代码中指定实际绘制尺寸。

纹理元数据

元数据文件与纹理同名,并追加 .mcmeta 后缀。例如 textures/block/example.png 的元数据位于 textures/block/example.png.mcmeta。格式如下,所有设置均可省略:

{
    // Metadata for a general texture
    "texture": {
        // Whether the texture will be blurred if needed. Defaults to false.
        // Currently specified by the codec, but unused otherwise both in the files and in code.
        "blur": true,
        // Whether the texture will be clamped if needed. Defaults to false.
        // Currently specified by the codec, but unused otherwise both in the files and in code.
        "clamp": true,
        // Sets the strategy used when generating mipmaps (lower resolutions of textures used at
        // a distance).
        // Can either be:
        // - `mean`: The default that averages the color between four pixels.
        // - `cutout`: 'mean', except that all levels are generated from the original texture
        // rather than the close mipmap, with alpha value snapped to 0 or 1 using a threshold
        // of 0.2.
        // - `strict_cutout`: 'cutout', except the alpha value snaps using a threshold of 0.6.
        // - `dark_cutout`: 'mean', except that the surrounding pixels are only included in the
        // average if their alpha is not 0.
        "mipmap_strategy": "mean",
        // Offsets the cutoff alpha when determining whether a pixel should be made either fully
        // opaque or transparent for mipmaps. For example, setting to 0.3 with the 'cutout' strategy
        // changes the alpha value snap to 0.2 + 0.3 = 0.5.
        "alpha_cutoff_bias": 0.3
    },

    // Metadata for a texture used as a gui sprite
    "gui": {
        // Specifies how the texture will be scaled if needed. Can be one of these three:
        "scaling": {
            "type": "stretch" // default
        },
        "scaling": {
            "type": "tile",
            "width": 16,
            "height": 16
        },
        "scaling": {
            // Like "tile", but allows specifying the border offsets.
            "type": "nine_slice",
            "width": 16,
            "height": 16,
            // May also be a single int that is used as the value for all four sides.
            "border": {
                "left": 0,
                "top": 0,
                "right": 0,
                "bottom": 0
            },
            // When true the center part of the texture will be applied like
            // the stretch type instead of a nine slice tiling.
            "stretch_inner": true
        }
    },

    // Metadata for an animated texture
    // See below
    "animation": {}
}

动态纹理

Minecraft 原生支持方块与物品的动态纹理。各动画帧在同一纹理文件中竖向排列,例如包含 8 帧的 16×16 动画使用一张 16×128 的 PNG。

要播放动画,而不是显示为拉伸变形的纹理,元数据中必须包含 animation 对象。它可以为空,也可以包含以下可选设置:

{
    "animation": {
        // A custom order in which the frames are played. If omitted, the frames are played top to bottom.
        "frames": [1, 0],
        // How long one frame stays before switching to the next animation stage, in frames. Defaults to 1.
        "frametime": 5,
        // Whether to interpolate between animation stages. Defaults to false.
        "interpolate": true,
        // Width and height of one animation stage. If omitted, uses the texture width for both of these.
        "width": 12,
        "height": 12
    }
}

来源: NeoForged/Documentation(26.1)。中文翻译依据提交 4a680cdd4d15。