User Tools

Site Tools


tutorial:custom_model

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:custom_model [2020/08/13 22:46] – [custom model] Begin item rendering subsection technici4ntutorial:custom_model [2020/09/09 15:57] – [custom model] Fix resource reloading "bug" technici4n
Line 1: Line 1:
-====== Creating a custom block model (DRAFT) ======+====== Rendering Blocks and Items Dynamically using a custom Model ======
 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.
  
Line 195: Line 195:
 ===== Rendering the item ===== ===== Rendering the item =====
 As you can see in the picture, the item is not rendered correctly. Let's fix this. As you can see in the picture, the item is not rendered correctly. Let's fix this.
-TODO+ 
 +==== Updating the model ==== 
 +We will re-use the same model class, with just a small change: 
 +  * We will need a ''ModelTransformation'' that rotates/translates/scales the model depending on its position (in right hand, in left hand, in gui, in item frame, etc...). As we are creating a model for a regular block, we will use the one from "minecraft:block/block" which we will load during model baking. 
 + 
 +We will update our ''FourSidedFurnaceModel'' class as follows: 
 +<code java> 
 +    // The minecraft default block model 
 +    private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier("minecraft:block/block"); 
 + 
 +    private ModelTransformation transformation; 
 +     
 +    // We need to add the default model to the dependencies 
 +    public Collection<Identifier> getModelDependencies() { 
 +        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<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) { 
 +        // 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() 
 +    @Override 
 +    public ModelTransformation getTransformation() { 
 +        return transformation; 
 +    } 
 + 
 +    @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<Random> supplier, RenderContext renderContext) { 
 +        renderContext.meshConsumer().accept(mesh); 
 +    } 
 +</code> 
 + 
 +==== Loading the model ==== 
 +Let's update the ''ModelResourceProvider'' we created earlier: 
 +<code java> 
 +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("tutorial:block/four_sided_furnace"); 
 +    public static final Identifier FOUR_SIDED_FURNACE_MODEL_ITEM = new Identifier("tutorial:item/four_sided_furnace"); 
 + 
 +    @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; 
 +        } 
 +    } 
 +
 +</code> 
 + 
 +===== Final result ===== 
 +{{:tutorial:four_sided_furnace_render_final.png?nolink&600|}} 
 + 
 +Et voilà! Enjoy! 
 + 
 +===== More dynamic rendering ===== 
 +The ''renderContext'' parameter in ''emitBlockQuads'' and ''emitItemQuads'' contains a ''QuadEmitter'' which you can use to build a model on the fly. 
 +<code java> 
 +    @Override 
 +    public void emitBlockQuads(BlockRenderView blockRenderView, BlockState blockState, BlockPos blockPos, Supplier<Random> supplier, RenderContext renderContext) { 
 +        QuadEmitter emitter = renderContext.getEmitter(); 
 +        /* With this emitter, you can directly append the quads to the chunk model. */ 
 +    } 
 +</code>
tutorial/custom_model.txt · Last modified: 2024/04/27 08:58 by florens