CADViewComponent/src/Text.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import { Font, FontLoader, Mesh, ShapeGeometry, Vector3 } from "three";
import { ColorMaterial } from "./ColorPalette";
import { MoveMatrix } from "./GeUtils";
2018-05-28 17:59:38 +08:00
class FontLoaderUtil {
private static defFont: Font;
static Load(): Font {
if (!this.defFont) {
const f = require("../resources/fonts/helvetiker_regular.typeface.json");
let loader = new FontLoader();
this.defFont = loader.parse(f);
}
2018-05-28 17:59:38 +08:00
return this.defFont;
}
}
2018-05-28 17:59:38 +08:00
export class DbText extends Mesh {
/**
*
* ,8()0
* @param {string} str
* @param {number} height
* @memberof DbText
*/
constructor(str: string, height: number = 5) {
let font = FontLoaderUtil.Load();
2018-05-28 17:59:38 +08:00
let shapes: THREE.Shape[] = font.generateShapes(str, height, 0.1);
let geometry = new ShapeGeometry(shapes);
2018-05-28 17:59:38 +08:00
geometry.computeBoundingBox();
2018-05-28 17:59:38 +08:00
super(geometry, ColorMaterial.GetLineMaterial(5));
2018-05-28 17:59:38 +08:00
let center = geometry.boundingBox.getCenter(new Vector3());
this.applyMatrix(MoveMatrix(new Vector3(-center.x, 0, 0)));
}
}