@blooooork/three-ui
Version:
Professional 3D user interface components for Three.js applications
132 lines (131 loc) • 3.71 kB
JavaScript
import * as s from "three";
class p {
constructor(e, t) {
this.parent = e, this.camera = t, this.object = new s.Object3D(), this.parent.add(this.object);
}
dispose() {
this.parent.remove(this.object), this.object.traverse((e) => {
e.geometry && e.geometry.dispose(), e.material && (Array.isArray(e.material) ? e.material.forEach((t) => t.dispose()) : e.material.dispose());
});
}
update() {
}
}
class d extends p {
constructor(e, t, o = {}) {
super(e, t);
const {
width: i = 10,
height: a = 2.75,
depth: n = 0.2,
color: r = 16777215,
wireframe: h = !0,
position: c = new s.Vector3(0, 9, 0)
} = o, f = new s.BoxGeometry(i, a, n), l = new s.MeshBasicMaterial({
color: r,
wireframe: h,
transparent: !0,
opacity: h ? 1 : 0.8
});
this.titleMesh = new s.Mesh(f, l), this.titleMesh.position.copy(c), this.object.add(this.titleMesh);
}
setTitle(e) {
console.log("Title set to:", e);
}
resize(e) {
this.titleMesh.geometry.dispose(), this.titleMesh.geometry = new s.BoxGeometry(e, this.titleMesh.geometry.parameters.height, this.titleMesh.geometry.parameters.depth);
}
}
class u extends p {
constructor(e, t, o = {}) {
super(e, t);
const {
radius: i = 0.44,
spacing: a = 3.5,
links: n = []
} = o;
this.links = /* @__PURE__ */ new Map(), n.forEach((r, h) => {
const c = new s.CircleGeometry(i), f = new s.MeshBasicMaterial({
map: r.texture,
transparent: !0,
depthTest: !1
}), l = new s.Mesh(c, f);
l.position.x = i * (a * h), l.userData.url = r.url, this.links.set(r.id, l), this.object.add(l);
});
}
setPosition(e, t, o) {
this.object.position.set(e, t, o);
}
onClick(e) {
const t = new s.Raycaster();
t.setFromCamera(e, this.camera);
const o = t.intersectObjects(Array.from(this.links.values()));
if (o.length > 0) {
const i = o[0].object.userData.url;
i && window.open(i, "_blank");
}
}
}
class M extends p {
constructor(e, t, o = {}) {
super(e, t);
const {
width: i = 10,
height: a = 5,
color: n = 16777215,
opacity: r = 0.8
} = o, h = new s.PlaneGeometry(i, a), c = new s.MeshBasicMaterial({
color: n,
transparent: !0,
opacity: r,
depthTest: !1
});
this.frameMesh = new s.Mesh(h, c), this.object.add(this.frameMesh);
}
setText(e) {
console.log("Text set to:", e);
}
setSize(e, t) {
this.frameMesh.geometry.dispose(), this.frameMesh.geometry = new s.PlaneGeometry(e, t);
}
animate(e, t = 1e3) {
const o = this.object.position.clone(), i = Date.now(), a = () => {
const n = Date.now(), r = Math.min((n - i) / t, 1);
this.object.position.lerpVectors(o, e, r), r < 1 && requestAnimationFrame(a);
};
a();
}
}
class w extends p {
constructor(e, t, o = {}) {
super(e, t);
const {
width: i = 1,
height: a = 1,
color: n = 16777215,
hoverColor: r = 65280
} = o, h = new s.PlaneGeometry(i, a), c = new s.MeshBasicMaterial({
color: n,
transparent: !0,
opacity: 0.8,
depthTest: !1
});
this.buttonMesh = new s.Mesh(h, c), this.object.add(this.buttonMesh), this.originalColor = n, this.hoverColor = r, this.isHidden = !1;
}
onHover(e) {
this.buttonMesh.material.color.setHex(e ? this.hoverColor : this.originalColor);
}
onClick(e) {
this.isHidden = !this.isHidden, e && e(this.isHidden);
}
setPosition(e, t, o) {
this.object.position.set(e, t, o);
}
}
export {
w as HideButton,
u as LinkContainer,
M as TextFrame,
d as TitleBlock,
p as UIComponent
};