CAD/client/src/helpers/point3dHelper.ts

53 lines
1.3 KiB
TypeScript

import * as THREE from "three";
export type Point3d = {
position: THREE.Vector3,
mesh: THREE.Mesh,
}
export class Point3dHelper {
private scene: THREE.Scene;
private readonly baseMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff });
private readonly markers: Record<string, Point3d> = {};
constructor(scene: THREE.Scene) {
this.scene = scene;
}
private ensure(id: string): Point3d {
if (!this.markers[id]) {
this.markers[id] = {
position: new THREE.Vector3(),
mesh: new THREE.Mesh(
new THREE.SphereGeometry(0.1, 8, 8),
this.baseMaterial,
),
};
this.scene.add(this.markers[id].mesh);
}
return this.markers[id];
}
private disposePoint(id: string) {
const point = this.markers[id];
if (point) {
this.scene.remove(point.mesh);
point.mesh.geometry.dispose();
delete (this.markers[id]);
}
}
public dispose() {
for (const id in this.markers)
this.disposePoint(id);
}
public set(id: string, position: THREE.Vector3Like) {
const point = this.ensure(id);
point.position.copy(position);
point.mesh.position.copy(position);
}
}