-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathMeshText2D.ts
64 lines (50 loc) · 2.02 KB
/
MeshText2D.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import THREE = require("three");
import { Text2D } from "./Text2D";
export class MeshText2D extends Text2D {
public mesh: THREE.Mesh;
protected geometry: THREE.PlaneGeometry;
constructor(text = '', options = {}) {
super(text, options);
}
raycast (): void {
this.mesh.raycast.apply(this.mesh, arguments)
}
updateText(): void {
this.cleanUp() // cleanup previous texture
this.canvas.drawText(this._text, {
font: this._font,
fillStyle: this._fillStyle,
shadowBlur: this._shadowBlur,
shadowColor: this._shadowColor,
shadowOffsetX: this._shadowOffsetX,
shadowOffsetY: this._shadowOffsetY,
lineHeight: this._lineHeight,
align: this.align,
backgroundColor: this._backgroundColor,
horizontalPadding: this._horizontalPadding,
verticalPadding: this._verticalPadding
})
this.texture = new THREE.Texture(this.canvas.canvas);
this.texture.needsUpdate = true;
this.applyAntiAlias()
if (!this.material) {
this.material = new THREE.MeshBasicMaterial({ map: this.texture, side: this.side });
this.material.transparent = true
} else {
this.material.map = this.texture
}
if (!this.mesh) {
this.geometry = new THREE.PlaneGeometry(this.canvas.width, this.canvas.height);
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.add(this.mesh)
}
this.mesh.position.x = ((this.canvas.width/2) - (this.canvas.textWidth/2)) + ((this.canvas.textWidth/2) * this.align.x)
this.mesh.position.y = (- this.canvas.height/2) + ((this.canvas.textHeight/2) * this.align.y)
// manually update geometry vertices
this.geometry.vertices[0].x = this.geometry.vertices[2].x = -this.canvas.width/2
this.geometry.vertices[1].x = this.geometry.vertices[3].x = this.canvas.width/2
this.geometry.vertices[0].y = this.geometry.vertices[1].y = this.canvas.height/2
this.geometry.vertices[2].y = this.geometry.vertices[3].y = -this.canvas.height/2
this.geometry.verticesNeedUpdate = true
}
}