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/12 20:26] – [custom model] Add a clarification regarding transitive texture dependencies technici4ntutorial:custom_model [2023/06/25 21:41] – fix typo (EntType -> EnvType) andrew6rant
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 10: Line 10:
  
 <code java> <code java>
 +@Environment(EnvType.CLIENT)
 public class FourSidedFurnaceModel implements UnbakedModel, BakedModel, FabricBakedModel { public class FourSidedFurnaceModel implements UnbakedModel, BakedModel, FabricBakedModel {
 </code> </code>
Line 18: 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("minecraft:block/furnace_front_on")), +            new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier("minecraft:block/furnace_front_on")), 
-            new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEX, new Identifier("minecraft:block/furnace_top"))+            new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier("minecraft:block/furnace_top"))
     };     };
     private Sprite[] SPRITES = new Sprite[2];     private Sprite[] SPRITES = new Sprite[2];
Line 71: Line 72:
     }     }
 </code> </code>
 +
 +Note that the type parameter "''Pair''" in ''getTextureDependencies'' method is ''com.mojang.datafixers.util.Pair'' instead of ''net.minecraft.util.Pair''.
  
 ==== BakedModel methods ==== ==== BakedModel methods ====
-The methods here are not used by the Fabric Renderer, so we don't really care about the implementation.+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+    @Override
     public List<BakedQuad> getQuads(BlockState state, Direction face, Random random) {     public List<BakedQuad> getQuads(BlockState state, Direction face, Random random) {
-        return null; // Don't need because we use FabricBakedModel instead+        // 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 false; // Again, we don't really care, etc...+        return true; // we want the block to have a shadow depending on the adjacent blocks
     }     }
  
     @Override     @Override
-    public boolean hasDepth() {+    public boolean isBuiltin() {
         return false;         return false;
     }     }
  
     @Override     @Override
-    public boolean isSideLit() {+    public boolean hasDepth() {
         return false;         return false;
     }     }
  
     @Override     @Override
-    public boolean isBuiltin() {+    public boolean isSideLit() {
         return false;         return false;
     }     }
  
     @Override     @Override
-    public Sprite getSprite() {+    public Sprite getParticleSprite() {
         return SPRITES[1]; // Block break particle, let's use furnace_top         return SPRITES[1]; // Block break particle, let's use furnace_top
     }     }
Line 143: Line 147:
 Let's register the model under the name ''tutorial:block/four_sided_furnace''. Let's register the model under the name ''tutorial:block/four_sided_furnace''.
 <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("tutorial:block/four_sided_furnace");     public static final Identifier FOUR_SIDED_FURNACE_MODEL = new Identifier("tutorial:block/four_sided_furnace");
Line 158: Line 163:
 Now we have to register this class in the client initializer, the entry point for client-specific code. Now we have to register this class in the client initializer, the entry point for client-specific code.
 <code java> <code java>
 +@Environment(EnvType.CLIENT)
 public class ExampleModClient implements ClientModInitializer { public class ExampleModClient implements ClientModInitializer {
     @Override     @Override
Line 174: Line 180:
     /* ... */     /* ... */
     "client": [     "client": [
-      "tutorial.path.to.ExampleModClient"+      "net.fabricmc.example.ExampleModClient"
     ]     ]
   },   },
 </code> </code>
  
-===== Wrapping up =====+===== 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 ''assets/your_mod_id/blockstates/your_block_id.json''. You can now register your block to use your new model. For example, if your block only has one block state, put this in ''assets/your_mod_id/blockstates/your_block_id.json''.
 <code json> <code json>
Line 193: Line 199:
 {{:tutorial:four_sided_furnace_render.png?nolink&600|}} {{:tutorial:four_sided_furnace_render.png?nolink&600|}}
  
 +===== 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 ''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 can use the transform provided by fabric in ''ModelHelper.MODEL_TRANSFORM_BLOCK''.
 +
 +We will update our ''FourSidedFurnaceModel'' class as follows:
 +<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<Random> supplier, RenderContext renderContext) {
 +        renderContext.meshConsumer().accept(mesh);
 +    }
 +</code>
 +
 +==== Loading the model ====
 +Let's update the ''ModelResourceProvider'' we created earlier:
 +<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("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