Jump to content

[1.18.1] Unsure how to approach rendering a vanilla Block/Blockmodel in a Screen


lukebickell

Recommended Posts

For my mod, I want to render a block in a custom GUI screen, similar to how the player model is rendered in the vanilla inventory. 

I've looked at the InventoryScreen code, but that isn't too helpful, since it's rendering an Entity, not a Block.

I've guessed that I can maybe use `BlockRendererDispatcher.renderSingleBlock()`, but haven't made much headway, and can't find any examples of this after a couple hours of googling.

 

Here's what I have, which doesn't crash or anything, but nothing shows up in the Screen when this is called:

PoseStack poseStack1 = RenderSystem.getModelViewStack();

BlockState blockState = new BlockState(Blocks.STONE, null, null);

MultiBufferSource.BufferSource bufferSource = MultiBufferSource.immediate(new BufferBuilder(2097152));

Minecraft.getInstance().getBlockRenderer().renderSingleBlock(blockState, poseStack1, bufferSource, 15728880, OverlayTexture.NO_OVERLAY, EmptyModelData.INSTANCE);


There are a few specific things I have questions on:
1. Is this the correct approach for rendering a block (vanilla stone) in a GUI screen?

2. Do I need either of the other two parameters of the BlockState?

3. Do I need to fetch the BufferSource from somewhere else, or does instantiating a new one like above work?

 

I know there's probably a LOT of concepts I don't understand here about rendering (just started learning a couple of days ago), but ANY help is appreciated here!

Edited by lukebickell
fix code block
Link to comment
Share on other sites

With some help from some discord folks and some finagling, I got a 3d block rendering in a GUI! :)

 

        ItemStack item = new ItemStack(Blocks.DIAMOND_BLOCK);

        RenderSystem.enableBlend();
        RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        PoseStack poseStack = RenderSystem.getModelViewStack();
        poseStack.pushPose();
        poseStack.translate(150, 150, (100.0F));
        poseStack.translate(8.0D, 8.0D, 0.0D);
        poseStack.scale(1.0F, -1.0F, 1.0F);
        poseStack.scale(16.0F, 16.0F, 16.0F);
        RenderSystem.applyModelViewMatrix();

        float angle = (System.currentTimeMillis() / 100) % 360;
        Quaternion rotation = Vector3f.YP.rotationDegrees(angle);
        PoseStack blockPoseStack = new PoseStack();
        blockPoseStack.pushPose();
        blockPoseStack.mulPose(rotation);
        blockPoseStack.scale(8, 8, 8);
        MultiBufferSource.BufferSource bufferSource = Minecraft.getInstance().renderBuffers().bufferSource();

        Minecraft.getInstance().getItemRenderer().renderStatic(item, ItemTransforms.TransformType.FIXED, 0, 0, blockPoseStack, bufferSource, 0);
        bufferSource.endBatch();

        poseStack.popPose();
        RenderSystem.applyModelViewMatrix();

A lot of the setup code was taken from the `ItemRenderer.renderGuiItem()` function, so I'm not really sure what's necessary and what isn't.

 

Only problem now is that the block appears very dark. I've tried using `Lighting.setupFor3DItems()`, but that hasn't seemed to do anything :(

Link to comment
Share on other sites

At last, figured it out;

I was missing two parameters in the renderStatic method:
 

Minecraft.getInstance().getItemRenderer().renderStatic(item, ItemTransforms.TransformType.FIXED, 0, 0, blockPoseStack, bufferSource, 0);

to

Minecraft.getInstance().getItemRenderer().renderStatic(item, ItemTransforms.TransformType.FIXED, 15728880, OverlayTexture.NO_OVERLAY, blockPoseStack, bufferSource, 0);

 

I have no idea what the 15728880 value is, which is probably why I didn't understand why it was necessary.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.