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/13 23:19] – [custom model] Formatting 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. | ||
Line 10: | Line 10: | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class FourSidedFurnaceModel implements UnbakedModel, | public class FourSidedFurnaceModel implements UnbakedModel, | ||
</ | </ | ||
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(" | + | 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 71: | Line 72: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Note that the type parameter "'' | ||
==== 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 | + | |
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 143: | Line 147: | ||
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 158: | Line 163: | ||
Now we have to register this class in the client initializer, | Now we have to register this class in the client initializer, | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class ExampleModClient implements ClientModInitializer { | public class ExampleModClient implements ClientModInitializer { | ||
@Override | @Override | ||
Line 174: | Line 180: | ||
/* ... */ | /* ... */ | ||
" | " | ||
- | "tutorial.path.to.ExampleModClient" | + | "net.fabricmc.example.ExampleModClient" |
] | ] | ||
}, | }, | ||
Line 197: | Line 203: | ||
==== 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 a 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 '' |
- | * We will need a '' | + | |
We will update our '' | We will update our '' | ||
<code java> | <code java> | ||
- | // The minecraft default block model | ||
- | private static final Identifier DEFAULT_BLOCK_MODEL = new Identifier(" | ||
- | |||
- | private boolean isBaked = false; | ||
- | private ModelTransformation transformation; | ||
- | | ||
- | // We need to add the default model to the dependencies | ||
- | public Collection< | ||
- | 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< | ||
- | // Don't bake twice | ||
- | if(isBaked) return this; | ||
- | isBaked = true; | ||
- | | ||
- | // 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() | // We need to implement getTransformation() and getOverrides() | ||
@Override | @Override | ||
public ModelTransformation getTransformation() { | public ModelTransformation getTransformation() { | ||
- | return | + | return |
} | } | ||
Line 256: | Line 235: | ||
Let's update the '' | Let's update the '' | ||
<code java> | <code java> | ||
+ | @Environment(EnvType.CLIENT) | ||
public class TutorialModelProvider implements ModelResourceProvider { | public class TutorialModelProvider implements ModelResourceProvider { | ||
public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); | public static final FourSidedFurnaceModel FOUR_SIDED_FURNACE_MODEL = new FourSidedFurnaceModel(); | ||
Line 270: | Line 250: | ||
} | } | ||
} | } | ||
- | </ | + | </ |
===== Final result ===== | ===== Final result ===== | ||
Line 276: | Line 256: | ||
Et voilà! Enjoy! | 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