50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { generatePlanarUVs } from '../core/uv/planarProjection';
|
|
import { generateBoxUVs } from '../core/uv/boxProjection';
|
|
import { generateCylindricalUVs } from '../core/uv/cylindricalProjection';
|
|
|
|
export type ProjectionType = 'planar' | 'box' | 'cylindrical';
|
|
|
|
export interface UVWorkerMessage {
|
|
id: string;
|
|
positions: Float32Array;
|
|
normals?: Float32Array;
|
|
projection: ProjectionType;
|
|
axis?: 'x' | 'y' | 'z';
|
|
}
|
|
|
|
export interface UVWorkerResponse {
|
|
id: string;
|
|
uvs: Float32Array;
|
|
error?: string;
|
|
}
|
|
|
|
self.onmessage = (event: MessageEvent<UVWorkerMessage>) => {
|
|
const { id, positions, normals, projection, axis } = event.data;
|
|
|
|
try {
|
|
const context = { positions, normals };
|
|
let uvs: Float32Array;
|
|
|
|
switch (projection) {
|
|
case 'planar':
|
|
uvs = generatePlanarUVs(context, axis || 'z');
|
|
break;
|
|
case 'box':
|
|
uvs = generateBoxUVs(context);
|
|
break;
|
|
case 'cylindrical':
|
|
uvs = generateCylindricalUVs(context, axis || 'y');
|
|
break;
|
|
default:
|
|
throw new Error(`Unsupported projection: ${projection}`);
|
|
}
|
|
|
|
// Transfer the ArrayBuffer back to main thread for performance
|
|
self.postMessage({ id, uvs } as UVWorkerResponse, {
|
|
transfer: [uvs.buffer],
|
|
});
|
|
} catch (error) {
|
|
self.postMessage({ id, error: String(error) } as UVWorkerResponse);
|
|
}
|
|
};
|