Search code examples
minecraft-forgeopengl-1.x

Minecraft transparent render hides blocks


I'm lost why does my texture renders as expected when I look it from south or east but hides the objects behind them when looking from north or west.

I have an invisible block that renders multiple items inside it and having trouble with a block that has a semi-transparent texture. Have tried to toggle all the block properties (eg render type, layer, opaque) for both the base and texture blocks and tried different blending options on render.

Forge version 1.12

Normal view Normal view from south Broken view Broken view from north Renderer

public class BlueRenderer extends TileEntitySpecialRenderer<TileEntityBlue> {

    @Override
    public void render(TileEntityBlue tile, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
        itemRenderer = mc.getRenderItem();
        textureManager = mc.getTextureManager();
        blockModelShapes = mc.getBlockRendererDispatcher().getBlockModelShapes();

        GlStateManager.pushAttrib();
        GlStateManager.pushMatrix();

        GlStateManager.translate(x, y, z);
        GlStateManager.disableRescaleNormal();

        renderItem(new GetBlock("minecraft:jukebox"), 0.5F);
        renderItem(new GetBlock("mymod:blue"), 1F);

        GlStateManager.popMatrix();
        GlStateManager.popAttrib();
    }

    private void renderItem(GetBlock block, float scale) {

        RenderHelper.enableStandardItemLighting();
        GlStateManager.enableLighting();
        GlStateManager.pushMatrix();
        GlStateManager.translate(0.5, 0.5, 0.5);
        GlStateManager.scale(scale, scale, scale);

        IBakedModel model = blockModelShapes.getModelForState(block.state);
        model = ForgeHooksClient.handleCameraTransforms(model, ItemCameraTransforms.TransformType.NONE, false);

        GlStateManager.enableBlend();
        GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        textureManager.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
        itemRenderer.renderItem(block.stack, model);

        GlStateManager.disableBlend();

        GlStateManager.popMatrix();
    }

Solution

  • After reading up on transparency - I had depth mask on and missing alpha functions.

        GlStateManager.enableBlend();
        GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
        if (transparent) {
            GlStateManager.depthMask(false);
            GlStateManager.alphaFunc(GL11.GL_LESS, 1.0F);
            itemRenderer.renderItem(block.stack, model);
            GlStateManager.depthMask(true);
        } else {
            GlStateManager.alphaFunc(GL11.GL_EQUAL, 1.0F);
            itemRenderer.renderItem(block.stack, model);
        }
        GlStateManager.disableBlend();
    

    Thing to keep in mind:

    Render transparent items last as otherwise they will not appear correctly "as inside".

        renderItem(new GetBlock("minecraft:jukebox"), 0.5F, false );
        renderItem(new GetBlock("mymod:blue"), 1F, true /*transparent*/);
    

    Solid item also needed blend function GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); as the very first solid item on the stack would glitch on certain angles:

    enter image description here