bimatter-viewer-react 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -0
- package/lib/Camera/useCameraFit.d.ts +2 -0
- package/lib/GeometryUtils/useGeometryIndex.d.ts +35 -0
- package/lib/GeometryUtils/useGeometryUtils.d.ts +13 -0
- package/lib/Hotkeys/useHotkeysUtils.d.ts +2 -0
- package/lib/Loaders/BmtLoader.d.ts +51 -0
- package/lib/Model.d.ts +7 -0
- package/lib/SelectableMesh.d.ts +10 -0
- package/lib/Selector/SelectionBox.d.ts +50 -0
- package/lib/Selector/useSelector.d.ts +3 -0
- package/lib/Viewer.d.ts +21 -0
- package/lib/api/ViewerApi.d.ts +40 -0
- package/lib/api/loader.d.ts +8 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +1165 -0
- package/lib/store/ViewerStore.d.ts +19 -0
- package/lib/store/ViewerStoreContext.d.ts +2 -0
- package/lib/store/ViewerStoreProvider.d.ts +7 -0
- package/lib/store/useViewerStore.d.ts +3 -0
- package/lib/types.d.ts +9 -0
- package/package.json +58 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,1165 @@
|
|
|
1
|
+
import e, { createContext as t, forwardRef as n, useCallback as r, useContext as i, useEffect as a, useImperativeHandle as o, useMemo as s, useRef as c, useState as l } from "react";
|
|
2
|
+
import { Canvas as u, useThree as d } from "@react-three/fiber";
|
|
3
|
+
import { OrbitControls as f, Stats as p } from "@react-three/drei";
|
|
4
|
+
import { Box3 as m, BufferAttribute as h, Color as g, DoubleSide as _, Matrix4 as v, Mesh as y, MeshLambertMaterial as b, PerspectiveCamera as x, Vector2 as S, Vector3 as C } from "three";
|
|
5
|
+
import { CONTAINED as w, INTERSECTED as T, MeshBVH as E, NOT_INTERSECTED as D, acceleratedRaycast as O } from "three-mesh-bvh";
|
|
6
|
+
import { Fragment as k, jsx as A, jsxs as j } from "react/jsx-runtime";
|
|
7
|
+
import { useStore as M } from "zustand";
|
|
8
|
+
import { createStore as ee } from "zustand/vanilla";
|
|
9
|
+
import N from "pako";
|
|
10
|
+
//#region src/viewer/SelectableMesh.tsx
|
|
11
|
+
y.prototype.raycast = O;
|
|
12
|
+
var te = e.memo(function({ material: e, data: t, meshMatrix: n }) {
|
|
13
|
+
let r = c(null), i = s(() => {
|
|
14
|
+
let t = new b({
|
|
15
|
+
color: new g(e.r, e.g, e.b),
|
|
16
|
+
transparent: !!(e.opacity && e.opacity != 1),
|
|
17
|
+
opacity: e.opacity ?? 1,
|
|
18
|
+
side: _
|
|
19
|
+
});
|
|
20
|
+
return t.onBeforeCompile = (n) => {
|
|
21
|
+
let r = new g("rgb(17, 148, 189)"), i = new g("rgb(104, 198, 227)");
|
|
22
|
+
n.uniforms.baseColor = { value: new g(e.r, e.g, e.b) }, n.uniforms.selectionColor = { value: r }, n.uniforms.preSelectionColor = { value: i }, n.uniforms.preSelectionId = { value: -1 }, n.vertexShader = n.vertexShader.replace("#include <common>", "\n #include <common>\n attribute float selected;\n attribute uint ids;\n flat varying float vSel;\n flat varying float vPreselectId;\n "), n.vertexShader = n.vertexShader.replace("#include <begin_vertex>", "\n #include <begin_vertex>\n vSel = selected;\n vPreselectId = float(ids);\n "), n.fragmentShader = n.fragmentShader.replace("#include <common>", "\n #include <common>\n uniform vec3 baseColor;\n uniform vec3 selectionColor;\n uniform vec3 preSelectionColor;\n flat varying float vPreselectId;\n uniform float preSelectionId;\n flat varying float vSel;\n "), n.fragmentShader = n.fragmentShader.replace("vec4 diffuseColor = vec4( diffuse, opacity );", "\n vec4 diffuseColor;\n if(vSel > 0.5){\n diffuseColor = vec4(selectionColor, 1.0);\n } \n else if(preSelectionId == vPreselectId){\n diffuseColor = vec4(preSelectionColor, 1.0);\n }\n else{\n diffuseColor = vec4(baseColor, opacity);\n }\n "), t.userData.shader = n;
|
|
23
|
+
}, t;
|
|
24
|
+
}, [e]), o = s(() => new Uint8Array(t.ids.length), [t]);
|
|
25
|
+
return a(() => {
|
|
26
|
+
if (!r.current) return;
|
|
27
|
+
let e = r.current.geometry, t = new E(e, { indirect: !0 });
|
|
28
|
+
return Object.assign(e, { boundsTree: t }), () => {
|
|
29
|
+
i.dispose(), Object.assign(e, { boundsTree: null });
|
|
30
|
+
};
|
|
31
|
+
}, [i]), /* @__PURE__ */ j("mesh", {
|
|
32
|
+
matrix: n,
|
|
33
|
+
matrixAutoUpdate: !1,
|
|
34
|
+
ref: r,
|
|
35
|
+
onPointerLeave: (e) => {
|
|
36
|
+
e.object.preselected && (e.object.preselected = !1);
|
|
37
|
+
},
|
|
38
|
+
children: [/* @__PURE__ */ j("bufferGeometry", { children: [
|
|
39
|
+
/* @__PURE__ */ A("bufferAttribute", {
|
|
40
|
+
attach: "attributes-position",
|
|
41
|
+
args: [t.pos, 3]
|
|
42
|
+
}),
|
|
43
|
+
/* @__PURE__ */ A("bufferAttribute", {
|
|
44
|
+
attach: "attributes-ids",
|
|
45
|
+
args: [t.ids, 1]
|
|
46
|
+
}),
|
|
47
|
+
/* @__PURE__ */ A("bufferAttribute", {
|
|
48
|
+
attach: "attributes-selected",
|
|
49
|
+
args: [o, 1]
|
|
50
|
+
}),
|
|
51
|
+
/* @__PURE__ */ A("bufferAttribute", {
|
|
52
|
+
attach: "index",
|
|
53
|
+
args: [t.ind, 1]
|
|
54
|
+
})
|
|
55
|
+
] }), /* @__PURE__ */ A("primitive", { object: i })]
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/viewer/Model.tsx
|
|
60
|
+
function ne({ modelID: e, data: t }) {
|
|
61
|
+
let n = s(() => {
|
|
62
|
+
if (!t) return {
|
|
63
|
+
data: [],
|
|
64
|
+
coordinationMatrix: null
|
|
65
|
+
};
|
|
66
|
+
let n = null;
|
|
67
|
+
return {
|
|
68
|
+
data: Object.values(t).map((t) => {
|
|
69
|
+
let r = new v().fromArray(JSON.parse(t.matrix)).invert();
|
|
70
|
+
n ||= r.clone().invert();
|
|
71
|
+
let i = t.mat;
|
|
72
|
+
return {
|
|
73
|
+
data: t,
|
|
74
|
+
key: `${e}_${i.name}`,
|
|
75
|
+
material: i,
|
|
76
|
+
meshMatrix: r,
|
|
77
|
+
coordinationMatrix: n
|
|
78
|
+
};
|
|
79
|
+
}),
|
|
80
|
+
coordinationMatrix: n
|
|
81
|
+
};
|
|
82
|
+
}, [t, e]);
|
|
83
|
+
return t ? /* @__PURE__ */ A("group", {
|
|
84
|
+
matrix: n.coordinationMatrix ?? void 0,
|
|
85
|
+
matrixAutoUpdate: !!n.coordinationMatrix,
|
|
86
|
+
userData: { modelID: e },
|
|
87
|
+
children: n.data.map(({ data: e, key: t, material: n, meshMatrix: r }) => /* @__PURE__ */ A(te, {
|
|
88
|
+
material: n,
|
|
89
|
+
data: e,
|
|
90
|
+
meshMatrix: r
|
|
91
|
+
}, t))
|
|
92
|
+
}) : /* @__PURE__ */ A(k, {});
|
|
93
|
+
}
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/viewer/GeometryUtils/useGeometryIndex.ts
|
|
96
|
+
function re(e) {
|
|
97
|
+
return e instanceof Uint16Array || e instanceof Uint32Array ? e : e.length > 65535 ? new Uint32Array(e) : new Uint16Array(e);
|
|
98
|
+
}
|
|
99
|
+
function ie(e, t, n, r) {
|
|
100
|
+
let i = n.get(e);
|
|
101
|
+
if (i) return i;
|
|
102
|
+
i = {
|
|
103
|
+
record: t,
|
|
104
|
+
vertexRanges: []
|
|
105
|
+
}, n.set(e, i);
|
|
106
|
+
let a = r.get(e);
|
|
107
|
+
return a || (a = [], r.set(e, a)), a.push(i), i;
|
|
108
|
+
}
|
|
109
|
+
function ae(e, t, n, r, i, a) {
|
|
110
|
+
n && ie(e, r, i, a).vertexRanges.push({
|
|
111
|
+
count: n,
|
|
112
|
+
start: t
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function oe(e, t, n, r, i, a) {
|
|
116
|
+
n && (ie(e, r, i, a), r.triangleRanges.push({
|
|
117
|
+
count: n,
|
|
118
|
+
id: e,
|
|
119
|
+
start: t
|
|
120
|
+
}));
|
|
121
|
+
}
|
|
122
|
+
function se(e) {
|
|
123
|
+
let t = e;
|
|
124
|
+
for (; t;) {
|
|
125
|
+
let e = t.userData.modelID;
|
|
126
|
+
if (typeof e == "number") return e;
|
|
127
|
+
t = t.parent;
|
|
128
|
+
}
|
|
129
|
+
return 0;
|
|
130
|
+
}
|
|
131
|
+
function ce(e) {
|
|
132
|
+
let t = c({
|
|
133
|
+
elementIndex: /* @__PURE__ */ new Map(),
|
|
134
|
+
indexedModel: null,
|
|
135
|
+
isolatedIdsByModel: null,
|
|
136
|
+
isolatedIds: null,
|
|
137
|
+
records: /* @__PURE__ */ new Set()
|
|
138
|
+
});
|
|
139
|
+
return { buildIndex: r(() => {
|
|
140
|
+
let n = e.current, r = t.current;
|
|
141
|
+
if (!n || r.indexedModel === n && r.records.size) return r;
|
|
142
|
+
let i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Set();
|
|
143
|
+
return n.traverse((e) => {
|
|
144
|
+
if (!(e instanceof y)) return;
|
|
145
|
+
let t = e, n = t.geometry, r = n.index, o = n.attributes.ids;
|
|
146
|
+
if (!r || !o) return;
|
|
147
|
+
let s = se(t), c = re(r.array), l = {
|
|
148
|
+
hiddenIds: /* @__PURE__ */ new Set(),
|
|
149
|
+
initialVisible: t.visible,
|
|
150
|
+
mesh: t,
|
|
151
|
+
modelID: s,
|
|
152
|
+
originalBoundsTree: n.boundsTree ?? null,
|
|
153
|
+
originalIndex: c,
|
|
154
|
+
visibleStateKey: "",
|
|
155
|
+
triangleRanges: []
|
|
156
|
+
}, u = /* @__PURE__ */ new Map(), d = null, f = 0, p = c.length / 3;
|
|
157
|
+
for (let e = 0; e < p; e++) {
|
|
158
|
+
let t = o.getX(c[e * 3]);
|
|
159
|
+
d === null ? (d = t, f = e) : t !== d && (oe(d, f, e - f, l, u, i), d = t, f = e);
|
|
160
|
+
}
|
|
161
|
+
d !== null && oe(d, f, p - f, l, u, i);
|
|
162
|
+
let m = null, h = 0;
|
|
163
|
+
for (let e = 0; e < o.count; e++) {
|
|
164
|
+
let t = o.getX(e);
|
|
165
|
+
m === null ? (m = t, h = e) : t !== m && (ae(m, h, e - h, l, u, i), m = t, h = e);
|
|
166
|
+
}
|
|
167
|
+
m !== null && ae(m, h, o.count - h, l, u, i), a.add(l);
|
|
168
|
+
}), t.current = {
|
|
169
|
+
elementIndex: i,
|
|
170
|
+
indexedModel: n,
|
|
171
|
+
isolatedIdsByModel: null,
|
|
172
|
+
isolatedIds: null,
|
|
173
|
+
records: a
|
|
174
|
+
}, t.current;
|
|
175
|
+
}, [e]) };
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/viewer/store/ViewerStoreContext.ts
|
|
179
|
+
var le = t(null);
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/viewer/store/useViewerStore.ts
|
|
182
|
+
function P(e) {
|
|
183
|
+
return M(F(), e);
|
|
184
|
+
}
|
|
185
|
+
function F() {
|
|
186
|
+
let e = i(le);
|
|
187
|
+
if (!e) throw Error("useViewerStore must be used inside ViewerStoreProvider");
|
|
188
|
+
return e;
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/viewer/GeometryUtils/useGeometryUtils.ts
|
|
192
|
+
function ue(e, t) {
|
|
193
|
+
return e instanceof Uint32Array ? new Uint32Array(t) : new Uint16Array(t);
|
|
194
|
+
}
|
|
195
|
+
function de(e) {
|
|
196
|
+
let t = e.geometry;
|
|
197
|
+
Object.assign(t, { boundsTree: void 0 }), e.geometry.index?.count && Object.assign(t, { boundsTree: new E(e.geometry, { indirect: !0 }) });
|
|
198
|
+
}
|
|
199
|
+
function fe(e, t, n = !0) {
|
|
200
|
+
e.mesh.geometry.setIndex(new h(t, 1)), n && de(e.mesh);
|
|
201
|
+
}
|
|
202
|
+
function I(e) {
|
|
203
|
+
let t = e.mesh.geometry, n = t.index?.array;
|
|
204
|
+
e.mesh.visible = e.initialVisible, n !== e.originalIndex && fe(e, e.originalIndex, !1), e.originalBoundsTree ? Object.assign(t, { boundsTree: e.originalBoundsTree }) : (de(e.mesh), e.originalBoundsTree = e.mesh.geometry.boundsTree ?? null);
|
|
205
|
+
}
|
|
206
|
+
function pe(e) {
|
|
207
|
+
return e === null ? "*" : e.size ? Array.from(e).sort((e, t) => e - t).join(",") : "";
|
|
208
|
+
}
|
|
209
|
+
function me(e, t) {
|
|
210
|
+
return `h:${pe(e)}|i:${pe(t)}`;
|
|
211
|
+
}
|
|
212
|
+
function L(e, t, n) {
|
|
213
|
+
return !t.has(e) && (!n || n.has(e));
|
|
214
|
+
}
|
|
215
|
+
function R(e, t) {
|
|
216
|
+
Object.entries(e).forEach(([e, n]) => {
|
|
217
|
+
n.forEach((n) => t(Number(e), n));
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
function z(e, t, n) {
|
|
221
|
+
e.hiddenIds.has(t) || (e.hiddenIds.add(t), n.add(e));
|
|
222
|
+
}
|
|
223
|
+
function B(e, t, n) {
|
|
224
|
+
e && console.log(`${t}:`, Date.now() - n, "ms");
|
|
225
|
+
}
|
|
226
|
+
function V(e, t, n) {
|
|
227
|
+
let { hiddenIds: r, originalIndex: i, triangleRanges: a } = e, o = me(r, t);
|
|
228
|
+
if (e.visibleStateKey === o) return;
|
|
229
|
+
if (!r.size && !t) {
|
|
230
|
+
I(e), e.visibleStateKey = o;
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
let s = 0, c = 0;
|
|
234
|
+
if (a.forEach(({ count: e, id: n }) => {
|
|
235
|
+
L(n, r, t) ? s += e * 3 : c += e * 3;
|
|
236
|
+
}), !s) {
|
|
237
|
+
I(e), e.mesh.visible = !1, e.visibleStateKey = o;
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
if (!c) {
|
|
241
|
+
I(e), e.visibleStateKey = o;
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
e.mesh.visible = e.initialVisible;
|
|
245
|
+
let l = ue(i, s), u = 0;
|
|
246
|
+
for (let e = 0; e < a.length; e++) {
|
|
247
|
+
let { count: n, id: o, start: s } = a[e];
|
|
248
|
+
if (!L(o, r, t)) continue;
|
|
249
|
+
let c = s * 3, d = c + n * 3;
|
|
250
|
+
l.set(i.subarray(c, d), u), u += n * 3;
|
|
251
|
+
}
|
|
252
|
+
n || (e.originalBoundsTree = null), fe(e, l), e.visibleStateKey = o;
|
|
253
|
+
}
|
|
254
|
+
function H(e, t) {
|
|
255
|
+
return e.isolatedIdsByModel ? e.isolatedIdsByModel.get(t.modelID) ?? /* @__PURE__ */ new Set() : e.isolatedIds;
|
|
256
|
+
}
|
|
257
|
+
function he(e) {
|
|
258
|
+
let t = P((e) => e.selected), n = P((e) => e.resetSelected), i = P((e) => e.showStats), a = P((e) => e.userDevice) === "pc", o = e.buildIndex, s = r(() => Array.from(o().elementIndex.keys()), [o]), c = r((e) => {
|
|
259
|
+
let t = Date.now(), n = o(), r = /* @__PURE__ */ new Set();
|
|
260
|
+
e.forEach((e) => {
|
|
261
|
+
let t = n.elementIndex.get(e);
|
|
262
|
+
t && t.forEach(({ record: t }) => {
|
|
263
|
+
z(t, e, r);
|
|
264
|
+
});
|
|
265
|
+
}), r.forEach((e) => V(e, H(n, e), a)), B(i, "hideByIds", t);
|
|
266
|
+
}, [
|
|
267
|
+
o,
|
|
268
|
+
a,
|
|
269
|
+
i
|
|
270
|
+
]), l = r(() => {
|
|
271
|
+
let e = Date.now(), r = o(), s = /* @__PURE__ */ new Set();
|
|
272
|
+
R(t, (e, t) => {
|
|
273
|
+
let n = r.elementIndex.get(t);
|
|
274
|
+
n && n.forEach(({ record: n }) => {
|
|
275
|
+
n.modelID === e && z(n, t, s);
|
|
276
|
+
});
|
|
277
|
+
}), s.forEach((e) => V(e, H(r, e), a)), n(), B(i, "hideSelected", e);
|
|
278
|
+
}, [
|
|
279
|
+
o,
|
|
280
|
+
a,
|
|
281
|
+
n,
|
|
282
|
+
t,
|
|
283
|
+
i
|
|
284
|
+
]), u = r((e) => {
|
|
285
|
+
let t = Date.now(), n = o(), r = /* @__PURE__ */ new Set();
|
|
286
|
+
e.forEach((e) => {
|
|
287
|
+
let t = n.elementIndex.get(e);
|
|
288
|
+
t && t.forEach(({ record: t }) => {
|
|
289
|
+
t.hiddenIds.delete(e) && r.add(t);
|
|
290
|
+
});
|
|
291
|
+
}), r.forEach((e) => V(e, H(n, e), a)), B(i, "showByIds", t);
|
|
292
|
+
}, [
|
|
293
|
+
o,
|
|
294
|
+
a,
|
|
295
|
+
i
|
|
296
|
+
]);
|
|
297
|
+
return {
|
|
298
|
+
buildIndex: o,
|
|
299
|
+
getAllIds: s,
|
|
300
|
+
hideByIds: c,
|
|
301
|
+
hideSelected: l,
|
|
302
|
+
isolateByIds: r((e) => {
|
|
303
|
+
let t = Date.now(), n = o();
|
|
304
|
+
n.isolatedIdsByModel = null, n.isolatedIds = new Set(e), n.records.forEach((e) => V(e, H(n, e), a)), B(i, "isolateByIds", t);
|
|
305
|
+
}, [
|
|
306
|
+
o,
|
|
307
|
+
a,
|
|
308
|
+
i
|
|
309
|
+
]),
|
|
310
|
+
isolateSelected: r(() => {
|
|
311
|
+
let e = Date.now(), n = o(), r = /* @__PURE__ */ new Map();
|
|
312
|
+
R(t, (e, t) => {
|
|
313
|
+
let n = r.get(e);
|
|
314
|
+
n || (n = /* @__PURE__ */ new Set(), r.set(e, n)), n.add(t);
|
|
315
|
+
}), n.isolatedIdsByModel = r, n.isolatedIds = null, n.records.forEach((e) => V(e, H(n, e), a)), B(i, "isolateSelected", e);
|
|
316
|
+
}, [
|
|
317
|
+
o,
|
|
318
|
+
a,
|
|
319
|
+
t,
|
|
320
|
+
i
|
|
321
|
+
]),
|
|
322
|
+
resetIsolation: r(() => {
|
|
323
|
+
let e = Date.now(), t = o();
|
|
324
|
+
t.isolatedIdsByModel = null, t.isolatedIds = null, t.records.forEach((e) => V(e, H(t, e), a)), B(i, "resetIsolation", e);
|
|
325
|
+
}, [
|
|
326
|
+
o,
|
|
327
|
+
a,
|
|
328
|
+
i
|
|
329
|
+
]),
|
|
330
|
+
showAll: r(() => {
|
|
331
|
+
let e = Date.now(), t = o();
|
|
332
|
+
t.isolatedIdsByModel = null, t.isolatedIds = null, t.records.forEach((e) => {
|
|
333
|
+
e.hiddenIds.clear(), V(e, H(t, e), a);
|
|
334
|
+
}), B(i, "showAll", e);
|
|
335
|
+
}, [
|
|
336
|
+
o,
|
|
337
|
+
a,
|
|
338
|
+
i
|
|
339
|
+
]),
|
|
340
|
+
showByIds: u
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
//#endregion
|
|
344
|
+
//#region src/viewer/Hotkeys/useHotkeysUtils.tsx
|
|
345
|
+
function ge(e) {
|
|
346
|
+
a(() => {
|
|
347
|
+
let t = (t) => {
|
|
348
|
+
t.code === "KeyI" && e.isolateSelected(), t.code === "KeyH" && e.hideSelected(), t.code === "KeyC" && e.showAll();
|
|
349
|
+
};
|
|
350
|
+
return window.addEventListener("keydown", t), () => {
|
|
351
|
+
window.removeEventListener("keydown", t);
|
|
352
|
+
};
|
|
353
|
+
}, [P((e) => e.selected), e]);
|
|
354
|
+
}
|
|
355
|
+
//#endregion
|
|
356
|
+
//#region src/viewer/Selector/SelectionBox.ts
|
|
357
|
+
var _e = new C(), U = new C(), W = new S();
|
|
358
|
+
function ve(e) {
|
|
359
|
+
let t = e;
|
|
360
|
+
for (; t;) {
|
|
361
|
+
let e = t.modelID ?? t.userData.modelID;
|
|
362
|
+
if (typeof e == "number") return e;
|
|
363
|
+
t = t.parent;
|
|
364
|
+
}
|
|
365
|
+
return 0;
|
|
366
|
+
}
|
|
367
|
+
function ye(e) {
|
|
368
|
+
let t = e.getBoundingClientRect();
|
|
369
|
+
return {
|
|
370
|
+
height: t.height,
|
|
371
|
+
left: t.left,
|
|
372
|
+
top: t.top,
|
|
373
|
+
width: t.width
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function be(e) {
|
|
377
|
+
return {
|
|
378
|
+
boundsCheckMs: 0,
|
|
379
|
+
candidateIds: 0,
|
|
380
|
+
containedCandidateVertices: 0,
|
|
381
|
+
containedIdsMs: 0,
|
|
382
|
+
containedVerticesVisited: 0,
|
|
383
|
+
intersectsBoundsCalls: 0,
|
|
384
|
+
intersectsBoundsMs: 0,
|
|
385
|
+
intersectsRangeCalls: 0,
|
|
386
|
+
intersectsRangeMs: 0,
|
|
387
|
+
intersectsTriangleCalls: 0,
|
|
388
|
+
intersectsTriangleMs: 0,
|
|
389
|
+
matrixUpdateMs: 0,
|
|
390
|
+
meshCount: 0,
|
|
391
|
+
mode: e,
|
|
392
|
+
selectedIds: 0,
|
|
393
|
+
shapecastMs: 0,
|
|
394
|
+
skippedMeshCount: 0,
|
|
395
|
+
totalMs: 0,
|
|
396
|
+
trianglePointCalls: 0,
|
|
397
|
+
trianglePointMs: 0,
|
|
398
|
+
trianglePointVertices: 0,
|
|
399
|
+
triangleProjectionMs: 0
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function xe(e, t) {
|
|
403
|
+
return {
|
|
404
|
+
minX: Math.min(e.x, t.x),
|
|
405
|
+
minY: Math.min(e.y, t.y),
|
|
406
|
+
maxX: Math.max(e.x, t.x),
|
|
407
|
+
maxY: Math.max(e.y, t.y)
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function Se(e, t) {
|
|
411
|
+
return t.x >= e.x ? "contains" : "intersects";
|
|
412
|
+
}
|
|
413
|
+
function G(e, t) {
|
|
414
|
+
return Ce(e.x, e.y, t);
|
|
415
|
+
}
|
|
416
|
+
function Ce(e, t, n) {
|
|
417
|
+
return e >= n.minX && e <= n.maxX && t >= n.minY && t <= n.maxY;
|
|
418
|
+
}
|
|
419
|
+
function K(e, t, n, r, i, a) {
|
|
420
|
+
return (a - t) * (n - e) > (r - t) * (i - e);
|
|
421
|
+
}
|
|
422
|
+
function q(e, t, n, r, i, a, o, s) {
|
|
423
|
+
return K(e, t, i, a, o, s) !== K(n, r, i, a, o, s) && K(e, t, n, r, i, a) !== K(e, t, n, r, o, s);
|
|
424
|
+
}
|
|
425
|
+
function J(e, t, n, r, i) {
|
|
426
|
+
let a = .5 * (-r.y * i.x + n.y * (-r.x + i.x) + n.x * (r.y - i.y) + r.x * i.y), o = a < 0 ? -1 : 1, s = (n.y * i.x - n.x * i.y + (i.y - n.y) * e + (n.x - i.x) * t) * o, c = (n.x * r.y - n.y * r.x + (n.y - r.y) * e + (r.x - n.x) * t) * o;
|
|
427
|
+
return s >= 0 && c >= 0 && s + c <= 2 * a * o;
|
|
428
|
+
}
|
|
429
|
+
function we(e, t, n, r) {
|
|
430
|
+
return G(e, r) || G(t, r) || G(n, r) || J(r.minX, r.minY, e, t, n) || J(r.maxX, r.minY, e, t, n) || J(r.maxX, r.maxY, e, t, n) || J(r.minX, r.maxY, e, t, n) ? !0 : Y(e, t, r) || Y(t, n, r) || Y(n, e, r);
|
|
431
|
+
}
|
|
432
|
+
function Y(e, t, n) {
|
|
433
|
+
return q(e.x, e.y, t.x, t.y, n.minX, n.minY, n.maxX, n.minY) || q(e.x, e.y, t.x, t.y, n.maxX, n.minY, n.maxX, n.maxY) || q(e.x, e.y, t.x, t.y, n.maxX, n.maxY, n.minX, n.maxY) || q(e.x, e.y, t.x, t.y, n.minX, n.maxY, n.minX, n.minY);
|
|
434
|
+
}
|
|
435
|
+
function X(e, t, n, r, i = new S()) {
|
|
436
|
+
let a = _e.copy(t).applyMatrix4(e.matrixWorld).project(n);
|
|
437
|
+
return i.set((a.x + 1) / 2 * r.width + r.left, (-a.y + 1) / 2 * r.height + r.top);
|
|
438
|
+
}
|
|
439
|
+
function Te(e, t, n, r, i) {
|
|
440
|
+
let a = Infinity, o = -Infinity, s = Infinity, c = -Infinity, l = 0;
|
|
441
|
+
for (let u = 0; u < 2; u++) for (let d = 0; d < 2; d++) for (let f = 0; f < 2; f++) U.set(u ? t.max.x : t.min.x, d ? t.max.y : t.min.y, f ? t.max.z : t.min.z), X(e, U, r, i, W), a = Math.min(a, W.x), o = Math.max(o, W.x), s = Math.min(s, W.y), c = Math.max(c, W.y), G(W, n) && (l += 1);
|
|
442
|
+
return o < n.minX || a > n.maxX || c < n.minY || s > n.maxY ? D : l === 8 ? w : T;
|
|
443
|
+
}
|
|
444
|
+
function Ee(e, t, n, r) {
|
|
445
|
+
let i = Infinity, a = -Infinity, o = Infinity, s = -Infinity;
|
|
446
|
+
for (let c = 0; c < 2; c++) for (let l = 0; l < 2; l++) for (let u = 0; u < 2; u++) U.set(c ? t.max.x : t.min.x, l ? t.max.y : t.min.y, u ? t.max.z : t.min.z), X(e, U, n, r, W), i = Math.min(i, W.x), a = Math.max(a, W.x), o = Math.min(o, W.y), s = Math.max(s, W.y);
|
|
447
|
+
return {
|
|
448
|
+
minX: i,
|
|
449
|
+
minY: o,
|
|
450
|
+
maxX: a,
|
|
451
|
+
maxY: s
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
function De(e, t) {
|
|
455
|
+
return !(e.maxX < t.minX || e.minX > t.maxX || e.maxY < t.minY || e.minY > t.maxY);
|
|
456
|
+
}
|
|
457
|
+
function Oe(e, t, n, r) {
|
|
458
|
+
e.geometry.boundingBox || e.geometry.computeBoundingBox();
|
|
459
|
+
let i = e.geometry.boundingBox;
|
|
460
|
+
return i ? De(Ee(e, i, n, r), t) : !0;
|
|
461
|
+
}
|
|
462
|
+
function ke(e, t, n) {
|
|
463
|
+
let r = e.get(t);
|
|
464
|
+
r || (r = /* @__PURE__ */ new Set(), e.set(t, r)), r.add(n);
|
|
465
|
+
}
|
|
466
|
+
function Ae(e) {
|
|
467
|
+
let t = 0;
|
|
468
|
+
return e.forEach((e) => {
|
|
469
|
+
t += e.size;
|
|
470
|
+
}), t;
|
|
471
|
+
}
|
|
472
|
+
function Z(e, t, n) {
|
|
473
|
+
let r = e.geometry, i = r.index, a = r.attributes.ids;
|
|
474
|
+
if (!i || !a) return;
|
|
475
|
+
let o = t * 3;
|
|
476
|
+
ke(n, ve(e), a.getX(i.getX(o)));
|
|
477
|
+
}
|
|
478
|
+
function je({ model: e, camera: t, viewport: n, rect: r, candidateIdsByModel: i, profile: a }) {
|
|
479
|
+
let o = performance.now(), s = /* @__PURE__ */ new Map(), c = new C();
|
|
480
|
+
e.traverse((e) => {
|
|
481
|
+
let o = e;
|
|
482
|
+
if (!o.isMesh) return;
|
|
483
|
+
let l = o.geometry.attributes.ids, u = o.geometry.attributes.position;
|
|
484
|
+
if (!(!l || !u)) {
|
|
485
|
+
a && (a.containedVerticesVisited += l.count);
|
|
486
|
+
for (let e = 0; e < l.count; e++) {
|
|
487
|
+
let d = ve(o), f = l.getX(e);
|
|
488
|
+
if (!i.get(d)?.has(f)) continue;
|
|
489
|
+
a && (a.containedCandidateVertices += 1);
|
|
490
|
+
let p = `${d}:${f}`, m = s.get(p);
|
|
491
|
+
m || (m = {
|
|
492
|
+
id: f,
|
|
493
|
+
inside: 0,
|
|
494
|
+
modelID: d,
|
|
495
|
+
total: 0
|
|
496
|
+
}, s.set(p, m)), c.fromBufferAttribute(u, e), m.total += 1, G(X(o, c, t, n), r) && (m.inside += 1);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
let l = /* @__PURE__ */ new Map();
|
|
501
|
+
return s.forEach((e) => {
|
|
502
|
+
e.total > 0 && e.total === e.inside && ke(l, e.modelID, e.id);
|
|
503
|
+
}), a && (a.containedIdsMs += performance.now() - o), l;
|
|
504
|
+
}
|
|
505
|
+
function Me({ model: e, camera: t, domElement: n, rect: r, mode: i, profile: a }) {
|
|
506
|
+
let o = performance.now(), s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map();
|
|
507
|
+
if (!e) return s;
|
|
508
|
+
let l = ye(n), u = performance.now();
|
|
509
|
+
e.updateMatrixWorld(!0), t.updateMatrixWorld(), a && (a.matrixUpdateMs += performance.now() - u), e.traverse((e) => {
|
|
510
|
+
let n = e;
|
|
511
|
+
if (!n.isMesh) return;
|
|
512
|
+
a && (a.meshCount += 1);
|
|
513
|
+
let o = n.geometry.boundsTree;
|
|
514
|
+
if (!o || !n.geometry.index || !n.geometry.attributes.ids) {
|
|
515
|
+
a && (a.skippedMeshCount += 1);
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
let u = performance.now(), d = Oe(n, r, t, l);
|
|
519
|
+
if (a && (a.boundsCheckMs += performance.now() - u), !d) {
|
|
520
|
+
a && (a.skippedMeshCount += 1);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
let f = performance.now();
|
|
524
|
+
o.shapecast({
|
|
525
|
+
intersectsBounds: (e) => {
|
|
526
|
+
let i = performance.now(), o = Te(n, e, r, t, l);
|
|
527
|
+
return a && (a.intersectsBoundsCalls += 1, a.intersectsBoundsMs += performance.now() - i), o;
|
|
528
|
+
},
|
|
529
|
+
intersectsRange: (e, t, r) => {
|
|
530
|
+
let l = performance.now();
|
|
531
|
+
if (a && (a.intersectsRangeCalls += 1), !r) return a && (a.intersectsRangeMs += performance.now() - l), !1;
|
|
532
|
+
for (let r = e; r < e + t; r++) Z(n, o.resolveTriangleIndex(r), i === "contains" ? c : s);
|
|
533
|
+
return a && (a.intersectsRangeMs += performance.now() - l), !1;
|
|
534
|
+
},
|
|
535
|
+
intersectsTriangle: (e, o, u) => {
|
|
536
|
+
let d = performance.now();
|
|
537
|
+
if (a && (a.intersectsTriangleCalls += 1), !u) {
|
|
538
|
+
let i = performance.now(), o = X(n, e.a, t, l), s = X(n, e.b, t, l), c = X(n, e.c, t, l);
|
|
539
|
+
if (a && (a.triangleProjectionMs += performance.now() - i), !we(o, s, c, r)) return a && (a.intersectsTriangleMs += performance.now() - d), !1;
|
|
540
|
+
}
|
|
541
|
+
return i === "contains" ? Z(n, o, c) : Z(n, o, s), a && (a.intersectsTriangleMs += performance.now() - d), !1;
|
|
542
|
+
}
|
|
543
|
+
}), a && (a.shapecastMs += performance.now() - f);
|
|
544
|
+
});
|
|
545
|
+
let d = i === "contains" ? je({
|
|
546
|
+
model: e,
|
|
547
|
+
camera: t,
|
|
548
|
+
viewport: l,
|
|
549
|
+
rect: r,
|
|
550
|
+
candidateIdsByModel: c,
|
|
551
|
+
profile: a
|
|
552
|
+
}) : s;
|
|
553
|
+
return a && (a.candidateIds = Ae(c), a.selectedIds = Ae(d), a.totalMs = performance.now() - o), d;
|
|
554
|
+
}
|
|
555
|
+
function Ne(e) {
|
|
556
|
+
let t = be(e.mode);
|
|
557
|
+
return {
|
|
558
|
+
ids: Me({
|
|
559
|
+
...e,
|
|
560
|
+
profile: t
|
|
561
|
+
}),
|
|
562
|
+
profile: t
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
function Pe(e) {
|
|
566
|
+
return e === "contains" ? {
|
|
567
|
+
border: "rgb(37, 99, 235)",
|
|
568
|
+
background: "rgba(37, 99, 235, 0.16)"
|
|
569
|
+
} : {
|
|
570
|
+
border: "rgb(22, 163, 74)",
|
|
571
|
+
background: "rgba(22, 163, 74, 0.16)"
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function Fe(e, t, n) {
|
|
575
|
+
let r = Pe(Se(t, n));
|
|
576
|
+
if (!e.current) {
|
|
577
|
+
let t = document.createElement("div");
|
|
578
|
+
t.style.position = "fixed", t.style.pointerEvents = "none", t.style.zIndex = "10", e.current = t, document.body.appendChild(t);
|
|
579
|
+
}
|
|
580
|
+
let i = xe(t, n);
|
|
581
|
+
e.current.style.border = `1px solid ${r.border}`, e.current.style.background = r.background, e.current.style.left = `${i.minX}px`, e.current.style.top = `${i.minY}px`, e.current.style.width = `${i.maxX - i.minX}px`, e.current.style.height = `${i.maxY - i.minY}px`;
|
|
582
|
+
}
|
|
583
|
+
function Ie(e) {
|
|
584
|
+
e.current?.remove(), e.current = null;
|
|
585
|
+
}
|
|
586
|
+
//#endregion
|
|
587
|
+
//#region src/viewer/Selector/useSelector.tsx
|
|
588
|
+
function Le(e) {
|
|
589
|
+
if (!Array.isArray(e)) return e.userData.shader;
|
|
590
|
+
}
|
|
591
|
+
function Re(e) {
|
|
592
|
+
return Number(e.current?.modelID ?? 0);
|
|
593
|
+
}
|
|
594
|
+
function ze(e) {
|
|
595
|
+
let t = e;
|
|
596
|
+
for (; t;) {
|
|
597
|
+
let e = t.modelID ?? t.userData.modelID;
|
|
598
|
+
if (typeof e == "number") return e;
|
|
599
|
+
t = t.parent;
|
|
600
|
+
}
|
|
601
|
+
return 0;
|
|
602
|
+
}
|
|
603
|
+
function Be(e, t, n) {
|
|
604
|
+
let r = c("end"), i = P((e) => e.selected), o = P((e) => e.setSelected), s = P((e) => e.addSelected), l = P((e) => e.removeSelected), u = P((e) => e.showStats), f = c(null), p = c(null), m = c(!1), h = c(!1), g = c("add"), _ = c(null), v = c(null), b = c(/* @__PURE__ */ new Set()), x = (t) => {
|
|
605
|
+
if (f.current || r.current !== "end" && r.current !== "start") return;
|
|
606
|
+
let n = N(t, e.current)[0], i;
|
|
607
|
+
n && (i = n.object), e.current?.traverse((e) => {
|
|
608
|
+
if (!(e instanceof y)) return;
|
|
609
|
+
let t = e;
|
|
610
|
+
if (n && t === i) {
|
|
611
|
+
let e = n.faceIndex * 3, r = t.geometry.index.getX(e), i = t.geometry.attributes.ids.getX(r);
|
|
612
|
+
t.preselected = !0;
|
|
613
|
+
let a = Le(t.material);
|
|
614
|
+
if (!a) return;
|
|
615
|
+
a.uniforms.preSelectionId?.value !== i && (a.uniforms.preSelectionId = { value: i });
|
|
616
|
+
} else if (t.preselected) {
|
|
617
|
+
t.preselected = !1;
|
|
618
|
+
let e = Le(t.material);
|
|
619
|
+
if (!e) return;
|
|
620
|
+
e.uniforms.preSelectionId = { value: null };
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
}, C = c(null);
|
|
624
|
+
a(() => {
|
|
625
|
+
if (!e.current) return;
|
|
626
|
+
let t = n.buildIndex();
|
|
627
|
+
_.current !== t.indexedModel && (_.current = t.indexedModel, b.current = /* @__PURE__ */ new Set());
|
|
628
|
+
let r = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set();
|
|
629
|
+
Object.entries(i).forEach(([e, t]) => {
|
|
630
|
+
t.forEach((t) => {
|
|
631
|
+
r.add(t), a.add(`${e}:${t}`);
|
|
632
|
+
});
|
|
633
|
+
});
|
|
634
|
+
let o = /* @__PURE__ */ new Set(), s = b.current;
|
|
635
|
+
a.forEach((e) => {
|
|
636
|
+
if (!s.has(e)) {
|
|
637
|
+
let t = Number(e.split(":")[1]);
|
|
638
|
+
o.add(t);
|
|
639
|
+
}
|
|
640
|
+
}), s.forEach((e) => {
|
|
641
|
+
if (!a.has(e)) {
|
|
642
|
+
let t = Number(e.split(":")[1]);
|
|
643
|
+
o.add(t);
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
let c = /* @__PURE__ */ new Set();
|
|
647
|
+
o.forEach((e) => {
|
|
648
|
+
let n = t.elementIndex.get(e);
|
|
649
|
+
n && n.forEach(({ record: t, vertexRanges: n }) => {
|
|
650
|
+
let r = +!!i[t.modelID]?.has(e), a = t.mesh, o = a.geometry.attributes.selected;
|
|
651
|
+
if (!o) return;
|
|
652
|
+
let s = o.array;
|
|
653
|
+
n.forEach(({ count: e, start: t }) => {
|
|
654
|
+
s.fill(r, t, t + e);
|
|
655
|
+
}), c.add(a);
|
|
656
|
+
});
|
|
657
|
+
}), c.forEach((e) => {
|
|
658
|
+
let t = e.geometry.attributes.selected;
|
|
659
|
+
t && (t.needsUpdate = !0);
|
|
660
|
+
}), b.current = new Set(a);
|
|
661
|
+
let l = v.current;
|
|
662
|
+
if (l) {
|
|
663
|
+
u && C.current !== null && console.log(`selection ${l.action}:`, Date.now() - C.current, "ms"), v.current = null, C.current = null;
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
u && C.current !== null && (console.log("selection spend:", Date.now() - C.current, "ms"), C.current = null);
|
|
667
|
+
}, [i, u]);
|
|
668
|
+
function w() {
|
|
669
|
+
return Date.now();
|
|
670
|
+
}
|
|
671
|
+
let T = (t) => {
|
|
672
|
+
if (h.current) {
|
|
673
|
+
h.current = !1;
|
|
674
|
+
return;
|
|
675
|
+
}
|
|
676
|
+
if (C.current = w(), r.current !== "end" && r.current !== "start" && r.current > 15) {
|
|
677
|
+
r.current = "end";
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
let n = t.shiftKey, i = t.ctrlKey, a = N(t, e.current), c, u = Re(e);
|
|
681
|
+
if (a.length) {
|
|
682
|
+
let e = a[0], t = e.object;
|
|
683
|
+
u = ze(t);
|
|
684
|
+
let n = e.faceIndex * 3, r = t.geometry.index.getX(n);
|
|
685
|
+
c = t.geometry.attributes.ids.getX(r);
|
|
686
|
+
}
|
|
687
|
+
if (i) {
|
|
688
|
+
c !== void 0 && l(u, [c]);
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
if (n) {
|
|
692
|
+
c !== void 0 && s(u, [c]);
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
o(u, c === void 0 ? [] : [c], !0);
|
|
696
|
+
}, { camera: E, gl: D, raycaster: O } = d(), k = c(new S()), A = (e) => {
|
|
697
|
+
e.button !== 0 || !e.shiftKey && !e.ctrlKey || (f.current = new S(e.clientX, e.clientY), g.current = e.ctrlKey ? "remove" : "add", m.current = !1, t.current && (t.current.enabled = !1));
|
|
698
|
+
}, j = (e) => {
|
|
699
|
+
if (!f.current) return;
|
|
700
|
+
let t = new S(e.clientX, e.clientY), n = Math.abs(t.x - f.current.x), r = Math.abs(t.y - f.current.y);
|
|
701
|
+
n < 6 && r < 6 || (m.current ||= !0, e.preventDefault(), Fe(p, f.current, t));
|
|
702
|
+
}, M = (n) => {
|
|
703
|
+
if (!f.current) return;
|
|
704
|
+
C.current = w();
|
|
705
|
+
let r = m.current, i = new S(n.clientX, n.clientY), a = xe(f.current, i), o = Se(f.current, i);
|
|
706
|
+
if (f.current = null, m.current = !1, Ie(p), t.current && setTimeout(() => {
|
|
707
|
+
t.current && (t.current.enabled = !0);
|
|
708
|
+
}, 100), !r) return;
|
|
709
|
+
h.current = !0, n.preventDefault();
|
|
710
|
+
let { ids: c, profile: u } = Ne({
|
|
711
|
+
model: e.current,
|
|
712
|
+
camera: E,
|
|
713
|
+
domElement: D.domElement,
|
|
714
|
+
rect: a,
|
|
715
|
+
mode: o
|
|
716
|
+
}), d = Array.from(c.values()).reduce((e, t) => e + t.size, 0), _ = {
|
|
717
|
+
action: g.current,
|
|
718
|
+
box: u,
|
|
719
|
+
ids: d
|
|
720
|
+
};
|
|
721
|
+
d && (v.current = _, c.forEach((e, t) => {
|
|
722
|
+
let n = Array.from(e);
|
|
723
|
+
g.current === "remove" ? l(t, n) : s(t, n);
|
|
724
|
+
}));
|
|
725
|
+
};
|
|
726
|
+
function ee(e) {
|
|
727
|
+
let t = D.domElement.getBoundingClientRect();
|
|
728
|
+
return {
|
|
729
|
+
x: (e.clientX - t.left) / t.width * 2 - 1,
|
|
730
|
+
y: -((e.clientY - t.top) / t.height) * 2 + 1
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
let N = (e, t) => {
|
|
734
|
+
let { x: n, y: r } = ee(e);
|
|
735
|
+
return k.current.set(n, r), O.setFromCamera(k.current, E), t ? O.intersectObject(t, !0) : [];
|
|
736
|
+
};
|
|
737
|
+
a(() => {
|
|
738
|
+
let e = t.current;
|
|
739
|
+
if (!e) return;
|
|
740
|
+
D.domElement.addEventListener("pointerdown", A), D.domElement.addEventListener("pointermove", j), window.addEventListener("pointerup", M), D.domElement.addEventListener("contextmenu", o), D.domElement.addEventListener("click", T), D.domElement.addEventListener("pointerup", a), D.domElement.addEventListener("pointermove", x), D.domElement.addEventListener("pointerdown", i), e.addEventListener("change", n);
|
|
741
|
+
function n() {
|
|
742
|
+
typeof r.current == "number" ? r.current += 1 : r.current === "start" && (r.current = 1);
|
|
743
|
+
}
|
|
744
|
+
function i() {
|
|
745
|
+
r.current = "start";
|
|
746
|
+
}
|
|
747
|
+
function a() {
|
|
748
|
+
setTimeout(() => {
|
|
749
|
+
r.current = "end";
|
|
750
|
+
}, 100);
|
|
751
|
+
}
|
|
752
|
+
function o(e) {
|
|
753
|
+
e.ctrlKey && e.preventDefault();
|
|
754
|
+
}
|
|
755
|
+
return () => {
|
|
756
|
+
Ie(p), e.removeEventListener("change", n), D.domElement.removeEventListener("pointerdown", A), D.domElement.removeEventListener("pointermove", j), window.removeEventListener("pointerup", M), D.domElement.removeEventListener("contextmenu", o), D.domElement.removeEventListener("pointerup", a), D.domElement.removeEventListener("pointerdown", i), D.domElement.removeEventListener("pointermove", x), D.domElement.removeEventListener("click", T);
|
|
757
|
+
};
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
//#endregion
|
|
761
|
+
//#region src/viewer/Camera/useCameraFit.tsx
|
|
762
|
+
function Ve() {
|
|
763
|
+
let { camera: e } = d();
|
|
764
|
+
return (t, n, r) => {
|
|
765
|
+
if (!t.current || !(e instanceof x)) return;
|
|
766
|
+
let i = new m().setFromObject(t.current), a = i.getCenter(new C()), o = i.getSize(new C()), s = Math.max(o.x, o.y, o.z), c = e.fov * (Math.PI / 180), l = s / (2 * Math.tan(c / 2));
|
|
767
|
+
l *= 1.5, e.position.set(a.x, a.y, a.z + l), e.lookAt(a), n.current && (n.current.target.copy(a), n.current.update()), r.current && (r.current.position.set(i.max.x * 1.3, i.max.y + 10, i.max.z * 1.3), r.current.lookAt(a));
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
//#endregion
|
|
771
|
+
//#region src/viewer/store/ViewerStore.ts
|
|
772
|
+
function Q() {
|
|
773
|
+
return ee((e) => ({
|
|
774
|
+
selected: {},
|
|
775
|
+
showStats: !1,
|
|
776
|
+
userDevice: "pc",
|
|
777
|
+
setShowStats: (t) => e({ showStats: t }),
|
|
778
|
+
setUserDevice: (t) => e({ userDevice: t }),
|
|
779
|
+
setSelection: (t) => e({ selected: t }),
|
|
780
|
+
setSelected: (t, n, r = !1) => {
|
|
781
|
+
e((e) => {
|
|
782
|
+
if (r) {
|
|
783
|
+
let r = {};
|
|
784
|
+
return Object.keys(e.selected).forEach((e) => {
|
|
785
|
+
r[Number(e)] = Number(e) === t ? new Set(n) : /* @__PURE__ */ new Set();
|
|
786
|
+
}), r[t] || (r[t] = new Set(n)), { selected: r };
|
|
787
|
+
}
|
|
788
|
+
return { selected: {
|
|
789
|
+
...e.selected,
|
|
790
|
+
[t]: new Set(n)
|
|
791
|
+
} };
|
|
792
|
+
});
|
|
793
|
+
},
|
|
794
|
+
addSelected: (t, n) => {
|
|
795
|
+
e((e) => {
|
|
796
|
+
let r = new Set(e.selected[t] ?? []);
|
|
797
|
+
return n.forEach((e) => r.add(e)), { selected: {
|
|
798
|
+
...e.selected,
|
|
799
|
+
[t]: r
|
|
800
|
+
} };
|
|
801
|
+
});
|
|
802
|
+
},
|
|
803
|
+
removeSelected: (t, n) => {
|
|
804
|
+
e((e) => {
|
|
805
|
+
let r = new Set(e.selected[t] ?? []);
|
|
806
|
+
return n.forEach((e) => r.delete(e)), { selected: {
|
|
807
|
+
...e.selected,
|
|
808
|
+
[t]: r
|
|
809
|
+
} };
|
|
810
|
+
});
|
|
811
|
+
},
|
|
812
|
+
resetSelected: (t) => {
|
|
813
|
+
e((e) => {
|
|
814
|
+
if (t !== void 0) return { selected: {
|
|
815
|
+
...e.selected,
|
|
816
|
+
[t]: /* @__PURE__ */ new Set()
|
|
817
|
+
} };
|
|
818
|
+
let n = {};
|
|
819
|
+
return Object.keys(e.selected).forEach((e) => {
|
|
820
|
+
n[Number(e)] = /* @__PURE__ */ new Set();
|
|
821
|
+
}), { selected: n };
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
}));
|
|
825
|
+
}
|
|
826
|
+
//#endregion
|
|
827
|
+
//#region src/viewer/store/ViewerStoreProvider.tsx
|
|
828
|
+
function $({ children: e, store: t }) {
|
|
829
|
+
let [n] = l(() => t ?? Q());
|
|
830
|
+
return /* @__PURE__ */ A(le.Provider, {
|
|
831
|
+
value: n,
|
|
832
|
+
children: e
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
//#endregion
|
|
836
|
+
//#region src/viewer/api/ViewerApi.ts
|
|
837
|
+
function He(e) {
|
|
838
|
+
let t = {};
|
|
839
|
+
return Object.entries(e).forEach(([e, n]) => {
|
|
840
|
+
t[Number(e)] = Array.from(n);
|
|
841
|
+
}), t;
|
|
842
|
+
}
|
|
843
|
+
function Ue(e) {
|
|
844
|
+
let t = {};
|
|
845
|
+
return Object.entries(e).forEach(([e, n]) => {
|
|
846
|
+
t[Number(e)] = new Set(n);
|
|
847
|
+
}), t;
|
|
848
|
+
}
|
|
849
|
+
function We(e, t) {
|
|
850
|
+
return {
|
|
851
|
+
camera: { fitCamera: () => {
|
|
852
|
+
t()?.camera.fitCamera();
|
|
853
|
+
} },
|
|
854
|
+
geometryUtils: {
|
|
855
|
+
getAllIds: () => t()?.geometryUtils.getAllIds() ?? [],
|
|
856
|
+
hideByIds: (e) => {
|
|
857
|
+
t()?.geometryUtils.hideByIds(e);
|
|
858
|
+
},
|
|
859
|
+
hideSelected: () => {
|
|
860
|
+
t()?.geometryUtils.hideSelected();
|
|
861
|
+
},
|
|
862
|
+
isolateByIds: (e) => {
|
|
863
|
+
t()?.geometryUtils.isolateByIds(e);
|
|
864
|
+
},
|
|
865
|
+
isolateSelected: () => {
|
|
866
|
+
t()?.geometryUtils.isolateSelected();
|
|
867
|
+
},
|
|
868
|
+
resetIsolation: () => {
|
|
869
|
+
t()?.geometryUtils.resetIsolation();
|
|
870
|
+
},
|
|
871
|
+
showAll: () => {
|
|
872
|
+
t()?.geometryUtils.showAll();
|
|
873
|
+
},
|
|
874
|
+
showByIds: (e) => {
|
|
875
|
+
t()?.geometryUtils.showByIds(e);
|
|
876
|
+
}
|
|
877
|
+
},
|
|
878
|
+
selector: {
|
|
879
|
+
addSelected: (t, n) => {
|
|
880
|
+
e.getState().addSelected(t, n);
|
|
881
|
+
},
|
|
882
|
+
getSelected: () => He(e.getState().selected),
|
|
883
|
+
removeSelected: (t, n) => {
|
|
884
|
+
e.getState().removeSelected(t, n);
|
|
885
|
+
},
|
|
886
|
+
resetSelection: (t) => {
|
|
887
|
+
e.getState().resetSelected(t);
|
|
888
|
+
},
|
|
889
|
+
setSelected: (t, n, r = !1) => {
|
|
890
|
+
e.getState().setSelected(t, n, r);
|
|
891
|
+
}
|
|
892
|
+
},
|
|
893
|
+
utils: {
|
|
894
|
+
getShowStats: () => e.getState().showStats,
|
|
895
|
+
getUserDevice: () => e.getState().userDevice,
|
|
896
|
+
setShowStats: (t) => {
|
|
897
|
+
e.getState().setShowStats(t);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
}
|
|
902
|
+
//#endregion
|
|
903
|
+
//#region src/viewer/Loaders/BmtLoader.ts
|
|
904
|
+
var Ge = class {
|
|
905
|
+
view;
|
|
906
|
+
offset = 0;
|
|
907
|
+
constructor(e) {
|
|
908
|
+
this.view = new DataView(e);
|
|
909
|
+
}
|
|
910
|
+
readUint8() {
|
|
911
|
+
return this.view.getUint8(this.offset++);
|
|
912
|
+
}
|
|
913
|
+
readUint32() {
|
|
914
|
+
let e = this.view.getUint32(this.offset, !0);
|
|
915
|
+
return this.offset += 4, e;
|
|
916
|
+
}
|
|
917
|
+
readBytes(e) {
|
|
918
|
+
let t = new Uint8Array(this.view.buffer, this.offset, e);
|
|
919
|
+
return this.offset += e, t;
|
|
920
|
+
}
|
|
921
|
+
eof() {
|
|
922
|
+
return this.offset >= this.view.byteLength;
|
|
923
|
+
}
|
|
924
|
+
}, Ke = class {
|
|
925
|
+
decodeBuffer(e) {
|
|
926
|
+
return this.textDecoder.decode(N.inflate(e));
|
|
927
|
+
}
|
|
928
|
+
decoder = N;
|
|
929
|
+
textDecoder = new TextDecoder("utf-8");
|
|
930
|
+
ChunkType = {
|
|
931
|
+
PROP: 1,
|
|
932
|
+
MESH: 2,
|
|
933
|
+
STRUCTURE: 3,
|
|
934
|
+
GRIDS: 4,
|
|
935
|
+
MATRIX: 5
|
|
936
|
+
};
|
|
937
|
+
parseMesh(e) {
|
|
938
|
+
let t = new DataView(e.buffer, e.byteOffset), n = 0, r = () => {
|
|
939
|
+
let r = t.getUint32(n, !0);
|
|
940
|
+
n += 4;
|
|
941
|
+
let i = new Uint8Array(e.buffer, e.byteOffset + n, r);
|
|
942
|
+
return n += r, i;
|
|
943
|
+
};
|
|
944
|
+
return {
|
|
945
|
+
pos: r(),
|
|
946
|
+
ids: r(),
|
|
947
|
+
ind: (() => {
|
|
948
|
+
let r = t.getUint32(n, !0);
|
|
949
|
+
n += 4;
|
|
950
|
+
let i = t.getUint32(n, !0);
|
|
951
|
+
n += 4;
|
|
952
|
+
let a = new Uint8Array(e.buffer, e.byteOffset + n, r);
|
|
953
|
+
return n += r, {
|
|
954
|
+
data: a,
|
|
955
|
+
type: i
|
|
956
|
+
};
|
|
957
|
+
})(),
|
|
958
|
+
colorId: (() => {
|
|
959
|
+
let e = t.getUint8(n++) / 255, r = t.getUint8(n++) / 255, i = t.getUint8(n++) / 255, a = t.getUint8(n++) / 255, o = t.getUint32(n, !0);
|
|
960
|
+
return n += 4, {
|
|
961
|
+
r: e,
|
|
962
|
+
g: r,
|
|
963
|
+
b: i,
|
|
964
|
+
opacity: a,
|
|
965
|
+
name: o
|
|
966
|
+
};
|
|
967
|
+
})()
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
async loadModel(e) {
|
|
971
|
+
let t = await (await fetch(e)).blob();
|
|
972
|
+
console.log("Parsing BMT model");
|
|
973
|
+
let n = await t.arrayBuffer();
|
|
974
|
+
return this.parseBinaryFile(n);
|
|
975
|
+
}
|
|
976
|
+
parseBinaryFile(e) {
|
|
977
|
+
let t = new Ge(e);
|
|
978
|
+
if (new TextDecoder().decode(t.readBytes(3)) !== "BMT") throw Error("Invalid file");
|
|
979
|
+
let n = t.readUint8();
|
|
980
|
+
console.log(`model version: ${n}`);
|
|
981
|
+
let r = {}, i = (e) => this.decoder.inflate(e), a = "";
|
|
982
|
+
for (; !t.eof();) {
|
|
983
|
+
let e = t.readUint8(), n = t.readUint32(), o = t.readBytes(n);
|
|
984
|
+
switch (e) {
|
|
985
|
+
case this.ChunkType.MESH: {
|
|
986
|
+
let e = this.parseMesh(o), t = i(e.pos), n = i(e.ids), s = i(e.ind.data), c = e.colorId, l = new Float32Array(t.buffer), u = new Uint32Array(n.buffer), d;
|
|
987
|
+
if (s && s.length) d = e.ind.type === 0 ? new Uint16Array(s.buffer, s.byteOffset, s.byteLength / 2) : new Uint32Array(s.buffer, s.byteOffset, s.byteLength / 4);
|
|
988
|
+
else {
|
|
989
|
+
d = new Uint32Array(t.length);
|
|
990
|
+
for (let e = 0; e < t.length; e++) d[e] = e;
|
|
991
|
+
}
|
|
992
|
+
r[c.name] = {
|
|
993
|
+
pos: l,
|
|
994
|
+
ids: u,
|
|
995
|
+
ind: d,
|
|
996
|
+
mat: c,
|
|
997
|
+
matrix: a
|
|
998
|
+
};
|
|
999
|
+
break;
|
|
1000
|
+
}
|
|
1001
|
+
case this.ChunkType.PROP: break;
|
|
1002
|
+
case this.ChunkType.STRUCTURE: break;
|
|
1003
|
+
case this.ChunkType.GRIDS: break;
|
|
1004
|
+
case this.ChunkType.MATRIX:
|
|
1005
|
+
n && (a = this.decodeBuffer(o));
|
|
1006
|
+
break;
|
|
1007
|
+
default: break;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
return r;
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
//#endregion
|
|
1014
|
+
//#region src/viewer/api/loader.ts
|
|
1015
|
+
function qe(e) {
|
|
1016
|
+
return typeof e == "string" ? e : e.name;
|
|
1017
|
+
}
|
|
1018
|
+
function Je(e) {
|
|
1019
|
+
let t = qe(e).split(/[?#]/)[0].toLowerCase(), n = t.lastIndexOf(".");
|
|
1020
|
+
return n === -1 ? "" : t.slice(n + 1);
|
|
1021
|
+
}
|
|
1022
|
+
async function Ye(e) {
|
|
1023
|
+
let t = new Ke();
|
|
1024
|
+
return typeof e == "string" ? t.loadModel(e) : t.parseBinaryFile(await e.arrayBuffer());
|
|
1025
|
+
}
|
|
1026
|
+
async function Xe(e) {
|
|
1027
|
+
let t = Je(e);
|
|
1028
|
+
if (t === "ifc") return null;
|
|
1029
|
+
if (t === "bmt") return Ye(e);
|
|
1030
|
+
throw Error(`Unsupported model format: ${qe(e)}`);
|
|
1031
|
+
}
|
|
1032
|
+
async function Ze(e) {
|
|
1033
|
+
let t = await Promise.all(e.map(Xe)), n = {};
|
|
1034
|
+
return t.forEach((e, t) => {
|
|
1035
|
+
e && (n[t] = e);
|
|
1036
|
+
}), n;
|
|
1037
|
+
}
|
|
1038
|
+
var Qe = { loadModel: Ze };
|
|
1039
|
+
//#endregion
|
|
1040
|
+
//#region src/viewer/Viewer.tsx
|
|
1041
|
+
function $e() {
|
|
1042
|
+
let e = navigator, t = navigator.userAgent.toLowerCase(), n = navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1, r = window.matchMedia?.("(pointer: coarse)").matches ?? !1;
|
|
1043
|
+
return e.userAgentData?.mobile || n || /android|iphone|ipad|ipod|mobile|tablet/.test(t) || navigator.maxTouchPoints > 1 && r ? "mobile" : "pc";
|
|
1044
|
+
}
|
|
1045
|
+
function et({ modelsData: e, onReady: t, publicApi: n, setSceneApi: r }) {
|
|
1046
|
+
let i = c(null), o = c(null), s = c(null), l = ce(o), u = he(l), d = Ve(), m = c(!1), h = P((e) => e.showStats);
|
|
1047
|
+
return Be(o, i, l), ge(u), a(() => {
|
|
1048
|
+
if (!o.current) return;
|
|
1049
|
+
let e = requestAnimationFrame(() => {
|
|
1050
|
+
o.current?.updateMatrixWorld(!0), u.buildIndex(), d(o, i, s);
|
|
1051
|
+
});
|
|
1052
|
+
return () => cancelAnimationFrame(e);
|
|
1053
|
+
}, [e]), a(() => (r({
|
|
1054
|
+
camera: { fitCamera: () => d(o, i, s) },
|
|
1055
|
+
geometryUtils: {
|
|
1056
|
+
getAllIds: u.getAllIds,
|
|
1057
|
+
hideByIds: u.hideByIds,
|
|
1058
|
+
hideSelected: u.hideSelected,
|
|
1059
|
+
isolateByIds: u.isolateByIds,
|
|
1060
|
+
isolateSelected: u.isolateSelected,
|
|
1061
|
+
resetIsolation: u.resetIsolation,
|
|
1062
|
+
showAll: u.showAll,
|
|
1063
|
+
showByIds: u.showByIds
|
|
1064
|
+
}
|
|
1065
|
+
}), m.current || (m.current = !0, t?.(n)), () => r(null)), [
|
|
1066
|
+
d,
|
|
1067
|
+
u,
|
|
1068
|
+
t,
|
|
1069
|
+
n,
|
|
1070
|
+
r
|
|
1071
|
+
]), /* @__PURE__ */ j(k, { children: [
|
|
1072
|
+
/* @__PURE__ */ A("ambientLight", {
|
|
1073
|
+
intensity: 1.2,
|
|
1074
|
+
color: "#ffffff"
|
|
1075
|
+
}),
|
|
1076
|
+
/* @__PURE__ */ A("directionalLight", {
|
|
1077
|
+
ref: s,
|
|
1078
|
+
position: [
|
|
1079
|
+
10,
|
|
1080
|
+
10,
|
|
1081
|
+
10
|
|
1082
|
+
],
|
|
1083
|
+
color: "#ffffff",
|
|
1084
|
+
intensity: 2
|
|
1085
|
+
}),
|
|
1086
|
+
h && /* @__PURE__ */ A(p, {}),
|
|
1087
|
+
/* @__PURE__ */ A("group", {
|
|
1088
|
+
ref: o,
|
|
1089
|
+
children: Object.keys(e).map((t) => /* @__PURE__ */ A(ne, {
|
|
1090
|
+
data: e[Number(t)],
|
|
1091
|
+
modelID: Number(t)
|
|
1092
|
+
}, t))
|
|
1093
|
+
}),
|
|
1094
|
+
/* @__PURE__ */ A(f, { ref: i })
|
|
1095
|
+
] });
|
|
1096
|
+
}
|
|
1097
|
+
function tt({ camera: e = {
|
|
1098
|
+
position: [
|
|
1099
|
+
10,
|
|
1100
|
+
10,
|
|
1101
|
+
10
|
|
1102
|
+
],
|
|
1103
|
+
fov: 50
|
|
1104
|
+
}, dpr: t = window.devicePixelRatio, modelUrls: n, modelSources: r, modelsData: i, onReady: o, publicApi: s, setSceneApi: c, showStats: d }) {
|
|
1105
|
+
let [f, p] = l(), m = F(), h = P((e) => e.setShowStats), g = P((e) => e.setUserDevice), _ = i ?? f;
|
|
1106
|
+
return a(() => {
|
|
1107
|
+
g($e());
|
|
1108
|
+
}, [g]), a(() => {
|
|
1109
|
+
h(!!d);
|
|
1110
|
+
}, [h, d]), a(() => {
|
|
1111
|
+
if (i) return;
|
|
1112
|
+
let e = r ?? n;
|
|
1113
|
+
if (!e?.length) return;
|
|
1114
|
+
let t = !0;
|
|
1115
|
+
return Qe.loadModel(e).then((e) => {
|
|
1116
|
+
t && p(e);
|
|
1117
|
+
}), () => {
|
|
1118
|
+
t = !1;
|
|
1119
|
+
};
|
|
1120
|
+
}, [
|
|
1121
|
+
i,
|
|
1122
|
+
r,
|
|
1123
|
+
n
|
|
1124
|
+
]), _ ? /* @__PURE__ */ A(u, {
|
|
1125
|
+
camera: e,
|
|
1126
|
+
dpr: t,
|
|
1127
|
+
children: /* @__PURE__ */ A($, {
|
|
1128
|
+
store: m,
|
|
1129
|
+
children: /* @__PURE__ */ A(et, {
|
|
1130
|
+
modelsData: _,
|
|
1131
|
+
onReady: o,
|
|
1132
|
+
publicApi: s,
|
|
1133
|
+
setSceneApi: c
|
|
1134
|
+
})
|
|
1135
|
+
})
|
|
1136
|
+
}) : /* @__PURE__ */ A(k, {});
|
|
1137
|
+
}
|
|
1138
|
+
var nt = n(function(e, t) {
|
|
1139
|
+
let [n] = l(Q), i = c(!1), u = c(!1), d = c(null), { defaultSelected: f, onSelectedChange: p, selected: m } = e, h = r((e) => {
|
|
1140
|
+
d.current = e;
|
|
1141
|
+
}, []), g = s(() => We(n, () => d.current), [n]);
|
|
1142
|
+
return o(t, () => g, [g]), a(() => {
|
|
1143
|
+
i.current || !f || m !== void 0 || (i.current = !0, n.getState().setSelection(Ue(f)));
|
|
1144
|
+
}, [
|
|
1145
|
+
m,
|
|
1146
|
+
f,
|
|
1147
|
+
n
|
|
1148
|
+
]), a(() => {
|
|
1149
|
+
m !== void 0 && (u.current = !0, n.getState().setSelection(Ue(m)), u.current = !1);
|
|
1150
|
+
}, [m, n]), a(() => {
|
|
1151
|
+
if (p) return n.subscribe((e, t) => {
|
|
1152
|
+
e.selected !== t.selected && (u.current || p(He(e.selected)));
|
|
1153
|
+
});
|
|
1154
|
+
}, [p, n]), /* @__PURE__ */ A($, {
|
|
1155
|
+
store: n,
|
|
1156
|
+
children: /* @__PURE__ */ A(tt, {
|
|
1157
|
+
...e,
|
|
1158
|
+
publicApi: g,
|
|
1159
|
+
setSceneApi: h
|
|
1160
|
+
})
|
|
1161
|
+
});
|
|
1162
|
+
});
|
|
1163
|
+
nt.displayName = "Viewer";
|
|
1164
|
+
//#endregion
|
|
1165
|
+
export { Ke as BMTLoader, nt as Viewer, $ as ViewerStoreProvider, Q as createViewerStore, Qe as loader, P as useViewerStore, F as useViewerStoreApi };
|