Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding text in three.js

Tags:

three.js

I am visualizing a graph using Three.js and for each node of the graph I add a label using TextGeometry. It is a pretty small graph but when I add text my application gets really slow. What should I do about it?

like image 543
hAlE Avatar asked Feb 14 '23 14:02

hAlE


1 Answers

TextGeometry is more suitable for cases when you are really interested in rendering the text in 3D. It will create complex geometry that will surely slow your app down specially when there is a lot of text or you use CanvasRenderer.

For labels, it is generally better to use 2D labels, which are way faster to render. There are many different approaches to this. These can go on top of the Three.js rendering canvas, on a separate canvas, or even normal HTML nodes positioned using CSS properties. Alternatively, you can dynamically create small canvases of your label texts, and use them as sprite textures always facing camera - this might be the easiest way as the labels would be part of the 3D scene as your other objects. For a separate layer approach, you need to use unprojectVector or such to figure out screen XY coordinates to match your 3D scene positions.

See these SO posts for example: - Dynamically create 2D text in three.js - Canvas and SpriteMaterial - How do I add a tag/label to appear on top of several objects so that the tag always faces the camera when the user clicks the object?

like image 135
yaku Avatar answered Feb 17 '23 03:02

yaku