tutorial:custom_model
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
tutorial:custom_model [2023/06/25 21:41] – fix typo (EntType -> EnvType) andrew6rant | tutorial:custom_model [2023/12/31 21:40] – [Sprites] Missed `int`s gudenau | ||
---|---|---|---|
Line 22: | 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 35: | 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 47: | 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 56: | 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 72: | Line 78: | ||
} | } | ||
</ | </ | ||
- | |||
- | Note that the type parameter "'' | ||
==== BakedModel methods ==== | ==== BakedModel methods ==== | ||
Line 81: | 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 106: | Line 110: | ||
@Override | @Override | ||
public Sprite getParticleSprite() { | public Sprite getParticleSprite() { | ||
- | return | + | return |
} | } | ||
Line 132: | Line 136: | ||
| | ||
// We just render the mesh | // We just render the mesh | ||
- | | + | |
} | } | ||
Line 141: | Line 145: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Note: Make sure you override the '' | ||
===== Registering the model ===== | ===== Registering the model ===== | ||
- | Let's first write a '' | + | In order for the model to be rendered in game we need to register |
- | + | ||
- | Let's register the model under the name '' | + | |
<code java> | <code java> | ||
@Environment(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
- | public class TutorialModelProvider implements ModelResourceProvider | + | public class TutorialModelLoadingPlugin impelements ModelLoadingPlugin |
- | 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(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
Line 167: | Line 177: | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
- | | + | |
- | + | ||
/* Other client-specific initialization */ | /* Other client-specific initialization */ | ||
} | } | ||
Line 228: | Line 238: | ||
@Override | @Override | ||
public void emitItemQuads(ItemStack itemStack, Supplier< | public void emitItemQuads(ItemStack itemStack, Supplier< | ||
- | | + | |
} | } | ||
</ | </ | ||
Line 238: | Line 248: | ||
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