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 23:21] technici4ntutorial:custom_model [2020/09/09 15:57] – [custom model] Fix resource reloading "bug" technici4n
Line 1: Line 1:
-====== Creating a custom block model ======+====== 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 197: Line 197:
  
 ==== Updating the model ==== ==== Updating the model ====
-We will re-use the same model class, with a few changes: +We will re-use the same model class, with just small change:
-  * We will register the same model instance under a different name, so we'll make sure the model is only baked once.+
   * 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 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.
  
Line 206: Line 205:
     private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier("minecraft:block/block");     private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier("minecraft:block/block");
  
-    private boolean isBaked = false; 
     private ModelTransformation transformation;     private ModelTransformation transformation;
          
Line 217: Line 215:
     @Override     @Override
     public BakedModel bake(ModelLoader loader, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) {     public BakedModel bake(ModelLoader loader, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) {
-        // Don't bake twice 
-        if(isBaked) return this; 
-        isBaked = true; 
-         
         // Load the default block model         // Load the default block model
         JsonUnbakedModel defaultBlockModel = (JsonUnbakedModel) loader.getOrLoadModel(DEFAULT_BLOCK_MODEL);         JsonUnbakedModel defaultBlockModel = (JsonUnbakedModel) loader.getOrLoadModel(DEFAULT_BLOCK_MODEL);
Line 276: Line 270:
  
 Et voilà! Enjoy! 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: 2023/12/31 21:40 by gudenau