tutorial:datagen_model
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:datagen_model [2025/04/18 14:21] – [Addin Data Generation For a Directional Block] solidblock | tutorial:datagen_model [2025/04/18 14:59] (current) – [Model Generation] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Model Generation ====== | ====== Model Generation ====== | ||
- | We know that almost every item needs a corresponding **item baked model** (or //item model// for short), and an **item models definition** since 1.21.4. Each block needs a **block baked model** (or //block model// for short) and a **block states definition**, | + | :!: The page is translated from [[zh_cn: |
+ | |||
+ | We know that almost every item needs a corresponding **item baked model** (or //item model// for short), and an **item models definition** since 1.21.4. Each block needs a **block baked model** (or //block model// for short) and a **block states definition**, | ||
In data generator, models and definitions are generated together. Usually, when generation a model, a model id is returned, which will be used in the block states definition or item models definition. We will go into that from simple ones to details. | In data generator, models and definitions are generated together. Usually, when generation a model, a model id is returned, which will be used in the block states definition or item models definition. We will go into that from simple ones to details. | ||
Line 140: | Line 142: | ||
Well done! We successfully added all models, block states definitions and item models definitions needed with several lines of code. | Well done! We successfully added all models, block states definitions and item models definitions needed with several lines of code. | ||
- | ===== Directional blocks (taking vertical blocks as example) ===== | + | ===== Directional blocks (taking vertical |
+ | |||
+ | A directional block usually uses one block model, but in the block states definition, different model variants will be mapped into, such as different x-rotation, y-rotation and uvlock. (These words seem similar when we used to manually write block states definitions JSONs?) | ||
+ | |||
+ | Taking a vertical slab block we created in the [[directionalblock]] tutorial as an example, we generate models and block states definitions with data generator. | ||
+ | |||
+ | ==== Custom model ==== | ||
+ | |||
+ | We have created a '' | ||
+ | |||
+ | To inherit this template model in the data generator, we create a '' | ||
+ | |||
+ | <code java TutorialModelGenerator.java> | ||
+ | public class TutorialModelGenerator extends FabricModelProvider { | ||
+ | public static final Model VERTICAL_SLAB = new Model( | ||
+ | Optional.of(Identifier.of(" | ||
+ | Optional.empty(), | ||
+ | TextureKey.BOTTOM, | ||
+ | |||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Then we call the '' | ||
+ | <code java TutorialModelGenerator.java> | ||
+ | @Override | ||
+ | public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { | ||
+ | // ... | ||
+ | |||
+ | final Identifier verticalSlabModelId = VERTICAL_SLAB.upload(TutorialBlocks.POLISHED_ANDESITE_VERTICAL_SLAB, | ||
+ | blockStateModelGenerator.registerParentedItemModel(TutorialBlocks.POLISHED_ANDESITE_VERTICAL_SLAB, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Custom block states definition (since 1.21.5) ==== | ||
+ | It's now an important part — now we create a block states definition for a vertical slab block. Actually it's not so complicated, | ||
+ | |||
+ | In 1.21.5, a block states definition is '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | For '' | ||
+ | * **Method one**: Specify one model variant, and modify the variant according to block states, such as modifying x-rotation, y-rotation and uvlock. All block states use the same model id, with only possible different variants. | ||
+ | * **Method two**: Allocating model variants for different block states directly, and then you can also continue to modify variants. In this case, the block states may use different model ids. | ||
+ | |||
+ | Our vertical slab has two block state properties: '' | ||
+ | <code java TutorialModelGenerator.java> | ||
+ | @Override | ||
+ | public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) { | ||
+ | // ... | ||
+ | |||
+ | blockStateModelGenerator.blockStateCollector.accept( | ||
+ | VariantsBlockModelDefinitionCreator.of(TutorialBlocks.POLISHED_ANDESITE_VERTICAL_SLAB, | ||
+ | BlockStateModelGenerator.createWeightedVariant(verticalSlabModelId)) | ||
+ | .apply(BlockStateModelGenerator.UV_LOCK) | ||
+ | .coordinate(BlockStateVariantMap.operations(VerticalSlabBlock.FACING) | ||
+ | .register(Direction.NORTH, | ||
+ | .register(Direction.EAST, | ||
+ | .register(Direction.SOUTH, | ||
+ | .register(Direction.WEST, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | > Can we use method two? Of course yes! The code is the following: | ||
+ | > <code java TutorialModelGenerator.java> | ||
+ | blockStateModelGenerator.blockStateCollector.accept( | ||
+ | VariantsBlockModelDefinitionCreator.of( | ||
+ | TutorialBlocks.POLISHED_ANDESITE_VERTICAL_SLAB) | ||
+ | .with(BlockStateVariantMap.models(VerticalSlabBlock.FACING) | ||
+ | .register(Direction.NORTH, | ||
+ | .register(Direction.EAST, | ||
+ | .register(Direction.SOUTH, | ||
+ | .register(Direction.WEST, | ||
+ | ) | ||
+ | .apply(BlockStateModelGenerator.UV_LOCK) | ||
+ | ); | ||
+ | </ | ||
+ | > We find that in method two, when calling '' | ||
+ | |||
+ | In the method one and method two described above, apart from registering model operations or model variants one by one with the '' | ||
+ | |||
+ | If a block state has multiple properties that affect model variants, you can provide multiple properties in '' |
tutorial/datagen_model.1744986069.txt.gz · Last modified: 2025/04/18 14:21 by solidblock