文本转高斯泼溅
GsplatText 为每个非透明文本像素创建单元,让标签自然融入高斯泼溅场景。
备注
此功能基于渲染架构。
概览
该脚本组件:
- 用标准 CSS 字体绘制到画布。
- 每个可见像素一个单元。
- 位于 Y=0 的 XZ 平面。
- 支持填充、描边和自定义字体。
用实体变换调整大小和位置。
基本用法
// Import the script
const { GsplatText } = await import('path/to/gsplat-text.mjs');
// Add script component to an entity
entity.addComponent('script');
const textSplat = entity.script.create(GsplatText);
// Configure text
textSplat.text = 'Hello World';
textSplat.fontSize = 64;
textSplat.fillStyle = '#ffffff';
// Position and scale using entity transform
entity.setLocalPosition(0, 0, 0);
entity.setLocalScale(0.15, 0.15, 0.15);
属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
text | string | '' | 文本字符串 |
fontSize | number | 64 | 像素字号 |
fontFamily | string | 'sans-serif' | CSS 字体族 |
fillStyle | string | '#ffffff' | CSS 填充颜色 |
strokeStyle | string | 'rgba(0,0,0,0)' | CSS 描边颜色 |
strokeWidth | number | 0 | 像素描边宽度 |
padding | number | 0 | 文本周围像素间距 |
样式示例
基础白字
textSplat.text = 'Score: 100';
textSplat.fontSize = 48;
textSplat.fillStyle = '#ffffff';
描边文字
textSplat.text = 'GAME OVER';
textSplat.fontSize = 72;
textSplat.fillStyle = '#ff0000';
textSplat.strokeStyle = '#000000';
textSplat.strokeWidth = 3;
自定义字体
textSplat.text = 'Fancy Text';
textSplat.fontFamily = 'Georgia, serif';
textSplat.fontSize = 56;
自动更新
任意属性变化后自动重建,下一帧更新。
// Update text at runtime
textSplat.text = 'New Score: 200';
坐标系
与 GsplatImage 一样位于 XZ 平面:
- X:从左到右。
- Z:从上到下,从 +Y 看可正确阅读。
- Y:始终为 0。
文本适配 1x1 单位区域,再由实体变换缩放。
性能注意事项
数量取决于:
- 字号,越大像素越多。
- 文本长度。
- 描边宽度,增加像素。
长文本和大字号可能生成大量单元。
实时示例
Procedural Shapes 用于 CAD 可视化尺寸标签。
程序化形状示例
脚本位置
脚本位于引擎仓库:
scripts/esm/gsplat/gsplat-text.mjs