Ho to make Text in 3D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Liemaeu

Is there any other way of adding text in 3d than this method https://www.youtube.com/watch?v=ilBCnt_WE58 ?

With the Sprite3D and the Viewport I have to recreate the ViewportTexture of the Sprite3D when I change the text of the Label, otherwise the text looks ugly.

Is there another/better way for displaying text over a 3D object (e.g. MeshInstance)?

:bust_in_silhouette: Reply From: DaddyMonster

Yeah, make a spatial where you want it to appear in the scene. Then spawn a scene as its child with a spatial root and a TextureRect child (or whatever you prefer). Then you can add H/VBoxContainers, Buttons and RichTextLabels children to your heart’s desire to make it look fancy.

Then in the spatial root script:

var screen_pos = cam.unproject_position(global_transform.origin)
$TextureRect.set_position(screen_pos+offset)

nb. I didn’t watch the video so apologies if this is the same method.

Thank you very much!

I tried it (with a TextureRect), but I get the error:

 Unexpected token: '$':

Liemaeu | 2021-10-21 19:54

Not seen that error before… Really strange. You maybe have a space/tab $ TextureRect or something. Looks like the interpreter couldn’t parse it. Weird.

Try get node instead, it’s exactly equivalent:

func _physics_process(_delta):
    var screen_pos = cam.unproject_position(global_transform.origin)
    get_node("TextureRect").set_position(screen_pos+offset)

DaddyMonster | 2021-10-21 20:17

It worked!

Any way of scaling the text up/down when the camera gets closer to the object?

Liemaeu | 2021-10-21 20:32

I’m really baffled as to why $TextureRect didn’t work for you, the core coding in godot is rock solid. Parsing exceptions just don’t happen without a good reason.

Anyway, delighted you got it going. Hats off to you for implementing it.

Um, scaling. I’m genuinely not sure whether scale *= scale_factor on the spatial would work. No, ignore me, I’m being stupid. You can’t multiply a 3d basis by a 2d one, that’s undefined as the basis matrices rows and columns don’t match.

No, you’ll have to settle with:

$TextureRect.rect_scale = Vector2(scale_factor, scale_factor)

Obviously, scale_factor=1 is no change and you can go from there.

DaddyMonster | 2021-10-21 22:59