纹理

纹理

Minecraft 中的所有纹理都是 PNG 文件,位于某个命名空间的 textures 文件夹中。不支持 JPG、GIF 等其他图像格式。指向纹理的资源位置通常相对于 textures 文件夹,因此 examplemod:block/example_block 对应 assets/examplemod/textures/block/example_block.png。

纹理尺寸通常应为 2 的幂,例如 16×16 或 32×32。与旧版不同,现代 Minecraft 原生支持大于 16×16 的方块和物品纹理。如果自行渲染非 2 的幂尺寸纹理(例如 GUI 背景),可以创建一个尺寸取下一个可用的 2 的幂的空白文件(通常为 256×256),将纹理放在文件左上角,其余区域留空。然后在使用该纹理的代码中设置实际绘制尺寸。

纹理元数据

纹理元数据可放在与纹理同名、额外带有 .mcmeta 后缀的文件中。例如,textures/block/example.png 中的动态纹理需要配套的 textures/block/example.png.mcmeta 文件。.mcmeta 格式如下(所有项均可选):

{
    // 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,
    "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
            }
        }
    },
    // 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(1.21.1)。中文翻译依据提交 4a680cdd4d15。