Skip to content

Instantly share code, notes, and snippets.

@RainWarrior
Last active June 17, 2016 07:15
Show Gist options
  • Save RainWarrior/9573985 to your computer and use it in GitHub Desktop.
Save RainWarrior/9573985 to your computer and use it in GitHub Desktop.
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.client.registry.RenderingRegistry;
import net.minecraftforge.client.model.obj.WavefrontObject;
import net.minecraft.block.Block;
import net.minecraft.world.IBlockAccess;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
public class BlockRenderer implements ISimpleBlockRenderingHandler
{
private final int renderId;
private final Tessellator tes = Tessellator.instance;
private final WavefrontObject model;
public BlockRenderer(WavefrontObject model)
{
renderId = RenderingRegistry.getNextAvailableRenderId();
this.model = model;
}
@Override
public void renderInventoryBlock(Block block, int metadata, int modelId, RenderBlocks renderer)
{
RenderHelper.disableStandardItemLighting();
tes.startDrawingQuads();
tes.setColorOpaque_F(1, 1, 1);
Util.renderWithIcon(model, block.getIcon(0, metadata), tes);
tes.draw();
RenderHelper.enableStandardItemLighting();
}
@Override
public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer)
{
tes.setBrightness(block.getMixedBrightnessForBlock(world, x, y, z));
tes.setColorOpaque_F(1, 1, 1);
tes.addTranslation(x + .5F, y + .5F, z + .5F);
Util.renderWithIcon(model, block.getIcon(0, world.getBlockMetadata(x, y, z)), tes);
tes.addTranslation(-x - .5F, -y - .5F, -z - .5F);
return true;
}
@Override
public boolean shouldRender3DInInventory(int modelId)
{
return true;
}
@Override
public int getRenderId()
{
return renderId;
}
}
WavefrontObject model = (WavefrontObject)AdvancedModelLoader.loadModel(new ResourceLocation(modId.toLowerCase() + ":models/" + modelname + ".obj"));
BlockRenderer blockRenderer = new BlockRenderer(model);
RenderingRegistry.registerBlockHandler(blockRenderer);
YourBlock.setRenderId(blockRenderer.getRenderId());
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.util.IIcon;
import net.minecraftforge.client.model.obj.WavefrontObject;
import net.minecraftforge.client.model.obj.GroupObject;
import net.minecraftforge.client.model.obj.Face;
import net.minecraftforge.client.model.obj.Vertex;
import net.minecraftforge.client.model.obj.TextureCoordinate;
public class Util {
public static void renderWithIcon(WavefrontObject model, IIcon icon, Tessellator tes)
{
for(GroupObject go : model.groupObjects)
{
for(Face f : go.faces) {
Vertex n = f.faceNormal;
tes.setNormal(n.x, n.y, n.z);
for(int i = 0; i < f.vertices.length; i++)
{
Vertex v = f.vertices[i];
TextureCoordinate t = f.textureCoordinates[i];
tes.addVertexWithUV(v.x, v.y, v.z,
icon.getInterpolatedU(t.u * 16),
icon.getInterpolatedV(t.v * 16));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment