onRender method
Override for custom rendering.
Implementation
@override
void onRender(Canvas canvas, Matrix4 viewProjection) {
final mvp = viewProjection * worldMatrix;
final center4 = mvp.transformed(Vector4(0, 0, 0, 1));
if (center4.w <= 0.001) return;
final ndcX = center4.x / center4.w;
final ndcY = center4.y / center4.w;
final depth = center4.w;
final scaledFontSize = fontSize / depth;
// SOTA Vector-Based Sharp Rendering (SDF Simulation):
// Instead of rendering small fonts (which pixelates under lens magnifying glass),
// we render at a high-resolution base (120px) and scale down the Canvas transform.
// This forces the rasterizer to construct ultra-high density vector font contours.
const double baseHighResSize = 120.0;
final double scaleRatio = scaledFontSize / baseHighResSize;
canvas.save();
canvas.translate(ndcX, ndcY);
canvas.scale(scaleRatio, scaleRatio);
final textPainter = TextPainter(
text: TextSpan(
text: text,
style: TextStyle(
color: color,
fontSize: baseHighResSize,
fontWeight: fontWeight,
),
),
textDirection: TextDirection.ltr,
textAlign: textAlign,
)..layout(maxWidth: (800 / depth) / scaleRatio);
textPainter.paint(
canvas,
Offset(-textPainter.width / 2, -textPainter.height / 2),
);
canvas.restore();
}