tutorial:custom_model
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:custom_model [2020/08/12 18:29] – Add a custom model tutorial technici4n | tutorial:custom_model [2023/06/25 21:41] – fix typo (EntType -> EnvType) andrew6rant | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
It is possible to add models to the game using block model JSON files, but it is also possible to render them through Java code. In this tutorial, we will add a four-sided furnace model to the game. | It is possible to add models to the game using block model JSON files, but it is also possible to render them through Java code. In this tutorial, we will add a four-sided furnace model to the game. | ||
+ | |||
+ | Note that models are rendered when the chunks are rebuilt. If you need more dynamic rendering, you can use a '' | ||
===== Creating the model ===== | ===== Creating the model ===== | ||
Line 8: | Line 10: | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class FourSidedFurnaceModel implements UnbakedModel, | public class FourSidedFurnaceModel implements UnbakedModel, | ||
</ | </ | ||
Line 16: | Line 19: | ||
<code java> | <code java> | ||
private static final SpriteIdentifier[] SPRITE_IDS = new SpriteIdentifier[]{ | private static final SpriteIdentifier[] SPRITE_IDS = new SpriteIdentifier[]{ | ||
- | new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEX, new Identifier(" | + | new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier(" |
- | new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEX, new Identifier(" | + | new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier(" |
}; | }; | ||
private Sprite[] SPRITES = new Sprite[2]; | private Sprite[] SPRITES = new Sprite[2]; | ||
Line 28: | Line 31: | ||
</ | </ | ||
- | ==== '' | + | ==== UnbakedModel methods ==== |
<code java> | <code java> | ||
@Override | @Override | ||
Line 37: | Line 40: | ||
@Override | @Override | ||
public Collection< | public Collection< | ||
- | return Arrays.asList(SPRITE_IDS); | + | return Arrays.asList(SPRITE_IDS); |
} | } | ||
Line 47: | Line 50: | ||
SPRITES[i] = textureGetter.apply(SPRITE_IDS[i]); | SPRITES[i] = textureGetter.apply(SPRITE_IDS[i]); | ||
} | } | ||
- | // Build the mesh | + | // Build the mesh using the Renderer API |
Renderer renderer = RendererAccess.INSTANCE.getRenderer(); | Renderer renderer = RendererAccess.INSTANCE.getRenderer(); | ||
MeshBuilder builder = renderer.meshBuilder(); | MeshBuilder builder = renderer.meshBuilder(); | ||
Line 56: | Line 59: | ||
// 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 calld after .square() | + | // Set the sprite of the face, must be called |
- | emitter.spriteBake(0, | + | // We haven' |
+ | emitter.spriteBake(0, | ||
// Enable texture usage | // Enable texture usage | ||
emitter.spriteColor(0, | emitter.spriteColor(0, | ||
+ | // Add the quad to the mesh | ||
emitter.emit(); | emitter.emit(); | ||
} | } | ||
Line 68: | Line 73: | ||
</ | </ | ||
- | ==== '' | + | Note that the type parameter "'' |
- | TODO: check this | + | |
- | The methods here are not used by the Fabric Renderer, so we don't really care about the implementation. | + | ==== BakedModel methods ==== |
+ | Not all the methods here are used by the Fabric Renderer, so we don't really care about the implementation. | ||
<code java> | <code java> | ||
- | @Override | + | |
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. |
+ | return Collections.emptyList(); | ||
} | } | ||
@Override | @Override | ||
public boolean useAmbientOcclusion() { | public boolean useAmbientOcclusion() { | ||
- | return | + | return |
} | } | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
return false; | return false; | ||
} | } | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
return false; | return false; | ||
} | } | ||
@Override | @Override | ||
- | public boolean | + | public boolean |
return false; | return false; | ||
} | } | ||
@Override | @Override | ||
- | public Sprite | + | public Sprite |
return SPRITES[1]; // Block break particle, let's use furnace_top | return SPRITES[1]; // Block break particle, let's use furnace_top | ||
} | } | ||
Line 113: | Line 120: | ||
</ | </ | ||
- | ==== '' | + | ==== FabricBakedModel methods ==== |
<code java> | <code java> | ||
@Override | @Override | ||
Line 136: | Line 143: | ||
===== Registering the model ===== | ===== Registering the model ===== | ||
- | Let's first register | + | Let's first write a '' |
- | < | + | |
- | ModelLoadingRegistry.INSTANCE.registerResourceProvider(rm -> new TutorialModelProvider()); | + | |
- | </ | + | |
- | Let's register the model under the name '' | + | Let's register the model under the name '' |
<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 161: | ||
</ | </ | ||
- | ===== Wrapping up ===== | + | Now we have to register this class in the client initializer, |
- | You can now register your block to use our new model, for example if your block only has one block state, put this in `assets/ | + | <code java> |
+ | @Environment(EnvType.CLIENT) | ||
+ | public class ExampleModClient implements ClientModInitializer { | ||
+ | @Override | ||
+ | public void onInitializeClient() { | ||
+ | ModelLoadingRegistry.INSTANCE.registerResourceProvider(rm -> new TutorialModelProvider()); | ||
+ | |||
+ | /* Other client-specific initialization */ | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Don't forget to register this entrypoint in '' | ||
+ | <code json> | ||
+ | /* ... */ | ||
+ | " | ||
+ | /* ... */ | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | }, | ||
+ | </ | ||
+ | |||
+ | ===== Using the model ===== | ||
+ | You can now register your block to use your new model. For example, if your block only has one block state, put this in '' | ||
<code json> | <code json> | ||
{ | { | ||
Line 164: | Line 193: | ||
} | } | ||
} | } | ||
+ | </ | ||
- | Of course, you can implement much more complex | + | Of course, you can implement much more complex |
+ | |||
+ | {{: | ||
+ | |||
+ | ===== Rendering the item ===== | ||
+ | As you can see in the picture, the item is not rendered correctly. Let's fix this. | ||
+ | |||
+ | ==== Updating the model ==== | ||
+ | We will re-use the same model class, with just a small change: | ||
+ | * We will need a '' | ||
+ | |||
+ | We will update our '' | ||
+ | <code java> | ||
+ | // We need to implement getTransformation() and getOverrides() | ||
+ | @Override | ||
+ | public ModelTransformation getTransformation() { | ||
+ | return ModelHelper.MODEL_TRANSFORM_BLOCK; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public ModelOverrideList getOverrides() { | ||
+ | return ModelOverrideList.EMPTY; | ||
+ | } | ||
+ | |||
+ | // We will also implement this method to have the correct lighting in the item rendering. Try to set this to false and you will see the difference. | ||
+ | @Override | ||
+ | public boolean isSideLit() { | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | // Finally, we can implement the item render function | ||
+ | @Override | ||
+ | public void emitItemQuads(ItemStack itemStack, Supplier< | ||
+ | renderContext.meshConsumer().accept(mesh); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Loading the model ==== | ||
+ | Let's update the '' | ||
+ | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
+ | public class TutorialModelProvider implements ModelResourceProvider { | ||
+ | public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); | ||
+ | public static final Identifier FOUR_SIDED_FURNACE_MODEL_BLOCK = new Identifier(" | ||
+ | public static final Identifier FOUR_SIDED_FURNACE_MODEL_ITEM = new Identifier(" | ||
+ | |||
+ | @Override | ||
+ | public UnbakedModel loadModelResource(Identifier identifier, ModelProviderContext modelProviderContext) throws ModelProviderException { | ||
+ | if(identifier.equals(FOUR_SIDED_FURNACE_MODEL_BLOCK) || identifier.equals(FOUR_SIDED_FURNACE_MODEL_ITEM)) { | ||
+ | return FOUR_SIDED_FURNACE_MODEL; | ||
+ | } else { | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Final result ===== | ||
+ | {{: | ||
+ | |||
+ | Et voilà! Enjoy! | ||
+ | |||
+ | ===== More dynamic rendering ===== | ||
+ | The '' | ||
+ | <code java> | ||
+ | @Override | ||
+ | public void emitBlockQuads(BlockRenderView blockRenderView, | ||
+ | QuadEmitter emitter = renderContext.getEmitter(); | ||
+ | /* With this emitter, you can directly append the quads to the chunk model. */ | ||
+ | } | ||
</ | </ |
tutorial/custom_model.txt · Last modified: 2024/04/27 08:58 by florens