Source code for simvx.core.nodes_3d.text
"""Text3D -- 3D-projected text node."""
from __future__ import annotations
import numpy as np
from ..descriptors import Property
from .node3d import Node3D
[docs]
class Text3D(Node3D):
"""Text that floats at a 3D world position, projected to screen."""
text = Property("", hint="Text to display")
font_scale = Property(1.0, range=(0.1, 100.0), hint="Font size")
font_colour = Property((1.0, 1.0, 1.0, 1.0), hint="RGBA colour (0.0-1.0)")
[docs]
def draw(self, renderer):
if not self.text:
return
vp = getattr(renderer, '_view_proj', None)
if vp is None:
return
pos = self.world_position
pos4 = np.array([pos.x, pos.y, pos.z, 1.0], dtype=np.float32)
clip = vp @ pos4
if clip[3] <= 0:
return # behind camera
ndc_x = clip[0] / clip[3]
ndc_y = clip[1] / clip[3]
sx = (ndc_x * 0.5 + 0.5) * renderer.width
sy = (1.0 - (ndc_y * 0.5 + 0.5)) * renderer.height
colour = tuple(self.font_colour)
renderer.draw_text(self.text, (sx, sy),
scale=self.font_scale, colour=colour)