43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import * as THREE from 'three';
|
|
import type { MeshDto } from '../backend/dto';
|
|
|
|
export class GeometryCache {
|
|
private readonly items = new Map<string, THREE.BufferGeometry>();
|
|
|
|
public has(id: string): boolean {
|
|
return this.items.has(id);
|
|
}
|
|
|
|
public get(id: string): THREE.BufferGeometry | undefined {
|
|
return this.items.get(id);
|
|
}
|
|
|
|
public set(id: string, payload: THREE.BufferGeometry) {
|
|
this.items.set(id, payload);
|
|
}
|
|
|
|
public createMesh(id: string, dto: MeshDto): THREE.BufferGeometry {
|
|
let geometry = this.get(id);
|
|
if (geometry)
|
|
return geometry;
|
|
|
|
geometry = new THREE.BufferGeometry();
|
|
geometry.userData.id = id;
|
|
|
|
geometry.setAttribute('position', new THREE.BufferAttribute(dto.vertices, 3));
|
|
geometry.setAttribute('normal', new THREE.BufferAttribute(dto.normals, 3));
|
|
geometry.setIndex(new THREE.BufferAttribute(dto.indices, 1));
|
|
|
|
this.set(id, geometry);
|
|
|
|
return geometry;
|
|
}
|
|
|
|
unset(id: string) {
|
|
const geometry = this.get(id);
|
|
if (geometry) {
|
|
geometry.dispose();
|
|
this.items.delete(id);
|
|
}
|
|
}
|
|
} |