Jump to content

[1.11.2] Rendering block bounding box


Crypnotic

Recommended Posts

I'm trying to render a block's bounding box as a solid color cube that can be seen through other blocks. I've searched for a few hours for a method that is both up to date and working but have had no luck so far. I haven't worked with 3D rendering before, so I am trying to use this as a basis to get started, so please forgive me if I ask too many questions. If you are unsure what it is I am trying to accomplish, try googling "chest finder" and looking at the images. (P.S. No, I am not making another hacked client) I don't wish to be spoonfed code, but I need help getting pushed in the right direction. Cheers!

Edited by Crypnotic
Link to comment
Share on other sites

You can render the outline in any of forge's rendering events. I would recommend RenderWorldLastEvent.

To make it render on top of everything just disable depth in the GlStateManager. Don't forget to re-enale it once you are done.

The TileEntityStructureRenderer can assist you with rendering the box itself - see how it is done there, in vanilla.

Once you piece it all together you can store the positions of the blocks you want to outline in any array and in the event iterate through that array, offset the gl matrix accordingly(again, look at how vanilla handles it, generic entity rendering is the example you need) and render your box. 

Link to comment
Share on other sites

52 minutes ago, V0idWa1k3r said:

You can render the outline in any of forge's rendering events. I would recommend RenderWorldLastEvent.

To make it render on top of everything just disable depth in the GlStateManager. Don't forget to re-enale it once you are done.

The TileEntityStructureRenderer can assist you with rendering the box itself - see how it is done there, in vanilla.

Once you piece it all together you can store the positions of the blocks you want to outline in any array and in the event iterate through that array, offset the gl matrix accordingly(again, look at how vanilla handles it, generic entity rendering is the example you need) and render your box. 

2

I did, afaik, everything you explained. It's rendering lines now, but they're rather... weird. It should be outlining the Andesite block, but it definitely is not.

2017-04-30_11.21.05.png

Link to comment
Share on other sites

You need to offset the vertexbuffer/gl matrix(whichever you are using) before rendering the lines at the desired position.

Think of it this way:

Every time MC renders the world it offsets everything based on the player's position.

The RenderWorldLast event is fired when those offsets are applied.

If you want your outline to be stationary and not follow the player you need to 'negate' them by offsetting your rendering back to where it was(0,0,0) and then render at the desired position(x,y,z).

So your rendering would then look like

calculate offsets

offset(-offsets)

render boxes loop

offset(offsets).

 

As I usually do, here is a simple example:

 

// your positions. You might want to shift them a bit too
int sX = yourX;
int sY = yourY;
int sZ = yourZ;
// Usually the player
Entity entity = Minecraft.getMinecraft().getRenderViewEntity();
//Interpolating everything back to 0,0,0. These are transforms you can find at RenderEntity class
double d0 = entity.lastTickPosX + (entity.posX - entity.lastTickPosX) * (double)evt.getPartialTicks();
double d1 = entity.lastTickPosY + (entity.posY - entity.lastTickPosY) * (double)evt.getPartialTicks();
double d2 = entity.lastTickPosZ + (entity.posZ - entity.lastTickPosZ) * (double)evt.getPartialTicks();
//Apply 0-our transforms to set everything back to 0,0,0
Tessellator.getInstance().getBuffer().setTranslation(-d0, -d1, -d2);
//Your render function which renders boxes at a desired position. In this example I just copy-pasted the one on TileEntityStructureRenderer
renderBox(Tessellator.getInstance(), Tessellator.getInstance().getBuffer(), sX, sY, sZ, sX + 1, sY + 1, sZ + 1);
//When you are done rendering all your boxes reset the offsets. We do not want everything that renders next to still be at 0,0,0 :)
Tessellator.getInstance().getBuffer().setTranslation(0, 0, 0);

 

note that if you want to outline more than 1 box like this you would need to apply player offsets first, then render your boxes in a loop, then reset the offsets.

 

This is a result I get with my example:

 

2017-04-30_19.40.52.png

Edited by V0idWa1k3r
  • Like 1
Link to comment
Share on other sites

  • 3 years later...

@V0idWa1k3r Hi. I know it's been a good few years since this thread was active but I can't seem to get this code to work. In the renderBox function I found, it takes 3 extra int arguments. I looked at the code and those seem to be color arguments(r, g, b). However, the color only shows up when I'm looking in the positive x, positive z direction. Otherwise, the outline is just black. Do you have any idea why this is happening?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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