Jump to content

[1.16.5] Help with drawInBatch


KylM

Recommended Posts

Hello, so I am trying to render a nametag so serve as the purpose of a "waypoint". I created a function based on the code of how Minecraft renders nametags above players. The text seems to be appearing just "fine" and is placed correctly but there's a few problems:

1) The text itself is kind of hard to see. I tried figuring out how to change the opacity for example but I only managed to change the opacity of the box in its whole which did not help.

2) When I apply a color to my TextComponent, the color cannot be seen.

3) The text seems to be rotating in the wrong direction compared to the player's YAW.

Image of current result

-> As you can also see in the image, there's some weird thing going on with the lighting of the text. Some letters are rendered darker than the other based on my rotation.

Code: (The function below is the one I talked about earlier. The stuff in the event is just some circle calculations to calculate the position of where I want my waypoint rendered but also shows my parameters ofcourse)

@SubscribeEvent
    public void renderNametag(RenderWorldLastEvent event) {

        if (waypoint!=null) {
            Minecraft mc = Minecraft.getInstance();
            IRenderTypeBuffer.Impl renderTypeBuffer = Minecraft.getInstance().renderBuffers().bufferSource();
            TextComponent txt = new TranslationTextComponent("Test");
            txt.withStyle(TextFormatting.GREEN);

            //Pos Calc
            MathUtils.Point closest = null;
            MathUtils.Point targetPoint = new MathUtils.Point(waypoint.getX(), waypoint.getZ());
            int r = 5;
            for (MathUtils.Point point : MathUtils.getCircleLineIntersectionPoint(new MathUtils.Point(mc.player.xo, mc.player.zo), new MathUtils.Point(waypoint.getX(), waypoint.getZ()), new MathUtils.Point(mc.player.xo, mc.player.zo), r)) {
                if (closest==null || MathUtils.distance(targetPoint, point)<MathUtils.distance(targetPoint, closest)) {
                    closest = point;
                }
            }
            if (closest!=null) {
                renderNameTag(new MathUtils.Point(closest.getX()-mc.player.xo, closest.getY()-mc.player.zo), txt, event.getMatrixStack(), renderTypeBuffer, 0);
                return;
            }
            System.out.println("ERROR");
        }

    }

    protected void renderNameTag(MathUtils.Point pos, ITextComponent text, MatrixStack ms, IRenderTypeBuffer buffer, int p_225629_5_) {
        Minecraft mc = Minecraft.getInstance();
        float f = 0f;
        int i = 0;
        ms.pushPose();
        //ms.translate(pos.getX()-mc.player.xo, (double)f, pos.getZ()-mc.player.zo);
        ms.translate(pos.getX(), 0, pos.getY());
        //ms.translate(2, f, 0);
        Quaternion q = mc.getEntityRenderDispatcher().cameraOrientation();
        ms.mulPose(q);
        ms.scale(-0.025F, -0.025F, 0.025F);
        Matrix4f matrix4f = ms.last().pose();
        float f1 = Minecraft.getInstance().options.getBackgroundOpacity(0.5F);
        int j = (int)(f1 * 255.0F) << 24;
        FontRenderer fontrenderer = Minecraft.getInstance().font;
        float f2 = (float)(-fontrenderer.width(text) / 2);
        fontrenderer.drawInBatch(text, f2, i, 553648127, false, matrix4f, buffer, false, j, p_225629_5_);
        fontrenderer.drawInBatch(new StringTextComponent(""), f2, (float)i, /*553648127*/-1, false, matrix4f,buffer, true, 0, p_225629_5_);

        ms.popPose();
    }

 

If anyone knows how the FontRenderer#drawInBatch works, please help me because I'm having a tough time figuring it out.

Link to comment
Share on other sites

2 hours ago, OutCraft said:

Hi!

net.minecraft.client.renderer.debug.DebugRenderer has a method for rendering text in the air, it's called renderFloatingText

I'm not sure if this is what I need though, when I use this function I get a result that's way off what I'm trying to achieve.

Link to comment
Share on other sites

2 minutes ago, OutCraft said:

just renders a "Name tag" in the air. You need it with color, right? 

Yeah, I do. The code of how I adapted my event is below.

This gives however, like I said, a weird result. The text doesn't seem to be rendering in the 3D location I put it in but instead kinda floats about a fixed point and rotates in some weird ways.

Here's what I mean:

Image

@SubscribeEvent
    public void renderNametag(RenderWorldLastEvent event) {

        if (waypoint!=null) {
            Minecraft mc = Minecraft.getInstance();
            IRenderTypeBuffer.Impl renderTypeBuffer = Minecraft.getInstance().renderBuffers().bufferSource();
            TextComponent txt = new TranslationTextComponent("Test");
            txt.withStyle(TextFormatting.GREEN);

            //Pos Calc
            MathUtils.Point closest = null;
            MathUtils.Point targetPoint = new MathUtils.Point(waypoint.getX(), waypoint.getZ());
            int r = 5;
            for (MathUtils.Point point : MathUtils.getCircleLineIntersectionPoint(new MathUtils.Point(mc.player.xo, mc.player.zo), new MathUtils.Point(waypoint.getX(), waypoint.getZ()), new MathUtils.Point(mc.player.xo, mc.player.zo), r)) {
                if (closest==null || MathUtils.distance(targetPoint, point)<MathUtils.distance(targetPoint, closest)) {
                    closest = point;
                }
            }
            if (closest!=null) {
                DebugRenderer.renderFloatingText("test", closest.getX(), mc.player.yo, closest.getY(), 0xFFFFFF);
                //renderNameTag(new MathUtils.Point(closest.getX()-mc.player.xo, closest.getY()-mc.player.zo), txt, event.getMatrixStack(), renderTypeBuffer, 0);
                return;
            }
            System.out.println("ERROR");
        }

    }

 

Link to comment
Share on other sites

5 minutes ago, KylM said:
DebugRenderer.renderFloatingText("test", closest.getX(), mc.player.yo, closest.getY(), 0xFFFFFF);

Why closest.getX() and closest.getY()? Just pass in waypoint.getX() and waypoint.getY()

Sorry if my Posts are weird sometimes, I just try to help and learn as much as I can :D

Also: PLEASE use SPOILERS for logs!

Link to comment
Share on other sites

8 minutes ago, KylM said:
            //Pos Calc
            MathUtils.Point closest = null;
            MathUtils.Point targetPoint = new MathUtils.Point(waypoint.getX(), waypoint.getZ());
            int r = 5;
            for (MathUtils.Point point : MathUtils.getCircleLineIntersectionPoint(new MathUtils.Point(mc.player.xo, mc.player.zo), new MathUtils.Point(waypoint.getX(), waypoint.getZ()), new MathUtils.Point(mc.player.xo, mc.player.zo), r)) {
                if (closest==null || MathUtils.distance(targetPoint, point)<MathUtils.distance(targetPoint, closest)) {
                    closest = point;
                }
            }

I don't understand what you're calculating

Sorry if my Posts are weird sometimes, I just try to help and learn as much as I can :D

Also: PLEASE use SPOILERS for logs!

Link to comment
Share on other sites

10 minutes ago, OutCraft said:

Why closest.getX() and closest.getY()? Just pass in waypoint.getX() and waypoint.getY()

After adapting I still get a weird result though. The text just seems to be doing about the same it was doing before. It rotates around and isn't in the position I want it in at all.

Just to clarify: the idea is that the text always faces the player (meaning it's not weirdly rotated away from the player) and always points to the "waypoint".

Link to comment
Share on other sites

12 minutes ago, OutCraft said:

Then look at how DebugRenderer renders the color and make the same in your renderNameTag function, if DebugRenderer isn't working for you 

Yeah, but I'd still be stuck with my other 2 problems though.

Link to comment
Share on other sites

20 minutes ago, OutCraft said:

Which problems? I thought your method is working 

Problems 1 and 3 mentioned in the OP:

1) The text itself is kind of hard to see. I tried figuring out how to change the opacity for example, but I only managed to change the opacity of the box in its whole which did not help.

3) The text seems to be rotating in the wrong direction compared to the player's YAW.

Link to comment
Share on other sites

It seems that the following post seemed to be trying to do about the same as I am. It had a simple solution of using EntityRenderer#drawNameplate, but that function doesn't exist in 1.16.5, and I can't find any renames or alternatives for it except for the original method I mentioned in my OP, but that method is protected and requires an entity.

 

Edited by KylM
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



  • 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.