three-base 2.0.87 → 2.0.89

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/threeUtils.ts DELETED
@@ -1,351 +0,0 @@
1
- /*
2
- * @Author: xiaosihan
3
- * @Date: 2022-02-24 22:49:42
4
- * @Last Modified by: xiaosihan
5
- * @Last Modified time: 2023-05-03 00:36:18
6
- */
7
-
8
- import {
9
- Box3,
10
- BufferAttribute,
11
- BufferGeometry,
12
- Clock,
13
- EventDispatcher,
14
- Material,
15
- Mesh,
16
- MeshStandardMaterial,
17
- Object3D,
18
- Vector3,
19
- } from "three";
20
- import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter";
21
-
22
- class ThreeUtils extends EventDispatcher {
23
- constructor() {
24
- super();
25
-
26
- this.requestAnimationFrame();
27
- }
28
- isProd = process.env.NODE_ENV === "production";
29
-
30
- isDev = process.env.NODE_ENV === "development";
31
-
32
- _timeTemp = performance.now();
33
-
34
- _accumulate = 0; // 事件分发次数
35
-
36
- _box3 = new Box3();
37
- _size = new Vector3();
38
- _center = new Vector3();
39
-
40
- _clock = new Clock();
41
-
42
- requestAnimationFrame() {
43
- this._accumulate += 1;
44
- const timeTemp = (this._timeTemp = performance.now());
45
-
46
- this.dispatchEvent({
47
- type: "requestAnimationFrame",
48
- timeTemp,
49
- accumulate: this._accumulate,
50
- } as never);
51
-
52
- this._computerDomZoom();
53
-
54
- //执行一个计划任务
55
- let work = this.workList.splice(0, 1)[0];
56
- work && work();
57
-
58
- setTimeout(() => {
59
- this.requestAnimationFrame();
60
- }, 30 - this._clock.getDelta());
61
- }
62
-
63
- // 需要保持宽高比的元素列表
64
- private _keepZoomDoms: Array<{
65
- dom: HTMLElement;
66
- width: number;
67
- height: number;
68
- }> = [];
69
-
70
- /**
71
- *计算需要保持比例的元素
72
- */
73
- private _computerDomZoom() {
74
- //1. 踢除没有出现在页面上的 dom 元素
75
- this._keepZoomDoms = this._keepZoomDoms.filter((l) =>
76
- this.inDocument(l.dom)
77
- );
78
-
79
- //2.设置dom的缩放值样式
80
- this._keepZoomDoms.map((item, i) => {
81
- let parentDom = item.dom.parentNode as HTMLDivElement;
82
- let scale = Math.min(
83
- parentDom.clientWidth / item.width,
84
- parentDom.clientHeight / item.height
85
- );
86
- Object.assign(item.dom.style, {
87
- position: "absolute",
88
- top: "50%",
89
- left: "50%",
90
- transform: `translate(-50%, -50%) scale(${scale})`,
91
- width: `${item.width}px`,
92
- height: `${item.height}px`,
93
- });
94
- });
95
- }
96
-
97
- /**
98
- * dom元素保持比例
99
- */
100
- keepZoom(
101
- dom: HTMLElement | null,
102
- width: number = 1920,
103
- height: number = 1080
104
- ) {
105
- if (dom) {
106
- // 去掉相同的dom存储的信息
107
- this._keepZoomDoms = this._keepZoomDoms.filter((l) => l.dom !== dom);
108
-
109
- // 把配置加入到列表中
110
- this._keepZoomDoms.push({ dom, width, height });
111
-
112
- dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
113
-
114
- this._computerDomZoom();
115
- }
116
- }
117
-
118
- workList: Array<() => void> = [];
119
-
120
- // 时钟对象
121
- clock = new Clock();
122
-
123
- // 按照任务队列依次执行计划任务 性能优化
124
- /**
125
- * @param {() => void} [callback]
126
- * @param {boolean} [first=true] 需要优先执行
127
- */
128
- addWork(work?: () => void, first: boolean = false) {
129
- // 加入任务队列
130
- if (work && !this.workList.includes(work)) {
131
- if (first) {
132
- this.workList.splice(1, 0, work); // 优先执行的任务回调
133
- } else {
134
- this.workList.push(work);
135
- }
136
- }
137
- }
138
-
139
- /**
140
- *判断一个元素是否在页面上
141
- */
142
- inDocument(dom: ParentNode | null): boolean {
143
- if (dom === null) {
144
- return false;
145
- } else if (dom.parentNode === null) {
146
- return false;
147
- } else if (dom.parentNode === document) {
148
- return true;
149
- } else {
150
- return this.inDocument(dom.parentNode);
151
- }
152
- }
153
-
154
- // 获取中心点
155
- getCenter(object3d: Object3D | BufferGeometry) {
156
- if ((object3d as Object3D).isObject3D) {
157
- this._box3.setFromObject(object3d as Object3D);
158
- this._box3.getCenter(this._center);
159
- } else if ((object3d as BufferGeometry).isBufferGeometry) {
160
- (object3d as BufferGeometry).computeBoundingBox();
161
- (object3d as BufferGeometry).boundingBox!.getCenter(this._center);
162
- }
163
-
164
- return this._center.clone();
165
- }
166
-
167
- // 获取模型大小
168
- getSize(object3d: Object3D | BufferGeometry) {
169
- if ((object3d as Object3D).isObject3D) {
170
- this._box3.setFromObject(object3d as Object3D);
171
- this._box3.getSize(this._size);
172
- } else if ((object3d as BufferGeometry).isBufferGeometry) {
173
- (object3d as BufferGeometry).computeBoundingBox();
174
- (object3d as BufferGeometry).boundingBox!.getSize(this._size);
175
- }
176
-
177
- return this._size.clone();
178
- }
179
-
180
- // 模型对齐底面
181
- alignBottom(object3d: Object3D | BufferGeometry, y = 0) {
182
- if ((object3d as Object3D).isObject3D) {
183
- this._box3.setFromObject(object3d as Object3D);
184
- this._box3.getCenter(this._center);
185
- this._box3.getSize(this._size);
186
- (object3d as Object3D).position.y -= this._center.y - this._size.y * 0.5;
187
- (object3d as Object3D).position.y += y;
188
- (object3d as Object3D).updateMatrixWorld(true);
189
- } else if ((object3d as BufferGeometry).isBufferGeometry) {
190
- (object3d as BufferGeometry).computeBoundingBox();
191
- (object3d as BufferGeometry).translate(
192
- 0,
193
- -(object3d as BufferGeometry).boundingBox!.min.y + y,
194
- 0
195
- );
196
- }
197
- }
198
-
199
- // 居中对象
200
- centerObject3D(object3d: Object3D | BufferGeometry) {
201
- if ((object3d as Object3D).isObject3D) {
202
- this._box3.setFromObject(object3d as Object3D);
203
- this._box3.getCenter(this._center);
204
- (object3d as Object3D).position.sub(this._center);
205
- (object3d as Object3D).updateMatrixWorld(true);
206
- } else if ((object3d as BufferGeometry).isBufferGeometry) {
207
- (object3d as BufferGeometry).center();
208
- }
209
- }
210
-
211
- //居中对象
212
- center(object3d: Object3D | BufferGeometry) {
213
- this.centerObject3D(object3d);
214
- }
215
-
216
- // 使用指定的维度(xyz)计算, 缩放至指定大小;
217
- scaleTo(
218
- object3d: Object3D | BufferGeometry,
219
- size: number = 1,
220
- key: Array<"x" | "y" | "z"> = ["x", "y", "z"]
221
- ) {
222
- const originSize = this.getSize(object3d);
223
- const scale = size / Math.max(...key.map((k) => originSize[k]));
224
-
225
- if ((object3d as Object3D).isObject3D) {
226
- (object3d as Object3D).scale.multiplyScalar(scale);
227
- (object3d as Object3D).updateMatrixWorld(true);
228
- } else if ((object3d as BufferGeometry).isBufferGeometry) {
229
- (object3d as BufferGeometry).scale(scale, scale, scale);
230
- }
231
- }
232
-
233
- // 应用全部变化
234
- applyAllTranstion(object3d: Object3D) {
235
- object3d.updateMatrixWorld(true);
236
- // 应用全部变换
237
- object3d.traverse((obj) => {
238
- obj.position.set(0, 0, 0);
239
- obj.rotation.set(0, 0, 0);
240
- obj.scale.set(1, 1, 1);
241
- if ((obj as Mesh).isMesh) {
242
- (obj as Mesh).geometry.applyMatrix4(obj.matrixWorld);
243
- }
244
- });
245
- }
246
-
247
- // 导出 glb
248
- exportGLB(input: Object3D, fileName: string = "threeUtils.exportGLB") {
249
- const gltfExporter = new GLTFExporter();
250
- gltfExporter.parse(
251
- input,
252
- (result) => {
253
- if (result instanceof ArrayBuffer) {
254
- const link = document.createElement("a");
255
- link.style.display = "none";
256
- document.body.appendChild(link); // Firefox workaround, see #6594
257
- link.href = URL.createObjectURL(
258
- new Blob([result], { type: "application/octet-stream" })
259
- );
260
- link.download = `${fileName}.glb`;
261
- link.click();
262
- }
263
- },
264
- (error: ErrorEvent) => {},
265
- {
266
- trs: true,
267
- onlyVisible: true, // 只导出可见的
268
- binary: true, // glb
269
- maxTextureSize: 4096,
270
- }
271
- );
272
- }
273
-
274
- //导出usdz
275
- exportUsdz() {}
276
-
277
- // 计算网格的体积
278
- getVolume(geometry: BufferGeometry) {
279
- if (!geometry.index) return;
280
- if (!geometry.isBufferGeometry) {
281
- console.log(
282
- "'geometry' must be an indexed or non-indexed buffer geometry"
283
- );
284
- return 0;
285
- }
286
- const isIndexed = geometry.index !== null;
287
- const position = geometry.attributes.position as BufferAttribute;
288
- let sum = 0;
289
- const p1 = new Vector3(),
290
- p2 = new Vector3(),
291
- p3 = new Vector3();
292
- if (!isIndexed) {
293
- const faces = position.count / 3;
294
- for (let i = 0; i < faces; i++) {
295
- p1.fromBufferAttribute(position, i * 3 + 0);
296
- p2.fromBufferAttribute(position, i * 3 + 1);
297
- p3.fromBufferAttribute(position, i * 3 + 2);
298
- sum += this.signedVolumeOfTriangle(p1, p2, p3);
299
- }
300
- } else {
301
- const index = geometry.index;
302
- const faces = index.count / 3;
303
- for (let i = 0; i < faces; i++) {
304
- p1.fromBufferAttribute(position, index.array[i * 3 + 0]);
305
- p2.fromBufferAttribute(position, index.array[i * 3 + 1]);
306
- p3.fromBufferAttribute(position, index.array[i * 3 + 2]);
307
- sum += this.signedVolumeOfTriangle(p1, p2, p3);
308
- }
309
- }
310
- return sum > 0 ? sum : 0;
311
- }
312
-
313
- signedVolumeOfTriangle(p1: Vector3, p2: Vector3, p3: Vector3) {
314
- return p1.dot(p2.cross(p3)) / 6.0;
315
- }
316
-
317
- //清理对象的网格和材质的缓存
318
- dipose(object3d: Object3D) {
319
- object3d.traverse((obj) => {
320
- const mesh = obj as Mesh;
321
- if (mesh.isMesh) {
322
- mesh.geometry.dispose();
323
- if (mesh.material instanceof Material) {
324
- mesh.material.dispose();
325
- } else if (mesh.material instanceof Array) {
326
- mesh.material.map((m) => {
327
- m.dispose();
328
- });
329
- }
330
- }
331
- });
332
- }
333
-
334
- // 获取文件名的后缀
335
- getSuffix(filename: string) {
336
- const match = filename.match(/\.([^\.]+)$/);
337
- return match ? match[1].toLowerCase() : "";
338
- }
339
- // 从url 中提取文件名
340
- getFilename(url: string) {
341
- // 移除URL的查询字符串和哈希部分
342
- const path = url.split("?")[0].split("#")[0];
343
- // 使用lastIndexOf和substring从路径中提取文件名
344
- const filename = path.substring(path.lastIndexOf("/") + 1);
345
- return filename;
346
- }
347
- }
348
-
349
- const threeUtils = new ThreeUtils();
350
-
351
- export default threeUtils;