three#ShapeBufferGeometry TypeScript Examples
The following examples show how to use
three#ShapeBufferGeometry.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextMeshObject.ts From movy with MIT License | 4 votes |
updateText() {
if (this.shouldUpdate) {
// TODO: optimize: text update is slow.
this.children.length = 0;
let totalWidth = 0;
const letterPosX: number[] = [];
let minY = Number.MAX_VALUE;
let maxY = Number.MIN_VALUE;
const geometries: ShapeBufferGeometry[] = [];
for (const [i, char] of [...this.text].entries()) {
if (char === ' ') {
totalWidth += this.initParams.fontSize * 0.5;
} else {
let font: Font;
let glyph: any;
for (let j = 0; j < this.fonts.length; j++) {
font = this.fonts[j];
glyph = (font.data as any).glyphs[char];
if (glyph) {
break;
} else if (j == this.fonts.length - 1) {
glyph = (font.data as any).glyphs['?'];
}
}
const fontData = font.data as any;
const resolution = fontData.resolution;
const ha = (glyph.ha / resolution) * this.initParams.fontSize;
const shapes = font.generateShapes(char, this.initParams.fontSize);
let geometry;
if (this.initParams.text3D) {
const extrudeSettings = {
depth: this.initParams.fontSize * 0.2,
bevelEnabled: false,
};
geometry = new ExtrudeGeometry(shapes, extrudeSettings);
} else if (this.initParams.stroke) {
const style = SVGLoader.getStrokeStyle(
this.initParams.strokeWidth,
this.initParams.color.getStyle() // color in CSS context style
);
// Add shape.holes to shapes
const holeShapes = [];
for (let i = 0; i < shapes.length; i++) {
const shape = shapes[i];
if (shape.holes && shape.holes.length > 0) {
for (let j = 0; j < shape.holes.length; j++) {
const hole = shape.holes[j];
holeShapes.push(hole);
}
}
}
shapes.push.apply(shapes, holeShapes);
const geoms: BufferGeometry[] = [];
for (const shape of shapes) {
const points = shape.getPoints();
const geom = SVGLoader.pointsToStroke(
points.map((v) => new Vector3(v.x, v.y)),
style
);
geoms.push(geom);
}
geometry = geoms.length > 1 ? mergeBufferGeometries(geoms) : geoms[0];
} else {
geometry = new ShapeBufferGeometry(shapes);
}
geometry.computeBoundingBox();
geometries.push(geometry);
// Always create a separate material for each letter
const mesh = new Mesh(geometry, this.material.clone());
mesh.name = char;
const letterWidth = ha;
const xMid = 0.5 * letterWidth;
geometry.translate(
-0.5 * (geometry.boundingBox.min.x + geometry.boundingBox.max.x),
-0.5 * this.initParams.fontSize,
0
);
letterPosX.push(totalWidth + xMid);
totalWidth +=
letterWidth +
(i < this.text.length - 1
? this.initParams.letterSpacing * this.initParams.fontSize
: 0);
minY = Math.min(minY, geometry.boundingBox.min.y);
maxY = Math.max(maxY, geometry.boundingBox.max.y);
this.add(mesh);
}
}
// Center text geometry vertically
const deltaY = (maxY + minY) * 0.5;
if (this.initParams.centerTextVertically) {
for (const geometry of geometries) {
geometry.translate(0, -deltaY, 0);
}
}
this.children.forEach((letter, i) => {
letter.position.set(-0.5 * totalWidth + letterPosX[i], 0, 0);
});
this.shouldUpdate = false;
}
}