import { n as e, t } from "./three.webgpu-vPugBGTK.js";
import { a as n, i as r, n as i, r as a, s as o, t as s, u as c } from "./three.tsl-BarIGkA_.js";
import l, { useEffect as u, useMemo as d, useRef as f } from "react";
import { Color as p, DoubleSide as m } from "three";
import { MeshBVH as h } from "three-mesh-bvh";
import { jsx as g, jsxs as _ } from "react/jsx-runtime";
//#region src/viewer/WebGpuSelectableMesh.tsx
var v = 1 / 1.5;
function y({ colorPaletteTexture: c, material: l, materialMode: u, preselectionColor: d, selectionColor: f, useDoubleSideMaterial: h }) {
	let g = new p(l.r, l.g, l.b);
	u === "performance" && g.multiplyScalar(v);
	let _ = l.opacity ?? 1, y = {
		color: g.clone(),
		opacity: _,
		transparent: !!(_ && _ !== 1)
	}, b = h ? {
		...y,
		side: m
	} : y, x = u === "performance" ? new t(b) : new e(b);
	x.defaultAttributeValues = {
		...x.defaultAttributeValues,
		colorIndex: [0],
		ids: [0]
	};
	let S = a(i("colorIndex", "uint")), C = a(i("ids", "uint")), w = n(-1), T = n(new p(d)), E = n(new p(f)), D = n(g.clone()), O = n(_), k = r(c, o(S.add(.5).div(256), .5)).rgb, A = S.greaterThan(.5), j = s(S.sub(1)), M = j.lessThan(.5), N = j.greaterThan(.5), P = C.equal(w).and(N);
	return x.colorNode = P.select(T, M.select(E, A.select(k, D))), x.opacityNode = P.select(a(1), M.select(a(1), A.select(a(1), O))), x.userData.shader = { uniforms: {
		elementColorPalette: { value: c },
		preSelectionColor: T,
		preSelectionId: w,
		selectionColor: E
	} }, x;
}
var b = l.memo(function({ colorPaletteTexture: e, material: t, materialMode: n = "quality", data: r, isIfcSpace: i = !1, meshMatrix: a, useDoubleSideMaterial: o = !1 }) {
	let s = f(null), l = d(() => new Uint32Array(r.ids.length), [r.ids]), m = c((e) => e.preselectionColor), v = c((e) => e.selectionColor), b = f(m);
	b.current = m;
	let x = f(v);
	x.current = v;
	let S = d(() => y({
		colorPaletteTexture: e,
		material: t,
		materialMode: n,
		preselectionColor: b.current,
		selectionColor: x.current,
		useDoubleSideMaterial: o
	}), [
		e,
		t,
		n,
		o
	]);
	return u(() => {
		let e = S.userData.shader, t = (t, n) => {
			let r = e?.uniforms?.[t];
			r && (r.value instanceof p ? r.value.set(n) : r.value = new p(n));
		};
		t("preSelectionColor", m), t("selectionColor", v);
	}, [
		S,
		m,
		v
	]), u(() => {
		if (!s.current) return;
		let e = s.current.geometry;
		n !== "performance" && !e.attributes.normal && e.computeVertexNormals();
		let t = new h(e, { indirect: !0 });
		return Object.assign(e, { boundsTree: t }), () => {
			S.dispose(), Object.assign(e, { boundsTree: null });
		};
	}, [S]), /* @__PURE__ */ _("mesh", {
		frustumCulled: !0,
		matrix: a,
		matrixAutoUpdate: !1,
		name: i ? "space" : void 0,
		ref: s,
		userData: {
			isIfcSpace: i,
			keepColorIndexAttribute: !0,
			useUint32ColorIndex: !0
		},
		onPointerLeave: (e) => {
			let t = e.object;
			t.preselected &&= !1;
		},
		children: [/* @__PURE__ */ _("bufferGeometry", { children: [
			/* @__PURE__ */ g("bufferAttribute", {
				attach: "attributes-position",
				args: [r.pos, 3]
			}),
			r.norm && /* @__PURE__ */ g("bufferAttribute", {
				attach: "attributes-normal",
				args: [r.norm, 3]
			}),
			/* @__PURE__ */ g("bufferAttribute", {
				attach: "attributes-ids",
				args: [r.ids, 1]
			}),
			/* @__PURE__ */ g("bufferAttribute", {
				attach: "attributes-colorIndex",
				args: [l, 1]
			}),
			/* @__PURE__ */ g("bufferAttribute", {
				attach: "index",
				args: [r.ind, 1]
			})
		] }), /* @__PURE__ */ g("primitive", { object: S })]
	});
});
//#endregion
export { b as WebGpuSelectableMesh };
