house/src/shapes.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

import {
CSG,
Mesh,
MeshBuilder,
PhotoDome,
Vector3,
} from '@babylonjs/core/Legacy/legacy';
export function adjust(f: (... csgs: CSG[]) => CSG, ... ms: Mesh[]): Mesh {
const csgs = ms.map(m => CSG.FromMesh(m));
const c = f(... csgs);
if (ms.length > 0) {
ms.forEach(m => m.dispose());
const scene = ms[0].getScene();
const ans = c.toMesh(ms[0].name, null, scene, true);
ans.material = ms[0].material;
return ans;
} else {
return c.toMesh("adjusted");
}
}
export const subtractMany = (a: CSG, ... bs: CSG[]) => bs.reduce((a, b) => a.subtract(b), a);
export class PhotoSemiDome extends PhotoDome {
readonly _size: number;
constructor(... args: ConstructorParameters<typeof PhotoDome>) {
super(... args);
this._size = args[2].size!;
this._chopMesh();
}
_chopMesh() {
const d = this._size;
this._mesh.getChildMeshes()[0].dispose(); // remove the covering half-sphere
this._mesh = adjust(subtractMany,
this._mesh,
box(this._mesh.name + "_chop", d, d, d, 0, 0, -d/2));
this._mesh.parent = this;
}
}
export function box(name: string, width: number, height: number, depth: number, x?: number, y?: number, z?: number): Mesh {
const b = MeshBuilder.CreateBox(name, {});
b.scaling = new Vector3(width, height, depth);
b.position = new Vector3(x ?? 0, y ?? b.scaling.y/2, z ?? 0);
return b;
}