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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Minecraft java and forge seem to have it out for me, any ideas? I have had issues with exit code 1 while loading forge -fabric was having the same issues, but I seemed to fix it in these repairs? (tried different versions upwards from 1.19 to 1.20.2 for forge, nothing worked and I'm confident older versions wouldn't either) -The launcher itself loads fine, it will load the game within the launcher, and then crash the moment it tries to load outside of the launcher. I have tried the default launcher, legacy launcher, and the curse forge launcher, none have worked! (all have had the same problem) I think this started when I took a break and upgraded to windows 11 and wonder if that changed anything? I have tried the following: uninstalled & reinstalled java, (I have having issues where I download jdk 17 but only 8 would show up (would not show up anywhere but the control panel, not even the official uninstaller, I *think* I got that fixed) tried jarfix uninstalled & reinstalled minecraft tried alternate launchers -forge & legacy, & repaired them too. tried without mods updated game drivers uninstalled and reinstalled forge -including completely wiping it from my system changed java excutable path followed everything in the error code 1 post completely reset my pc I tried to change the launch path but couldn't seem to get it to work -the default launcher wont let me even see properties as an option, i have yet to try with the other two (i did try on legacy but it was being weird) I want to include my debug log; but its over the max size? sorry my brains gone numb from trying to fix this for the past 2 days
    • I personally create registries using DataPackRegistryEvent.NewRegistry. It's pretty powerful as long as you know how to make codecs. In some class, make a registry key like so: public static final ResourceKey<Registry<MyDataType>> MY_REGISTRY = ResourceKey.createRegistryKey(new ResourceLocation(MyMod.MOD_ID, "registry_name")); This will make a registry that holds objects of type "MyDataType". JSON files stored in data/<datapack_namespace>/modid/registryname/ will be parsed and put into this registry. Note that the directory structure has a folder named after your mod inside your datapack directory, so the actual path would look like: data/my_mod/my_mod/registry_name (or if another mod uses your registry: data/their_mod/my_mod/registry_name. To register this registry, subscribe to the DataPackRegistryEvent.NewRegistry event on the MOD event bus and call event.dataPackRegistry() for every registry you have. An example of what I'm doing for my mod (this is in the constructor for the main mod file): IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus(); bus.addListener((DataPackRegistryEvent.NewRegistry event) -> { event.dataPackRegistry(ModRegistries.INSULATOR_DATA, InsulatorData.CODEC); }); CODEC is a public static field inside the class for the custom data type I am registering. It holds a Codec<InsulatorData> that tells Minecraft how to serialize/deserialize the data. Most of my data so far can be represented by records, so I use RecordCodecBuilder.create() to do this: public static final Codec<InsulatorData> CODEC = RecordCodecBuilder.create(instance -> instance.group( // Yadda yadda ).apply(instance, InsulatorData::new)); What exactly to put here depends on what you're trying to do, so that's up to you. But basically you're just defining a list of codecs that correspond to the parameters of the record (data type). Most primitive data types have records in the Codec class (ex. Codec.INT) that you can use, and other classes like ResourceLocation have their own codecs as a static field (ex. ResourceLocation.CODEC). Hopefully this is what you're looking for, and that this helps.
    • Good days  I finnaly manage to fix and get a working gui from BlockItem and from BlockEntity the next issues i have to fix is this the block Entity model for some reason is moved to the south west  this briefcase model must be centered but is moved to a side, i have nothing weird in the code but this is happening // ########## ########## ########## ########## @Override public RenderShape getRenderShape(BlockState blkstate) {     return RenderShape.MODEL; } the block model is out of place However The hitBox of the block is right in place   ########################################################################################################### This next issue  Mi gui also adds the 5 slots from the player equipment the thing is theres no filter to determine whats can be put in an armor slot and what not      i have this method that draws the slots for the armor, the part i dont get i slike wheres the code that responds when you set an item in a item slot to do checks and allow the item to be set or not  //########## ########## ########## //Draw Player Armor private void addPlayerArmorSlots(Inventory playerInventory) { // 36 this.addSlot(new Slot(playerInventory, 39, 8, 8)); this.addSlot(new Slot(playerInventory, 38, 8, 26)); this.addSlot(new Slot(playerInventory, 37, 8, 44)); this.addSlot(new Slot(playerInventory, 36, 8, 62)); this.addSlot(new Slot(playerInventory, 40, 26, 53)); }   theres must be a method that triggers when you set a itemstack inside an slot and allows to do a check if its a valid item for that slot                                             
    • No like how do I use my new PreperableReloadListener, I know how to set it up now and register it, I just need to know how to use it in other classes.
    • Completely out of the blue my forge loader is lagging, although at 120 fps. Like when I turn it lags, or hit mobs it lags, or run sometimes although staying at 120 fps. I found another person having a similar problem:  But there was no solution I dont think. I tried everything from: 1. Updating my computer 2. Updating/reinstalling forge 3. Testing if allocated memory was problem (if it was low or high or mid nothing changed) 4. Adding various performance mods 5. Deleting most of my profiles if storage was a problem.   Really not sure what to do next or if forge is just broken for ever for me now.  
  • Topics

×
×
  • Create New...

Important Information

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