threepipe
Version:
A modern 3D viewer framework built on top of three.js, written in TypeScript, designed to make creating high-quality, modular, and extensible 3D experiences on the web simple and enjoyable.
60 lines • 1.88 kB
JavaScript
import { Group, Sphere } from 'three';
import { Box3B } from '../math/Box3B';
export class SelectionWidget extends Group {
_updater() {
const selected = this._object;
if (selected) {
const bbox = new Box3B().expandByObject(selected, false);
bbox.getCenter(this.position);
const scale = bbox.getBoundingSphere(new Sphere()).radius;
this.scale.setScalar(scale * this.boundingScaleMultiplier);
this.setVisible(true);
}
else {
this.setVisible(false);
}
}
constructor() {
super();
this.isWidget = true;
this._object = null;
this.boundingScaleMultiplier = 1.;
this.position.set(0, 0, 0);
this.visible = false;
this.renderOrder = 100; // Don't draw too early, thus obscuring other transparent objects
this.userData.bboxVisible = false;
this._updater = this._updater.bind(this);
}
setVisible(v) {
if (v !== this.visible) {
this.visible = v;
this.setDirty?.({ sceneUpdate: false });
}
}
attach(object) {
this.detach();
if (!object)
return this;
this._object = object;
this._object.addEventListener('objectUpdate', this._updater);
this._object.addEventListener('geometryUpdate', this._updater);
this._updater();
return this;
}
detach() {
if (!this._object)
return this;
this._object?.removeEventListener('objectUpdate', this._updater);
this._object?.removeEventListener('geometryUpdate', this._updater);
this._object = null;
this._updater();
return this;
}
get object() {
return this._object;
}
dispose() {
this.detach();
}
}
//# sourceMappingURL=SelectionWidget.js.map