Jump to content

[1.18.2] Rendering a block model clientside only


SoLegendary

Recommended Posts

I want to use BlockRendererDispatcher.renderSingleBlock() to render a block model in the world clientside only so that the player can see where they're about to place a structure without actually placing it.

I have the BlockPos and BlockState of each block I want to render this way, but I don't know how to get the MultiBufferSource and the IModelData.

In fact, renderSingleBlock() doesn't even accept any BlockPos as a parameter so how does it know where to render it?

Link to comment
Share on other sites

Ok so I managed to figure out the parameters, but the question of position remains, and my current code is just rendering all of the blocks on top of each other at the centre of my screen (it should be rendering that house).

Ok solved it - just forgot to push, translate and pop the matrix - I'll leave my final code below to anyone who wants to know how to do the same thing:

My current code (BuildingBlock is my custom class):

for (BuildingBlock block : blocksToPlace) {
  BlockRenderDispatcher renderer = MC.getBlockRenderer();
  BlockState bs = block.getBlockState;
  BlockPos bp = block.blockPos;
  IModelData modelData = renderer.getBlockModel(bs).getModelData(MC.level, bp, bs, ModelDataManager.getModelData(MC.level, bp));
  matrix.pushPose();
  matrix.translate(
    bp.getX() - originPos.getX(),
    bp.getY() - originPos.getY(),
    bp.getZ() - originPos.getZ());
  renderer.renderSingleBlock(
    bs, matrix,
    MC.renderBuffers().crumblingBufferSource(),
    15728880,
    OverlayTexture.NO_OVERLAY,
    modelData);
  matrix.popPose();
}

 

Edited by SoLegendary
Link to comment
Share on other sites

originPos is just the corner of the structure I'm placing, because the structure only has relative BlockPos data starting at (0,0) - this can be wherever you want it to be, whether its the blocks the player is looking at or something else.

In my case im building a top down gamemode, so its whatever my mouse cursor is over.

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.