tutorial:custom_model
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:custom_model [2022/03/21 04:12] – [Registering the model] solidblock | tutorial:custom_model [2024/04/27 08:58] (current) – typo florens | ||
---|---|---|---|
Line 10: | Line 10: | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class FourSidedFurnaceModel implements UnbakedModel, | public class FourSidedFurnaceModel implements UnbakedModel, | ||
</ | </ | ||
Line 21: | Line 22: | ||
new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, | new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, | ||
}; | }; | ||
- | private Sprite[] | + | private |
+ | |||
+ | // 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 34: | Line 40: | ||
@Override | @Override | ||
public Collection< | public Collection< | ||
- | return | + | return |
} | } | ||
@Override | @Override | ||
- | public | + | public |
- | | + | // This is related to model parents, it's not required for our use case |
} | } | ||
Line 46: | Line 52: | ||
public BakedModel bake(ModelLoader loader, Function< | public BakedModel bake(ModelLoader loader, Function< | ||
// Get the sprites | // Get the sprites | ||
- | for(int i = 0; i < 2; ++i) { | + | for(int i = 0; i < SPRITE_IDS.length; ++i) { |
- | | + | |
} | } | ||
// Build the mesh using the Renderer API | // Build the mesh using the Renderer API | ||
Line 55: | Line 61: | ||
for(Direction direction : Direction.values()) { | for(Direction direction : Direction.values()) { | ||
- | int spriteIdx = direction == Direction.UP || direction == Direction.DOWN ? 1 : 0; | + | |
+ | | ||
// Add a new face to the mesh | // Add a new face to the mesh | ||
emitter.square(direction, | emitter.square(direction, | ||
// Set the sprite of the face, must be called after .square() | // Set the sprite of the face, must be called after .square() | ||
// We haven' | // We haven' | ||
- | emitter.spriteBake(0, SPRITES[spriteIdx], | + | emitter.spriteBake(sprites[spriteIdx], |
// Enable texture usage | // Enable texture usage | ||
- | emitter.spriteColor(0, -1, -1, -1, -1); | + | emitter.color(-1, -1, -1, -1); |
// Add the quad to the mesh | // Add the quad to the mesh | ||
emitter.emit(); | emitter.emit(); | ||
Line 71: | Line 78: | ||
} | } | ||
</ | </ | ||
- | |||
- | Note that the type parameter "'' | ||
==== BakedModel methods ==== | ==== BakedModel methods ==== | ||
Line 80: | Line 85: | ||
public List< | public List< | ||
// Don't need because we use FabricBakedModel instead. However, it's better to not return null in case some mod decides to call this function. | // Don't need because we use FabricBakedModel instead. However, it's better to not return null in case some mod decides to call this function. | ||
- | return | + | return |
} | } | ||
Line 105: | Line 110: | ||
@Override | @Override | ||
public Sprite getParticleSprite() { | public Sprite getParticleSprite() { | ||
- | return | + | return |
} | } | ||
Line 131: | Line 136: | ||
| | ||
// We just render the mesh | // We just render the mesh | ||
- | | + | |
} | } | ||
Line 141: | Line 146: | ||
</ | </ | ||
- | ===== Registering | + | Note: Make sure you override |
- | Let's first write a '' | + | |
- | Let's register | + | ===== Registering |
+ | In order for the model to be rendered in game we need to register it. In order to register it you need to create a '' | ||
<code java> | <code java> | ||
@Environment(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
- | public class TutorialModelProvider | + | public class TutorialModelLoadingPlugin |
- | public static final Identifier | + | public static final ModelIdentifier |
@Override | @Override | ||
- | public | + | public |
- | if(identifier.equals(FOUR_SIDED_FURNACE_MODEL)) { | + | |
- | return new FourSidedFurnaceModel(); | + | pluginContext.modifyModelOnLoad().register((original, |
- | } else { | + | // This is called for every model that is loaded, so make sure we only target ours |
- | return | + | |
- | } | + | return new FourSidedFurnaceModel(); |
+ | } else { | ||
+ | // If we don't modify the model we just return | ||
+ | return original; | ||
+ | } | ||
+ | }); | ||
} | } | ||
} | } | ||
</ | </ | ||
- | Now we have to register | + | Then you need to register the plugin we just created: |
<code java> | <code java> | ||
- | @Environment(EntType.CLIENT) | + | @Environment(EnvType.CLIENT) |
public class ExampleModClient implements ClientModInitializer { | public class ExampleModClient implements ClientModInitializer { | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
- | | + | |
- | + | ||
/* Other client-specific initialization */ | /* Other client-specific initialization */ | ||
} | } | ||
Line 203: | Line 214: | ||
==== Updating the model ==== | ==== Updating the model ==== | ||
We will re-use the same model class, with just a small change: | We will re-use the same model class, with just a small change: | ||
- | * We will need a '' | + | * We will need a '' |
We will update our '' | We will update our '' | ||
<code java> | <code java> | ||
- | // The minecraft default block model | ||
- | private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier(" | ||
- | |||
- | private ModelTransformation transformation; | ||
- | | ||
- | // We need to add the default model to the dependencies | ||
- | public Collection< | ||
- | return Arrays.asList(DEFAULT_BLOCK_MODEL); | ||
- | } | ||
- | | ||
- | // We need to add a bit of logic to the bake function | ||
- | @Override | ||
- | public BakedModel bake(ModelLoader loader, Function< | ||
- | // Load the default block model | ||
- | JsonUnbakedModel defaultBlockModel = (JsonUnbakedModel) loader.getOrLoadModel(DEFAULT_BLOCK_MODEL); | ||
- | // Get its ModelTransformation | ||
- | transformation = defaultBlockModel.getTransformations(); | ||
- | | ||
- | /* Previous code */ | ||
- | } | ||
- | | ||
// We need to implement getTransformation() and getOverrides() | // We need to implement getTransformation() and getOverrides() | ||
@Override | @Override | ||
public ModelTransformation getTransformation() { | public ModelTransformation getTransformation() { | ||
- | return | + | return |
} | } | ||
Line 248: | Line 238: | ||
@Override | @Override | ||
public void emitItemQuads(ItemStack itemStack, Supplier< | public void emitItemQuads(ItemStack itemStack, Supplier< | ||
- | | + | |
} | } | ||
</ | </ | ||
Line 255: | Line 245: | ||
Let's update the '' | Let's update the '' | ||
<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(); | ||
- | public static final Identifier | + | public static final ModelIdentifier |
- | public static final Identifier | + | public static final ModelIdentifier |
@Override | @Override |
tutorial/custom_model.txt · Last modified: 2024/04/27 08:58 by florens