Skip to content

Commit 64f69eb

Browse files
author
Grégoire Roussel
committed
stashed changes from shensquared:text2d
1 parent c3a9cea commit 64f69eb

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

examples/demo.ipynb

+69
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,75 @@
153153
"vis.delete()"
154154
]
155155
},
156+
{
157+
"cell_type": "markdown",
158+
"metadata": {},
159+
"source": [
160+
"MeshCat supports simple 2d texts rendering. For example, to write 2d texts onto a geometry:"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"metadata": {},
167+
"outputs": [],
168+
"source": [
169+
"vis.set_object(g.Box([1, 1, 2]),g.MeshPhongMaterial(map=g.TextTexture('Hello, world!')))"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {},
175+
"source": [
176+
"It is also possible to simple write 'floating' texts onto a scene without attaching it to an object (e.g., for scene description):"
177+
]
178+
},
179+
{
180+
"cell_type": "code",
181+
"execution_count": null,
182+
"metadata": {},
183+
"outputs": [],
184+
"source": [
185+
"vis.delete()\n",
186+
"vis.set_object(g.SceneText('Hello, world!',font_size=100))"
187+
]
188+
},
189+
{
190+
"cell_type": "markdown",
191+
"metadata": {},
192+
"source": [
193+
"and just like the usual geometry/object, the scene texts can be rotated:"
194+
]
195+
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": null,
199+
"metadata": {},
200+
"outputs": [],
201+
"source": [
202+
"Rz = tf.rotation_matrix(np.pi/2, [0, 0, 1])\n",
203+
"Ry = tf.rotation_matrix(np.pi/2, [0, 1, 0])\n",
204+
"vis.set_transform(Ry.dot(Rz))"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"Under the hood, the `SceneTexts` are written onto a `Plane` geometry, and the plane size can be specified by width and height. These two parameters affect the texts size when the font_size itself is set too large; they would force a font downsizing when rendering so as to fit all the texts within the specified plane."
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": null,
217+
"metadata": {},
218+
"outputs": [],
219+
"source": [
220+
"for i in np.linspace(8,2,10):\n",
221+
" vis.set_object(g.SceneText('Hello, world!',width=2*i,height=2*i,font_size=300))\n",
222+
" time.sleep(0.05)"
223+
]
224+
},
156225
{
157226
"cell_type": "markdown",
158227
"metadata": {},

src/meshcat/commands.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from .geometry import Geometry, Object, Mesh, MeshPhongMaterial, OrthographicCamera, PerspectiveCamera, PointsMaterial, Points
1+
from .geometry import Geometry, Object, Mesh, MeshPhongMaterial, OrthographicCamera, PerspectiveCamera, PointsMaterial, Points, TextTexture
22
from .path import Path
33

44

src/meshcat/geometry.py

+47
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@ def intrinsic_transform(self):
8080
return np.diag(np.hstack((self.radii, 1.0)))
8181

8282

83+
class Plane(Geometry):
84+
85+
def __init__(self, width=1, height=1, widthSegments=1, heightSegments=1):
86+
super(Plane, self).__init__()
87+
self.width = width
88+
self.height = height
89+
self.widthSegments = widthSegments
90+
self.heightSegments = heightSegments
91+
92+
def lower(self, object_data):
93+
return {
94+
u"uuid": self.uuid,
95+
u"type": u"PlaneGeometry",
96+
u"width": self.width,
97+
u"height": self.height,
98+
u"widthSegments": self.widthSegments,
99+
u"heightSegments": self.heightSegments,
100+
}
101+
102+
83103
"""
84104
A cylinder of the given height and radius. By Three.js convention, the axis of
85105
rotational symmetry is aligned with the y-axis.
@@ -195,6 +215,26 @@ def lower(self, object_data):
195215
}
196216

197217

218+
class TextTexture(Texture):
219+
def __init__(self, text, font_size=100, font_face='sans-serif',
220+
width=200, height=100, position=[10, 10]):
221+
super(TextTexture, self).__init__()
222+
self.text = text
223+
# font_size will be passed to the JS side as is; however if the
224+
# text width exceeds canvas width, font_size will be reduced.
225+
self.font_size = font_size
226+
self.font_face = font_face
227+
228+
def lower(self, object_data):
229+
return {
230+
u"uuid": self.uuid,
231+
u"type": u"_text",
232+
u"text": unicode(self.text),
233+
u"font_size": self.font_size,
234+
u"font_face": self.font_face,
235+
}
236+
237+
198238
class GenericTexture(Texture):
199239
def __init__(self, properties):
200240
super(GenericTexture, self).__init__()
@@ -551,6 +591,13 @@ def PointCloud(position, color, **kwargs):
551591
)
552592

553593

594+
def SceneText(text, width=10, height=10, **kwargs):
595+
return Mesh(
596+
Plane(width=width,height=height),
597+
MeshPhongMaterial(map=TextTexture(text,**kwargs),transparent=True,
598+
needsUpdate=True)
599+
)
600+
554601
class Line(Object):
555602
_type = u"Line"
556603

0 commit comments

Comments
 (0)