39 lines
862 B
TypeScript
39 lines
862 B
TypeScript
|
import { CanvasTexture, Sprite, SpriteMaterial } from 'three';
|
||
|
import { CanvasUtils } from './canvas-utils';
|
||
|
|
||
|
export class NameTag {
|
||
|
public tag!: Sprite;
|
||
|
public width!: number;
|
||
|
private texture!: CanvasTexture;
|
||
|
private material!: SpriteMaterial;
|
||
|
|
||
|
constructor(private builder: CanvasUtils, private name: string) {
|
||
|
this.create();
|
||
|
}
|
||
|
|
||
|
create() {
|
||
|
const { texture, width, height } = this.builder.createTextCanvas(this.name);
|
||
|
|
||
|
this.texture = texture;
|
||
|
this.width = width;
|
||
|
|
||
|
this.material = new SpriteMaterial({
|
||
|
map: texture,
|
||
|
transparent: true,
|
||
|
});
|
||
|
|
||
|
const label = new Sprite(this.material);
|
||
|
|
||
|
const labelBaseScale = 0.01;
|
||
|
label.scale.x = width * labelBaseScale;
|
||
|
label.scale.y = height * labelBaseScale;
|
||
|
|
||
|
this.tag = label;
|
||
|
}
|
||
|
|
||
|
dispose() {
|
||
|
this.material.dispose();
|
||
|
this.texture.dispose();
|
||
|
}
|
||
|
}
|