Tutorial 15. Custom Model

Before you read, make sure you have seen the previous tutorial

Code is on github

So, in this tutorial, i will show you how to add a custom block model to the game.
When you watch this tutorial, i asume you already have a model and texture.
If you dont, go here to learn how to make models in Blender. The program is free, and very powerfull.

Now, you should already have a .Obj file (the model) and a texture file.
My model is this: Pastebin
I called this file Lamp.obj, and placed it in /resources/assets/mcmodtutorial/models.
My Texture (lamp.png):

This goes in resources/assets/mcmodtutorial/textures/models

We will use container we made last tutorial as block for this model.
I know, the purpose of the block doesnt fit the look, but it’s just for this tutorial :3

As you know, rendering is a client thing. Therefor, we need to register our renderers in our ClientPr0xy. Since ClientProxy extends CommonProxy, we need to edit that too.

And in ClientProxy, we override this. What we need to register, is a TileEntitySpecialRenderer (TESR). This takes 2 parameters, a TE class, and a renderer class. We will create the renderer class in a bit.

And ofcourse, in our main modfile, we call this.

So, lets start on the renderer.

We will need to create a class, which extends TileEntitySpecialRenderer. That class has stuff we need to render our model.

Cool, now we make 2 fields.
A resource location of our texture, and an IModelCustom.
The resource location needs 2 params, one is your modid, and one is your texture location.

In the constructor, we set our model location.

Last thing to do, is to render it.
Rendering is very hard, and covers a huge area. I will try to explain the most basic rendering.
For rendering, we use GL11.
GL11 has a special way of operating.
Firstly, we need to say that we’re gonna make a new ‘matrix’.
So, when we call  GL11.glPushMatrix();, we create a new matrix, which we fill next.
We ‘fill’ that matrix, with atleast 3 parts, and usually 4.
These parts are

  • glTranslated (Move ‘centre’ to a specific point)
  • glScalef (Scale the model)
  • glRotatef (Rotate the model)
  • bindTexture (Binding texture)

After you’ve done this, you NEED to pop the matrix.
So every PushMatrix, ends with a PopMatrix.

This all happens in the renderTileEnitityAt.
Binding textures happens with FMLClientHandler.instance().getClient().getTextureManager().bindTexture(texture);

So, finally, this is how the method looks like

So, thats the renderer.
Now, with rendering custom models come bugs.
To fix a few of the most common one, go to BlockInventoryContainer (the class that extends BlockContainer).

If your model isnt 1x1x1, you want to reset the bounding box.
You can do this in the constructor. For my model, i use this

So, thats the collision box.
Next, is the wellknown ‘see the entire world through a block’ bug.
Fixing this involves 1 method.

Because we have a custom model, we also need to override 2 more methods.

And finally, because i dont like the black pink particles, i disable all particles.


The Finished Source Code is on Github




Back to List

Next tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">