zh_cn:tutorial:custom_model
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
zh_cn:tutorial:custom_model [2021/08/30 09:49] – [注册模型] solidblock | zh_cn:tutorial:custom_model [2024/01/02 10:31] (current) – [UnbakedModel方法] solidblock | ||
---|---|---|---|
Line 2: | Line 2: | ||
可以通过方块模型 JSON 文件将模型添加到游戏,但也可以通过 Java 代码来渲染。本教程中,我们将会将一个四面熔炉模型添加到游戏。 | 可以通过方块模型 JSON 文件将模型添加到游戏,但也可以通过 Java 代码来渲染。本教程中,我们将会将一个四面熔炉模型添加到游戏。 | ||
- | 注意模型会在区块被重建时渲染。如果需要更加动态的渲染,可以使用 '' | + | 注意模型会在区块被重建时渲染。如果需要更加动态的渲染,可以使用 '' |
===== 创建模型 ===== | ===== 创建模型 ===== | ||
Line 8: | Line 8: | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class FourSidedFurnaceModel implements UnbakedModel, | public class FourSidedFurnaceModel implements UnbakedModel, | ||
</ | </ | ||
Line 20: | Line 21: | ||
}; | }; | ||
private Sprite[] SPRITES = new Sprite[2]; | private Sprite[] SPRITES = new Sprite[2]; | ||
+ | |||
+ | // Some constants to avoid magic numbers, these need to match the SPRITE_IDS | ||
+ | private static final int SPRITE_SIDE = 0; | ||
+ | private static final int SPRITE_TOP = 1; | ||
</ | </ | ||
Line 53: | Line 58: | ||
for(Direction direction : Direction.values()) { | for(Direction direction : Direction.values()) { | ||
- | int spriteIdx = direction == Direction.UP || direction == Direction.DOWN ? 1 : 0; | + | int spriteIdx = direction == Direction.UP || direction == Direction.DOWN ? SPRITE_TOP |
// 将新的面(face)添加到mesh | // 将新的面(face)添加到mesh | ||
emitter.square(direction, | emitter.square(direction, | ||
Line 73: | Line 78: | ||
注意这里不是所有的方法都会被 Fabric Renderer 使用,所以我们并不关心这个实现。 | 注意这里不是所有的方法都会被 Fabric Renderer 使用,所以我们并不关心这个实现。 | ||
<code java> | <code java> | ||
- | @Override | + | |
public List< | public List< | ||
- | return | + | return |
} | } | ||
Line 99: | Line 104: | ||
@Override | @Override | ||
- | public Sprite | + | public Sprite |
return SPRITES[1]; // 方块被破坏时产生的颗粒,使用furnace_top | return SPRITES[1]; // 方块被破坏时产生的颗粒,使用furnace_top | ||
} | } | ||
Line 141: | Line 146: | ||
我们用 '' | 我们用 '' | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class TutorialModelProvider implements ModelResourceProvider { | public class TutorialModelProvider implements ModelResourceProvider { | ||
public static final Identifier FOUR_SIDED_FURNACE_MODEL = new Identifier(" | public static final Identifier FOUR_SIDED_FURNACE_MODEL = new Identifier(" | ||
Line 156: | Line 162: | ||
现在我们要将这个类注册到客户端初始化器(仅适用于客户端的代码的入口点)中。 | 现在我们要将这个类注册到客户端初始化器(仅适用于客户端的代码的入口点)中。 | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class ExampleModClient implements ClientModInitializer { | public class ExampleModClient implements ClientModInitializer { | ||
@Override | @Override | ||
Line 196: | Line 203: | ||
==== 更新模型 ==== | ==== 更新模型 ==== | ||
我们复用相同的模型类,但是有一点点小改变: | 我们复用相同的模型类,但是有一点点小改变: | ||
- | * 我们会使用 '' | + | * 我们会使用 '' |
- | 我们会更新我们的 | + | 现在对 |
<code java> | <code java> | ||
- | // Minecraft默认方块模型 | + | // Minecraft 默认方块模型 |
private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier(" | private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier(" | ||
private ModelTransformation transformation; | private ModelTransformation transformation; | ||
| | ||
- | // 我们会将默认模型添加到其依赖中 | + | // 将默认模型添加到依赖中 |
public Collection< | public Collection< | ||
return Arrays.asList(DEFAULT_BLOCK_MODEL); | return Arrays.asList(DEFAULT_BLOCK_MODEL); | ||
} | } | ||
| | ||
- | // 我们会给bake函数添加一点逻辑 | + | // 我们给 bake 函数添加一点逻辑 |
@Override | @Override | ||
public BakedModel bake(ModelLoader loader, Function< | public BakedModel bake(ModelLoader loader, Function< | ||
// 加载默认方块模型 | // 加载默认方块模型 | ||
JsonUnbakedModel defaultBlockModel = (JsonUnbakedModel) loader.getOrLoadModel(DEFAULT_BLOCK_MODEL); | JsonUnbakedModel defaultBlockModel = (JsonUnbakedModel) loader.getOrLoadModel(DEFAULT_BLOCK_MODEL); | ||
- | // 获取ModelTransformation | + | // 获取 ModelTransformation |
transformation = defaultBlockModel.getTransformations(); | transformation = defaultBlockModel.getTransformations(); | ||
| | ||
Line 221: | Line 228: | ||
} | } | ||
| | ||
- | // 我们会实现 getTransformation() 和 getOverrides() | + | // 我们需要实现 getTransformation() 和 getOverrides() |
@Override | @Override | ||
public ModelTransformation getTransformation() { | public ModelTransformation getTransformation() { | ||
Line 232: | Line 239: | ||
} | } | ||
| | ||
- | // 我们也会使用此方法以使得物品渲染时有正确的光照。尝试将其设为false,你就会看到不同。 | + | // 我们也会使用此方法以使得物品渲染时有正确的光照。尝试将其设为 false,你就会看到不同。 |
@Override | @Override | ||
public boolean isSideLit() { | public boolean isSideLit() { | ||
Line 248: | Line 255: | ||
更新我们先前创建的 '' | 更新我们先前创建的 '' | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class TutorialModelProvider implements ModelResourceProvider { | public class TutorialModelProvider implements ModelResourceProvider { | ||
public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); | public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); |
zh_cn/tutorial/custom_model.1630316983.txt.gz · Last modified: 2021/08/30 09:49 by solidblock