User Tools

Site Tools


zh_cn:tutorial:blockappearance

操纵方块的外观

这是教程的 1.15+ (对 1.16、1.17、1.18 和 1.19 也适用)版本。如需1.14版本,请参考操纵方块的外观(1.14)

使方块透明

您可能已经注意到,即使方块的材质是透明的,它仍然看起来是不透明的。如需解决,请将方块的渲染层设为 cutout 或 transparent。

客户端模组初始化器中,添加:

@Environment(EnvType.CLIENT)
public class ExampleModClient implements ClientModInitializer() {
    public void onInitializeClient() {
         BlockRenderLayerMap.INSTANCE.putBlock(ExampleMod.MY_BLOCK, RenderLayer.getCutout());
         // 如果有半透明纹理,可以将`RenderLayer.getCutout()` 替换为 `RenderLayer.getTranslucent()`。
    }
}

您可能还想使方块透明。为此,可在方块设置中使用 nonOpaque 方法。

     public static final Block MY_BLOCK = new Block(FabricBlockSettings.create().nonOpaque());
如果你不像这样把方块标记为非不透明的,则方块后面的面不会渲染,你将会“看穿”整个世界(到虚空或渲染范围之外之类的)。

确保在 fabric.mod.json 中添加了客户端入口点,你可以这样做:

  "entrypoints": {
    "main": [
      "mod.fabricmc.examplemod.ExampleMod"
    ],
    "client": [
      "mod.fabricmc.examplemod.ExampleModClient"
    ]
  },

注意:对于不透明但不完整的方块,你可能需要覆盖 getOutlineShape 方法并返回非完整的图形,以避免看穿整个世界。

使方块不可见

首先,我们需要使该方块看起来不可见。为此,我们在方块类中重写 getRenderType 并返回 BlockRenderType.INVISIBLE

    @Override
    public BlockRenderType getRenderType(BlockState state) {
        return BlockRenderType.INVISIBLE;
    }

然后,我们需要通过使其 outlineShape 不存在来使我们的方块不能被选中。因此,覆盖 getOutlineShape 并返回一个空的 VoxelShape

    @Override
    public VoxelShape getOutlineShape(BlockState state, BlockView blockView, BlockPos pos, ShapeContext context) {
       return VoxelShapes.empty();
    }

zh_cn/tutorial/blockappearance.txt · Last modified: 2023/11/18 08:40 by solidblock