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
tutorial:custom_model [2024/04/27 08:58] – typo florenstutorial:custom_model [2024/08/27 04:33] (current) solidblock
Line 15: Line 15:
  
 ==== Sprites ==== ==== Sprites ====
-A ''Sprite'' is necessary for rendering a texture. We must first create a ''SpriteIdentifier'' and then get the corresponding ''Sprite'' while baking the model. +A ''Sprite'' is necessary for rendering a texture. We must first create a ''SpriteIdentifier'' and then get the corresponding ''Sprite'' while baking the model. Here, we will use two furnace textures. They are block textures, so they must be loaded from the block atlas ''PlayerScreenHandler.BLOCK_ATLAS_TEXTURE''.
-Here, we will use two furnace textures. They are block textures, so they must be loaded from the block atlas ''SpriteAtlasTexture.BLOCK_ATLAS_TEX''.+
 <code java> <code java>
 +    // for versions before 1.21, replace `Identifier.ofVanilla` with `new Identifier`.
     private static final SpriteIdentifier[] SPRITE_IDS = new SpriteIdentifier[]{     private static final SpriteIdentifier[] SPRITE_IDS = new SpriteIdentifier[]{
-            new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier("minecraft:block/furnace_front_on")), +            new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, Identifier.ofVanilla("block/furnace_front_on")), 
-            new SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, new Identifier("minecraft:block/furnace_top"))+            new SpriteIdentifier(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, Identifier.ofVanilla("block/furnace_top"))
     };     };
     private final Sprite[] sprites = new Sprite[SPRITE_IDS.length];     private final Sprite[] sprites = new Sprite[SPRITE_IDS.length];
Line 50: Line 50:
  
     @Override     @Override
-    public BakedModel bake(ModelLoader loader, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) {+    public BakedModel bake(Baker baker, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings rotationContainer) {
         // Get the sprites         // Get the sprites
         for(int i = 0; i < SPRITE_IDS.length; ++i) {         for(int i = 0; i < SPRITE_IDS.length; ++i) {
Line 153: Line 153:
 @Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
 public class TutorialModelLoadingPlugin implements ModelLoadingPlugin { public class TutorialModelLoadingPlugin implements ModelLoadingPlugin {
-    public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL = new ModelIdentifier("tutorial", "four_sided_furnace", "");+    public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL = new ModelIdentifier(Identifier.of("tutorial", "four_sided_furnace"), "");
  
     @Override     @Override
Line 160: Line 160:
         pluginContext.modifyModelOnLoad().register((original, context) -> {         pluginContext.modifyModelOnLoad().register((original, context) -> {
             // This is called for every model that is loaded, so make sure we only target ours             // This is called for every model that is loaded, so make sure we only target ours
-            if(context.id().equals(FOUR_SIDED_FURNACE_MODEL)) {+            final ModelIdentifier id = context.topLevelId()
 +            if(id != null && id.equals(FOUR_SIDED_FURNACE_MODEL)) {
                 return new FourSidedFurnaceModel();                 return new FourSidedFurnaceModel();
             } else {             } else {
Line 185: Line 186:
  
 Don't forget to register this entrypoint in ''fabric.mod.json'' if you haven't done it yet: Don't forget to register this entrypoint in ''fabric.mod.json'' if you haven't done it yet:
-<code json+<code javascript
-/* ... */+
 +  [...]
   "entrypoints": {   "entrypoints": {
-    /* ... */+    [...]
     "client": [     "client": [
       "net.fabricmc.example.ExampleModClient"       "net.fabricmc.example.ExampleModClient"
     ]     ]
   },   },
 +  [...]
 +}
 </code> </code>
  
 ===== Using the model ===== ===== 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 [[blocks|register your block]] to use your new model. We assume this block is with id ''tutorial:four_sided_furnace''. 
-<code json>+ 
 +<code java TutorialBlocks.java> 
 +public final class TutorialBlocks { 
 +  [...] 
 +  public static final Block FOUR_SIDED_FURNACE = register("four_sided_furnace", new Block(AbstractBlock.Settings.copy(Blocks.FURNACE).luminance(x -> 15))); 
 +  [...] 
 +
 +</code> 
 +<code javascript src/main/resources/assets/tutorial/blockstates/four_sided_furnace.json>
 { {
   "variants": {   "variants": {
Line 243: Line 255:
  
 ==== Loading the model ==== ==== Loading the model ====
-Let's update the ''ModelResourceProvider'' we created earlier:+Let's update the ''TutorialModelLoadingPlugin'' we created earlier:
 <code java> <code java>
 +
 @Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
-public class TutorialModelProvider implements ModelResourceProvider +public class TutorialModelLoadingPlugin implements ModelLoadingPlugin 
-    public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); +  public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL = new ModelIdentifier(Identifier.of("tutorial", "four_sided_furnace"), ""); 
-    public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL_BLOCK = new ModelIdentifier("tutorial", "four_sided_furnace", ""); +  public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL_ITEM = new ModelIdentifier(Identifier.of("tutorial", "four_sided_furnace"), "inventory");
-    public static final ModelIdentifier FOUR_SIDED_FURNACE_MODEL_ITEM = new ModelIdentifier("tutorial", "four_sided_furnace", "inventory");+
  
-    @Override +  @Override 
-    public UnbakedModel loadModelResource(Identifier identifierModelProviderContext modelProviderContextthrows ModelProviderException +  public void onInitializeModelLoader(Context pluginContext) { 
-        if(identifier.equals(FOUR_SIDED_FURNACE_MODEL_BLOCK) || identifier.equals(FOUR_SIDED_FURNACE_MODEL_ITEM)) { +    // We want to add our model when the models are loaded 
-            return FOUR_SIDED_FURNACE_MODEL+    pluginContext.modifyModelOnLoad().register((originalcontext-> 
-        } else { +      // This is called for every model that is loaded, so make sure we only target ours 
-            return null; +      final ModelIdentifier id = context.topLevelId(); 
-        } +      if (id != null && (id.equals(FOUR_SIDED_FURNACE_MODEL) || id.equals(FOUR_SIDED_FURNACE_MODEL_ITEM))) { 
-    }+        return new FourSidedFurnaceModel()
 +      } else { 
 +        // If we don't modify the model we just return the original as-is 
 +        return original; 
 +      
 +    }); 
 +  }
 } }
 </code> </code>
tutorial/custom_model.1714208315.txt.gz · Last modified: 2024/04/27 08:58 by florens