Jump to content

Drawing text ingame using Font.draw


XPModder

Recommended Posts

Hi.

I am working on a mod and wanted to render some text during the LevelLastRenderEvent. I am trying to draw the Text using Minecraft.getInstance().font.draw(), by passing in the PoseStack from the event, as well as my Text.

For some reason I can't get it to actually render anything.
I am not quite sure what the x and y coordinates mean, that the method requires, but I tried many different values. I also looked at some other mods and tried to find an example of this actually being done, but I haven't really found something helpful...
Are the x and y parameters of the function world coordinates (like block coordinates) or are they relative to something else?

Ho do I find the correct coordinates to use?

Also, why is it not rendering anything at all for me right now?

 

Here is how I am trying to render the text at the moment.

PoseStack matrixStack = event.getPoseStack();

String text = "Collectable XP: ";
text += entity.getCollectedXP();

LogHelper.info("Render: " + text);

matrixStack.pushPose();

matrixStack.scale(1, 1, 1);

instance.font.draw(matrixStack, text, 7, 100, 0xffffff);

matrixStack.popPose();

This is inside an eventhandler for the LevelLastRenderEvent and with the LogHelper printing it to the log, I can see that it is definitively getting to this point and executing it.

The logging of this is just temporary, so I know that its actually trying to render...

Link to comment
Share on other sites

For 1.18? See OverlayRegistry for how to register an IIngameOverlay as part of the in game gui.

e.g. see ForgeInGameGui.HUD_TEXT that draws the debug text when you press F3 (amongst other things).

 

If you really do want to draw text in the world, look at the some vanilla code that does this, e.g. EntityRenderer.renderNameTag()

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Yes. It is for 1.18.2.

I do want to render the text in world, as the text will show some data about a specific type of blockentity.

Basically I want to draw some text above the block, when it is being looked at by the player. The text will show information about the blockentity.

I have looked at EntityRenderer.renderNameTag() now, but I am still having problems.

Unfortunately all the parameters in both the renderNameTag method, as well as all of the methods of the Font class do not have meaningful names, which makes it a lot harder to find out how something is working. I am still not sure what some of the parameters do.

I have now tried to use the method font.drawInBatch instead of font.draw, as this is what the renderNameTag method uses, but I still can't get any text to actually show up ingame.

Link to comment
Share on other sites

18 minutes ago, XPModder said:

Unfortunately all the parameters in both the renderNameTag method, as well as all of the methods of the Font class do not have meaningful names, which makes it a lot harder to find out how something is working. I am still not sure what some of the parameters do.

https://github.com/ParchmentMC/Parchment/wiki/Getting-Started

Link to comment
Share on other sites

This isn't really my area of expertise but..

 

You will need to do more than just drawing the text, you also need to change the pose (translate, rotate, etc.) to match where to draw it in the world.

e.g. for the entity rendering this is done in the EntityRendererDispatcher.render(). But renderNameTag() makes some additional changes so the text is correctly aligned to the camera.

 

For blocks I guess you can get the basic logic by looking at where it calls BlockEntityRendererDispatcher.render() in LevelRenderer.render().

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

If it just for the block being looked at, you might find subscribing for DrawSelectionEvent.HightlightBlock more useful? 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

13 minutes ago, warjort said:

If it just for the block being looked at, you might find subscribing for DrawSelectionEvent.HightlightBlock more useful? 

I will look into that.

 

I looked further into the renderNameTag method and also where it is being called from.

I still don't know 100% how everything works, but the renderNameTag method only translates the pose only by adjusting the y value of it. It does not modify the y and z translation.

So I tried doing essentially the same: translating in the y direction and leaving  x and z as is. I also adjusted the text to the camera, similar to the way it is done in renderNameTag.

 

I also looked at another project of mine, where I rendered a block in the world, that isn't actually there, but that project uses getActiveRenderInfo().getProjectedView() in the translation. This other project is sitting in 1.16.5 and there it works, but the ActiveRenderInfo class does not exist anymore and I can't find anything that would be the equivalent to getProjectedView() in 1.18.2.

I still havent been able to get it to work.

My code looks like this now:

                    PoseStack matrixStack = event.getPoseStack();

                    String text = "Collectable XP: ";
                    text += entity.getCollectedXP();

                    LogHelper.info("Render: " + text);

                    matrixStack.pushPose();

                    matrixStack.scale(1, 1, 1);

                    matrixStack.translate(0, 1, 0);

                    matrixStack.mulPose(instance.getEntityRenderDispatcher().cameraOrientation());

                    MultiBufferSource bufferSource = instance.renderBuffers().bufferSource();

                    int packedLight = instance.getEntityRenderDispatcher().getPackedLightCoords(playerIn, 0);

                    //instance.font.draw(matrixStack, text, 7, 80, 0xffffff);
                    instance.font.drawInBatch(text, 7, 10, 0x20ffffff, false, matrixStack.last().pose(), bufferSource, false, 0, packedLight);

                    matrixStack.popPose();

 

Link to comment
Share on other sites

Quote

So I tried doing essentially the same: translating in the y direction and leaving  x and z as is. I also adjusted the text to the camera, similar to the way it is done in renderNameTag.

It moves it up by the height of the entity model + half a block and it also does the mulPose and scale.

You are missing the part where it initially translates to the block position, see my comments above. 

 

This doesn't make sense to me.

Quote

matrixStack.mulPose(instance.getEntityRenderDispatcher().cameraOrientation());

This is just the value of camera.rotation() in the last call to EntityRenderDispatcher().prepare()

I doubt this is intended to be used outside the entity rendering calls themselves.

 

Also, if you use the HighlightBlock event you will be given a buffer source in the event state.

You also get a camera.

 

See this thread for tools/info to help translate old mods to the newer versions.

https://forums.minecraftforge.net/topic/115137-1192-translating-classesmethods-from-1165/

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

16 minutes ago, warjort said:

You are missing the part where it initially translates to the block position, see my comments above.

Can you show me where that is actually done in the game code?

If I look at the EntityRenderer class I can only find a single call to translate(), which is the one that moves it to the correct height.

 

18 minutes ago, warjort said:

Also, if you use the HighlightBlock event you will be given a buffer source in the event state.

You also get a camera.

Yes, I see that. I have now moved my code to the HighlightBlock event and inside the mulPose() call I now have event.getCamera.getRotation(). I also adjusted everything else to use the additional information availbale through that event.

 

I also tried to translate the pose to the block position, but it didn't fix the problem. I also couldn't see that being done anywhere in the vanilla code, so I assumed it was not neccessary....

Link to comment
Share on other sites

Forget the EntityRenderer (it was EntityRendererDispatcher anyway). I was just using that to explain the translation is in 2 steps.

* Step 1 - translate to the relative entity/block position

* Step 2 - do additional block/entity specific changes like moving the name plate upwards (inside the Renderer)

 

It is done this way so people that write BlockEntityRenderers or EntityRenderers don't have to deal with step 1. It is done for them.

You have neither, so you need to do both steps.

 

As I said above, an example of the code you want is in the LevelRenderer for the BlockEntityRenderDispatcher (d0, d1, d2 are the x,y,z of the camera)

If you are going to force me to feed it to you on a spoon...

Quote


            BlockPos blockpos3 = blockentity.getBlockPos();
            p_109600_.pushPose();

 

            // HERE
            p_109600_.translate((double)blockpos3.getX() - d0, (double)blockpos3.getY() - d1, (double)blockpos3.getZ() - d2);
 

          this.blockEntityRenderDispatcher.render(blockentity, p_109601_, p_109600_, multibuffersource$buffersource);
            p_109600_.popPose();
 

 

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

As I said above, this is not my area of expertise. There is something of the blind leading the blind here. 🙂

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Ok. I think I understand it now.

The pose is translated first by the difference between camera and block position to move it to where the block is. Then it is translated upwards, so it is above the block/entity and not inside it.

I am doing that now, but it still doesn't fix the problem...

PoseStack matrixStack = event.getPoseStack();

String text = "Collectable XP: ";
text += entity.getCollectedXP();

LogHelper.info("Render: " + text);

matrixStack.pushPose();

Vec3 cameraPosition = event.getCamera().getPosition();

matrixStack.translate((double)hitPos.getX() - cameraPosition.x, (double)hitPos.getY() - cameraPosition.y, (double)hitPos.getZ() - cameraPosition.z);

matrixStack.scale(1, 1, 1);

matrixStack.translate(0, 1.5, 0);

matrixStack.mulPose(event.getCamera().rotation());

MultiBufferSource bufferSource = event.getMultiBufferSource();

int packedLight = instance.getEntityRenderDispatcher().getPackedLightCoords(instance.player, event.getPartialTicks());

//instance.font.draw(matrixStack, text, 7, 80, 0xffffff);
instance.font.drawInBatch(text, 7, 10, 0x20ffffff, false, matrixStack.last().pose(), bufferSource, false, 0, packedLight);

matrixStack.popPose();

It still doesn't draw the text ingame.

 

I think I understand how most of the game works, but all the graphics and rendering stuff is still somewhat beyond my understanding...

 

9 hours ago, warjort said:

As I said above, this is not my area of expertise. There is something of the blind leading the blind here. 🙂

Well, we can hope that someone else, who understands this better, picks up this thread and helps us out...

Link to comment
Share on other sites

I had a go at implementing something like what you are doing, this is what I came up with based on your code:

    public static void highlightBlock(DrawSelectionEvent.HighlightBlock event) {
        Minecraft instance = Minecraft.getInstance();
        BlockHitResult hit = event.getTarget();
        Vec3 pos = hit.getLocation();
        Component text = new TextComponent("test");

        PoseStack matrixStack = event.getPoseStack();
        matrixStack.pushPose();
        Vec3 cameraPosition = event.getCamera().getPosition();
        Quaternion rotation = event.getCamera().rotation();
        matrixStack.translate((double) pos.x - cameraPosition.x, (double) pos.y - cameraPosition.y, (double) pos.z - cameraPosition.z);
        matrixStack.scale(-0.025F, -0.025F, -0.025F);
        matrixStack.mulPose(rotation);

        Font font = instance.font;
        float f2 = (float)(-font.width(text) / 2);
        MultiBufferSource bufferSource = event.getMultiBufferSource();
        int packedLight = instance.getEntityRenderDispatcher().getPackedLightCoords(instance.player, event.getPartialTicks());
        instance.font.drawInBatch(text, f2, 0f, -1, false, matrixStack.last().pose(), bufferSource, false, 0, packedLight);
        matrixStack.popPose();
    }

This is not perfect since it actually draws the text kind of inside the block. Something you can see is especially a problem when you look at solid block from above.

I'm sure you can fix this and other shortcomings. 🙂

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

I got it to work now!

The problem apperently was the scaling of the matrixStack. For some reason with the matrixStack.scale(1, 1, 1) it does not create any text at all, but as soon as I changed it to -0.025F, it worked!

That was the only change required to make it work in some capacity. I later made some other changes to, but they were not neccersary to make the text show up ingame.

I have no idea why that is the case, as I thought, the text would simply be way to big, if the scale was of...

Seing as it needs a negative scale, maybe scaling it to 1, 1, 1 made the text super small. So small that it wasnt visible....

 

Also: any translation should be done before the scaling, otherwise its way off...

 

Thanks for the help!

Link to comment
Share on other sites

A negative scaling means a reflection and I guess the reflection in the y direction is because world coords are "updside down" compared to screen coords (which I think is what the font drawing uses?)

 

I tried your 1,1,1 scaling and it is drawing the text, just not where you can see it in first person. You can see the bottom of the backwards large text when you press F5. 🙂

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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

    • 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;     }  
    • It is an issue with quark - update it to this build: https://www.curseforge.com/minecraft/mc-mods/quark/files/3642325
    • Remove Instant Massive Structures Mod from your server     Add new crash-reports with sites like https://paste.ee/  
    • Update your drivers: https://www.amd.com/en/support/graphics/amd-radeon-r9-series/amd-radeon-r9-200-series/amd-radeon-r9-280x
  • Topics

×
×
  • Create New...

Important Information

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