jr-util
Version:
这是MJR的JS工具库。定期更新
94 lines (93 loc) • 3.13 kB
JavaScript
const i = (r, e = "horizontal", o = 1024) => {
const c = document.createElement("canvas"), t = c.getContext("2d");
return c.width = o, c.height = o, t.translate(e === "horizontal" ? o : 0, e === "vertical" ? o : 0), t.scale(e === "horizontal" ? -1 : 1, e === "vertical" ? -1 : 1), t.drawImage(r, 0, 0, o, o), t.scale(1, 1), t.translate(0, 0), c;
}, m = (r, e) => new Promise((o, c) => {
new e.CubeTextureLoader().load(r, (t) => {
const n = t.source.data;
return n[0] = i(n[0], "vertical"), n[1] = i(n[1], "vertical"), n[2] = i(n[2]), n[3] = i(n[3]), n[4] = i(n[4], "vertical"), n[5] = i(n[5], "vertical"), t.needsUpdate = !0, t.generateMipmaps = !1, t.wrapS = e.ClampToEdgeWrapping, t.wrapT = e.ClampToEdgeWrapping, o(t);
});
}), u = (r, e) => new Promise((o, c) => {
new e.CubeTextureLoader().load(r, (t) => {
const n = t.source.data, a = [
{
i: 0,
img: i(n[0], "vertical")
},
{
i: 1,
img: i(n[1], "vertical")
},
{
i: 2,
img: i(n[2])
},
{
i: 3,
img: i(n[3])
},
{ i: 4, img: i(n[4], "vertical") },
{
i: 5,
img: i(n[5], "vertical")
}
];
return o(a);
});
}), d = (r) => {
const e = Math.floor(2 * Math.PI * r), o = Math.floor(2 * r);
return {
width: e,
height: o
};
}, M = (r, e, o, c) => {
r.children.length !== 0 && r.children.forEach((t) => {
try {
const n = t.userData.id || "00001";
if (!e)
return;
const a = e.querySelector(`#${n}`);
if (a && a.children.length > 0) {
const s = new c.Vector3();
s.copy(t.position), s.project(o);
const l = (s.x * 0.5 + 0.5) * window.innerWidth, h = (-s.y * 0.5 + 0.5) * window.innerHeight;
s.z < 1 && s.z > -1 ? (a.children[0].style.left = `${l}px`, a.children[0].style.top = `${h}px`, a.children[0].style.display = "block") : a.children[0].style.display = "none";
}
} catch {
}
});
}, p = (r, e, o, c, t) => {
(Math.abs(e) >= t || Math.abs(e) === 0) && (e = 1), e > 0 ? e = t - e : e < 0 && (e = Math.abs(e) + t);
const n = Math.PI - r / o * 2 * Math.PI, a = Math.PI / 2 - e / c * Math.PI, s = t * Math.cos(a) * Math.sin(n), l = t * Math.sin(a), h = t * Math.cos(a) * Math.cos(n);
return {
x: s,
y: l,
z: h
};
}, w = (r, e, o, c, t) => {
const n = d(c), a = p(e, o, n.width, n.height, c), s = new t.MeshBasicMaterial({
color: 16711680,
transparent: !0,
opacity: 0,
wireframe: !1
}), l = new t.Mesh(new t.BoxGeometry(0.1, 0.1, 0.1), s);
return l.position.set(a.x, a.y, a.z), l.userData.id = r, l;
}, g = (r, e) => {
if (!r || e.length === 0)
return;
const o = e.shift();
if (r && r.source && r.source.data) {
const { i: c, img: t } = o;
r.source.data[c] = t, r.needsUpdate = !0;
}
g(r, e);
};
export {
w as threeAddMovingCube,
d as threeCalculateRectangleSize,
i as threeFlipImage,
m as threeLoadTextureFunc,
u as threeLoadTextureFuncImages,
p as threePlaneToSphereSize,
g as threeUpdateCubeTextureFace,
M as threeUpdateLabelPosition
};