Jump to content

[1.11.2] Rendering "ghost" blocks


Recommended Posts

Well hello there :D


I'm back with rendering-related questions. My main goal is to render my block that I'm about to place in the position it would be placed if I pressed right-click. Here's an example: say I'm looking in the distance, no block outlines are rendered. But, if I'm looking at e.g. dirt (which is near me), I would see it's bounding box (the black outline). If I were to place my block, it would be placed in front of that dirt block. What I want to do is to render my own block in that place (with a certain level of transparency), without actually placing it. I hope I explained it well enough for you to understand.

Link to comment
Share on other sites

You will need to use one of forge's render events, like RenderWorldLastEvent. You can get the block the player is looking at with the World::rayTraceBlocks method. I believe it's HitResult returns you the BlockPos of the block the player is looking at with EnumFacing of the side the player is looking at. Your rendering position would then be the raytraceresult position offset by that facing. To render a blockstate in the world you can look at RenderFallingBlock class. There might be some issues with transparency but in theory you can just enable blend with appropriate source and destination factors and either use gl's color method or manually override vertex color data in the vertexbuffer you are rendering your block with using VertexBuffer::putColor.

Just do not forget to properly offset your rendering, or you will be the third person in a row with the rendered object 'following' the player/not rendering :D


  • Like 1
Link to comment
Share on other sites

DrawBlockHighlightEvent would be more appropriate.

  • Like 2

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.


Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.


DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I did everything you said, but the only thing I get is the black bounding box that you look at don't get rendered, instead, gray lines that form triangles on the block's face renders. The block itself is never shown.

	public void highlightGhostBlock(DrawBlockHighlightEvent event)
		EntityPlayer player = event.getPlayer();
		BlockPos position = event.getTarget().getBlockPos().offset(player.getHorizontalFacing());
		ItemStack stack = player.getHeldItemMainhand();
		if (stack.getItem() instanceof BSMItemBlock)
			IBlockState stateToRender = ((BSMItemBlock)stack.getItem()).getDetails(player.world, position, player, 0, 0, 0, event.getTarget().sideHit).getSecondElement();
            //GlStateManager.alphaFunc(0, 0);
            Tessellator tessellator = Tessellator.getInstance();
            VertexBuffer vertexbuffer = tessellator.getBuffer();
            position.add(0.5, 0, 0.5);
            vertexbuffer.begin(7, DefaultVertexFormats.BLOCK);
            BlockPos blockpos = new BlockPos(position.getX(), position.getY(), position.getZ());
            GlStateManager.translate(position.getX(), position.getY(), position.getZ());
            BlockRendererDispatcher blockrendererdispatcher = Minecraft.getMinecraft().getBlockRendererDispatcher();
            blockrendererdispatcher.getBlockModelRenderer().renderModel(event.getPlayer().world, blockrendererdispatcher.getModelForState(stateToRender), stateToRender, blockpos, vertexbuffer, false, MathHelper.getPositionRandom(position));



Link to comment
Share on other sites

And... congratulations! You are the third person in a row who forgot about applying correct translations before rendering! :D



With correct translations applied I get the following from your code:



Also if you want your block to be transparent you need to setup GL's blend too, not only alpha :P

Edited by V0idWa1k3r
  • Like 2
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.

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.


  • Create New...

Important Information

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