three-base 2.0.84 → 2.0.86

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.js CHANGED
@@ -4,15 +4,15 @@
4
4
  * @Last Modified by: xiaosihan
5
5
  * @Last Modified time: 2023-05-03 00:36:18
6
6
  */
7
- import { Box3, Clock, EventDispatcher, Material, Vector3 } from "three";
8
- import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter';
7
+ import { Box3, Clock, EventDispatcher, Material, Vector3, } from "three";
8
+ import { GLTFExporter } from "three/examples/jsm/exporters/GLTFExporter";
9
9
  class ThreeUtils extends EventDispatcher {
10
10
  constructor() {
11
11
  super();
12
12
  this.requestAnimationFrame();
13
13
  }
14
- isProd = (process.env.NODE_ENV === "production");
15
- isDev = (process.env.NODE_ENV === "development");
14
+ isProd = process.env.NODE_ENV === "production";
15
+ isDev = process.env.NODE_ENV === "development";
16
16
  _timeTemp = performance.now();
17
17
  _accumulate = 0; // 事件分发次数
18
18
  _box3 = new Box3();
@@ -21,11 +21,11 @@ class ThreeUtils extends EventDispatcher {
21
21
  _clock = new Clock();
22
22
  requestAnimationFrame() {
23
23
  this._accumulate += 1;
24
- const timeTemp = this._timeTemp = performance.now();
24
+ const timeTemp = (this._timeTemp = performance.now());
25
25
  this.dispatchEvent({
26
26
  type: "requestAnimationFrame",
27
27
  timeTemp,
28
- accumulate: this._accumulate
28
+ accumulate: this._accumulate,
29
29
  });
30
30
  this._computerDomZoom();
31
31
  //执行一个计划任务
@@ -42,7 +42,7 @@ class ThreeUtils extends EventDispatcher {
42
42
  */
43
43
  _computerDomZoom() {
44
44
  //1. 踢除没有出现在页面上的 dom 元素
45
- this._keepZoomDoms = this._keepZoomDoms.filter(l => this.inDocument(l.dom));
45
+ this._keepZoomDoms = this._keepZoomDoms.filter((l) => this.inDocument(l.dom));
46
46
  //2.设置dom的缩放值样式
47
47
  this._keepZoomDoms.map((item, i) => {
48
48
  let parentDom = item.dom.parentNode;
@@ -53,7 +53,7 @@ class ThreeUtils extends EventDispatcher {
53
53
  left: "50%",
54
54
  transform: `translate(-50%, -50%) scale(${scale})`,
55
55
  width: `${item.width}px`,
56
- height: `${item.height}px`
56
+ height: `${item.height}px`,
57
57
  });
58
58
  });
59
59
  }
@@ -63,7 +63,7 @@ class ThreeUtils extends EventDispatcher {
63
63
  keepZoom(dom, width = 1920, height = 1080) {
64
64
  if (dom) {
65
65
  // 去掉相同的dom存储的信息
66
- this._keepZoomDoms = this._keepZoomDoms.filter(l => l.dom !== dom);
66
+ this._keepZoomDoms = this._keepZoomDoms.filter((l) => l.dom !== dom);
67
67
  // 把配置加入到列表中
68
68
  this._keepZoomDoms.push({ dom, width, height });
69
69
  dom.dataset.a = "这个元素缩放比例由 threeUtils.keepZoom 方法设置";
@@ -136,7 +136,7 @@ class ThreeUtils extends EventDispatcher {
136
136
  this._box3.setFromObject(object3d);
137
137
  this._box3.getCenter(this._center);
138
138
  this._box3.getSize(this._size);
139
- object3d.position.y -= (this._center.y - (this._size.y * 0.5));
139
+ object3d.position.y -= this._center.y - this._size.y * 0.5;
140
140
  object3d.position.y += y;
141
141
  object3d.updateMatrixWorld(true);
142
142
  }
@@ -164,7 +164,7 @@ class ThreeUtils extends EventDispatcher {
164
164
  // 使用指定的维度(xyz)计算, 缩放至指定大小;
165
165
  scaleTo(object3d, size = 1, key = ["x", "y", "z"]) {
166
166
  const originSize = this.getSize(object3d);
167
- const scale = size / Math.max(...key.map(k => originSize[k]));
167
+ const scale = size / Math.max(...key.map((k) => originSize[k]));
168
168
  if (object3d.isObject3D) {
169
169
  object3d.scale.multiplyScalar(scale);
170
170
  object3d.updateMatrixWorld(true);
@@ -177,7 +177,7 @@ class ThreeUtils extends EventDispatcher {
177
177
  applyAllTranstion(object3d) {
178
178
  object3d.updateMatrixWorld(true);
179
179
  // 应用全部变换
180
- object3d.traverse(obj => {
180
+ object3d.traverse((obj) => {
181
181
  obj.position.set(0, 0, 0);
182
182
  obj.rotation.set(0, 0, 0);
183
183
  obj.scale.set(1, 1, 1);
@@ -191,10 +191,10 @@ class ThreeUtils extends EventDispatcher {
191
191
  const gltfExporter = new GLTFExporter();
192
192
  gltfExporter.parse(input, (result) => {
193
193
  if (result instanceof ArrayBuffer) {
194
- const link = document.createElement('a');
195
- link.style.display = 'none';
194
+ const link = document.createElement("a");
195
+ link.style.display = "none";
196
196
  document.body.appendChild(link); // Firefox workaround, see #6594
197
- link.href = URL.createObjectURL(new Blob([result], { type: 'application/octet-stream' }));
197
+ link.href = URL.createObjectURL(new Blob([result], { type: "application/octet-stream" }));
198
198
  link.download = `${fileName}.glb`;
199
199
  link.click();
200
200
  }
@@ -202,12 +202,11 @@ class ThreeUtils extends EventDispatcher {
202
202
  trs: true,
203
203
  onlyVisible: true,
204
204
  binary: true,
205
- maxTextureSize: 4096
205
+ maxTextureSize: 4096,
206
206
  });
207
207
  }
208
208
  //导出usdz
209
- exportUsdz() {
210
- }
209
+ exportUsdz() { }
211
210
  // 计算网格的体积
212
211
  getVolume(geometry) {
213
212
  if (!geometry.index)
@@ -246,7 +245,7 @@ class ThreeUtils extends EventDispatcher {
246
245
  }
247
246
  //清理对象的网格和材质的缓存
248
247
  dipose(object3d) {
249
- object3d.traverse(obj => {
248
+ object3d.traverse((obj) => {
250
249
  const mesh = obj;
251
250
  if (mesh.isMesh) {
252
251
  mesh.geometry.dispose();
@@ -254,13 +253,26 @@ class ThreeUtils extends EventDispatcher {
254
253
  mesh.material.dispose();
255
254
  }
256
255
  else if (mesh.material instanceof Array) {
257
- mesh.material.map(m => {
256
+ mesh.material.map((m) => {
258
257
  m.dispose();
259
258
  });
260
259
  }
261
260
  }
262
261
  });
263
262
  }
263
+ // 获取文件名的后缀
264
+ getSuffix(filename) {
265
+ const match = filename.match(/\.([^\.]+)$/);
266
+ return match ? match[1].toLowerCase() : "";
267
+ }
268
+ // 从url 中提取文件名
269
+ getFilename(url) {
270
+ // 移除URL的查询字符串和哈希部分
271
+ const path = url.split("?")[0].split("#")[0];
272
+ // 使用lastIndexOf和substring从路径中提取文件名
273
+ const filename = path.substring(path.lastIndexOf("/") + 1);
274
+ return filename;
275
+ }
264
276
  }
265
277
  const threeUtils = new ThreeUtils();
266
278
  export default threeUtils;